2010-08-08 50 views
1

我想用SIGNAL SIGSUR1兩個進程之間的通信,但我得到的編譯器錯誤:爲什麼我得到編譯器錯誤:'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); 
} 
} 
+0

選擇每個級別具有更多縮進的縮進樣式(建議4個,但Linux內核要求8個)。在你的功能打開/關閉括號的佈局方面保持一致。並使用代碼按鈕縮進代碼! – 2010-08-08 04:16:33

回答

8

改爲使用SIGUSR1。

+4

+1幾乎滿足最小答案長度,並有完整正確的答案。 :-) – 2010-08-08 04:23:05

相關問題