help on interprocess communication

John Mundt john at chinet.chi.il.us
Wed Feb 21 01:21:09 AEST 1990


In article <6456 at cps3xx.UUCP> bruey at 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<stdio.h>
>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 at admctr.chi.il.us *OR* fred at teacha.chi.il.us
(312) 998-5007 (Day voice) || -432-8860 (Answer Mach) && -432-5386 Modem  



More information about the Comp.lang.c mailing list