am having a little issue to make the program count the number of times a signal is sent to the child process. Wrong value is being displayed.
The parent should send each element of the array through the pipe to the child, then the child should read and display what was sent. count
holds the number of signals sent to child.
Any help please?
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <signal.h>
int count = 0;//number of SIGUSR1 signal sent to child by parent
void sigHandler(int signum){
signal(SIGUSR1, sigHandler);
if (signum == SIGINT){
printf("SINGINT Received\n");
count++;
}
}
int main(void){
int pid, fd[2], ato, i;
pipe(fd);
pid = fork();
int x[] = {1, 2, 3, 4, 5, 6, 7};
char bufP[3], bufC[3];
signal(SIGINT, sigHandler);
if (pid == 0){
//child
printf("I am Child\n");
close(fd[1]);
read(fd[0], bufC, sizeof(bufC));
close(fd[0]);
ato = atoi(bufC);
printf("Child read: %d\n", ato);
sleep(2);
}else{
//parent
printf("I am Parent\n");
for (i=0; i<7; i++){
close(fd[0]);
sprintf(bufP, "%d", x[i]);
write(fd[1], bufP, sizeof(bufP));
close(fd[1]);
sleep(1);
kill(pid, SIGINT); //sending signal to child
}
}
printf("Count: %d\n", count); //number of times signal was sent
}
Thanks