我正在练习信号。当我编译代码时,我收到了未声明的SIGALRM和未声明的SIGSTP错误消息。
main.c:46:16:错误:'SIGALARM'未声明(首次使用此功能) signal(SIGALARM,(void(*)(int))sig_alarm);
main.c:68:16:错误:'SIGSTP'未声明(首次使用此功能) signal(SIGSTP,(void(*)(int))int_handler);
#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
void sig_alarm(int sig){
kill(sig,SIGKILL);
}
void parent1(int sig){
kill(sig, SIGUSR1);
}
void parent2(int sig){
if(sig == 0){/*child is running*/
kill(sig, SIGUSR2);
}else if(sig > 0){/*parent is running*/
printf("Goodbye!");
exit(0);
}
}
void int_handler(int sig){}
void child1(int sig){
printf("Signal received!");
}
void child2(int sig){
printf("Child exiting...");
exit(0);
}
int main(){
pid_t pid;
if((pid = fork()) < 0){
perror("fork error");
}else if(pid > 0){/*parent*/
signal(SIGALARM, (void (*)(int))sig_alarm);
alarm(10);
signal(SIGTSTP, (void (*)(int))parent1);
while(1){
pause();
}
alarm(0);
signal(SIGALARM, (void (*)(int))sig_alarm);
alarm(10);
signal(SIGINT, (void (*)(int))parent2);
while(1){
pause();
}
alarm(0);
}else{/*child*/
signal(SIGINT, (void (*)(int))int_handler);
signal(SIGSTP, (void (*)(int))int_handler);
signal(SIGUSR1, (void (*)(int))child1);
while(1){
pause();
}
signal(SIGUSR2, (void (*)(int))child2);
while(1){
pause();
}
}
return 0;
}
答案 0 :(得分:4)