Challenge - 5 Problems
Parallel Execution Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Output of background jobs with wait
What will be the output of this script when run in a bash shell?
Bash Scripting
echo "Start" (sleep 1; echo "Job 1 done") & (sleep 2; echo "Job 2 done") & wait echo "All jobs finished"
Attempts:
2 left
💡 Hint
Remember that 'wait' pauses the script until all background jobs finish.
✗ Incorrect
The script prints 'Start' immediately, then runs two background jobs that sleep and print messages. The 'wait' command pauses the script until both jobs finish, so their outputs appear before 'All jobs finished'. Job 1 finishes before Job 2 due to shorter sleep.
📝 Syntax
intermediate1:30remaining
Correct syntax for running commands in parallel
Which option correctly runs three commands in parallel and waits for all to finish?
Attempts:
2 left
💡 Hint
Use '&' to run commands in background and 'wait' to pause until all finish.
✗ Incorrect
Option C correctly runs cmd1, cmd2, and cmd3 in background using '&' and then waits for all to finish with 'wait'. Option C misses '&' before cmd3 so cmd3 runs in foreground. Option C runs wait in background which is incorrect. Option C runs all commands sequentially in foreground.
🔧 Debug
advanced2:00remaining
Fix the issue with parallel job output mixing
This script runs two background jobs but their outputs sometimes mix on the terminal. What is the main cause?
Bash Scripting
echo "Start" (sleep 1; echo "Job 1 done") & (sleep 1; echo "Job 2 done") & wait echo "End"
Attempts:
2 left
💡 Hint
Think about how multiple background jobs write to the terminal at the same time.
✗ Incorrect
When multiple background jobs write to the terminal at the same time, their outputs can interleave or mix because stdout is shared. This is normal behavior unless output is redirected or synchronized.
🚀 Application
advanced2:30remaining
Limit number of parallel jobs in a loop
You want to run 10 commands in parallel but only allow 3 to run at the same time. Which approach achieves this?
Bash Scripting
for i in {1..10}; do some_command $i & # limit to 3 parallel jobs # ??? done wait
Attempts:
2 left
💡 Hint
You can pause the loop every few jobs to limit concurrency.
✗ Incorrect
Option A uses a counter to wait after every 3 jobs, ensuring only 3 run in parallel at once. Option A runs all jobs at once, no limit. Option A delays but does not limit concurrency. Option A waits only at the end, no limit during loop.
🧠 Conceptual
expert3:00remaining
Understanding job control and process IDs in parallel scripts
In a bash script, you run multiple background jobs and want to track their process IDs (PIDs) to wait for specific ones. Which method correctly captures and waits for each job's PID?
Attempts:
2 left
💡 Hint
The special variable $! holds the PID of the last background job started.
✗ Incorrect
Option A correctly runs cmd1 and cmd2 in background, then immediately captures their PIDs with $!. Option A captures $! before starting jobs, so PIDs are wrong. Option A runs commands in foreground, so $! is empty or unrelated.