Re: IPC



In article <4390cfbe$0$27607$4fafbaef@xxxxxxxxxxxxxxxxxxx>, noob
<n00b@xxxxxxxxx> wrote:

> Jim Gibson ha scritto:
> >>Yes, so.....
> >>
> >>example.pl:
> >>#!/usr/bin/perl
> >>#
> >>@processes=();
> >>for $i (0..3){
> >> $pid = open($processes[$i],"|yes > temp.txt &");
> >
> >
> > This is a nasty command that attempts to fill up your disk with a file
> > containing 'y's. Please use something more benign like sleep to test a
> > long-running process. Fortunately, I have a big disk, because I didn't
> > inspect your code carefully enough before running it as is.
>
> I know, but I have to manage the script that has concurrent access to a
> file. It's only a example.

But writing the file has nothing to do with being able to wait for a
child process. You need to be able to partition your problems and solve
them one at a time. You should also be kind to those whom you are
asking for help and not fill up their disks with test data. Doing so
will decrease the probability that you will get the help you need in
the future.

> > The exec command is just the Unix exec command: it doesn't return
> > (unless there is an error). You want to use the system command here ...
> >
>
> it's weird!

What is weird? The exec command is doing what it is supposed to do
exactly as documented.


>
> > However, you can certainly wait for the shell process to
> > complete, because the shell process will wait for the command process
> > to complete before completing itself.
> >
>
> Hmmm I don't know......I tried but it seems that it doesn't work.

"it doesn't work" is a poor description of a problem. However, I re-ran
my test, and found a problem. With the command '|sleep 10 &', the
parent shell will NOT wait for the child process to complete. You need
to take out the '&' so that it will wait. Here is the corrected
program:

#!/usr/local/bin/perl
use strict;
use warnings;
my(@pids, @processes);
for my $i (0..3){
$pids[$i] = open($processes[$i],"|sleep 10 > /dev/null");
print "child $i pid = $pids[$i]\n";
}
system("ps");
for my $i (0..$#processes) {
my $pid = $pids[$i];
waitpid($pid,0);
print "child process $pid finished, closing pipe\n";
close $processes[$i];
}


which produces on my system:

child 0 pid = 22187
child 1 pid = 22188
child 2 pid = 22189
child 3 pid = 22190
PID TT STAT TIME COMMAND
22082 p1 Ss 0:00.36 -tcsh
22186 p1 R+ 0:00.05 perl noob.pl
22187 p1 S+ 0:00.01 sh -c sleep 10 > /dev/null
22188 p1 S+ 0:00.01 sh -c sleep 10 > /dev/null
22189 p1 S+ 0:00.01 sh -c sleep 10 > /dev/null
22190 p1 S+ 0:00.01 sh -c sleep 10 > /dev/null
22191 p1 S+ 0:00.01 sleep 10
22192 p1 S+ 0:00.01 sleep 10
22193 p1 S+ 0:00.01 sleep 10
22194 p1 S+ 0:00.01 sleep 10
child process 22187 finished, closing pipe
child process 22188 finished, closing pipe
child process 22189 finished, closing pipe
child process 22190 finished, closing pipe

Why did I add the ' > /dev/null'? If you take that out, you will get
something like this:

child 0 pid = 22181
child 1 pid = 22182
child 2 pid = 22183
child 3 pid = 22184
PID TT STAT TIME COMMAND
22082 p1 Ss 0:00.36 -tcsh
22180 p1 S+ 0:00.05 perl noob.pl
22181 p1 S+ 0:00.01 sleep 10
22182 p1 S+ 0:00.01 sleep 10
22183 p1 S+ 0:00.01 sleep 10
22184 p1 S+ 0:00.01 sleep 10
child process 22181 finished, closing pipe
child process 22182 finished, closing pipe
child process 22183 finished, closing pipe
child process 22184 finished, closing pipe


What is happening is that if there are no shell meta-characters in your
command, no shell process is executed. The open statement executes the
command itself.

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
.



Relevant Pages

  • [kde] Re: How to use Plasmoid shortcuts
    ... Disabling the "command line" in Krunner fixed it, ... simply means "run in the current shell" not as a child process, ... because it executes the content of the called script in the ... Second, and the reason I use it to call my kde launcher (simply "k", I ...
    (KDE)
  • Re: Spawning process with environment variables
    ... process exits parent process exits too. ... The simple way to solve this is to call fork before. ... read its exit return code (what child process' main returns) ... If you don't mind a shell running the command for you simply use ...
    (comp.unix.programmer)
  • Re: IPC
    ... In my script I control for processes with lsof command and so in this example I put in it. ... > parent shell will NOT wait for the child process to complete. ... closing pipe ...
    (comp.lang.perl.misc)
  • Re: C++ on Unix - serious coding help needed. Were in the Silicon valley location
    ... it's not possible for a child process to ... his 'hm' command a curiosity, ... and object code area separate, ... There are also a few other technical problems with his, ...
    (comp.os.linux.development.apps)
  • Re: C++ on Unix - serious coding help needed. Were in the Silicon valley location
    ... it's not possible for a child process to ... his 'hm' command a curiosity, ... and object code area separate, ... There are also a few other technical problems with his, ...
    (comp.os.linux.misc)