how do i ensure n processes always run?

Discussion in 'Perl Misc' started by dmedhora@gmail.com, Oct 21, 2005.

  1. Guest

    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
     
    , Oct 21, 2005
    #1
    1. Advertising

  2. Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    > 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
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Oct 21, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dot net work
    Replies:
    0
    Views:
    444
    Dot net work
    Jun 27, 2004
  2. Joergen Bech
    Replies:
    0
    Views:
    370
    Joergen Bech
    Jul 25, 2005
  3. Dot net work

    Ensure selected treeview node is always displayed?

    Dot net work, Jun 15, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    157
    Dot net work
    Jun 15, 2004
  4. Thomas
    Replies:
    0
    Views:
    92
    Thomas
    Jun 10, 2005
  5. Thomas
    Replies:
    0
    Views:
    90
    Thomas
    Jun 14, 2005
Loading...

Share This Page