alarm函數

這個函數是設置一個定時器,在接下來的某個時刻該定時器會超時,發生超時後,產生SIGALRM信號。函數

產生信號後,進程的行爲分兩種狀況:測試

1. 忽略或者不捕獲此信號spa

  終止調用該alarm函數的進程code

2. 捕獲此信號blog

  根據信號處理程序採起動做進程

 

測試代碼:it

 1 #include <signal.h>
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <unistd.h>
 5 
 6 #define USE_ALARM
 7 
 8 #ifdef USE_ALARM
 9 static int x;
10 static void sig_alarm(int signo)
11 {
12     printf("alarm! -- %d\n",x++);
13 }
14 #endif
15 
16 int main(int argc,char** argv)
17 {
18     #ifdef USE_ALARM
19     if(signal(SIGALRM,sig_alarm)==SIG_ERR)
20     {
21         printf("error 1\n");
22         exit(1);
23     }
24     #endif
25     alarm(1);
26     while(1)
27     {
28         alarm(1);        
29         pause();
30     }
31     return 0;
32 }

分別不使用宏定義USE_ALARM,執行效果以下:io

相關文章
相關標籤/搜索