发送SIGINT到所有进程组儿童prevent控制-C [英] Prevent control-c from sending SIGINT to all process group children

查看:131
本文介绍了发送SIGINT到所有进程组儿童prevent控制-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个类POSIX环境中创建两个子过程(OS X / Linux)在C.基本上,它开始一个外部程序,并提供了传递消息的基本支持一个非常糟糕的,完全不透明的API接口来来回回。流程树看起来是这样的:

I'm trying to interface with a really crappy, completely opaque API that creates two subprocesses within a POSIX-like environment (OS X/Linux) in C. Basically, it starts an external program and provides rudimentary support for passing messages back and forth. The process tree looks something like this:

+ My_program
 \
  + an initiation shell script (csh -f -c external_program_startup_script)
   \
    - the external program instance

当我preSS控制-C终端,而 My_program 运行,控制终端发送SIGINT在其进程组的所有进程 - 上述所有三个过程。我的希望的SIGINT去的程序实例,但如果它也击中shell脚本那么这中间的过程被终止,通信链路被切断。

When I press control-c in the terminal while My_program is running, the controlling terminal sends SIGINT to all processes in its process group — all three above processes. I want SIGINT to get to the program instance, but if it also hits the shell script then that middle process is terminated and the communication link is severed.

My_program ,我可以设置一个信号处理程序忽略SIGINTs。但我有过两个孩子的过程绝对没有控件(API甚至不暴露自己的PID),所以如改变自己的进程组或附加的处理程序将无法正常工作的现有解决方案。有没有一种方法,以prevent从前台进程组中发送SIGINT到所有进程的控制终端?

Within My_program, I can setup a signal handler to ignore SIGINTs. But I have absolutely no control over the two child processes (the API doesn't even expose their PIDs), so existing solutions such as changing their process group or attaching handlers won't work. Is there a way to prevent the controlling terminal from sending SIGINT to all processes in the foreground process group?

(有问题的API是 MATLAB的libeng ,它允许一个外部C程序中处理MATLAB的命令。但是它有发送中断超出该操作系统提供绝对没有的功能。)

(The API in question is MATLAB's libeng, which allows an external C program to process commands within MATLAB. But it has absolutely no functionality for sending interrupts beyond that which the OS provides.)

推荐答案

您需要运行在单独的对照组​​的子流程。您可以通过 setpgid的正确使用()函数实现这个目标。 叉()呼叫后,在子进程运行 setpgid(),则执行的是程序要运行在不同的组。

You need to run sub-processes in separate control group. You can achieve that by proper usage of setpgid() function. After fork() call, in the child process run setpgid(), then exec the program you want to be run in separate group.

这篇关于发送SIGINT到所有进程组儿童prevent控制-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆