I am Trying to send the exit code of child proccess when raise "SIGUSR1" i worte a handler that waits for the process but it does not recive the exit code correctly istead it recives -1
this is my code at main
void main()
{
signal(SIGUSR1, handler);
int pid = fork();
if (pid == -1)
perror("error in forking");
else if (pid == 0)
{
printf("\ni am child and my pid %d\n", getpid());
raise(SIGUSR1);
exit(100);
}
sleep(5);
printf("\nnow i am the parent and my = %d\n ", getpid());
}
and this is the handler code
void handler(int signunm)
{
int pid, stat_loc;
pid = wait(&stat_loc);
if (!(stat_loc & 0x00FF))
printf("\nI am called with the process %d with value %d\n", pid, stat_loc >> 8);
}
and the output something like
i am child and my pid 45892
I am called with the process -1 with value 4
now i am the parent and my = 45891
and it should be 100
not -1
You should use kill(PPID, SIGUSR1)
function not the raise(SIGUSR1)
function so you send a signal to the parent to handle it not to the child itself. You can get the PPID
through calling getppid()