Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!utah-gr!sunset.utah.edu!u-jeivan
From: u-jeivan%sunset.utah.edu@utah-gr.UUCP (Eric Ivancich)
Newsgroups: comp.unix.questions
Subject: Question on Deadlock
Keywords: deadlock
Message-ID: <2676@utah-gr.UUCP>
Date: 22 Jun 88 00:40:30 GMT
Sender: news@utah-gr.UUCP
Reply-To: u-jeivan%ug.utah.edu.UUCP@utah-gr.UUCP (Eric Ivancich)
Organization: University of Utah Computer Science Department
Lines: 86

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 


#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@ug.utah.edu
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||