Python中的OS模塊提供了與操作係統進行交互的函數。操作係統屬於Python的標準實用程序模塊。該模塊提供了使用依賴於操作係統的函數的便攜式方法。
os.WEXITSTATUS()
Python中的方法用於獲取exit(2)係統調用中的進程使用的整數參數,如果os.WIFEXITED(status)
是真的。如果os.WIFEXITED(status)
如果為False,則該方法返回無意義的值。
用法: os.WEXITSTATUS(status)
參數:
status:此參數采用os.system(),os.wait()方法或os.waitpid()方法返回的過程狀態代碼(整數值)。
返回類型:此方法返回exit(2)係統調用中的進程使用的整數值。
代碼:用於os.WEXITSTATUS()
方法
# Python program to explain os.WEXITSTATUS() 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:
# Create one more child
pid2 = os.fork()
if pid2:
print("\nIn parent process")
# Wait for the completion
# of first child process and
# get its pid and
# exit status indication using
# os.waitpid() method
info1 = os.waitpid(pid, 0)
# Wait for the completion
# of second child process and
# get its pid and
# exit status indication using
# os.waitpid() method
info2 = os.waitpid(pid2, 0)
# os.waitpid() method returns a tuple
# first attribute represents child's pid
# while second one represents
# exit status indication
# Get the integer parameter
# used first child process
# in exit(2) system call
# firstly check if
# os.WIFEXITED() is True or not
if os.WIFEXITED(info1[1]):
code = os.WEXITSTATUS(info1[1])
print("First child's exit code:", code)
else :
print("First child does not exited using exit(2) system call.")
# Get the integer parameter
# used second child process
# in exit(2) system call
# firstly check if
# os.WIFEXITED() is True or not
if os.WIFEXITED(info2[1]):
code = os.WEXITSTATUS(info2[1])
print("\nSecond child's exit code:", code)
else :
print("\nSecond child does not exited using exit(2) system call.")
else :
print("\nIn second child process")
print("Process ID:", os.getpid())
print("Hey ! there")
print("Second child aborted")
# os.abort() method will
# generate a SIGABRT signal
# to the current process.
os.abort()
else :
print("In first child process")
print("Process ID:", os.getpid())
print("Hello ! Geeks")
print("First child exiting..")
# Exit with status code 5
# using os._exit() method
os._exit(5)
輸出:
In first child process Process ID:11614 Hello! Geeks First child exiting.. In second child process Process ID:11615 Hey! there Second child aborted In parent process First child's exit code:5 Second child does not exited using exit(2) system call.
參考文獻: https://docs.python.org/3/library/os.html#os.WEXITSTATUS
相關用法
- Python next()用法及代碼示例
- Python os.dup()用法及代碼示例
- Python set()用法及代碼示例
- Python Decimal max()用法及代碼示例
- Python PIL ImageOps.fit()用法及代碼示例
- Python os.rmdir()用法及代碼示例
- Python sympy.det()用法及代碼示例
- Python Decimal min()用法及代碼示例
- Python os.readlink()用法及代碼示例
- Python os.writev()用法及代碼示例
- Python os.readv()用法及代碼示例
- Python PIL RankFilter()用法及代碼示例
- Python os.rename()用法及代碼示例
- Python os.sendfile()用法及代碼示例
注:本文由純淨天空篩選整理自ihritik大神的英文原創作品 Python | os.WEXITSTATUS() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。