Skip to content

Instantly share code, notes, and snippets.

@nakkaya
Created April 12, 2011 16:06
Show Gist options
  • Save nakkaya/915801 to your computer and use it in GitHub Desktop.
Save nakkaya/915801 to your computer and use it in GitHub Desktop.
#include <sys/wait.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char *argv[]){
//write end [1] read end [0]
int pipe_p_c1[2];
int pipe_c1_p[2];
int pipe_c1_c2[2];
int pipe_c2_p[2];
pipe(pipe_p_c1);
pipe(pipe_c1_p);
pipe(pipe_c1_c2);
pipe(pipe_c2_p);
if (fork() == 0) { // C1
printf("C1 Here.\n");
char ch[7];
read(pipe_p_c1[0], &ch, 7);
printf("From P in C1 %s \n", ch);
write(pipe_c1_p[1], "Kyrenia", 7);
write(pipe_c1_c2[1], "Baphos", 6);
} else {
if (fork() == 0) { // C2
printf("C2 Here.\n");
char ch[6];
read(pipe_c1_c2[0], &ch, 6);
printf("From C1 in C2 %s \n", ch);
write(pipe_c2_p[1], "Larnaca", 7);
}else{
printf("P Here.\n");
write(pipe_p_c1[1], "Nicosia",7);
char ch[7];
read(pipe_c1_p[0], &ch, 7);
printf("From C1 in P %s \n", ch);
read(pipe_c2_p[0], &ch, 7);
printf("From C2 in P %s \n", ch);
wait(NULL);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment