python os kill_Python os.kill()用法及代码示例

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

os.kill()Python中的方法用于将指定的信号发送到具有指定进程ID的进程。在信号模块中定义了主机平台上可用的特定信号的常量。

用法: os.kill(pid, sig)

参数:

pid:一个整数值,表示要向其发送信号的进程标识。

sig一个整数,表示信号号或在要发送的信号模块中定义的主机平台上可用的信号常数。

返回类型:此方法不返回任何值。

代码:用于os.kill()方法

# Python program to explain os.kill() 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:

print("\nIn parent process")

# send signal 'SIGSTOP'

# to the child process

# using os.kill() method

# 'SIGSTOP' signal will

# cause the process to stop

os.kill(pid, signal.SIGSTOP)

print("Signal sent, child stopped.")

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

# waitpid() method returns a

# tuple whose first attribute

# represents child's pid

# and second attribute

# represnting child's status indication

# os.WSTOPSIG() returns the signal number

# which caused the process to stop

stopSignal = os.WSTOPSIG(info[1])

print("Child stopped due to signal no:", stopSignal)

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

# send signal 'SIGCONT'

# to the child process

# using os.kill() method

# 'SIGCONT' signal will

# cause the process to continue

os.kill(pid, signal.SIGCONT)

print("\nSignal sent, child continued.")

else :

print("\nIn child process")

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

print("Hello ! Geeks")

print("Exiting")

输出:

In child process

In parent process

Signal sent, child stopped.

Child stopped due to signal no:19

Signal name:SIGSTOP

Signal sent, child continued.

Process ID:5166

Hello! Geeks

Exiting

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值