This code is handle SIGINT signal during 100 seconds or print timeout if SIGINT didn't arrive.
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int main (int argc, char *argv[])
{
sigset_t mask;
sigset_t orig_mask;
struct timespec timeout;
sigemptyset (&mask);
sigaddset (&mask, SIGINT);
if (sigprocmask(SIG_BLOCK, &mask, &orig_mask) < 0) {
perror ("sigprocmask");
return 1;
}
timeout.tv_sec = 100;
timeout.tv_nsec = 0;
int v =sigtimedwait(&mask, NULL, &timeout);
if (errno == EAGAIN) {
printf ("Timeout\n");
return -1;
}
if(v== SIGINT){
printf("SIGINT\n");
return 1;
}
return 0;
}
When code is in sigtimedwait
if another signal than SIGINT will arrive, is the code will continue ? Or sigtimedwait
will finish only when SIGINT will arrive?
In addition if before this code I will register to another signal like signal(SIGUSR1, handle_sig);
, when the code in sigtimedwait
and SIGUSR1
will arrived ,is handle_sig
will called?or it will blocked ?
int main (int argc, char *argv[])
{
signal(SIGUSR1, handle_sig);//
sigset_t mask;
sigset_t orig_mask;
struct timespec timeout;
sigemptyset (&mask);
sigaddset (&mask, SIGINT);
if (sigprocmask(SIG_BLOCK, &mask, &orig_mask) < 0) {
perror ("sigprocmask");
return 1;
}
timeout.tv_sec = 100;
timeout.tv_nsec = 0;
int v =sigtimedwait(&mask, NULL, &timeout);
if (errno == EAGAIN) {
printf ("Timeout\n");
return -1;
}
if(v== SIGINT){
printf("SIGINT\n");
return 1;
}
return 0;
}