Tuesday, November 19, 2024
Google search engine
HomeLanguagesPython | os.waitid() method

Python | os.waitid() method

OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.

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

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: Use of os.waitid() method




# Python program to explain os.waitid() method 
  
# importing os module  
import os 
  
# Create a child process
# using os.fork() method 
pid = os.fork()
  
  
# a Non-zero process id (pid)
# indicates the parent process 
if pid :
      
    # Wait for the completion of
    # child process using
    # os.waitid() method
  
    # Specify idtype
    idtype = os.P_PID
  
    # Specify id
    id = pid
      
    # Specify option
    option = os.WEXITED
  
    status = os.waitid(idtype, id, option)
  
    print("\nIn 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: Use of os.waitid() method




# Python program to explain os.waitid() method 
  
# importing os module  
import os 
  
# Create a child process
# using os.fork() method 
pid = os.fork()
  
  
# a Non-zero process id (pid)
# indicates the parent process 
if pid :
      
    # Create one more child process 
    pid2 = os.fork()
      
    if pid2 :
      
        # Wait for the completion of
        # any child processes using
        # os.waitid() method
  
        # Specify idtype
        idtype = os.P_ALL
  
        # Specify id
        # As idtype is os.P_ALL
                # method will wait for 
        # any children and specified id 
        # is ignored.             
        id = pid
  
        # Specify option
        option = os.WSTOPPED | os.WEXITED
      
        status = os.waitid(idtype, id, option)
  
        print("\nIn parent process-")
          
        # Print status
        print("Status of completed child process:")
        print(status)
  
    else :
        print("\nIn 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")
     


Output:

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)

References:

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments