Task: Wait For Me Processes
Enter the labs/lab-06/tasks/wait-for-me-processes/ directory, run make skels, open the support/src folder and go through the practice items below.
Use the tests/checker.sh script to check your solutions.
wait_for_me_processes ...................... passed ... 100
100 / 100
-
Run the code in
wait_for_me_processes.py(e.g:python3 wait_for_me_processes.py). The parent process creates one child that writes and message to the given file. Then the parent reads that message. Simple enough, right? But running the code raises aFileNotFoundError. If you inspect the file you gave the script as an argument, it does contain a string. What’s going on?In order to solve race conditions, we need synchronization. This is a mechanism similar to a set of traffic lights in a crossroads. Just like traffic lights allow some cars to pass only after others have already passed, synchronization is a means for threads to communicate with each other and tell each other to access a resource or not.
The most basic form of synchronization is waiting. Concretely, if the parent process waits for the child to end, we are sure the file is created and its contents are written.
-
Use
join()to make the parent wait for its child before reading the file.