This os.wait()
method is used by a thread or process to wait for the child's process completion. When execution completes, it returns a process ID and an existing status as a tuple.
Process ID: This is an integer to store the thread's unique identifier.
Exist status: A 16-bit number, whose lower byte is a signal number (which indicates whether a process should be killed or not), while the higher byte shows the exit status.
# Signatureos.wait()
It does not take any argument value.
(pid, exit_status)
: It returns a tuple containing the process ID and the exist status code.
Let's look at this method in detail with a code example:
# Program to test os.wait()# including os moduleimport os# Creata a child process by invoking fork()pid = os.fork()# pid > 0 expect a parent processif pid:# invoking os.wait()# so that child process completestatus = os.wait()print("In parent process")print("Terminated child's process id:", status[0])# Higher byte of status contain signal number# that killed child processprint("Signal number that killed child process:", status[1])else:print("In Child process")print("Process ID:", os.getpid())print("Child process Completed")
In the code snippet above, we're going to create a parent and child process to test how os.wait()
is working.
Line 5: We invoke the fork()
to create a child process from the parent or main process.
Line 10: os.wait()
will put the parent thread to wait for a child process to complete its execution.
Lines 11–15: These lines are only executed when the child process gets completed.
Line 18: The os.getpaid()
method returns the process ID of the current process.