gpt4 book ai didi

c - 使用报警功能

转载 作者:行者123 更新时间:2023-12-05 05:42:18 26 4
gpt4 key购买 nike

该程序实现了多个警报,以允许进程定义多个计时器(使用 alarm 函数)。问题是当我编译时我收到了一些警告,我该如何摆脱它们?另外,我觉得实现的不太好,我也想收到这方面的反馈

警告:

main.c:16:55: warning: implicit declaration of function ‘getppid’ [-Wimplicit-function-declaration]
16 | printf("sending kill SIGALRM to the parent %d\n", getppid());
| ^~~~~~~
main.c: In function ‘set_alarm’:
main.c:26:9: warning: implicit declaration of function ‘fork’ [-Wimplicit-function-declaration]
26 | if( fork() == 0 ){
| ^~~~
main.c:28:9: warning: implicit declaration of function ‘alarm’ [-Wimplicit-function-declaration]
28 | alarm(secs);
| ^~~~~
main.c:30:9: warning: implicit declaration of function ‘pause’ [-Wimplicit-function-declaration]
30 | pause();
| ^~~~~

代码:

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
/*
The OPTIONAL parent SIGALRM handler. Does nothing useful apart from logging the SIGALRM received from the child.
*/
void parent_alrm_handler(int signo) {
printf("Parent proc caught alrm signal\n");
return;
}

/*
The child SIGALRM handler.Replays the SIGALRM back to the parent when it receives it.
*/
void child_alrm_handler(int signo){
printf("sending kill SIGALRM to the parent %d\n", getppid());
kill(getppid(), SIGALRM);
}

/*
Since each proc has single timer. I create child proceses when the req for new alarms is made.
*/
void set_alarm(int secs){
signal(SIGALRM, parent_alrm_handler);
if( fork() == 0 ){
signal(SIGALRM, child_alrm_handler);
alarm(secs);
printf ("setting an alarm\n");
pause();
exit(0);
}
}

int
main(void) {
set_alarm(1); //alarm 1
set_alarm(4); //alarm 2
for(;;)
pause();
return 0;
}

最佳答案

您没有包含定义这些函数的 unistd.h

关于c - 使用报警功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72076919/

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