2021-09-19
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 |