Sighandler doesn,t work
Dec 9, 2013 at 8:38am UTC
Hy friends, I've written this code, it receives signal but does not call signal handler, how can I fix it ?(edited) it should be implemented by using sigwait
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<signal.h>
void sig_child_handler(int unused){
write(STDERR_FILENO, "SigChild Recieved\n" , 18);
}
int main(int argc, char argv[]){
int sig;
struct sigaction sact;
sact.sa_handler = sig_child_handler;
sigemptyset(&sact.sa_mask);
sact.sa_flags = 0;
sigaction(SIGCHLD, &sact, NULL);
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGCHLD);
sigprocmask(SIG_BLOCK, &set, NULL);
sigwait(&set, &sig);
fprintf(stderr, "sig = %d\n" , sig);
return 0;
}
Last edited on Dec 10, 2013 at 7:44pm UTC
Dec 10, 2013 at 10:36am UTC
anyone ???
Dec 10, 2013 at 3:16pm UTC
In futrue, it would help if you said what you want the program to do as well as posting the code. If the compiler can't figure it out, then neither can I.
I've assumed you're trying to handle the SIGCHLD signal. That signal notifies you that a child process has terminated.
I've modified your example to make it create a child the exits, so you can see the handler in action.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include <unistd.h>
#include <signal.h> // sigaction
#include <sys/wait.h> // waitpid
#include <stdio.h> // printf
#include <string.h> // strlen
void sig_child_handler(int unused)
{
static const char msg[] = "SIGCHLD Recieved\n" ;
write(STDERR_FILENO, msg, strlen(msg));
}
int main()
{
// SIGCHLD: Request notification of a child process terminating
struct sigaction sact;
sact.sa_handler = sig_child_handler;
sigemptyset(&sact.sa_mask);
sact.sa_flags = 0;
sigaction(SIGCHLD, &sact, NULL);
// Create a child and let it termiinate
int child_pid = fork();
if (child_pid == 0)
{
// Child: terminate with code 20
return 20;
}
// Parent: Wait for child to termiinate
int status;
waitpid(child_pid, &status, 0);
printf("child terminated with code %d\n" , WEXITSTATUS(status));
return 0;
}
Dec 10, 2013 at 7:39pm UTC
yes, I want to call it by sigwait function ... like I did in code and I am sending child signal to this process from command line.
Topic archived. No new replies allowed.