Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / 一步一步学Linux C:raise

信号的发送的关键是使系统知道向哪个进程发送信号以及发送什么信号。其中要注意的是能否向某一进程发送某个特定的信号是和用户的权限密切相关的。例如,只有系统管理员才能发送SIGKILL信号终止进程。用于发送信号的系统调用。
  1. #include <sys/types.h>   
  2.   
  3. #include <signal.h>   
  4.   
  5. #include <unistd.h>   
  6.   
  7. #include <sys/time.h>   
  8.   
  9. int kill(pid_t pid,int signumber);  
  10.   
  11. int raise(int signumber);  
  12.   
  13. unsigned int alarm(unsigned intseconds);  
  14.   
  15. int setitimer(int which,const structitimerval *value,struct itimerval *oldvalue);  
raise:用于向进程自身发送信号。成功返回0,失败返回-1。
  1. #include <stdio.h>   
  2. #include <signal.h>   
  3. void func();  
  4. void main()  
  5. {  
  6.      charbuffer[100];  
  7.      if(SIG_ERR== signal(SIGINT,&func))  
  8.      {  
  9.          printf("signalerror!! ");  
  10.          exit(-1);  
  11.      }  
  12.      for(;;)  
  13.      {  
  14.          fgets(buffer,sizeof(buffer),stdin);  
  15.          if(strcmp(buffer,"sigint ")== 0)  
  16.               raise(SIGINT);  
  17.          else  
  18.               printf("nocampare ");  
  19.      }  
  20. }  
  21.   
  22. void func()  
  23. {  
  24.      printf("hellofunc ");  
  25. }