python优秀代码示例_Python os.WTERMSIG()用法及代码示例

Python中的OS模块提供了与操作系统进行交互的功能。操作系统属于Python的标准实用程序模块。该模块提供了使用依赖于操作系统的功能的便携式方法。

os.WTERMSIG()Python中的方法用于获取导致进程退出的信号号。此方法采用流程状态代码,由os.wait(),os.system()或者os.waitpid()方法作为参数。

用法: os.WTERMSIG(status)

参数:

status:此参数采用os.system(),os.wait()方法或os.waitpid()方法返回的过程状态代码(整数值)。

返回类型:此方法返回一个整数值,该整数值表示导致进程退出的信号号。

代码1:用于os.WTERMSIG()方法

# Python program to explain os.WTERMSIG() method

# importing os and signal module

import os, signal

# Create a child process

# using os.fork() method

pid = os.fork()

# pid greater than 0

# indicates the parent process

if pid :

# Wait for the completion of

# the child process and get

# its pid and

# exit status indication

info = os.wait()

# info is a tuple

# info[0] represents child's id

# info[1] represents exit status code

print("\nIn parent process")

# Get the signal number which caused

# the child process to exit

sig_num = os.WTERMSIG(info[1])

print("Child process exited due to signal no:", sig_num )

# We can get the signal name

# corresponding to the signal number

# in following way

print("Signal name:", signal.Signals(sig_num).name)

else :

print("In Child process")

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

print("Hello ! Geeks")

# os.abort() method will

# generate a SIGABRT signal

# to the current process

# and will produce core dump.

os.abort()

输出:

In Child process

Process ID: 3421

Hello! Geeks

In parent process

Child process exited due to signal no: 6

Signal name: SIGABRT

代码2:用于os.WTERMSIG()方法

# Python program to explain os.WTERMSIG() method

# importing os and signal module

import os, signal

# 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 :

# Wait for the completion of

# first child process and get

# its pid and

# exit status indication

# using os.waitpid() method

child1_info = os.waitpid(pid, 0)

# Wait for the completion of

# second child process and get

# its pid and exit status indication

# using os.waitpid() method

child2_info = os.waitpid(pid2, 0)

# os.waitpid() method

# returns a tuple which

# represnts child's pid

# and exit status code

print("\nIn parent process")

# Get the signal number which caused

# first child process to exit

sig_num = os.WTERMSIG(child1_info[1])

if sig_num != 0 :

print("First child process exited due to signal no:", sig_num )

print("Signal name:", signal.Signals(sig_num).name)

else:

print("First child exited normally")

# Get the signal number which caused

# second child process to exit

sig_num = os.WTERMSIG(child2_info[1])

if sig_num != 0 :

print("Second child process exited due to signal no:", sig_num )

print("Signal name:", signal.Signals(sig_num).name)

else:

print("Second child exited normally")

# sig_num equal to 0 represents

# that no signal caused

# the process to exit

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")

# os.abort() method will

# generate a SIGABRT signal

# to the current process

os.abort()

输出:

In First child process

Process ID: 3752

Hello! Geeks

In second child process

Process id: 3753

Hey! there

Exiting..

In parent process

First child process exited due to signal no: 6

Signal name: SIGABRT

Second child exited normally

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值