Why doesn't this work (fork and pipe problem) ???

Dale Anglin dale at mddc.UUCP
Tue Jul 17 21:23:41 AEST 1984


[]

I have a need to read the "who" list into a program buffer.  I wrote a test
program to verify that it would work.  The output of the following program
is as expected except that the program seems to hang up in the "read" call.

CRT dialog...
% cc tst.c -o tst
% tst
about to read who
chris    ttyj5   Jul 17 08:15
dale     ttyj3   Jul 17 08:31

... at this point, "tst" does not return to the shell.  It can be ^Z'd and
^C'd, but does not terminate as expected.
file tst.c cut here -----------------------------------------------
/*
 *-- Why doesn't this work ???
 *
 *	Program is supposed to execute and read the "who" processor output
 */

main()
{
	int	pipefd[2];
	int	pid;
	int	chkpid;
	char	buffer[100];


	pipe(pipefd);

	switch(pid=fork())
	{
	case -1:				/* error */
	    printf("could not fork\n");
	    exit(0);

	case 0:					/* child */
	    close(1);
	    dup(pipefd[1]);
	    printf("about to exec who\n");
	    execlp("/bin/who", "who", (char*)0);
	    printf("could not execute\n");
	    exit(0);

	default:				/* parent */
	    close(0);
	    dup(pipefd[0]);

	    printf("about to read who\n");
	    while (read(0,buffer,1) == 1)
	    {
		putchar(buffer[0]);
	    }

	    printf("after who output\n");
	    while ((chkpid = wait(0)) != pid && chkpid > 0) /* null */;
	}

	printf("about to exit\n");

	exit(0);
}
end of file tst.c cut here ----------------------------------------

I was hoping the "read" statement would EOF on the pipe.

Any ideas ?   Thanks in advance.

Dale Anglin
Management Decisions Development Corp.
7209 Dixie Highway
Fairfield, Ohio   45014
(513) 874-6464

...{ucbvax,decvax,inhp4,mhuxi}!cbosgd!mddc!dale			(uucp)



More information about the Comp.unix.wizards mailing list