Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!usc!brutus.cs.uiuc.edu!ux1.cso.uiuc.edu!tank!gargoyle!chinet!john From: john@chinet.chi.il.us (John Mundt) Newsgroups: comp.lang.c Subject: Re: help on interprocess communication Message-ID: <1990Feb20.142109.15663@chinet.chi.il.us> Date: 20 Feb 90 14:21:09 GMT References: <6456@cps3xx.UUCP> Reply-To: john@chinet.chi.il.us (John Mundt) Distribution: usa Organization: Chinet - Chicago Public Access UNIX Lines: 53 In article <6456@cps3xx.UUCP> bruey@cpsin2.cps.msu.edu () writes: > >Can any of you brainy netlanders give me a hint why this >doesn't work? I've been cursing at this code for days >and I can't get it to work. (I'm not even sure that the >forks are in the right order. So basically, I'm lost.) > > >#include >main() >{ > int pid; > int id1,id2,id3; >int rc=0,status,parent; >int filedes[2]; >char *a,*buf1,*buf; > > > pipe(filedes); > if (fork()) > { > printf("%d %d are filedes \n",filedes[0],filedes[1]); > buf = "first message"; > write(filedes[1],buf,14); > if(fork()) > { > read(filedes[1],buf1,14); > printf("in second %s \n",buf1); > } > } > >} When you fork, you have two processes, the child and the parent. The child process gets a return of 0 from fork if all goes well, the parent the process ID of the child. The normal way to do the above is to put it in a switch statement like this: switch(fork()) { case -1 : horrible_error_routine; exit(1); /* error */ case 0 : do_child_thing(); /* like write to pipe */ exit(0); /* and die right away */ default : do_parent_thing(); /* like read the pipe */ } Yours failed above since you never had a child process do anything. You never left the parent process. And, you only need one fork. Hope this helps. -- --------------------- John Mundt Teachers' Aide, Inc. P.O. Box 1666 Highland Park, IL john@admctr.chi.il.us *OR* fred@teacha.chi.il.us (312) 998-5007 (Day voice) || -432-8860 (Answer Mach) && -432-5386 Modem