*BSD News Article 79592


Return to BSD News archive

Path: euryale.cc.adfa.oz.au!newshost.carno.net.au!harbinger.cc.monash.edu.au!nntp.coast.net!howland.erols.net!EU.net!usenet2.news.uk.psi.net!uknet!usenet1.news.uk.psi.net!uknet!dispatch.news.demon.net!demon!awfulhak.demon.co.uk!awfulhak.demon.co.uk!awfulhak.demon.co.uk!not-for-mail
From: brian@awfulhak.demon.co.uk (Brian Somers)
Newsgroups: comp.unix.bsd.freebsd.misc
Subject: Re: Blocking with pipes.
Date: 30 Sep 1996 21:44:55 +0100
Organization: Coverform Ltd.
Lines: 44
Message-ID: <52pbg7$mg@anorak.coverform.lan>
References: <52k448$sjj@herald.concentric.net>
NNTP-Posting-Host: localhost.coverform.lan
X-NNTP-Posting-Host: awfulhak.demon.co.uk
X-Newsreader: TIN [version 1.2 PL2]

Andrew Verba (averba@eden.rutgers.edu) wrote:

: In a program that I am trying to write, I have

: switch(fork()) {
: case 0:
: 	dup2(fd[1],STDOUT_FILENO);
: 	dup2(fd[0],STDIN_FILENO);
: 	close(fd[1]);
: 	close(fd[0]);
:  	execvp(argv[0],argv);
: 	perror("BLA");
: 	exit(0);
: default:
: 	write(fd[1],buffer,counter);
: 	close(fd[1]);
: 	
: 	while(read(fd[0],string,50) > 0 ) {
: 		do something with string;
: 	}
: 	close(fd[0]);
: 	break;
: }

: Why does this read some of the input from the executing file and then
: hang (block) ?

What does the executing program do ?  You've set up a scenario where the
above program writes "buffer" to the pipe, then reads it again !  At
the same time, you've got argv[0] running - also potentially both reading
and writing from/to the same pipe.

When you run this sort of thing, the first line the above "read" sees is
whatever buffer contains - it's read back immediately.  If you're lucky
however, your "executed program" may manage to read it first - or even
write something first so that the above read can read that something
before seeing "buffer" - or maybe it'll never see "buffer" 'cos argv[0]
gets it....

The scenario is silly - it's just a pile of race conditions.

--
Brian <brian@awfulhak.demon.co.uk>
Don't _EVER_ lose your sense of humour....