#include #include #include #include #include #include typedef struct CLL { char ch; struct CLL *next; } CLL; char *readstring (int fd, int *len); int main (int argc, char **argv) { int p[2]; //An array of two ints. p[0] is for reading, p[1] is for writing. pipe (p); //Set up the pipe. int pid = fork (); if (pid==0) { //If I'm the child // dup2 (p[0],0); //Redirect standard input to connect to the reading end of the pipe // close (p[0]); close (p[1]); int strlength; char *info = readstring (p[0],&strlength); // Reads a string from standard input, which is now the pipe! write (1,"From my parent: ",17); write (1,info,strlength); write (1,"\n",1); } else { //If I'm the parent close (p[0]); write (1,"Enter a string: ",17); int fkblen; char *fromkb = readstring (0,&fkblen); write (p[1],fromkb,fkblen); write (p[1],"\n",1); wait (NULL); write (1,"Child has died!\n",16); } return 0; } char *readstring (int fd, int *len) { CLL *L = NULL; int ct = 0; char buf; char *final; while (1) { read (fd,&buf,1); if (buf=='\n') break; CLL *N = malloc (sizeof (CLL)); N->ch = buf; N->next = L; L = N; ct++; } final = malloc (ct+1); *len = ct; final[ct] = '\0'; while (ct > 0) { ct--; final[ct] = L->ch; CLL *N = L; L = L->next; free (N); } return final; }