Is there a way to block certain signals and unblock other signals in the same set?
I just dont seem to get my head around it!
An example
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
// Block signal SIGUSR1 in this thread
pthread_sigmask(SIG_BLOCK, &set, NULL);
sigaddset(&set, SIGALRM);
// Listen to signal SIGUSR2
pthread_sigmask(SIG_UNBLOCK, &set, NULL);
pthread_t printer_thread1, printer_thread2;
pthread_create(&printer_thread1, NULL, print, (void *)&f1);
pthread_create(&printer_thread2, NULL, print, (void *)&f2);
bool tl = true;
while(1)
{
if(tl)
{
// thread1 does something
kill(pid, SIGUSR1);
// main thread waits for SIGALRM
sigwait(&set, &sig);
tl = !tl;
}
else
{
// thread2 does something
kill(pid, SIGUSR2);
// main thread waits for SIGALRM
sigwait(&set, &sig);
tl = !tl;
}
}
I am not allowed to use Mutexs, semaphores etc only signals.
Can someone help? :)
Is there a way to block certain signals and unblock other signals in
the same set?
With pthread_sigmask, you can choose to either:
add a set of signals to the set of blocked signals, using the constant SIG_BLOCK
remove a set of signals to the set of blocked signals, using the constant SIG_UNBLOCK
define the set of signals to be blocked, using the constant SIG_SET
In other words, there's a current set of blocked signals for the thread, and you can modify it as specified above, one operation at a time.
The important point is that newly created threads inherit the signal mask of the creating thread, so you can set the mask of the new thread right before creating it, or in the function that the new thread will run, at your convenience.
Regarding your example, I suppose that you are trying to have printer_thread1 block SIGUSR2 and SIGALRM, and have printer_thread2 block SIGUSR1 and SIGALRM, and have the main thread block SIGUSR1 and SIGUSR2, so that each thread can send a signal that will be caught by a single thread (without the code of print, f1 and f2, it's impossible to know for sure what is your intent in your example).
You should be able to achieve that by the following code:
sigset_t set;
pthread_t printer_thread1, printer_thread2;
// Block signal SIGUSR1 & SIGALRM in printer_thread1
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
sigaddset(&set, SIGALRM);
pthread_sigmask(SIG_SET, &set, NULL);
pthread_create(&printer_thread1, NULL, print, (void *)&f1);
// Block signal SIGUSR2 & SIGALRM in printer_thread2
sigaddset(&set, SIGUSR2);
sigaddset(&set, SIGALRM);
pthread_sigmask(SIG_SET, &set, NULL);
pthread_create(&printer_thread2, NULL, print, (void *)&f2);
// Block signal SIGUSR1 & SIGUSR2 in the main thread
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
sigaddset(&set, SIGUSR2);
// Listen to signal SIGALRM
pthread_sigmask(SIG_SET, &set, NULL);
bool tl = true;
while(1)
{
if(tl)
{
// thread1 does something
kill(pid, SIGUSR1);
// main thread waits for SIGALRM
sigwait(&set, &sig);
tl = !tl;
}
else
{
// thread2 does something
kill(pid, SIGUSR2);
// main thread waits for SIGALRM
sigwait(&set, &sig);
tl = !tl;
}
}
see these man pages :
pthread_sigmask
sigprocmask
for further details.
I think what you want to do here is
// Block signal SIGUSR1 in this thread
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
pthread_sigmask(SIG_BLOCK, &set, NULL);
// Listen to signal SIGALRM
sigemptyset(&set);
sigaddset(&set, SIGALRM);
pthread_sigmask(SIG_UNBLOCK, &set, NULL);
The set is only used to tell it what to block or unblock. Once passed to the command, you are free to reset it and build up another signal mask. If you skip the sigemptyset, the set will still contain SIGUSR1, which will subsequently be unblocked again. Well, I think that is how it works, at least - it has been a long time since I used signals.
Related
for a homework assignment I have to use only linux system calls to manage signals in a game, programmed in C.
One thing I have to do, it's to call indefinitly sigsuspend in a thread and wait for a SIGALRM,then if there is a sigalarm (signal alarm) I have to printf() something (doesn't matter what).
However, I tried several things but it doesn't work, I don't know how to "deblock" sigsuspend and print what i want. And there is a timer in the game which send sigalrm. My code works but not the part with the sigsuspend
Of course I have also a sigarlm handler which does some stuff with SDL when it receives a sigarlm.
I don't really how I have to use masks with sigsuspend
Code :
//initialising sigaction struct
int init (void)
{
// Signal handler
sigact.sa_handler = sigalrm_handler;
sigact.sa_flags = 0;
sigemptyset(&sigact.sa_mask);
// Linked list
head = NULL;
sigaction(SIGALRM, &sigact, NULL);
pthread_create(&tid_sigrecv, NULL, (void *)deamon, NULL);
return 1; // Implementation ready ?
}
The sigsuspend part
void deamon(void * arg)
{
int sig;
while(1) {
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGALRM);
sigsuspend(&mask);
printf("in thread\n");
}
}
Currently it only does not print "in thread" when a sigarlm is received, what should I do to use sigsuspend properly ? I am obliged to use sigsuspend
You create a thread that should wait for the signal SIGALRM thanks to sigsuspend(), but instead you open the thread to all signals but SIGALRM.
Replace
sigemptyset(&mask); // clear mask
sigaddset(&mask, SIGALRM); // set mask to SIGALRM
sigsuspend(&mask); // wait for any signal but blocks SIGALRM
with
sigfillset(&mask); // mask set to all signals
sigdelset(&mask, SIGALRM); // remove SIGALRM from the mask
sigsuspend(&mask); // block all signals but SIGALRM
this way sigsuspend() waits for a non blocked signal (SIGALRM).
For testing, you could send the thread the SIGALRM signal using
pthread_kill(tid_sigrecv, SIGALRM);
from the main thread, for instance.
This is the coursework of my class. I'm not sure whether my answer is correct or not. Can someone give me any instruction. In the first question, I can't figure out why there is a race condition, is my explanation wrong?
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
void sigHandler(int signo) {
/* Line C */
}
int main() {
pid_t pid;
sigset_t newmask, waitmask, oldmask;
if ((pid = fork()) == 0) { // Child
struct sigaction action;
sigemptyset(&newmask);
sigemptyset(&waitmask);
sigaddset(&newmask, SIGUSR1);
sigaddset(&waitmask, SIGUSR2);
sigprocmask(SIG_BLOCK, &newmask, &oldmask);
action.sa_flags = 0;
sigemptyset(&action.sa_mask);
action.sa_handler = sigHandler;
sigaction(SIGUSR2, &action, NULL);
sigaction(SIGINT, &action, NULL);
sigaddset(&action.sa_mask, SIGINT);
sigaction(SIGUSR1, &action, NULL);
/* Line A: critical section */
sigsuspend(&waitmask); /* Line B */
} else { // Parent
int stat;
kill(pid, SIGUSR1);
kill(pid, SIGUSR2);
kill(pid, SIGINT);
pid = wait(&stat);
}
return 0;
}
Although sigsuspend() is used here, there is still a race condition. Why?
Since we don't know when the parent process executes first or the child process. If the parent executes first, the parent sends SIGUSR1, SIGUSR2 and SIGINT to the child, since SIGUSR1 is now blocked, the SIGUSR2 and SIGINT will be correctly catched, but we then sigaddset(&action.sa_mask, SIGINT), which means the following line sigaction(SIGUSR1, &action, NULL) will blocked both SIGUSR1 and SIGINT. Finally, the Line B calls sigsuspend(&waitmask), which will wait signals excepts SIGUSR2. But here is my question: since sigsuspend(&waitmask) didn't block SIGUSR1, why there is still a race condition? Were the SIGUSR2 and SIGINT catched already?
Suppose the child process receives (a)SIGUSR2 at line A, (b)SIGINT when blocked at line B, and (c)SIGUSR1 when SIGINT is caught at line C, how the child process' signal mask changes? Explain your answer.
(a) The mask contains SIGUSR1 (by sigprocmask()) and SIGINT(by sigaddset()).
(b) The mask contains only SIGUSR2 (by sigsuspend()).
After sigsuspend(), the mask restore to SIGUSR1 and SIGINT.
(c) The mask contains SIGINT and SIGUSR1, so it'll block SIGUSR1.
Suppose the child process receives (a)SIGUSR1 at line A, (b)SIGUSR2 when blocked at line B, and (c)SIGINT when SIGUSR1 is caught at line C, how the child process' signal mask changes? Explain your answer.
(a) The mask contains both SIGUSR1 (by sigprocmask()) and SIGINT (by sigaddset()) and the SIGUSR1 is blocked.
(b) The mask contains only SIGUSR2(by sigsuspend()).
After sigsuspend(), the mask restore to SIGUSR1 and SIGINT.
(c) The mask contains SIGINT and SIGUSR1, so it'll block SIGINT.
This is the coursework of my class. I'm not sure whether my answer is correct or not. Can someone give me any instruction. In the first question, I can't figure out why there is a race condition, is my explanation wrong?
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
void sigHandler(int signo) {
/* Line C */
}
int main() {
pid_t pid;
sigset_t newmask, waitmask, oldmask;
if ((pid = fork()) == 0) { // Child
struct sigaction action;
sigemptyset(&newmask);
sigemptyset(&waitmask);
sigaddset(&newmask, SIGUSR1);
sigaddset(&waitmask, SIGUSR2);
sigprocmask(SIG_BLOCK, &newmask, &oldmask);
action.sa_flags = 0;
sigemptyset(&action.sa_mask);
action.sa_handler = sigHandler;
sigaction(SIGUSR2, &action, NULL);
sigaction(SIGINT, &action, NULL);
sigaddset(&action.sa_mask, SIGINT);
sigaction(SIGUSR1, &action, NULL);
/* Line A: critical section */
sigsuspend(&waitmask); /* Line B */
} else { // Parent
int stat;
kill(pid, SIGUSR1);
kill(pid, SIGUSR2);
kill(pid, SIGINT);
pid = wait(&stat);
}
return 0;
}
Although sigsuspend() is used here, there is still a race condition. Why?
Since we don't know when the parent process executes first or the child process. If the parent executes first, the parent sends SIGUSR1, SIGUSR2 and SIGINT to the child, since SIGUSR1 is now blocked, the SIGUSR2 and SIGINT will be correctly catched, but we then sigaddset(&action.sa_mask, SIGINT), which means the following line sigaction(SIGUSR1, &action, NULL) will blocked both SIGUSR1 and SIGINT. Finally, the Line B calls sigsuspend(&waitmask), which will wait signals excepts SIGUSR2. But here is my question: since sigsuspend(&waitmask) didn't block SIGUSR1, why there is still a race condition? Were the SIGUSR2 and SIGINT catched already?
Suppose the child process receives (a)SIGUSR2 at line A, (b)SIGINT when blocked at line B, and (c)SIGUSR1 when SIGINT is caught at line C, how the child process' signal mask changes? Explane your answer.
(a) The mask contains SIGUSR1 (by sigprocmask()) and SIGINT(by sigaddset()).
(b) The mask contains only SIGUSR2 (by sigsuspend()).
After sigsuspend(), the mask restore to SIGUSR1 and SIGINT.
(c) The mask contains SIGINT and SIGUSR1, so it'll block SIGUSR1.
Suppose the child process receives (a)SIGUSR1 at line A, (b)SIGUSR2 when blocked at line B, and (c)SIGINT when SIGUSR1 is caught at line C, how the child process' signal mask changes? Explane your answer.
(a) The mask contains both SIGUSR1 (by sigprocmask()) and SIGINT (by sigaddset()) and the SIGUSR1 is blocked.
(b) The mask contains only SIGUSR2(by sigsuspend()).
After sigsuspend(), the mask restore to SIGUSR1 and SIGINT.
(c) The mask contains SIGINT and SIGUSR1, so it'll block SIGINT.
I'm writing a program that forks two children to do something. These two children send two different signals to its parent when their job is done. In the meanwhile, the parent waits for its children using two pause().
However, the program stopped after the first pause() and waits for another signal at the second pause(). Using gdb, I find that two signals from children are received, but only one pause() is finished.
What is the cause of this problem?
In main:
struct sigaction parent_act;
struct sigaction child_act[2];
// set the signal handlers
parent_act.sa_handler = &p0_handler;
child_act[0].sa_handler = &p1_handler;
child_act[1].sa_handler = &p2_handler;
// set the behavior when child get signal SIGUSR1 and SIGUSR2
sigaction(SIGUSR1, &child_act[0], NULL);
sigaction(SIGUSR2, &child_act[1], NULL);
// fork two child
for(i = 0; i < 2; i++){
pid[i] = fork();
// child process
else if(pid[i] == 0){
pause(); // wait for signal
return 0;
}
}
// set the behavior when parent get signal SIGUSR1 and SIGUSR2
sigaction(SIGUSR1, &parent_act, NULL);
sigaction(SIGUSR2, &parent_act, NULL);
kill(pid[0], SIGUSR1); // signal the child to do its job
kill(pid[1], SIGUSR2); // signal the other child to do its job
pause(); // wait for child
pause(); // wait for child
In handlers:
void p0_handler(int dummy)
{
return;
}
void p1_handler(int dummy)
{
// do something
kill(getppid(), SIGUSR1); // tell parent it's done
return;
}
void p2_handler(int dummy)
{
// do something
kill(getppid(), SIGUSR2); // tell parent it's done
return;
}
First child sends SIGUSR1 to parent and the second one sends SIGUSR2. It seems the first pause() received two signals. Is that possible?
Most likely both signals arrive at the same time (or the second one arrives during execution of the signal handler) at which point the second pause will wait indefinitely.
The problem is that there is no guarantee that even if you like set a flag "One child finished" the other signal does not arrive before pause is called.
The only way to make this waterproof with signals is to use select on a timeout.
int num_signals_received = 0;
void p0_handler(int dummy)
{
// signal safe action
__sync_fetch_and_add(&num_signals_received, 1);
return;
}
instead of pause:
struct timeval timeout = {1,0}; // 1 second
while (num_signals_received < 2)
select(0, NULL, NULL, NULL, &timeout);
If you are not set on signals for synchronisation then I would advise you move to either shared memory (mmap/MAP_SHARED) or file descriptor based (pipe(2)) synchrosisation.
I have recently started programming using signals. I used them in my multithreaded server code in C. Here is the part of code pertinent to signals, but it doesn't work as it should do:
the signal Handler:
void* Ctrl_C_handler(void *arg)
{
int *sock_fd_ptr = (int*)arg;
sigset_t set;
int err, sig;
err = sigemptyset(&set); // Clear the signals holder set
if(err) perror("sigemptyset(&set)");
err = sigaddset(&set, SIGINT); // Add the SIGINT: Ctrl+C
if(err) perror("siaddset(&set, SIGINT)");
err = pthread_sigmask(SIG_UNBLOCK, &set, NULL); // Set mask as to unblock SIGINT
if(err) perror("pthread_sigmask(SIG_SETMASK, &set, NULL)");
printf("Signal handler active:\n");
while(1) // Ctrl+C
{
err = sigwait(&set, &sig); // Wait for signal to occur
if(err)
{
perror("sigwait(&set, &sig)");
printf("Error handling the signal, SigHandlerThread exiting..\n");
break;
}
if(sig == SIGINT)
{
printf("Ctrl+C detected by server !!\n");
printf("No more connections will be accepted!!");
if(*sock_fd_ptr > 0)
{
close(*sock_fd_ptr);
*sock_fd_ptr = -1;
break;
}
}
}
return NULL;
}
Inside Main():
/*********** Signal Handling *************/
sigset_t set; // declare a set to hold the signals
err = sigfillset(&set); // Fill the set with all the signals
if(err) perror("sigfillset(&set)");
err = sigthreadmask(SIG_BLOCK, &set, NULL); // Block/Mask the signals in the set
if(err) perror("sigthreadmask(SIG_BLOCK, &set, NULL)");
err = pthread_create(&sig_handler_tid, NULL, Ctrl_C_handler, (void *)&sock_fd);
// Create a thread for handling signals
if(err) perror("pthread_create");
I read this method here. I tried sending kill -s SIGINT <pid of my program> from different terminal window, but the program exits.
When the SIGINT is sent to your process, it is delivered to the only thread that has it unblocked, your Ctrl_C_handler thread. Signal delivery means taking whatever action is associated with the signal, and the default action for SIGINT is, as you know, process termination.
But why doesn't sigwait() calmly intercept the signal, as you intend?
sigwait() is designed to remove a signal from a mask of pending signals — that is, generated signals whose delivery is suspended (SIG_BLOCKed) — without the usual asynchronous drama of signal delivery.
So, don't SIG_UNBLOCK SIGINT in your thread. Instead, keep it blocked and the code will work as you intend. If you scrutinize the reference you provided, you'll see that that sample code blocks all signals before calling sigwait().