Hello,
I am currently writing a program to create two processes and then have them communicate using a pipe. Here is my code:
Code:
#include <stdio.h>
int main() {
int pid, processes[2];
int fork();
pipe(processes);
pid = fork();
if (pid == 0) {
close(processes[0]);
char buf[BUFSIZ];
char message[] = "Hi, this pid 0. Here is something for you!";
printf("Sending message: %s\n",message);
write(processes[1],message, strlen(message)+1);
printf("pid %d send completed\n", pid);
}
else {
close(processes[1]);
char buf[BUFSIZ];
while ( read(processes[0],buf,BUFSIZ) > 0 )
printf("pid %d received message: %s \n",pid,buf);
}
}
This succeeds in sending a message one way, but how would I go about sending both ways? Basically, what I want to do is have the process that reads the message be able to send a reply back to the process it received the message from. Also, how would I get the time that the message was sent at? I have tried a number of different things to get both of these to work and none have done so. Any help would be greatly appreciated. Thanks.