On Fri, 26 Jan 2007 16:51:18 -0800 (PST)
Ritesh <[EMAIL PROTECTED]> wrote:

> thanks ian and dmick.
> 
> so, if i have a following line in my code: 
> pclose(popen("/tmp/somescript","w"));
> 
> the parent process that executes this line, will wait till the "somescript" 
> completes and writes something on stdout? or it will continue ahead ? 

If you really want to startup a script and throw away its output then
fork/exec it or spawn it and point its stdout at /dev/null.

If you look at the pclose source you'll notice that the parent side of
the pipe is close and then we reap the child.  What might not be
obvious is that causes the child's standard input and output to become
invalid typically causes it to exit when it next tries to do an IO
operation.

If for some reason you startup up something that didn't end up doing IO
operations or you started something that didn't exit in this situation
pclose could hang.  But you wouldn't usu. choose to use popen in that
case.

> 
> lets say the script "somescript" sleeps in a while loop, will the parent 
> process continue what it was doing or it will be blocked on pclose call. 
> 
> pardon me if this is too basic ;-) but i somehow have this doubt. 

About what?

> 
> thanks for your help.

The child process continues in paralell with the parent.  They share a
pipe for communication.  If the child delays doing something the parent
can go on or whatever.  But two typical scenarios exist.

1. The parent starts up the child and then blocks on the pipe waiting
for input from the child.  The "ls *.c" code on the man page is a good
example of that.

2. The parent starts up the child and then uses poll/select to
multiplex input from the child with other IO sources.

There is a fine[1] example in the man page that you can modify and play
with to see this behavior.  If you are still confused use dtrace to
watch what happens in you application or possible take a copy of
popen.c, link against it, put printf()s in it/whatever, and walk
through it until it makes sense.

                mph

[1] Although the indentation in the 'popen("ls *.c",)' example would
suggest that the writer thought it was doing something slightly
different then it really is.

> rp.
>  
>  
> This message posted from opensolaris.org
> _______________________________________________
> opensolaris-code mailing list
> [email protected]
> http://mail.opensolaris.org/mailman/listinfo/opensolaris-code
_______________________________________________
opensolaris-code mailing list
[email protected]
http://mail.opensolaris.org/mailman/listinfo/opensolaris-code

Reply via email to