Running tasks in parallel
Launching background jobs with &, waiting for them with wait while keeping their exit codes, understanding the subshell and its lost variables, bounding parallelism with xargs -P and predicting the gain with Amdahl's law.
- 35 cards
- 6 topics
- Free
The outline, topic by topic
Each topic is weighted the way the exam weighs it.
- Concepts
- Questions
- Sort and link
- The steps
- Scenarios
- Understand
A few cards from the deck
Background job: what is it?
Show the answer
A command launched with a trailing ampersand: the shell does not wait for it and returns at once; the variable $! gives its process number.
sleep 30 &returns at once. Withoutwait, a script that ends leaves its background jobs behind, without knowing their outcome.What does this Bash script print?
```bash
n=0
( n=5 ) & wait
echo $n`051n
Show the answer
0( n=5 )runs in a subshell that works on a copy ofn: the parent shell keeps 0.waitchanges nothing, it only waits for the end.The variable $! holds the … of the last command launched in the ….
Show the answer
The variable $! holds the PID of the last command launched in the background.
It must be copied at once (
pid=$!): the next launch overwrites it.wait: what is it?
Show the answer
A shell builtin that blocks the shell until background jobs finish; given a PID, it returns that job's exit code.
With no argument, it waits for all the jobs and always returns 0, whatever their failures. To detect an error, use one
waitper PID.Backing up four servers in parallel and counting the failures: which step comes after "Launch each backup with & in a loop"?
- Walk through the PID list with a second loop
- Run wait PID for each one and read its exit code
- Increment a counter for every non-zero code
- Copy $! into the PID list after each launch
Show the answer
Copy $! into the PID list after each launch
The PIDs must be kept before waiting for them; the counter is incremented in the main shell, not in a subshell, so it survives the loop.