linux信号学习02
未決信號集與阻塞信號集(信號屏蔽字)
阻塞信號集: 將某些信號加入集合,對他們設置屏蔽,當屏蔽x信號后,再收到該信號,該信號的處理將推后(解除屏蔽后)
未決信號集:
a. 信號產生,未決信號集中描述該信號的位立刻翻轉為1,表信號處于未決狀態。當信號被處理對應位翻轉回為0。這一時刻往往非常短暫。
b. 信號產生后由于某些原因(主要是阻塞)不能抵達。這類信號的集合稱之為未決信號集。在屏蔽解除前,信號一直處于未決狀態。
相當于 阻塞信號集在未決信號集前面設置了一堵墻
系統api產生信號
kill函數
參數介紹:
pid >0,要發送的進程ID
pid =0,代表當前進程組內所有進程
pid =-1, 代表有權限發送的所有進程
pid <-1, 代表 -pid對應組內所有進程
sig 對應的信號
kill函數代碼示例
de <sys/types.h> #include <signal.h> #include <sys/stat.h> #include <unistd.h> #include <stdio.h>int main() {int i=0;pid_t pid;for(; i<5; ++i) {pid = fork();if(pid==0){break;}}if(i=2) {printf("son---%d will kill fatherProgress\n",i);sleep(5);kill(getppid(), SIGKILL);while (1) {sleep(1);}} if(i=5) {while(1) {printf("father ----\n");sleep(1);}}return 0; }–分割線–
alarm
man alarm
alarm() arranges for a SIGALRM signal to be delivered to the calling
process in seconds seconds.
If seconds is zero, any pending alarm is canceled.
In any event any previously set alarm() is canceled.
alarm() 函數 給調用者(自己)發送 一個 SIGALRM 信號,在 seconds秒后。
如果 seconds是0, 取消之前的 設置的 alarm。
return返回值
返回之前設置的 alarm剩余的秒數,如果之前沒有設置alarm,就返回0.
alarm代碼示例:
#include <stdio.h> #include <signal.h> #include <unistd.h>int main() {int ret_1 = alarm(5);printf("%d\n", ret_1);sleep(2);int ret_2 = alarm(4);int num = 4;printf("%d\n", ret_2);while (1) {printf(" will ararm fater %d\n", num--);sleep(1);}return 0; }–分割線–
setitimer函數,周期性發送信號
man setitimer
參數介紹:
which 三種選擇
new_value 要設置的鬧鐘時間
old_value 原鬧鐘時間
return: 成功返回0, 失敗-1,并設置errno
setitimer 代碼示例:
總結
以上是生活随笔為你收集整理的linux信号学习02的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 看女性不孕
- 下一篇: linux 与信号集操作相关的函数