I am trying to send two messages from the parent to the receiver. Only one is received. Receiver uses stdin and stdout for the pipe, and outputs its results into std err. Here is my code.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
int main(int argc,char * argv[])
{
char buffer[100]; // The pipe's buffer
int pipes[2];
pid_t childpid;
if ( pipe(pipes) ){
fprintf(stderr,"FATAL ERROR IN PIPE");
}
if((childpid = fork()) == -1){
perror("fork");
exit(1);
}
if(childpid == 0){
close(pipes[1]);
dup2(pipes[0],STDIN_FILENO);
scanf("%s\n",buffer);
fprintf(stderr,"REC: %s\n",buffer);
scanf("%s\n",buffer);
fprintf(stderr,"REC: %s\n",buffer);
sleep(50);
}
else
{
close(pipes[0]);
// Read in a string from the pipe
char* arr = "HelloWorld\n";
write(pipes[1],arr,strlen(arr)+1);
write(pipes[1],arr,strlen(arr)+1);
sleep(50);
}
return 0;
}