当Sigint发送给父母时,不要将Sigint发送给孩子

发布于 2025-02-13 03:06:03 字数 1215 浏览 1 评论 0原文

有没有一种方法可以在发送给父母的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 cloneed-execveed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤还要旧人安 2025-02-20 03:06:03

请注意,这是将键盘生成的信号(例如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 with sigprocmask before spawning children with clone. The children inherit the signal mask.

After spawning children unblock SIGINT. A pending SIGINT 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文