Why do I get compiler error: ‘SIGSUR1’ was not declared in this scope?
I want to use the SIGNAL SIGSUR1 to communicate between two processes, but I get the compiler error:
error: ‘SIGSUR1’ was not declared in this scope .
What's the fix?
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <stdlib.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
void cursor(int y)
{
int i;
printf("%c[%d;%df",0x1B,y,0);
}
void handle(int fd,int turtle_current_pos){
fcntl(fd,F_SETFL,O_NONBLOCK);
write(fd,&turtle_current_pos,sizeof(int));
}
int getdist(int fd,int hare_pos,int max_dist)
{
int r,n;
raise(0,SIGSUR1);
fcntl(fd,F_SETFL,O_NONBLOCK);
if((n=read(fd,&r,sizeof(int)))){
if((hare_pos-r-max_dist)>0)
return 0;
else
return 1;
}
}
void print(char b,int a){
fflush(stdout);
if(b=='T') cursor(10);
else cursor(15);
for(int i=0;i<a;i++) printf(" ");
printf("%c\n",b);
}
void turtle(int fd,int sec1,int turtle_speed){
signal(SIGSUR1,handle);
struct timeval b;
int flag=1,turtle_current_pos,turtle_previous_pos=0,sec2;
turtle_current_pos=0;
while(turtle_current_pos<100){
sleep(2);
gettimeofday(&b,NULL);
sec2=b.tv_sec;
//printf("%d\n",sec2);
turtle_current_pos=(sec2-sec1)*turtle_speed;
fflush(stdout);
if((turtle_current_pos-turtle_previous_pos)>=1){
turtle_previous_pos=turtle_current_pos;
print('T',turtle_previous_pos);
}
}
}
int main(){
system("clear");
pid_t pid,God_pid,hare_pid,Report_pid;
int max_dist=10,sleep_time=1,pipe1[2],speed_hare=5,speed_turtle=1;
p开发者_运维问答ipe(pipe1);
pid=fork();
if(pid!=0){
int hare_current_pos=0,hare_previous_pos=0,sec1,sec2;
struct timeval n;
gettimeofday(&n,NULL);
sec1=n.tv_sec;
sec2=sec1;
close(pipe1[1]);
while(hare_current_pos<100){
sleep(1);
if(getdist(pipe1[0],hare_current_pos,max_dist)==0){
sleep(sleep_time);
gettimeofday(&n,NULL);
sec1=n.tv_sec;
sec2=sec1;
fflush(stdout);
}
else{
gettimeofday(&n,NULL);
sec2=n.tv_sec;
gettimeofday(&n,NULL);
hare_current_pos+=speed_hare*(sec2-sec1);
//printf("\n%d",hare_current_pos);
fflush(stdout);
if(hare_current_pos-hare_previous_pos){
print('H',hare_current_pos);
hare_previous_pos=hare_current_pos;
}
}
}
}
else{
close(pipe1[0]);
struct timeval b;
gettimeofday(&b,NULL);
fflush(stdout);
turtle(pipe1[1],b.tv_sec,speed_turtle);
}
}
Try SIGUSR1 instead.
精彩评论