Python | os.waitid () method

Python Methods and Functions

os.waitid() in Python is used by a process to wait for one or more child processes to finish.

Syntax: os.waitid (idtype, id, options)

Parameters:
id: An integer value representing the process id of child to wait on.
idtype: The idtype and id parameter specify which child the method waits for.

Return type: This method returns an object which represents the data contained in siginfo_t structure.

Code # 1: Method Usage os.waitid()

# Python program to explain the os.waitid () method

 
# import of the os module

import os 

 
# Create a child process
# using the os.fork () method

pid = os.fork ()

  

 
# Nonzero process id (pid)
# points to the parent process

if pid:

 

# Wait for completion

# child process using

  # os.waitid () method

  

  # Specify idtype

idtype = os.P_PID

 

# Specify an identifier

id = pid

 

# Specify an option

  option = os.WEXITED

 

status = os.waitid (idtype, id , option)

 

print ( " In parent process- " )

  

  # Print status

print ( "Status of child process: " )

  print (status)

 

 

else :

print ( "In Child process-" )

print ( "Process ID:" , os.getpid ())

print ( " Hello! Geeks " )

  print ( "Exiting .." )

 

Output:

 In Child process- Process ID: 10309 Hello! Geeks Exiting .. In parent process- Status of child process: posix.waitid_result (si_pid = 10309, si_uid = 1000, si_signo = 17, si_status = 0 , si_code = 1) 

Code # 2: Using the method os.waitid()

# Python program to explain the os.waitid () method

  
# import of the os module

import os 

< code class = "undefined spaces">  
# Create a child process
# using the os.fork method ()

pid = os.fork ()

 

 
# Nonzero process id (pid)
# points to the parent process

if pid:

 

# Create another child process

pid2 = os.fork ()

 

if pid2:

  

# Wait for completion

# any child processes using the

# os.waitid () method

 

# Specify idtype

  idtype = os.P_ALL

 

# Specify an identifier

# Since the idtype is os.P_ALL

  # method will wait

# any children and specified id

# ignored.

id = pid

 

# Specify an option

option = os.WSTOPPED | os.WEXITED

 

status = os.waitid (idtype, id , option)

 

print ( "In parent process -" )

 

# Print status

  print ( "Status of completed child process:" )

print (status)

  

else :

  print ( "In Second Child process-" )

  print ( "Process ID:" , os.getpid ())

print ( "Hey! There " )

  print ( "Exiting" )

 

 

else :

print ( "In First Child process-" )

print ( "Process ID: " , os.getpid ())

  print ( "Hello! Geeks" )

print ( "Exiting" )

 

Exit :

 In First Child process- Process ID: 11524 Hello! Geeks Exiting In Second Child process- Process ID: 11525 Hey! There Exiting In parent process- Status of completed child process: posix.waitid_result (si_pid = 11524, si_uid = 1000, si_signo = 17, si_status = 0, si_code = 1) 

Links: