[英]Sending Signals to Child Processes to Update Value
I am new to the fork() function in C and have been having some problems.我是 C 中的 fork() function 的新手,遇到了一些问题。 I haven't been able to find an answer that I can understand.我一直没能找到我能理解的答案。 I have a parent process which takes input from the user to signal a child process to update a value.我有一个父进程,它接受用户的输入来通知子进程更新一个值。 What I have below is a simplified version of the actual program.我下面的是实际程序的简化版本。 I understand that I am not checking return values or anything of the like.我知道我没有检查返回值或类似的东西。
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
void parentFunc(int *pid);
void childFunc(void);
void countSignal(int signo);
int count = 10000;
int main(void){
signal(SIGUSR1, countSignal);
int pid[3];
// create child processes
pid[2] = fork();
if(pid[2] > 0){
pid[1] = fork();
if(pid[1] > 0){
pid[0] = fork();
if(pid[0] > 0){
parentFunc(pid);
}
else childFunc();
}
else childFunc();
}
else childFunc();
return 0;
}
void parentFunc(int *pid){
signal(SIGUSR1, SIG_DFL);
signal(SIGUSR2, SIG_DFL);
int identifier;
while(1){
identifier = fgetc(stdin) - '0';
kill(pid[identifier-1], SIGUSR1);
}
}
void childFunc(void){
srand(getpid());
while(1){
count -= (rand() % (3001 - 1000) + 1000);
sleep(5);
}
}
void countSignal(int signo){
signal(SIGUSR1, countSignal);
count += 2000;
printf("Child %d has been fed by Parent %d\n", getpid(), getppid());
printf("New count level: %d\n", count);
}
When a child receives a signal, does it then skip the sleep(5) function?当孩子收到信号时,它会跳过睡眠(5)function 吗? The issue I am having is that every time I try to update a value it seems to add 2000 and then immediately subtract the random value instead of subtracting on 5 second intervals.我遇到的问题是,每次我尝试更新一个值时,它似乎都会添加 2000,然后立即减去随机值,而不是每隔 5 秒减去一次。
I would like it to receive signals to add 2000 at any interval but only subtract every 5 seconds.我希望它接收信号以在任何时间间隔添加 2000,但每 5 秒只减去一次。 Thanks.谢谢。
When a child receives a signal, does it then skip the sleep(5) function?当孩子收到信号时,它会跳过睡眠(5)function 吗?
Yes, if the signal is not ignored, it skips the remaining sleep duration.是的,如果不忽略信号,它会跳过剩余的睡眠时间。 From man 3 sleep
, emphasis added:来自man 3 sleep
,重点补充说:
DESCRIPTION描述
sleep() causes the calling thread to sleep either until the number of real-time seconds specified in seconds have elapsed or until a signal arrives which is not ignored . sleep() 导致调用线程休眠,直到以秒为单位指定的实时秒数已经过去,或者直到一个未被忽略的信号到达。
RETURN VALUE返回值
Zero if the requested time has elapsed, or the number of seconds left to sleep, if the call was interrupted by a signal handler .如果请求的时间已经过去,则为零;如果调用被信号处理程序中断,则为剩余睡眠秒数。
With that in mind, consider the following snippet:考虑到这一点,请考虑以下代码片段:
unsigned seconds = 5;
while (seconds)
seconds = sleep(seconds);
For more precision, consider man 2 nanosleep
.为了更精确,请考虑man 2 nanosleep
。
Aside: man 7 signal-safety
is worth reading to understand what POSIX defines as safe to do inside of a signal handler (does not include calling printf
).旁白: man 7 signal-safety
值得一读,以了解 POSIX 在信号处理程序内部定义的安全操作(不包括调用printf
)。
See also: sig_atomic_t
另见: sig_atomic_t
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.