Async processes - Linux

This is a discussion on Async processes - Linux ; Is there a way to run a program asynchronously from a shell script but make it so the next program runs after all the dispatched asynchronous programs finish. Something like: .... myApp file1 & myApp file2 & .... myApp fileN ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: Async processes

  1. Async processes

    Is there a way to run a program asynchronously from a shell
    script but make it so the next program runs after all
    the dispatched asynchronous programs finish. Something like:

    ....
    myApp file1 &
    myApp file2 &
    ....
    myApp fileN &
    hold_on_until_all_instances_of_myApp_finish
    the rest of script

    Most importantly, CPU time is important so I do not
    want to run something that would constantly check
    if any instance of myApp is still running. What I want
    ideally is a kernel API exposing thread-like mutexes
    and such for full-blown processes.
    Thanks in advance for any help.


  2. Re: Async processes

    Spake verm25@hotmail.com:
    > Is there a way to run a program asynchronously from a shell
    > script but make it so the next program runs after all
    > the dispatched asynchronous programs finish. Something like:
    >
    > ...
    > myApp file1 &
    > myApp file2 &
    > ...
    > myApp fileN &
    > hold_on_until_all_instances_of_myApp_finish
    > the rest of script


    Yes, you need to use the $$ variable to get the PID of the last
    backgrounded process. Something like

    PIDS=""

    # for each process
    my-program variables &
    PIDS="$PIDS $$"
    # end_for

    for PID in $PIDS; do
    # ps + awk magic
    done

    > Most importantly, CPU time is important so I do not
    > want to run something that would constantly check
    > if any instance of myApp is still running. What I want
    > ideally is a kernel API exposing thread-like mutexes
    > and such for full-blown processes.


    That sounds like you need something heavier than shell scripts.

    --
    Trent Buck, Student Errant
    Bill Gates is a true genius. He's made installing and maintaining windows
    apps so ridiculously difficult and expensive that businesses and consumers
    will actually buy into the idea of having their applications on someone
    else's server. -- Ukab the Great

  3. Re: Async processes

    verm25@hotmail.com wrote:
    > Is there a way to run a program asynchronously from a shell
    > script but make it so the next program runs after all
    > the dispatched asynchronous programs finish. Something like:
    >
    > ...
    > myApp file1 &
    > myApp file2 &
    > ...
    > myApp fileN &
    > hold_on_until_all_instances_of_myApp_finish
    > the rest of script
    >
    > Most importantly, CPU time is important so I do not
    > want to run something that would constantly check
    > if any instance of myApp is still running. What I want
    > ideally is a kernel API exposing thread-like mutexes
    > and such for full-blown processes.
    > Thanks in advance for any help.


    Take a look at "wait", it is a builtin in most shells:

    From "man bash":

    wait [n]
    Wait for the specified process and return its termination sta-
    tus. n may be a process ID or a job specification; if a job
    spec is given, all processes in that job's pipeline are waited
    for. If n is not given, all currently active child processes
    are waited for, and the return status is zero. If n specifies a
    non-existent process or job, the return status is 127. Other-
    wise, the return status is the exit status of the last process
    or job waited for.


    Pretty much the same for "sh" or "ksh", haven't used "csh" in so
    long that I don't remember.

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

  4. Re: Async processes

    Thanks, that helps. A bit expensive on CPU but this will work
    for now (until I can double check that that the libraries I am
    using are thread safe so I can rewrite my program using threads
    which seems like the right way to do things in my case).

    Larry Starr wrote:
    > verm25@hotmail.com wrote:
    > > Is there a way to run a program asynchronously from a shell
    > > script but make it so the next program runs after all
    > > the dispatched asynchronous programs finish. Something like:
    > >
    > > ...
    > > myApp file1 &
    > > myApp file2 &
    > > ...
    > > myApp fileN &
    > > hold_on_until_all_instances_of_myApp_finish
    > > the rest of script
    > >
    > > Most importantly, CPU time is important so I do not
    > > want to run something that would constantly check
    > > if any instance of myApp is still running. What I want
    > > ideally is a kernel API exposing thread-like mutexes
    > > and such for full-blown processes.
    > > Thanks in advance for any help.

    >
    > Take a look at "wait", it is a builtin in most shells:
    >
    > From "man bash":
    >
    > wait [n]
    > Wait for the specified process and return its termination sta-
    > tus. n may be a process ID or a job specification; if a job
    > spec is given, all processes in that job's pipeline are waited
    > for. If n is not given, all currently active child processes
    > are waited for, and the return status is zero. If n specifies a
    > non-existent process or job, the return status is 127. Other-
    > wise, the return status is the exit status of the last process
    > or job waited for.
    >
    >
    > Pretty much the same for "sh" or "ksh", haven't used "csh" in so
    > long that I don't remember.
    >
    > ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet

    News==----
    > http://www.newsfeeds.com The #1 Newsgroup Service in the World!

    120,000+ Newsgroups
    > ----= East and West-Coast Server Farms - Total Privacy via Encryption

    =----


+ Reply to Thread