信号
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include<unistd.h> 4 #include<string.h> 5 #include<sys/types.h> 6 #include<sys/wait.h> 7 #include<sys/ipc.h> 8 #include<sys/shm.h> 9 #include<signal.h> 10 11 int func1(int a,int b) 12 { 13 return a+b; 14 } 15 16 void catch_Signal(int Sign) 17 { 18 switch(Sign) 19 { 20 case SIGINT: 21 printf("SIGINT Signal\n"); 22 exit(0); 23 case SIGALRM: 24 printf("SIGALRM Signal\n"); 25 // alarm(5); 26 break; 27 } 28 29 } 30 31 void mysleep(int i) 32 { 33 alarm(i); 34 pause(); 35 } 36 37 int signal1(int signo,void (*func)(int)) 38 { 39 struct sigaction act,oact; 40 act.sa_handler = func; 41 sigemptyset(&act.sa_mask); 42 act.sa_flags = 0; 43 return sigaction(signo,&act,&oact); 44 45 } 46 47 int main(int arg ,char *args[]) 48 { 49 // int (*p)(int,int); 50 // p = func1; //鍥炶皟鍑芥暟 51 // printf("p=%d\n",p(4,6)); 52 printf("SIGINT = %d,SIGALRM = %d\n",SIGINT,SIGALRM); 53 54 // signal(SIGINT,catch_Signal); 55 // signal(SIGALRM,catch_Signal); 56 signal1(SIGINT,catch_Signal); 57 signal1(SIGALRM,catch_Signal); 58 59 int i=0; 60 mysleep(10); 61 printf("LLLLLLLLLLLLLLLLLLLLL\n"); 62 while(1) 63 { 64 scanf("%d",&i); 65 printf("hello%d\n",i++); 66 sleep(1); 67 } 68 69 return EXIT_SUCCESS; 70 }