Pipes...

Michael Corbett corbettm at cutmcvax.cs.curtin.edu.au
Mon May 13 23:23:46 AEST 1991


sweetmr at SCT60A.SUNYCT.EDU (michael sweet) writes:

>In a project I am working on, I want to fork and exec another process with
>pipes to *both* the standard input and output of the new process.  This will
>eventually be used to control the 'tar' program via a GUI.  Anyways, my test
>programs  seems to create the 2 sets of pipes and fork the new program fine,
>but when it comes time to actually *write* anything to the standard input of
>the second process, the first process sits there waiting t (presumably blocked.)

I think the problem is in closing all your pipes.  You see a pipe doesn't
get passed an EOF until all accesses to the writing end of the pipe have
been closed.  Someone else had a similar problem on this news group 
recently.  Basically, all you need to ensure is that all the file descriptors
open to the write end of your pipes have been close.  Obviously, you should
also close the read end of the pipe.  

>The code goes something like this:

I have made the changes to the code which I think should work.  And marked
the additional lines with a <------

> pipe(stdin_pipes);
> pipe(stdout_pipes);

> child_pid = fork();
> if (child_pid == 0)
>  {
>   dup2(stdin_pipes[0], 0);
>   dup2(stdout_pipes[1], 1);
    close(stdin_pipes[0]);			<------
>   close(stdin_pipes[1]);
>   close(stdout_pipes[0]);
    close(stdout_pipes[1]);			<------

>   execlp("/bin/sh", "sh", "-c", command, NULL);
>   exit(errno);

>  };

> if (child_id < 0) /* error forking! */
>  {
>   close(all pipes);
>  }
  else
>  {
>   close(stdin_pipes[0]);
>   close(stdout_pipes[1]);
>   child_stdin = fdopen(stdin_pipes[1], "w");
>   child_stdout = fdopen(stdout_pipes[0]"r");
    close(stdin_pipes[1]);			<------
    close(stdout_pipes[0]);			<------
    .... [insert your code]			<------
    fclose(child_stdin);			<------
    fclose(child_stdout);			<------
>  };

	[stuff deleted]
>me stumped!

hope it helps/works :-) 
--

>>           Michael Corbett             | "Last night, I thought my arm was <<
>> corbettm at anger.cipal.cs.curtin.edu.au | hanging out of bed.  So I got out <<
>> corbettm at cutmcvax.cs.curtin.edu.au    | to push it in."                   <<



More information about the Comp.sys.sgi mailing list