* Problem with infinite loop in signal handler
@ 2009-07-22 16:42 Mirski Paweł
2009-07-22 20:31 ` Gedare Bloom
0 siblings, 1 reply; 5+ messages in thread
From: Mirski Paweł @ 2009-07-22 16:42 UTC (permalink / raw)
To: linux-c-programming
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?
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: Problem with infinite loop in signal handler 2009-07-22 16:42 Problem with infinite loop in signal handler Mirski Paweł @ 2009-07-22 20:31 ` Gedare Bloom 2009-07-25 5:34 ` Mirski Paweł 0 siblings, 1 reply; 5+ messages in thread From: Gedare Bloom @ 2009-07-22 20:31 UTC (permalink / raw) To: Mirski Paweł; +Cc: linux-c-programming 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 ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Problem with infinite loop in signal handler 2009-07-22 20:31 ` Gedare Bloom @ 2009-07-25 5:34 ` Mirski Paweł 2009-07-26 13:58 ` Gunnar Larisch 0 siblings, 1 reply; 5+ messages in thread From: Mirski Paweł @ 2009-07-25 5:34 UTC (permalink / raw) To: linux-c-programming I have found another problem, but your solution can improve situation I think. When I have removed printf after pthread_kill program works. I thinks scenario is: 1. Signal arrives when printf still works 2. Printf (or system) has locked some structeres associated with stream 3. Then thread1 is suspended so printf will never return and never unlock theese structures 4. In main thread printf blocks on stdout stream. To check my concept I have used fprintf to stderr and it does't blocks main thread. Only prints to stdout does. What are you thinking about this concept? W dniu 22 lipca 2009 22:31 u¿ytkownik Gedare Bloom <gedare@gwmail.gwu.edu> napisa³: > 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 ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Problem with infinite loop in signal handler 2009-07-25 5:34 ` Mirski Paweł @ 2009-07-26 13:58 ` Gunnar Larisch 2009-07-28 6:45 ` Mirski Paweł 0 siblings, 1 reply; 5+ messages in thread From: Gunnar Larisch @ 2009-07-26 13:58 UTC (permalink / raw) To: Mirski Paweł; +Cc: linux-c-programming Hi Mirski, your signal handler function: > >> void suspend(int sig) { > >> printf("suspending\n"); > >> fflush(stdout); > >> while(1); > >> } Have you also tried your programm without calling printf and fflush in the signal handler? Do you know the manpage signal (7) ? There is a list of Async-signal-safe functions. Can you tell me, why you need an endless loop in the signal handler? Regards, Gunnar -- 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 ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Problem with infinite loop in signal handler 2009-07-26 13:58 ` Gunnar Larisch @ 2009-07-28 6:45 ` Mirski Paweł 0 siblings, 0 replies; 5+ messages in thread From: Mirski Paweł @ 2009-07-28 6:45 UTC (permalink / raw) To: linux-c-programming Hello, Not exactly infinite loop but sigsuspend. Infinite loop was only example to show the problem. Without printf in sig handler problem also occures. I am writing simple stop-the-world garbage collector so I need this construction to stop (almost)all threads in application. I think only solution for this problem is not using any stream that client application may use. Thanks for help anyway :) W dniu 26 lipca 2009 15:58 u¿ytkownik Gunnar Larisch <gunmuc@googlemail.com> napisa³: > Hi Mirski, > > your signal handler function: > >> >> void suspend(int sig) { >> >> printf("suspending\n"); >> >> fflush(stdout); >> >> while(1); >> >> } > > Have you also tried your programm without calling printf and fflush in the > signal handler? Do you know the manpage signal (7) ? There is a list of > Async-signal-safe functions. > > Can you tell me, why you need an endless loop in the signal > handler? > > Regards, > Gunnar > -- 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 ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2009-07-28 6:45 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2009-07-22 16:42 Problem with infinite loop in signal handler Mirski Paweł 2009-07-22 20:31 ` Gedare Bloom 2009-07-25 5:34 ` Mirski Paweł 2009-07-26 13:58 ` Gunnar Larisch 2009-07-28 6:45 ` Mirski Paweł
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).