Why is this code not working as expected? - c

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().

Related

C - Use sigsuspend in a thread doesn't work

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.

Block SIGCHILD in program with multiple custom signal handlers

I have a program that needs to use one custom signal handler for SIGINT and one for SIGCHILD.
I therefore added two struct sigaction called sigchildStruct and sigintStruct, and used sigaction to define two custo signal handling functions: handleSigInt() and handleSigChild()
First of all, is this the way you are supposed to do it; needing to register two separate sigaction structs?
Second of all, i need to block SIGCHILD during part of the code execution, i only want to receive the signal at one place in the code, so i used:
sigdelset(&sigchildStruct.sa_mask,SIGCHLD);
// Catch SIGCHILD signal here
sigaddset(&sigchildStruct.sa_mask,SIGCHLD);
Is that how you would do that? More importantly: there are two sigaction structs, but do i only need to change the sa_mask on one of them, or on both? Now i only changed the sa_mask on the struct called sigchildStruct, and not on the one called sigintStruct.
The rest of the code:
void handleSigchild(int sig) {
int childPID,childExitStatus;
printf("\nSIGCHILD received\n");
while ((childPID = waitpid(-1,&childExitStatus,WNOHANG)) >0) {
if (childExitStatus==2) {printf("Background process: %d%s",childPID," terminated by SIGINT\n");}
else if (childExitStatus!=0) {printf("Background process: %d%s",childPID," unknown command\n");}
printf("Background process: %d%s\n",childPID," has exited");
}
}
void handleSigInt(int sig) {
// SIGINT will be sent to all child processes so nothing needs to be done
printf("\nSIGINT received\n");
}
int main(int argc, char ** argv) {
// Sigaction for SIGCHILD
struct sigaction sigchildStruct;
sigchildStruct.sa_handler = &handleSigchild;
sigemptyset(&sigchildStruct.sa_mask);
sigaddset(&sigchildStruct.sa_mask,SIGCHLD);
sigchildStruct.sa_flags = SA_NOCLDSTOP;
if (sigaction(SIGCHLD, &sigchildStruct, 0) == -1) {
printf("Couldnt register signal handler: %s\n",strerror(errno));
exit(1);
}
// Sigaction for SIGINT
struct sigaction sigintStruct;
sigintStruct.sa_handler = &handleSigInt;
sigemptyset(&sigintStruct.sa_mask);
sigintStruct.sa_flags = SA_RESTART;
if (sigaction(SIGINT, &sigintStruct, 0) == -1) {
printf("Couldnt register signal handler: %s\n",strerror(errno));
exit(1);
}
sigdelset(&sigchildStruct.sa_mask,SIGCHLD);
// Catch SIGCHILD signal here
sigaddset(&sigchildStruct.sa_mask,SIGCHLD);
}
Because nobody answered, I'll try to do so. When I am dealing with signals I am using the function signal. This function takes two parameters: signal code and signal handler function. To start catching the signals I would put the code:
signal(SIGINT, handleSigInt);
signal(SIGCHILD, handleSigChild);
To stop catching the signal, I would execute:
signal(SIGINT, SIG_IGN);
signal(SIGCHILD, SIG_IGN);
Hope it helps.
There is several mistakes in your code.
1) It is not necessary to add SIGCHLD signal in the set of blocked signals during its handling. Every catched signal is blocked during its handling, so that there is no concurrent reentrance in the handling function :
struct sigaction sigchildStruct;
sigchildStruct.sa_handler = &handleSigchild;
sigemptyset(&sigchildStruct.sa_mask);
// sigaddset(&sigchildStruct.sa_mask,SIGCHLD); // unnecessary
sigchildStruct.sa_flags = SA_NOCLDSTOP;
2) You modified the sa_mask value of the struct after setting the action, which has no effect. If you want to block the delivery of signals for some time you need to modify the process signal mask. There is a function to do that : sigprocmask. So you can do something like :
sigset_t oldMask, newMask;
sigemptyset(&newMask);
sigaddset(&newMask,SIGCHLD);
sigprocmask(SIG_BLOCK,&newMask,&oldMask);
// now protected from SIGCHLD delivery, signal will be blocked...
sigprocmask(SIG_SETMASK,&oldMask,NULL);
// old protection reinstalled...

Using two pause() to catch two signals but failed

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.

posix threads block signal and unblock

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.

Non-blocking check for signals in a loop

I have a thread in an application that has a loop like this:
...
while (1)
{
checkDatabase();
checkChildren();
sleep(3);
}
...
checkDatabase() is self-explanatory; checkChildren() simply calls waitpid(-1, &status, WNOHANG) to deal with child processes that have either exited or received a signal.
The application works fairly well, but it has default signal handling. The problem is that this parent process has a number of threads (don't worry about child processes for now) and I don't have any experience with synchronous signals, let alone in a POSIX threads application. I have used signal() before but apparently it's non-portable and it doesn't do what I need anyway. I have no experience at all with sigaction methods, and I can't find good documentation on how to fill in the structs and so on.
What I need to do is to synchronously catch terminating signals like SIGINT, SIGTERM and SIGQUIT in the above loop (and I need to ignore SIGPIPE altogether so that I can catch the EPIPE error from IO methods), so it would look like this:
...
while (1)
{
checkDatabase();
checkChildren();
checkForSignals();
sleep(3);
}
...
All other threads should not have anything to do with the signal; only the thread that executes this loop should be aware of it. And, obviously, it needs to be a non-blocking check so the loop doesn't block during its first iteration. The method called if a signal is found will sort out the other threads and destroy mutexes, and all that.
Could anyone please give me a heads-up? Many thanks.
(Following the question's comments, and for completeness, this solution tries to avoid signal handlers.)
It is possible to block signals from being raised through sigprocmask() (or, rather, pthread_sigmask() since you're using threads). From there on, the signals that were raised but blocked are available through sigpending().
Therefore, you could do something like (error checking omitted for brevity):
sigset_t blocked;
sigemptyset(&blocked);
sigaddset(&blocked, SIGINT);
sigaddset(&blocked, SIGTERM);
sigaddset(&blocked, SIGQUIT);
pthread_sigmask(SIG_BLOCK, &blocked, NULL); // Block SIGINT, SIGTERM and SIGQUIT.
signal(SIGPIPE, SIG_IGN); // Ignore SIGPIPE.
Then, later:
void checkForSignals(void)
{
sigset_t pending;
sigpending(&pending);
if (sigismember(&pending, SIGINT)) {
// Handle SIGINT...
}
if (sigismember(&pending, SIGTERM)) {
// Handle SIGTERM...
}
if (sigismember(&pending, SIGQUIT)) {
// Handle SIGQUIT...
}
}
Since sigpending() does not block, this seems to match your requirements.
Create a signal handler for SIGINT, SIGTERM and SIGQUIT, using the same function. In that signal function just set a flag that can be polled in your loop.
Something like this:
/* Global variable, will be set to non-zero if SIGINT, SIGTERM or SIGQUIT is caught */
int term_signal_set = 0;
void my_signal_handler(int)
{
term_signal_set = 1;
}
/* ... */
signal(SIGINT, my_signal_handler);
signal(SIGTERM, my_signal_handler);
signal(SIGQUIT, my_signal_handler);
signal(SIGPIPE, SIG_IGN); /* So functions return EPIPE */
while (1)
{
/* ... */
if (term_signal_set > 0)
break; /* Or do something else */
sleep(3);
}
In a multithreaded application receiving a signal, there is no predetermination, which thread receives the signal. Typical workaraounds include setting a global variable in the signal handler and checking it from a dedicated thread.
So in your case the signal handler (called from whatever thread) would just set something like a global variable for the signal received, and in CheckForSignals() you would test it.
sigaction is the way to go. man sigaction should help you. Here is an example from the web
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
struct sigaction act;
void sighandler(int signum, siginfo_t *info, void *ptr)
{
printf("Received signal %d\n", signum);
printf("Signal originates from process %lu\n",
(unsigned long)info->si_pid);
}
int main()
{
printf("I am %lu\n", (unsigned long)getpid());
memset(&act, 0, sizeof(act));
act.sa_sigaction = sighandler;
act.sa_flags = SA_SIGINFO;
sigaction(SIGTERM, &act, NULL);
// Waiting for CTRL+C...
sleep(100);
return 0;
}

Resources