linux信號學習02

  1. 未決信號集與阻塞信號集(信號屏蔽字)
    阻塞信號集: 將某些信號加入集合,對他們設置屏蔽,當屏蔽x信號后,再收到該信號,該信號的處理將推后(解除屏蔽后)
    未決信號集:
    a. 信號產生,未決信號集中描述該信號的位立刻翻轉為1,表信號處于未決狀態。當信號被處理對應位翻轉回為0。這一時刻往往非常短暫。
    b. 信號產生后由于某些原因(主要是阻塞)不能抵達。這類信號的集合稱之為未決信號集。在屏蔽解除前,信號一直處于未決狀態。
    相當于 阻塞信號集在未決信號集前面設置了一堵墻
    在這里插入圖片描述

  2. 系統api產生信號
    kill函數

#include <sys/types.h>
#include <signal.h>
int kill(pid_t pid, int sig);

參數介紹:
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

 #include <unistd.h>unsigned int alarm(unsigned int seconds);

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

#include <sys/time.h>
int getitimer(int which, struct itimerval *curr_value);
int setitimer(int which, const struct itimerval *new_value,
struct itimerval *old_value);

參數介紹:
which 三種選擇

真實時間ITIMER_REAL    decrements in real time, and delivers SIGALRM upon expi‐ration.計算進程執行時間ITIMER_VIRTUAL decrements  only  when  the  process  is  executing, anddelivers SIGVTALRM upon expiration.計算進程執行 + 調度時間
ITIMER_PROF    decrements both when the process executes and  when  thesystem  is  executing on behalf of the process.  Coupledwith ITIMER_VIRTUAL, this timer is usually used to  pro‐file  the time spent by the application in user and ker‐nel space.  SIGPROF is delivered upon expiration.
struct itimerval {struct timeval it_interval; /* Interval for periodic timer */  周期定時器間隔struct timeval it_value;    /* Time until next expiration */ 下次執行時間
};struct timeval {time_t      tv_sec;         /* seconds */ 秒數suseconds_t tv_usec;        /* microseconds */ 微秒數
};

new_value 要設置的鬧鐘時間
old_value 原鬧鐘時間

return: 成功返回0, 失敗-1,并設置errno
setitimer 代碼示例:

#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>void catch(int sign) {if(sign == SIGALRM) {printf("catch signal is %d\n", sign);}   
}
int main() {signal(SIGALRM, catch);struct itimerval new_value = {{3, 0},{1, 0}};setitimer(ITIMER_REAL, &new_value,  NULL);while (1){printf(" setitimer test\n");sleep(1);}return 0;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/382512.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/382512.shtml
英文地址,請注明出處:http://en.pswp.cn/news/382512.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

task_struct 結構如何查看及分析

cd /find -name sched.hvim usr/src/kernels/3.10.0862.6.3.el7.x86_64/include/linux/sched.hhttps://www.cnblogs.com/zxc2man/p/6649771.html 進程是處于執行期的程序以及它所管理的資源&#xff08;如打開的文件、掛起的信號、進程狀態、地址空間等等&#xff09;的總稱。…

linux 與信號集操作相關的函數

與信號集操作相關的函數 #include <signal.h> 清空信號集 全都為0 int sigemptyset(sigset_t *set);填充信號集 全都為1 int sigfillset(sigset_t *set);添加某個信號到信號集 int sigaddset(sigset_t *set, int signum);從集合中刪除某個信號 int sigdelset(sigset_t *s…

軟件工程學習筆記《三》代碼優化和性能測試

文章目錄軟件工程學習筆記目錄如何在開源社區提問&#xff1f;代碼審查代碼優化運行結果參數解釋代碼優化原則對常見的數據結構排序算法進行測試關于冒泡排序優化的探討結果軟件工程學習筆記目錄 [https://blog.csdn.net/csdn_kou/article/details/83754356] 如何在開源社區提…

linux信號捕捉

信號捕捉&#xff0c;防止進程意外死亡 signal函數 man signal #include <signal.h> typedef void (*sighandler_t)(int); sighandler_t signal(int signum, sighandler_t handler);參數介紹&#xff1b; signum 要捕捉的信號 handler 要執行的捕捉函數指針&#xff0c…

軟件工程學習筆記《目錄》

軟件工程學習筆記《目錄》 軟件工程學習筆記《一》什么是軟件工程 軟件工程學習筆記《二》代碼規范 軟件工程學習筆記《三》代碼優化和性能測試 軟件工程學習筆記《四》需求分析

linux進程利用SIGCHLD信號,來實現父進程回收子進程

子進程執行完畢后&#xff0c;會向父進程發出 SIGCHLD信號 &#xff0c; 這段代碼實現的就是i&#xff0c;父進程接受到子進程 發出的SIGCHLD信號&#xff0c;實現對子進程進行回收&#xff0c;從而避免僵尸進程 #include <stdio.h> #include <unistd.h> #include…

WWW軟件全球使用排名

https://w3techs.com/technologies/overview/web_server/all Apache份額一直下降呀&#xff01;

軟件工程學習筆記《四》需求分析

文章目錄軟件工程學習筆記《目錄》需求工程師當代的需求工程師需要具備的能力當代的需求工程師需要努力的方向當代的需求工程師需要注意的錯誤需求的定義需求目標需求分析的實質需求分析的關鍵應該涵蓋的內容&#xff1f;需求規約&#xff08;作為較客觀的參照&#xff09;單個…

linux守護進程

先了解 linux系統中 會話的概念 會話是進程組的更高一級&#xff0c;多個進程組對應一個會話。 會話是一個或多個進程組的集合 創建一個會話需要注意以下5點事項&#xff1a; a. 調用進程不能是進程組組長&#xff0c; 該進程變成新會話首進程&#xff08;session header&#…

python3爬蟲學習筆記

文章目錄python3的文本處理jieba庫的使用統計hamlet.txt文本中高頻詞的個數統計三國演義任務高頻次數爬蟲爬取百度首頁爬取京東某手機頁面BeautifulSoup使用request進行爬取&#xff0c;在使用 BeautifulSoup進行處理&#xff01;擁有一個更好的排版BeautifulSoup爬取百度首頁原…

linux 線程學習初步01

線程的概念 進程與線程內核實現 通過函數clone實現的 ps -Lf pidLinux內核線程實現原理 同一個進程下的線程&#xff0c;共享該進程的內存區&#xff0c; 但是只有stack區域不共享。 線程共享資源 a.文件描述符表 b.每種信號的處理方式 c.當前工作目錄 d.用戶id和組id 線程…

python3字符串處理,高效切片

高級技巧&#xff1a;切片&#xff0c;迭代&#xff0c;列表&#xff0c;生成器 切片 L [Hello, World, !]print("-------1.一個一個取-------") print(L[0]) print(L[1]) print(L[2])print("-------2.開辟一個新列表把內容存進去-------") r [] for i…

linux線程學習初步02

殺死線程的函數 int pthread_cancel(pthread_t thread); 參數介紹&#xff1a;需要輸入的tid 返回值&#xff1a;識別返回 errno成功返回 0 被殺死的線程&#xff0c;退出狀態值為一個 #define PTHREAD_CANCELED((void *)-1)代碼案例&#xff1a; #include <stdio.h> #…

python的文件基本操作和文件指針

讀寫模式的基本操作 https://www.cnblogs.com/c-x-m/articles/7756498.html r,w,a r只讀模式【默認模式&#xff0c;文件必須存在&#xff0c;不存在則拋出異常】w只寫模式【不可讀&#xff1b;不存在則創建&#xff1b;存在則清空內容】a之追加寫模式【不可讀&#xff1b;不…

python3 將unicode轉中文

decrypted_str.encode(utf-8).decode(unicode_escape)

HTTP菜鳥教程速查手冊

HTTP協議&#xff08;HyperText Transfer Protocol&#xff0c;超文本傳輸協議&#xff09;是因特網上應用最為廣泛的一種網絡傳輸協議&#xff0c;所有的WWW文件都必須遵守這個標準。 HTTP是一個基于TCP/IP通信協議來傳遞數據&#xff08;HTML 文件, 圖片文件, 查詢結果等&am…

mysql學習筆記01-創建數據庫

創建數據庫&#xff1a; 校驗規則&#xff1a;是指表的排序規則和查詢時候的規則 utf8_general_ci 支持中文&#xff0c; 且不區分大小寫 utf8_bin 支持中文&#xff0c; 區分大小寫 比如&#xff1a; create database db3 character set utf8 collate utf8_general_ci; &…

python的Web編程

首先看一下效果 完整代碼 import socket from multiprocessing import ProcessHTML_ROOT_DIR ""def handle_client(client_socket):request_data client_socket.recv(1024)print("request data:", request_data)response_start_line "HTTP/1.0 20…

mysql 學習筆記 02創建表

表結構的創建 比如&#xff1a; create table userinfo (id int unsigned comment id號name varchar(60) comment 用戶名password char(32),birthday date ) character set utf8 engine MyISAM;comment 表示注釋的意思 不同的存儲引擎&#xff0c;創建的表的文件不一樣