Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

os module
python programming
wait
communitycreator

What is the os.wait() method in Python?

AKASH BAJWA

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

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.

Syntax


# Signature
os.wait()

Parameters

It does not take any argument value.

Return value

(pid, exit_status): It returns a tuple containing the process ID and the exist status code.

Code

Let's look at this method in detail with a code example:

# Program to test os.wait()
# including os module
import os
# Creata a child process by invoking fork()
pid = os.fork()
# pid > 0 expect a parent process
if pid:
# invoking os.wait()
# so that child process complete
status = os.wait()
print("In parent process")
print("Terminated child's process id:", status[0])
# Higher byte of status contain signal number
# that killed child process
print("Signal number that killed child process:", status[1])
else:
print("In Child process")
print("Process ID:", os.getpid())
print("Child process Completed")
The os.wait method implementation

Code explanation

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.

RELATED TAGS

os module
python programming
wait
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring