歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 一步一步學Linux C:sigsuspend 進程阻塞

一步一步學Linux C:sigsuspend 進程阻塞

日期:2017/3/1 10:26:13   编辑:Linux編程

int sigsuspend(const sigset_t *sigmask);

此函數用於進程的掛起,sigmask指向一個信號集。當此函數被調用時,sigmask所指向的信號集中的信號將賦值給信號掩碼。之後進程掛起。直到進程捕捉到信號,並調用處理函數返回時,函數sigsuspend返回。信號掩碼恢復為信號調用前的值,同時將errno設為EINTR。進程結束信號可將其立即停止。

  1. #include <stdio.h>
  2. #include <signal.h>
  3. void checkset();
  4. void func();
  5. void main()
  6. {
  7. sigset_tblockset,oldblockset,zeroset,pendmask;
  8. printf("pid:%ld\n",(long)getpid());
  9. signal(SIGINT,func);
  10. sigemptyset(&blockset);
  11. sigemptyset(&zeroset);
  12. sigaddset(&blockset,SIGINT);
  13. sigprocmask(SIG_SETMASK,&blockset,&oldblockset);
  14. checkset();
  15. sigpending(&pendmask);
  16. if(sigismember(&pendmask,SIGINT))
  17. printf("SIGINTpending\n");
  18. if(sigsuspend(&zeroset)!= -1)
  19. {
  20. printf("sigsuspenderror\n");
  21. exit(0);
  22. }
  23. printf("afterreturn\n");
  24. sigprocmask(SIG_SETMASK,&oldblockset,NULL);
  25. printf("SIGINTunblocked\n");
  26. }
  27. void checkset()
  28. { sigset_tset;
  29. printf("checksetstart:\n");
  30. if(sigprocmask(0,NULL,&set)<0)
  31. {
  32. printf("checksetsigprocmask error!!\n");
  33. exit(0);
  34. }
  35. if(sigismember(&set,SIGINT))
  36. printf("sigint\n");
  37. if(sigismember(&set,SIGTSTP))
  38. printf("sigtstp\n");
  39. if(sigismember(&set,SIGTERM))
  40. printf("sigterm\n");
  41. printf("checksetend\n");
  42. }
  43. void func()
  44. {
  45. printf("hellofunc\n");
  46. }
Copyright © Linux教程網 All Rights Reserved