Python | os.WIFSTOPPED () method

Python Methods and Functions

os.WIFSTOPPED() in Python is used to check if a process has stopped. This method accepts the process status code returned by os.wait() , os.system () or os.waitpid() as a parameter and returns True if the process was stopped , otherwise it returns False.

Syntax: os.WIFSTOPPED (status)

Parameter:
status: This parameter takes process status code (an integer value) as returned by os.system (), os.wait () or os.waitpid () method.

Return type: This method returns a boolean value of class 'bool'. This method returns True if the process has been stopped, otherwise returns False.

Code: using the os.WIFSTOPPED ( )

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

 
# OS and signal module import

import os, signal

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

pid = os.fork ()

  

  
# pid large e 0
# refers to the parent process

if pid:

 

  # Send signal & # 39; SIGSTOP & # 39;

# child process

  # using the os.kill () method

# the signal will call the child

  # stop the process

os. kill (pid, signal.SIGSTOP)

 

# Get the child's pid and

  # status code using

# os. waitpid () method

info = os.waitpid (pid, os.WSTOPPED)

 

# info is a tuple

# info [0] represents the child's pid

# info [1] represents the exit status code

 

print ( "In parent process" )

 

  # Checking the child process

  # was stopped or not

  # using the os.WIFSTOPPED () method

isStopped = os.WIFSTOPPED (info [ 1 ]) 

 

print ( "Has child process been stopped?" )

print (isStopped)

 

 

else < / code> :

print ( "In Child process" )

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

  print ( " Hello! Geeks " )

  

Exit :

 In Child process Process ID: 10224 Hello! Geeks In parent process Has child process been stopped ? True 

Links: https://docs.python.org/3/library/os.html#os.WIFSTOPPED





Get Solution for free from DataCamp guru