In C Linux, is there any way to wait for signal like SIGUSR1
and SIGUSR2
without loop?
For eaxmple in this code I wait for SIGUSR1
or SIGUSR2
to print message, but while I want , I do while true....
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
void signal_callback_handler(int signum)
{
printf("Caught signal %d\n",signum);
}
int main()
{
signal(SIGUSR1, signal_callback_handler);
signal(SIGUSR2, signal_callback_handler);
while(1)
{
printf("Program processing stuff here.\n");
sleep(1);
}
return EXIT_SUCCESS;
}
How can I wait and do not do anything (do not do while true) until SIGUSR1
or SIGUSR2
arrived ?
For example :
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
void signal_callback_handler(int signum)
{
printf("Caught signal %d\n",signum);
}
int main()
{
signal(SIGUSR1, signal_callback_handler);
signal(SIGUSR2, signal_callback_handler);
printf("this line happned after signal arrived \n ");
return EXIT_SUCCESS;
}
select()
. That's from the top of my head cause I know thatselect()
can block forever.select(2)
– Posseprintf
inside signal handlers, ever. – Declarative