我想使用信号SIGSUR1在两个进程之间进行通信,但我得到编译器错误:

error: ‘SIGSUR1’ was not declared in this scope .

怎么解决?
#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;

 pipe(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 Sigusser1 Instead

关于c - 为什么会出现编译器错误:在此范围内未声明“SIGSUR1”?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3433042/

10-10 16:26