通过 PID 分配信号处理程序
assign signal handler by pid
如果我知道某个进程的pid不运行代码(比如火狐) 如何为其分配信号处理程序(例如 SIGINT)?
我现在有 :
pid = fork();
printf("forked and my pid is %dn",pid);
//check for errors
if (pid<0){
printf("Error: invoking fork to start ss has failed, Exitingn ");
exit(1);
}
//the child process runs the gulp
if (pid==0){
printf("STARTING THE FIREFOXn");
//calling signal(somehandler,SIGINT); here will bind the child, which is replaced by the firefox new process,hence won't invoke the "somehandler"
if (execv(args[0],args)<0){
perror("Error: running s with execvp has failed, Exitingn");
}
//invoking signal(somehandler,SIGINT); will obviously not do anything
printf("IVE BEEN KILLEDn");
}
//dad is here
printf("DAD IS GOING TO KILLn");
if (pid>0){
sleep(6);
//how do I bind a handler to that signal????
kill(get_pidof(string("firefox")),SIGINT);
}
您只能在流程中建立信号处理程序。换句话说,你不能让 Firefox 在获得 SIGINT 时调用你的信号处理程序。
编辑
正如您所注意到的,信号处理程序确实不会在执行之后保留 - 进程的图像被替换,因此没有意义。所以,就像我之前说的:即使你控制了它的父级,你也无法firefox
调用你的处理程序。
我需要我的程序运行另一个程序(比如火狐),并且知道 当火狐死亡或崩溃时
在这种情况下,您想为SIGCHLD
建立一个信号处理程序:当孩子死亡时,您的进程将跳转到它。
正如Cnucitar在这里回答的那样,您只能从进程内部更改信号处理程序。
如果你想在 Firefox 中制作一个信号处理程序,你可以修补它,也许通过一个插件。但我相信这将是一个坏主意。
相关文章:
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 如何在信号处理程序和普通函数中对全局变量进行互斥读写操作
- 有可能在信号处理程序中设置promise吗
- 在信号处理程序中捕获C++未处理的异常并恢复应用程序
- 通过安装信号处理程序关闭多线程应用程序
- 为什么我的信号处理程序只执行一次?
- GTK C++:找不到信号处理程序 您是否使用 -rdynamic 进行了编译?
- C++ 在信号处理程序后继续执行
- 没有信号处理程序的POSIX定时器的目的是什么?
- 为什么这个信号处理程序不能捕获 SIGHUP 或 SIGQUIT?
- 如何在C++中使用 std::bind 函数作为信号处理程序?
- 在 C++17 中,是否未定义使用无锁原子学保护从信号处理程序传递的数据?
- 如何将信号处理程序添加为方法
- 窗口上信号处理程序的异步安全写入函数
- 使用信号处理程序处理从 FIFO 接收的数据
- gtkmm/glade - 将信号处理程序连接到 MenuShell
- GTK+3 编译错误"找不到信号处理程序",您是否使用 -rdynamic 进行了编译?
- Linux 中的信号处理程序使用 sigaction (C++)
- 使用 enum 参数将 C++ 对象信号连接到 QML 信号处理程序
- 信号处理程序不要求使用dlopen或dlclose