为什么我会收到编译器错误:“SIGSUR1”在此范围未声明? [英] Why do I get compiler error: ‘SIGSUR1’ was not declared in this scope?

查看:539
本文介绍了为什么我会收到编译器错误:“SIGSUR1”在此范围未声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 错误:'SIGSUR1没有在这个范围内声明。

什么是修复?

 的#include<&stdio.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&; SYS / time.h中>
#包括LT&;&stdlib.h中GT;
#包括LT&;&fcntl.h GT;
#包括LT&;&signal.h中GT;
#包括LT&; SYS / types.h中>无效光标(int y)对
{
     INT I;
     的printf(%C [%d个;%DF,0x1B,Y,0);
}
无效手柄(INT FD,诠释turtle_current_pos){
 使用fcntl(fd,F_SETFL,O_NONBLOCK);
 写(FD,和放大器; turtle_current_pos,sizeof的(INT));
}
INT getdist(INT FD,诠释hare_pos,诠释max_dist)
{
 INT R,N;
 提高(0,SIGSUR1);
 使用fcntl(fd,F_SETFL,O_NONBLOCK); 如果((N =读(FD,&放大器; R,的sizeof(int)的))){
  如果((hare_pos-R-max_dist)大于0)
   返回0;
  其他
   返回1;
 }
}无效打印(炭B,诠释一){
fflush(标准输出); 如果(二=='T')光标(10);
 其他光标(15); 的for(int i = 0; I< A;我++)的printf();
  的printf(%C \\ N,B);}
无效龟(INT FD,诠释SEC1,诠释turtle_speed){
 信号(SIGSUR1,手柄);
 timeval结构B:
 INT标志= 1,turtle_current_pos,turtle_ previous_pos = 0秒2; turtle_current_pos = 0; 而(turtle_current_pos< 100){
      睡眠(2);
      gettimeofday的(和b,NULL);
      秒2 = b.tv_sec;
      //的printf(%d个\\ N,秒2);
      turtle_current_pos =(秒2-SEC1)* turtle_speed;
  fflush(标准输出);  如果((turtle_current_pos-turtle_ previous_pos)GT = 1){
   turtle_ previous_pos = turtle_current_pos;
   打印(T,turtle_ previous_pos);
  }
 }
}
诠释主(){
 系统(清除); 将为pid_t PID,God_pid,hare_pid,Report_pid; INT max_dist = 10,SLEEP_TIME = 1,PIPE1 [2],speed_hare = 5,speed_turtle = 1; 管(PIPE1); PID =叉(); 如果(PID!= 0){
  INT hare_current_pos = 0,hare_ previous_pos = 0,SEC1,秒2;
  timeval结构N;
  函数gettimeofday(安培; N,NULL);
  SEC1 = n.tv_sec;
  秒2 = SEC1;
  关闭(PIPE1 [1]);  而(hare_current_pos< 100){
   睡眠(1);
   如果(getdist(PIPE1 [0],hare_current_pos,max_dist)== 0){
    睡眠(SLEEP_TIME);
    函数gettimeofday(安培; N,NULL);
    SEC1 = n.tv_sec;
    秒2 = SEC1;
    fflush(标准输出);
   }
   其他{
    函数gettimeofday(安培; N,NULL);
    秒2 = n.tv_sec;
    函数gettimeofday(安培; N,NULL);
    hare_current_pos + = speed_hare *(秒2-SEC1);
    //的printf(\\ N%D,hare_current_pos);
    fflush(标准输出);    如果(hare_current_pos-hare_ previous_pos){
     打印(H,hare_current_pos);
     hare_ previous_pos = hare_current_pos;
    }
   }
  }
 }
 其他{
  关闭(PIPE1 [0]);
  timeval结构B:
  gettimeofday的(和b,NULL);
  fflush(标准输出);
  龟(PIPE1 [1],b.tv_sec,speed_turtle);
 }
}


解决方案

尝试SIGUSR1代替。

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;

 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 SIGUSR1 instead.

这篇关于为什么我会收到编译器错误:“SIGSUR1”在此范围未声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆