#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 p2c[2]; //An array of two ints. p[0] is for reading, p[1] is for writing. int c2p[2]; pipe (p2c); //Set up the pipe. pipe (c2p); int pid = fork (); if (pid==0) { //If I'm the child close (p2c[1]); close (c2p[0]); int strlength; char *info = readstring (p2c[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); for (int i = 0; i < strlength; i++) if (info[i] >= 97 && info[i] <= 122) info[i]-=32; write (c2p[1],info,strlength); write (c2p[1],"\n",1); free (info); close (p2c[0]); close (c2p[1]); } else { //If I'm the parent close (p2c[0]); close (c2p[1]); write (1,"Enter a string: ",17); int fkblen; char *fromkb = readstring (0,&fkblen); write (p2c[1],fromkb,fkblen); write (p2c[1],"\n",1); free (fromkb); fromkb = readstring (c2p[0],&fkblen); write (1,"From child: ",13); write (1,fromkb,fkblen); write (1,"\n",1); wait (NULL); write (1,"Child has died!\n",16); free (fromkb); close (p2c[1]); close (c2p[0]); } 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; }