Question on Deadlock

Eric Ivancich u-jeivan%sunset.utah.edu at utah-gr.UUCP
Wed Jun 22 10:40:30 AEST 1988


I have a question concerning deadlock.  I fear that everyone knows
this but me, but I have to learn somehow.  I understand the basic
concept of deadlock--two (in a simple case) entities, both waiting for
an action of the other.  However, I do not understand why the attached
code produces deadlock.

Provided Segment_B is commented out, everything works.  Segment_A
fires and then Segment_C does.

But, if Segment_B is not commented out, I get deadlock; only Segment_A
fires.  This is not how I thought it would run.  It seems that
Segment_A should fire, followed by Segment_C, followed by Segment_D,
and then Segment_B.  What is wrong with my logic?

Please respond by e-mail, and I will summarize to the net.

Thanks,

Eric

-------------------------------------------------------------------------------

#include <stdio.h>


#define STDIN	0
#define STDOUT	1
#define STDERR	2
#define READ	0
#define WRITE	1


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]);

	close (STDOUT);			/* parent writes to child */
	dup (p2c [WRITE]);
	close (p2c [WRITE]);

					/* SEGMENT_A */
	fprintf (stderr, "Parent sends message\n");
	printf ("Parent to child\n");

					/* 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]);

	close (STDOUT);			/* child writes to parent */
	dup (c2p [WRITE]);
	close (c2p [WRITE]);

					/* SEGMENT_C */
	gets (buffer);
	fprintf (stderr, "Child receives: %s\n", buffer);

					/* SEGMENT_D */
	fprintf (stderr, "Child sends message\n");
	printf ("Child to parent\n");
    }
}

||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||



More information about the Comp.unix.questions mailing list