Question on Deadlock

Root Boy Jim rbj at cmr.icst.nbs.gov
Fri Jun 24 09:07:28 AEST 1988


? From: Eric Ivancich <u-jeivan%sunset.utah.edu at utah-gr.uucp>

Below is a fragment the deadlock program:

? main ()
? {
?     int  p2c [2],
? 	 c2p [2];
?     char buffer [128];
?     pipe (p2c);				/* create pipes */
?     pipe (c2p);
?     if (fork ())  {
? 					/* PARENT */
? 	close (STDIN);			/* parent reads from child */
? 	dup (c2p [READ]);
? 	close (c2p [READ]);
ADD: 	close (c2p [WRITE]);
? 	close (STDOUT);			/* parent writes to child */
? 	dup (p2c [WRITE]);
? 	close (p2c [WRITE]);
ADD: 	close (p2c [READ]);
? 					/* SEGMENT_A */
? 	fprintf (stderr, "Parent sends message\n");
? 	printf ("Parent to child\n");
ALSO:	fclose(stdout);
? 					/* SEGMENT_B */
? /*	gets (buffer);
?  *	fprintf (stderr, "Parent receives: %s\n", buffer);
?  */
?     }  else  {
? 					/* CHILD */
? 	close (STDIN);			/* child reads from parent */
? 	dup (p2c [READ]);
? 	close (p2c [READ]);
ADD: 	close (p2c [WRITE]);
? 	close (STDOUT);			/* child writes to parent */
? 	dup (c2p [WRITE]);
? 	close (c2p [WRITE]);
ADD: 	close (c2p [READ]);
? 					/* SEGMENT_C */
? 	gets (buffer);
? 	fprintf (stderr, "Child receives: %s\n", buffer);
? 					/* SEGMENT_D */
? 	fprintf (stderr, "Child sends message\n");
? 	printf ("Child to parent\n");
ALSO:	fclose(stdout);
?     }
? }

While this is not your problem, you have not closed all the unneeded pipes.
I have indicated the extra close statements needed with `ADD:' in column one.
Remember that the child will inherit *all* open file descriptors from the
parent, and thus both ends of the pipes need to be closed in both the
parent and the child.

If you run the program with the `ADD:' and the `ALSO:' lines, it should
(hopefully) run, as the close flushes buffers for you.

I suppose I could also quibble with your coding convention; I dislike
spaces between the array and its subscript, between the function name
and its argument list, as well as spaces around the `->', `.', `++', and
`--' operators, and unary `*', `!', `~', and `&', which you didn't use,
but I have seen elsewhere. The rationale is that all these operators bind
so tightly (in fact I consider `->', `.', and `[]' to be more like variable
name specifiers than `operators') that they shoul be written that way.
Of course, it's all a matter of taste, which you are free to define.

? |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
? They pelted us with rocks and garbage.    - Late Night with David Letterman
? INFO: Eric Ivancich : University of Utah
? UUCP: {ihnp4, hplabs, decvax, arizona}!utah-ug!u-jeivan
? ARPA: u-jeivan at ug.utah.edu
? |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

	(Root Boy) Jim Cottrell	<rbj at icst-cmr.arpa>
	National Bureau of Standards
	Flamer's Hotline: (301) 975-5688
	The opinions expressed are solely my own
	and do not reflect NBS policy or agreement
	Careful with that VAX Eugene!



More information about the Comp.unix.questions mailing list