So. I have to read and write from a named pipe and I tried to accomplish this by doing the following : Parent process writes a command to a FIFO then waits for the kid to die. The child process reads the command, and writes something in another fifo. The parent Parent reads the message from the other FIFO and prints the result. I am restricted to using fopen because in my code I use the open from unistd elsewhere, so I don't have the nonblock flag. I ll attach a snippet below. Also, if I use the "w" flag on the child process, the code freezes completely. I get FIFO is a blocking way of communication but I don't get why my parent can't read. Thanks in advance.
int main()
{
FILE *fp1;
FILE* fp2;
unlink(FIFO_NAME1);
unlink(FIFO_NAME2);
//FIFO_NAME[1|2] is a macro containing the FIFO path
if(mkfifo(FIFO_NAME1, 0666) == -1)
{
printf("Something went wrong");
return -1;
}
if(mkfifo(FIFO_NAME2, 0666) == -1)
{
printf("Something went wrong");
return -1;
}
while(1)
{
char command[MAX_SIZE];
pid_t pid;
if((pid=fork()) !=0)
{
//parent
fgets(command, MAX_SIZE, stdin);
if((fp1= fopen(FIFO_NAME1, "w")) == NULL)
{
printf("Error");
}
fprintf(fp1, "%s", command);
fclose(fp1);
wait(SIGKILL);
//it never prints this or execute past the wait
printf("Here \n");
char response[MAX_SIZE];
if((fp2= fopen(FIFO_NAME2,"r"))==NULL)
perror("");
fgets(response, MAX_SIZE, fp2);
fclose(fp2);
//printf("%s",response);
}
else
{
//child
char msg[MAX_SIZE];
if((fp1 = fopen(FIFO_NAME1, "r")) == NULL)
{
printf("Error");
}
fgets(msg, MAX_SIZE, fp1);
fclose(fp1);
if((fp2= fopen(FIFO_NAME2, "w+")) == NULL)
{
printf("Error");
}
char buffer[MAX_SIZE];
strcpy(buffer,"TEST MESSAGE");
fprintf(fp2, "%s", buffer);
fclose(fp2);
fflush(stdout);
kill(getpid(), SIGKILL);
}
}
}