如何在C中的pthread中为system()调用启用SIGINT信号? [英] How to enable SIGINT signal for system() call in a pthread in C?

查看:112
本文介绍了如何在C中的pthread中为system()调用启用SIGINT信号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下有关system()的手册说,它会阻止通过system()调用运行的任何二进制程序的SIGINT和SIGQUIT信号.

The below manual on system() says it blocks SIGINT and SIGQUIT signal for any binary program run through system() call. https://man7.org/linux/man-pages/man3/system.3.html#:~:text=The%20system()%20library%20function,the%20command%20has%20been%20completed.

密码:

thread_1()
{
...
system("binary application");

}


main() {
...
pid = pthread_create(thread_1);
pthread_cancel(pid);

}

pthread_cancel向线程1发出SIGINT,这会杀死线程1,但不会杀死二进制应用程序.

pthread_cancel issues SIGINT to thread 1 which kill the thread 1, but not the binary application.

如何进行二进制申请"?收到SIGINT信号?

How to make the "binary application" receive the SIGINT signal?

推荐答案

手册页还显示:

(这些信号将根据其默认值在执行命令的子进程.)

(These signals will be handled according to their defaults inside the child process that executes command.)

因此,请回答您的问题如何进行二进制申请"".接收到SIGINT信号?";没关系,还是会的.阻塞发生在调用命令的线程中,而不是命令进程中.

So to answer your question "How to make the "binary application" receive the SIGINT signal?"; it's ok, it will anyway. The blocking happens in the thread that calls the command, not the command process.

要在下面回答@Hanu的评论,请使用 wait()系统调用集:您可以从那里获取system()调用内命令的pid,然后可以安全地关闭子线程或根据wait()的结果采取措施.但是我不知道如果进程终止,您需要清除哪些资源:Linux将释放与系统调用的进程相关的所有资源:操作系统清除pthread的方式有所不同当他们完成并处理资源时-请参见此SO答案.

To answer @Hanu's comment below, use the wait() set of system calls: you can get the pid of the command inside the system() call from there, and you can safely close your child thread or take action depending on the result of wait(). But I don't know what resources you would need to clean if the process has terminated: Linux will free all the resources associated with the process called by system: there is a distinction between how the OS cleans pthreads when they finish and process resources - see this SO answer .

这篇关于如何在C中的pthread中为system()调用启用SIGINT信号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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