实验:sigsuspend(),sigprocmask()
生活随笔
收集整理的這篇文章主要介紹了
实验:sigsuspend(),sigprocmask()
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
實驗:sigsuspend(),sigprocmask()
源代碼:/* * Program: pause_suspend.c * To test the difference between sigsuspend() and paus(). * Author: zsl * Date: 2014-10-17 * First release. * 參見網頁:http://blog.csdn.net/liwentao1091/article/details/6619089 * * */ #include <stdio.h> #include <stdlib.h> #include <signal.h> #include <unistd.h> #include <string.h> /* * Handler for SIGINT (Ctrl-C), SIGQUIT (Ctrl-\) * */ void sig_func(int signo) { if ( SIGINT == signo ) // just print a line. { printf(" SIGINT is processing...\n "); } if ( SIGQUIT == signo ) // print a line and exit. { printf(" SIGQUIT is processing ...\n "); printf(" Now exiting ...\n "); exit(EXIT_SUCCESS); } } int main(void) { int i; sigset_t maskset, set_quit; sigemptyset(&maskset); sigemptyset(&set_quit); // initialize two sets. sigaddset(&maskset, SIGINT); // mask SIGINT sigaddset(&set_quit, SIGQUIT); // suspend SIGQUIT // signal the two signals: SIGINT, SIGQUIT signal(SIGINT, sig_func); signal(SIGQUIT, sig_func); while(1) { /* First to mask the signal of the process: SIGINT */ sigprocmask(SIG_BLOCK, &maskset, NULL); for(i = 0; i < 10; i ++) { write(1, "* ", strlen("* ")); sleep(1); } printf("\n"); #if 1 /* * while sigsuspend(), SIGQUIT is blocked. * but SIGINT is unblocked. * If you want to execute the two signals, you * should Ctrl-\, then Ctrl-C. * */ printf("Before sigsuspend() ... \n"); sigsuspend(&set_quit); #else sigprocmask(SIG_UNBLOCK, &maskset, NULL); pause(); #endif } return 0; } 程序的運行:在打印“* ?”的時候,SIGINT (Ctrl-C)被 阻塞了。而SIGQUIT沒有被阻塞,只要Ctrl-\就會終止程序。在sigsuspend() 的時候,SIGQUIT (Ctrl-\)被阻塞了,而SIGINT 沒有被阻塞,只要 Ctrl-C 就會進入 signal handler 中執行。。如果想在 sigsuspend() 中對兩個信號都進行處理,那么在 sigsuspend() 的時候先 SIGQUIT, 然后 SIGINT。如下是執行效果圖:
來自為知筆記(Wiz)
源代碼:
來自為知筆記(Wiz)
轉載于:https://www.cnblogs.com/LinTeX9527/p/4031428.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的实验:sigsuspend(),sigprocmask()的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [LeetCode]Find Minim
- 下一篇: 关于Spring batch的学习之CS