JDS
JDS

Reputation: 16988

Synchronization of processes in the shell?

I am forking off a bunch of processes, and I want to create a "Barrier" for my main original process to wait until all of the other processes finish. The code flow looks like so:

#...the main thread is executing now, and reaches this foreach loop:

foreach arg (myArgs)

    some_command arg & #these are the processes being forked off

end

#I want the main thread to WAIT here until all the above processes finish!

#... More code below here for the main thread to continue with

I'm not sure what synchronization primitive I want, or even what's available in the unix shell environment. Maybe something like a barrier for the main thread?

Thanks for any help.

Upvotes: 0

Views: 267

Answers (1)

John Kugelman
John Kugelman

Reputation: 361645

A bare wait will wait for all child processes to exit.

wait: wait [n]

Wait for the specified process and report its termination status. If N is not given, all currently active child processes are waited for, and the return code is zero. N may be a process ID or a job specification; if a job spec is given, all processes in the job's pipeline are waited for.

Upvotes: 2

Related Questions