on receiving a SIGUSR1 signal, I want to display the value read by the child from the pipe.
Having a little issue. It is always displaying 0 despite getppid() was written to pipe by parent process. Any solution? `
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
char bufP[10], bufC[10];
int gpid;
void handler(int signum){
signal(SIGUSR1, handler);
if (signum == SIGUSR1){
printf("SIGUSR1 received\n");
gpid = atoi(bufC);
printf("Grandparent: %d\n", gpid);
exit(0);
}
}
int main(void){
int pid, fd[2];
pipe(fd);
pid = fork();
signal(SIGUSR1, handler);
if (pid == 0){
//child
close(fd[1]);
read(fd[0], bufC, sizeof(bufC));
close(fd[0]);
}else{
//parent
close(fd[0]);
sprintf(bufP, "%d", getppid());
write(fd[1], bufP, sizeof(bufP));
kill(pid, SIGUSR1);
close(fd[1]);
}
}
`
Thanks for your response.