📅  最后修改于: 2023-12-03 14:46:22.481000             🧑  作者: Mango
在Linux的信号处理中,有一个 SIGCONT 信号,表示继续执行一个已经暂停的进程。在Python的os模块中,可以使用os.WIFCONTINUED()方法来判断一个进程是否是由SIGCONT信号恢复执行的。
os.WIFCONTINUED(status)
如果进程是由SIGCONT信号恢复执行的,返回True;否则返回False。
import os
import signal
pid = os.fork()
if pid == 0:
print("Child process is starting...")
os.kill(os.getpid(), signal.SIGSTOP)
print("Child process is resumed...")
os._exit(0)
else:
print(f"Parent process is waiting for child process {pid}")
pid, status = os.waitpid(pid, 0)
if os.WIFSTOPPED(status):
print(f"Child process {pid} is stopped by signal {os.WSTOPSIG(status)}")
os.kill(pid, signal.SIGCONT)
print(f"Child process {pid} is resumed by signal SIGCONT")
os.waitpid(pid, 0)
print(f"Child process {pid} is exited with status {os.WEXITSTATUS(status)}")
elif os.WIFEXITED(status):
print(f"Child process {pid} is exited normally with status {os.WEXITSTATUS(status)}")
elif os.WIFSIGNALED(status):
print(f"Child process {pid} is killed by signal {os.WTERMSIG(status)}")
以上是一个关于子进程(SIGSTOP | SIGCONT)的示例代码,在示例代码中,我们使用os.WIFCONTINUED()方法来判断子进程是否被恢复执行。
Parent process is waiting for child process 1234
Child process is starting...
Child process is resumed...
Child process 1234 is stopped by signal 19
Child process 1234 is resumed by signal SIGCONT
Child process 1234 is exited with status 0