linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Mirski Paweł" <mirskip87@gmail.com>
To: linux-c-programming@vger.kernel.org
Subject: Problem with infinite loop in signal handler
Date: Wed, 22 Jul 2009 18:42:45 +0200	[thread overview]
Message-ID: <62c21b850907220942p60fd9362kcbd82158ce685dd8@mail.gmail.com> (raw)

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?

             reply	other threads:[~2009-07-22 16:42 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-22 16:42 Mirski Paweł [this message]
2009-07-22 20:31 ` Problem with infinite loop in signal handler Gedare Bloom
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=62c21b850907220942p60fd9362kcbd82158ce685dd8@mail.gmail.com \
    --to=mirskip87@gmail.com \
    --cc=linux-c-programming@vger.kernel.org \
    /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).