My program has two child processes and a parent. Every time the first child program gets a SIGINT signal, it's supposed to pipe the message "Record created" to it's parent, and then the parent is supposed to write that message into a specific file.
My problem is that it only does it the first time and doesn't do it anymore afterwards, so when it gets the first SIGINT, it pipes and writes properly, but every SIGINT afterwards, it doesn't write the message for some reason.
Here's the main for my program.
Code:
void main(void)
{
sigignore(SIGINT);
sigignore(SIGQUIT);
sigset(SIGTSTP);
int child1, child2; //the two forked processes
int fd[2]; //
char reader[150]; //buffer for the pipe
FILE *logfile;
int bytes;
pipe(fd);
child1 = fork(); //forking for the first child
if (child1 > 0) //checking to fork a parent and not a child
{
child2 = fork(); //second child is created
}
pid_t pid;
int status;
if (child1 == 0)
{
sigset(SIGINT, got_int);
write(fd[1], "Record created", (strlen("Record created")+1));
for(;;);
_exit(1);
}
if (child2 == 0)
{
if(sigset(SIGQUIT, got_quit) == 1) {
write(fd[1], "Record found", (strlen("Record found")+1));
}
else
write(fd[1], "Record not found", (strlen("Record not found")+1));
for(;;);
_exit(2);
}
else {
logfile = fopen("a1.log", "a+b");
bytes = read(fd[0], reader, sizeof(reader));
fprintf(logfile, "%s\n", reader);
fclose(logfile); //writing to the logfile
}
pid = wait(&status); //two waits since there's two child processes
pid = wait(&status);
}
The important code is the child1 and the parent program which is this part
Code:
else {
logfile = fopen("a1.log", "a+b");
bytes = read(fd[0], reader, sizeof(reader));
fprintf(logfile, "%s\n", reader);
fclose(logfile); //writing to the logfile
}
Any suggestions on how I should modify it?