Method Name:
os.wait
Method Signature:
os.wait()
Return Value:
A Python tuple consisting of the existing child process id and a sixteen-bit integer to denote exit status.
Overview:
- The wait() method of os module in Python enables a parent process to synchronize with the child process. i.e, To wait till the child process exits and then proceed.
- The method os.fork() creates a child process in Unix. In the program flow, if the return value of os.fork() is greater than zero then the process can be identified as the parent process, and os.fork() can be called as shown in the Python example below. The non-zero return value is the pid – the process id of the child process.
Example:
# import the os module of Python import os
# Create a child process retVal = os.fork()
if retVal is 0: # Child process printing numbers for i in range(0,10): print("Child process printing integer value %d"%(i)) print("Child process %d exiting"%(os.getpid())) else: # Parent process waiting childProcExitInfo = os.wait() print("Child process %d exited"%(childProcExitInfo[0])) print("Parent process %d exiting after child has exited"%(os.getpid())) |
Output:
Child process printing integer value 0 Child process printing integer value 1 Child process printing integer value 2 Child process printing integer value 3 Child process printing integer value 4 Child process printing integer value 5 Child process printing integer value 6 Child process printing integer value 7 Child process printing integer value 8 Child process printing integer value 9 Child process 3031 exiting Child process 3031 exited Parent process 3030 exiting after child has exited |