how do i ensure n processes always run?

D

dmedhora

Hi,


I want to read commands from a file and execute 5 of them
simultaneously. Then if any command(s) finish running, I
want to continue reading from the file and executing the
commands such that at any 1 time, there are always, say, 5
processes running.


This script however should be able to have multiple instances
of it running as well, so for example a usage of:


myscript 1 5


and an input file containing:
1 sleep 40
1 sleep 45
1 sleep 32
1 sleep 34
1 sleep 34
1 sleep 34
1 sleep 34
1 sleep 34
1 sleep 34
1 sleep 34
1 sleep 34
1 sleep 34
1 sleep 34
1 sleep 34
1 sleep 34
1 sleep 40
etc..
should execute the first 5 sleeps then if 2 sleeps finish I
should be able to read and execute 3 more...etc


I can do this successfully if my logic greps for sleep and
I just call the script once. But,


I should *also* be able to fire off


myscript 2 5 (run commands beginning with 2, keeping 5 running)
and
myscript 4 4 (run commands beginning with 4, keeping 4 running)


here myscript 2 5 would read in from file as below, say,
2 sleep 34
2 sleep 32
2 sleep 99
2 sleep 80
2 sleep 70
2 sleep 50
2 sleep 80
2 sleep 30
2 sleep 80
2 sleep 40
2 sleep 10
2 sleep 81
2 sleep 20
etc


so if I simultaneously run
myscript 1 5
myscript 2 5
then I cannot depend on grep sleep *and* keep the counts proper.


If i run them off as background processes then they don't
have any parents (parent becomes init or 1)..:( so i can't
figure how to keep the parent child links as well.


I would really really appreciate it if someone can help me with what
logic i should use to write this out..
Thanks
d
 
A

Anno Siegel

Hi,


I want to read commands from a file and execute 5 of them
simultaneously. Then if any command(s) finish running, I
want to continue reading from the file and executing the
commands such that at any 1 time, there are always, say, 5
processes running.

[extensive specification snipped]
I would really really appreciate it if someone can help me with what
logic i should use to write this out..

Study the functions fork(), waitpid() and signal handling (perldoc -f fork,
perldoc -f waitpid, and %SIG in perldoc perlvar) and how they work together
(perldoc perlipc). For code examples, look for "server", particularly
"forking server" in perlipc. More examples are in the _Perl Cookbook_.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top