linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Gedare Bloom <gedare@gwmail.gwu.edu>
To: "Mirski Paweł" <mirskip87@gmail.com>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: Problem with infinite loop in signal handler
Date: Wed, 22 Jul 2009 16:31:23 -0400	[thread overview]
Message-ID: <9b6278b80907221331m39d4adf0oc6cd8057d9cd6565@mail.gmail.com> (raw)
In-Reply-To: <62c21b850907220942p60fd9362kcbd82158ce685dd8@mail.gmail.com>

The problem is that the thread looping in the signal handler has equal
priority to the 'main' thread, so that it is exhausting all of the
compute resources and the main thread is never scheduled.

You can correct this by giving your newly created thread a lower
priority (see below).  Good luck!

-G

--- orig.c	2009-07-22 16:28:44.000000000 -0400
+++ test.c	2009-07-22 16:28:02.000000000 -0400
@@ -1,5 +1,6 @@
 #include <signal.h>
 #include <stdio.h>
+#include <sched.h>

 void suspend(int sig) {
    printf("suspending\n");
@@ -28,6 +29,8 @@

    struct sigaction sa;
    pthread_t thread1;
+	struct sched_param sp;
+	int policy;

    sa.sa_handler = suspend;
    sa.sa_flags = SA_RESTART;
@@ -35,6 +38,9 @@
    sigaction(SIGUSR1, &sa, NULL);

    pthread_create(&thread1, 0, loop_func, 0);
+	pthread_getschedparam(thread1, &policy, &sp);
+	sp.sched_priority = sched_get_priority_min(policy);
+	pthread_setschedparam(thread1, policy, &sp);
    wait_some_time();

    pthread_kill(thread1, SIGUSR1);
@@ -43,4 +49,3 @@

    return 0;
 }
-


2009/7/22 Mirski Paweł <mirskip87@gmail.com>:
> Hi all,
> I have problem with handling signals in simple program. Program starts
> one thread (only task of this thread is printing "Hello World") and
> after while sends signal to this thread (signal handler is previously
> registered). In signal handler is infinite loop that should suspend
> thread forever. But it suspends whole application. I think this
> problem is related with printf function because when I replace it with
> with linux write sys function problem does not occures. And one more
> info: problem occures not always, but most of time; maybe somewhere
> there is a race condition.
> This is whole code of application:
>
> #include <signal.h>
> #include <stdio.h>
>
> void suspend(int sig) {
>    printf("suspending\n");
>    fflush(stdout);
>    while(1);
> }
>
>
> void* loop_func(void* arg) {
>    int i = 0;
>    while(1) {
>        if(i % 10000 == 0) {
>            printf("Hello World %d\n", i);
>            fflush(stdout);
>        }
>        i++;
>    }
> }
>
> void wait_some_time() {
>    int i;
>    for(i = 0; i < 10000000; i++) { }
> }
>
> int main(void) {
>
>    struct sigaction sa;
>    pthread_t thread1;
>
>    sa.sa_handler = suspend;
>    sa.sa_flags = SA_RESTART;
>    sigemptyset(&sa.sa_mask);
>    sigaction(SIGUSR1, &sa, NULL);
>
>    pthread_create(&thread1, 0, loop_func, 0);
>    wait_some_time();
>
>    pthread_kill(thread1, SIGUSR1);
>    printf("Waiting for terminate...\n");
>    wait_some_time();
>
>    return 0;
> }
>
> Could any body tell me why this signal handler hangs whole
> application? It should hangs only one thread. It seems to be be a bug
> in linux kernel or maybe in C library. How can I solve this problem?
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  reply	other threads:[~2009-07-22 20:31 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-22 16:42 Problem with infinite loop in signal handler Mirski Paweł
2009-07-22 20:31 ` Gedare Bloom [this message]
2009-07-25  5:34   ` Mirski Paweł
2009-07-26 13:58     ` Gunnar Larisch
2009-07-28  6:45       ` Mirski Paweł

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9b6278b80907221331m39d4adf0oc6cd8057d9cd6565@mail.gmail.com \
    --to=gedare@gwmail.gwu.edu \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=mirskip87@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).