Python中的OS模块提供了与操作系统进行交互的功能。操作系统属于Python的标准实用程序模块。该模块提供了使用依赖于操作系统的功能的便携式方法。
os._exit()Python中的方法用于以指定状态退出进程,而无需调用清理处理程序,刷新stdio缓冲区等。
注意:此方法通常在os.fork()系统调用之后的子进程中使用。退出流程的标准方法是sys.exit(n)方法。
用法: os._exit(status)
参数:
status:代表退出状态的整数值或高于定义的值。
返回类型:此方法在调用过程中不返回任何值。
代码:用于os._exit()方法
# Python program to explain os._exit() method
# importing os module
import os
# Create a child process
# using os.fork() method
pid = os.fork()
# pid greater than 0
# indicates the parent process
if pid > 0:
print("\nIn parent process")
# Wait for the completion
# of child process and
# get its pid and
# exit status indication using
# os.wait() method
info = os.waitpid(pid, 0)
# os.waitpid() method returns a tuple
# first attribute represents child's pid
# while second one represents
# exit status indication
# Get the Exit code
# used by the child process
# in os._exit() method
# firstly check if
# os.WIFEXITED() is True or not
if os.WIFEXITED(info[1]):
code = os.WEXITSTATUS(info[1])
print("Child's exit code:", code)
else :
print("In child process")
print("Process ID:", os.getpid())
print("Hello ! Geeks")
print("Child exiting..")
# Exit with status os.EX_OK
# using os._exit() method
# The value of os.EX_OK is 0
os._exit(os.EX_OK)
输出:
In child process
Process ID:15240
Hello! Geeks
Child exiting..
In parent process
Child's exit code:0