当Sigint发送给父母时,不要将Sigint发送给孩子
有没有一种方法可以在发送给父母的sigint
时未将sigint
发送到子进程?示例:
main.c
:
#define _GNU_SOURCE
#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sched.h>
int run_python(void* arg){
(void) arg;
char *const args[] = {"python3", "run.py", NULL };
execve("/usr/bin/python3", args, NULL);
return 0;
}
int main(void){
char *stack = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 1234, 0);
clone(run_python, stack + 4096, 0, NULL);
sleep(10);
return 0;
}
正在以儿童为子运行的程序run.py
:
import sys
import time
import signal
f = open("output.output", 'w')
def recieved_sigint(*args):
print("Received sigint", file=f, flush=True)
sys.exit(1)
signal.signal(signal.SIGINT, recieved_sigint)
while(True):
print("Test", file=f, flush=True)
time.sleep(1)
问题是执行main.c
并按下时ctrl+C
run.py
程序还接收sigint
。字符串打印了“收到的sigint”
。
有没有办法不将sigint
发送给clone
ed- execve
ed的孩子?
Is there a way to not sent SIGINT
to a child process when SIGINT
is sent to a parent? Example:
main.c
:
#define _GNU_SOURCE
#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sched.h>
int run_python(void* arg){
(void) arg;
char *const args[] = {"python3", "run.py", NULL };
execve("/usr/bin/python3", args, NULL);
return 0;
}
int main(void){
char *stack = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 1234, 0);
clone(run_python, stack + 4096, 0, NULL);
sleep(10);
return 0;
}
The program that is being run as a child run.py
:
import sys
import time
import signal
f = open("output.output", 'w')
def recieved_sigint(*args):
print("Received sigint", file=f, flush=True)
sys.exit(1)
signal.signal(signal.SIGINT, recieved_sigint)
while(True):
print("Test", file=f, flush=True)
time.sleep(1)
The problem is when executing main.c
and pressing Ctrl+C
the run.py
program also receives SIGINT
. The string "Received sigint"
is printed.
Is there a way to not send SIGINT
to a child that was clone
ed-execve
ed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意,这是将键盘生成的信号(例如
sigint
)发送到过程组连接到终端。block
sigint
在父母中带有SigProcMask
在用克隆
产卵之前。孩子们继承了信号面具。产卵后,
sigint
。悬而未决的sigint
将在解放时(如果有)交付。用
sig_ign
忽略信号时,在产卵儿童过程的同时,会丢弃信号,这可能会导致零星的错过信号竞赛条件,但不会可靠地再现。Note that this is the kernel sending keyboard-generated signals, such as
SIGINT
, to all processes in the process group attached to the terminal.Block
SIGINT
in the parent withsigprocmask
before spawning children withclone
. The children inherit the signal mask.After spawning children unblock
SIGINT
. A pendingSIGINT
will be delivered upon unblocking, if any.Ignoring the signal with
SIG_IGN
while spawning children processes risks discarding the signal which may lead to sporadic missed signal race conditions which don't reproduce reliably.