Re: pipe blocking semantics

1998-10-09 Thread Henrik Nordstrom

[EMAIL PROTECTED] wrote:

 After the fork(), are there two separate copies of pfd[] ?

Yes. Fork duplicates the entire process, including any open
filedescriptors.

---
Henrik Nordström



RE: pipe blocking semantics

1998-10-09 Thread subbaraom




 --
From:  [EMAIL PROTECTED][SMTP:[EMAIL PROTECTED]]
Sent:  Friday, October 09, 1998 12:02 PM
To:  [EMAIL PROTECTED]
Subject:  pipe blocking semantics

I've been looking at this small program which performs an "ls | wc",
trying to work out how the blocking semantics works.

I understand that fork() has resulted in two identical processes being
run simultaneously but I can't work out how the parent process is
being forced to execute before the child.


 There is no guarantee that parent process will be executed before   
the child only thing is "ls" and "wc" use write and read system calls   
which are blocking in nature. So as long as there is no data in the pipe   
"wc" will be waiting and whenever the process running "ls" is scheduled   
"ls" will write the data in to pipe. Only when ls finish writing the EOF   
the wc will terminate.

Hope you got the point.

 -subbaraom

After the fork(), are there two separate copies of pfd[] ?

/* ls | wc */
#include stdio.h
#include unistd.h

int main(void)
{
  int pfd[2];
  int pid;

  pipe(pfd);
  pid=fork();
  if (pid==0)
  {
/* child */
close(pfd[1]);
dup2(pfd[0],0);
close(pfd[0]);
execlp("wc", "wc", (char *) 0 );
  }
  else
  {
/* parent */
close(pfd[0]);
dup2(pfd[1],1);
close(pfd[1]);
execlp("ls", "ls", (char *) 0);
  }
  return(0);
}



Re: pipe blocking semantics

1998-10-09 Thread Glynn Clements


[EMAIL PROTECTED] wrote:

 I've been looking at this small program which performs an "ls | wc",
 trying to work out how the blocking semantics works.
 
 I understand that fork() has resulted in two identical processes being
 run simultaneously but I can't work out how the parent process is
 being forced to execute before the child.

The parent process isn't forced to execute before the child.

However, as wc continuously reads from stdin, if wc is started first,
it will block as soon as it tries to read from stdin. It will be
reawakened once ls has actually written something to the pipe.

-- 
Glynn Clements [EMAIL PROTECTED]