develooper Front page | perl.beginners | Postings from January 2002

RE: why does open()ing happen only at the <FD> line ?

Thread Previous | Thread Next
Bob Showalter
January 12, 2002 09:16
RE: why does open()ing happen only at the <FD> line ?
Message ID:
> -----Original Message-----
> From: Prahlad Vaidyanathan []
> Sent: Friday, January 11, 2002 4:52 AM
> To:
> Cc: Michael Fowler
> Subject: Re: why does open()ing happen only at the <FD> line ?
> Hi,
> On Thu, 10 Jan 2002 Michael Fowler spewed into the ether:
> [-- snip --]
> > Um, how do you figure?  The sudo program prints out the 
> "Password:" prompt,
> > and from your output you can clearly see "test" is after 
> "Password:".  So
> > sudo is being run before "test" is printed.
> Yes, I noticed that _just_ after I posted. This is kinda 
> weird, but the
> test case behaves differently at different points in time.
> Sometimes, 'test' is printed, and only then the sudo prompt 
> appears. At
> other times, the sudo prompt appears, 'test' is printed, and 
> it waits at
> the next line for the password (like my post yesterday).

There are two separate processes writing to your terminal.
One, the sudo, writes "Password", while the other writes "test".
In the absence of an explicit syncronization mechanism, there
is no way to guarantee which one will run first.

> ie.
> <test1>
> $ ./
> test
> Password:[waits for password]
> ....
> </test1>
> <test2>
> $ ./
> Password:test
>     [waits for password]
> ....
> </test2>
> I'm guessing this has to do with how long it takes to invoke 
> sudo (given
> system load, etc). But, all the same, shouldn't it wait for 
> the password
> to be entered _before_ printing 'test' ? Now I'm totally stumped.

The process that "waits for the password" is different from the
process that prints "test". You didn't tell the second to wait
for the first, so it doesn't wait.

If you want your script to wait until sudo has completely finished,
you need to use system(), backticks, or wait(). These wait for the
child process to finish. fork() and pipe open() do not wait (But
close() on the handle from pipe open() *does* wait).

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About