我的导师发布了这个程序来举例说明信号,我试图编译它,但我收到了三个警告:
Buzzoff.c:17:5: warning: implicit declaration of function 'alarm' is invalid in
C99 [-Wimplicit-function-declaration]
alarm(2);
^
Buzzoff.c:21:5: warning: implicitly declaring library function 'exit' with type
'void (int) __attribute__((noreturn))'
exit(-1);
^
Buzzoff.c:21:5: note: please include the header <stdlib.h> or explicitly provide
a declaration for 'exit'
Buzzoff.c:33:5: warning: implicit declaration of function 'pause' is invalid in
C99 [-Wimplicit-function-declaration]
pause();
^
3 warnings generated.
我仍然不明白信号是如何工作的,而且似乎无法弄清楚这个程序到底出了什么问题。有人可以帮助我了解问题所在以及如何解决吗?
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
void handler(int s)
{
printf("Please wake up 1730 !!!!!!! \n");
}
int cnt = 0;
void boom(int s)
{
if (cnt<5) {
printf("tick\n");
cnt = cnt+1;
alarm(2);
}
else {
printf("Boom!\n");
exit(-1);
}
}
int main()
{
signal(SIGINT, handler);
signal(SIGALRM, boom);
alarm(5);
while(1) {
printf("...ZZZZzzzzz...\n");
pause();
}
}