Problem with infinite loop in signal handler

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux