Bird
0
0

Which approach correctly achieves this?

hard📝 Application Q15 of 15
Node.js - Child Processes
You want to run two separate scripts worker1.js and worker2.js in parallel using fork. You also want to collect their results and print "All done" only after both finish. Which approach correctly achieves this?
AFork both scripts, listen for 'exit' events on both, then print after both exit.
BFork one script, then fork the second inside the first child's 'exit' event.
CFork both scripts and print "All done" immediately after forking.
DUse <code>exec</code> instead of <code>fork</code> to run scripts sequentially.
Step-by-Step Solution
Solution:
  1. Step 1: Understand parallel execution with fork

    Forking both scripts starts them in parallel. To know when both finish, listen for their 'exit' events.
  2. Step 2: Wait for both exit events before printing

    Track both exits with counters or flags, then print "All done" only after both have exited.
  3. Final Answer:

    Fork both scripts, listen for 'exit' events on both, then print after both exit. -> Option A
  4. Quick Check:

    Wait for both exits before printing = B [OK]
Quick Trick: Use 'exit' events on both children to sync completion [OK]
Common Mistakes:
  • Starting second child inside first child's exit
  • Printing before children finish
  • Using exec for parallel child processes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes