#include #include int main() { int pipes[2]; int messagelen = 22; char data[50]; int childPID; pipe(pipes); childPID = fork(); if(childPID != 0) { // remember - if fork returns a non-zero value, this is the parent close(pipes[0]); // the parent doesn't read - just writes. strcpy(data, "Go clean your room! (from Parent)"); messagelen = strlen(data); write(pipes[1], &messagelen, sizeof(int)); write(pipes[1], data, messagelen * sizeof(char) + 1); //Add 1 for \0 //at end of string printf("Message Sent, length= %d\n", messagelen); } else { close(pipes[1]); // and the child just reads, with no writing read(pipes[0], &messagelen, sizeof(int)); read(pipes[0], data, messagelen * sizeof(char) + 1); printf("Uh oh - I've (child %d) been told: \"%s\" \n(len=%d)\n", getpid(), data, messagelen); } } /* gcc -o fork2 fork2.c ./fork2 Message Sent, length= 33 Uh oh - I've (child 18985) been told: "Go clean your room! (from Parent)" (len=33) */