gpt4 book ai didi

c - 如何使用 C 中的信号同步两个进程(子进程和父进程)?

转载 作者:行者123 更新时间:2023-11-30 19:04:47 25 4
gpt4 key购买 nike

我想使用信号(SIGUSR1)来同步C中的进程。我希望父进程等待信号,当收到此信号时,向子进程发送相同的信号。我写了一小段来刺激推理,但它并没有消失。

#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>



void trataSIGUSR1(int sigNum) {
printf("SIGUSR1\n");
}

int main(void) {

pid_t pid;
struct sigaction sa;

pid = fork();

if (pid == 0) {
struct sigaction sa = {0};
sa.sa_handler = trataSIGUSR1;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(SIGUSR1,&sa,NULL);
pause();
printf("This never execute");
} else {
printf("I'am father: %d!\n",getppid());
kill(0,SIGUSR1);
pause();
}
}

输出

I'am father: 12082!
User defined signal 1: 30

最佳答案

简单的提示是使用 pause()kill()pause() 用于阻止进程执行,直到收到任何信号,一旦收到信号,do_something()kill() 用于发送 SIGUSR1 信号。

此外,当您使用 pause() 时,它将暂停进程,直到收到任何信号,并且对于该信号,默认操作应该是用户定义的 ISR。来自 pause() 的手册页

RETURN VALUE pause() returns only when a signal was caught and the signal-catching function returned. In this case pause() returns -1, and errno is set to EINTR.

这是示例所需的示例代码

//int nSIGINT = 0; /* declare variable of type volatile sigatomic_t  */
volatile sigatomic_t nSIGINT;
void trataSIGINT(int sigNum) {
nSIGINT = 1;/* set the flag as needed */
}
int main(void ){
int pid;
pid=fork();/* create child process */
if(pid==0) {
//signal(SIGUSR1,trataSIGINT);/* instead of signal() use sigaction */
struct sigaction sa = {0}; /* initialize sa or fill all its members*/
sa.sa_handler = trataSIGINT;/* set the handler to trataSIGINT*/
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(SIGUSR1,&sa,NULL); /* when child received SIGUSR1, trataSIGINT gets called */
pause(); /* wait until any signal received */
/* do_something_child() code, this you want to run only after receiving signal */

}
else {
/* do_something_parent() */
printf("parent about to send user signal to child\n");
kill(pid,SIGUSR1); /*send SIGUSR1 to child */
wait(0); /* wait till child completes */
}
return 0;
}

旁注,用于在收到 SIGUSR1 时在 trataSIGINT() 中设置标志,而不是声明 int nSIGINT = 0; 声明标志变量作为 volatile sigatomic_t 类型的类型。

From ISO/IEC 9899:2011 §7.14.1.1 The signal function

¶5 If the signal occurs other than as the result of calling the abort or raise function, the behavior is undefined if the signal handler refers to any object with static or thread storage duration that is not a lock-free atomic object other than by assigning a value to an object declared as volatile sig_atomic_t, or the signal handler calls any function in the standard library other than the abort function, the _Exit function, the quick_exit function, or the signal function with the first argument equal to the signal number corresponding to the signal that caused the invocation of the handler. Furthermore, if such a call to the signal function results in a SIG_ERR return, the value of errno is indeterminate.252)

252) If any signal is generated by an asynchronous signal handler, the behavior is undefined.

关于c - 如何使用 C 中的信号同步两个进程(子进程和父进程)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51074469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com