[Linux學習筆記]訊號處理

來源:互聯網
上載者:User

訊號的作用:通知進程非同步事件的發生。

在Linux中可以識別29中不同的訊號,進程可以顯示的用kill或killpg系統調用來向另一個進程發訊號。訊號發生時,核心中斷當前進程,進程執行處理函數來響應訊號,訊號結束後恢複正常的進程處理。

訊號    值  處理動作 發出訊號的原因----------------------------------------------------------------------SIGHUP    1     A  終端掛起或者控制進程終止SIGINT    2     A  鍵盤中斷(如break鍵被按下)SIGQUIT   3      C  鍵盤的退出鍵被按下SIGILL    4     C  非法指令SIGABRT   6     C  由abort(3)發出的退出指令SIGFPE    8     C  浮點異常SIGKILL   9      AEF  Kill訊號SIGSEGV   11     C  無效的記憶體引用SIGPIPE   13     A  管道破裂: 寫一個沒有讀連接埠的管道SIGALRM   14     A  由alarm(2)發出的訊號SIGTERM   15     A  終止訊號SIGUSR1   30,10,16  A  使用者自訂訊號1SIGUSR2   31,12,17  A  使用者自訂訊號2SIGCHLD   20,17,18  B  子進程結束訊號SIGCONT   19,18,25    進程繼續(曾被停止的進程)SIGSTOP   17,19,23  DEF 終止進程SIGTSTP   18,20,24  D  控制終端(tty)上按下停止鍵SIGTTIN   21,21,26  D  後台進程企圖從控制終端讀SIGTTOU   22,22,27  D  後台進程企圖從控制終端寫
SIGBUS     10,7,10     C      匯流排錯誤(錯誤的記憶體訪問)SIGPOLL                A      Sys V定義的Pollable事件,與SIGIO同義SIGPROF    27,27,29    A      Profiling定時器到SIGSYS     12,-,12     C      無效的系統調用 (SVID)SIGTRAP    5           C      跟蹤/斷點捕獲SIGURG     16,23,21    B      Socket出現緊急條件(4.2 BSD)SIGVTALRM  26,26,28    A      實際時間警示時鐘訊號(4.2 BSD)SIGXCPU    24,24,30    C      超出設定的CPU時間限制(4.2 BSD)SIGXFSZ    25,25,31    C      超出設定的檔案大小限制(4.2 BSD)

相關函數:

1.捕捉訊號:signal

用於決定系統訊號的響應。

格式:

#include <signal.h>void (*signal(int signo, void (*func)(int)))(int);//func為SIG_IGN表示核心忽略此訊號
//func為SIG_DFL表示接到此訊號的動作時預設動作
//func為函數地址時為捕捉訊號

2.發送訊號:kill和raise、alarm、getitimer和setitimer、pause

kill:向其他進程發訊號

raise:向當前進程發訊號

alarm:設定一個時間值(鬧鐘時間),當所設定被超過產生SIGLRM訊號,預設動作時終止進程

getitimer和setitimer:擷取和設定間隔定時器的函數

pause:使進程掛起,直到捕捉到一個訊號

格式:

#include <sys/types.h>#include <signal.h>int kill(pid_t pid, int sig);int raise(int sig);//raise等價於kill(getpid(),sig)//#include <unist.h>unsigned int alarm(unsigned int seconds);//#include <sys/time.h>int getitimer(int which, struct itimerval *value);intsetitimer(int which, const struct itimerval *value,struct itimerval *ovalue);//#include <unistd.h>int pause(void);

下面說說

【訊號屏蔽字】:

每個進程都有一個屏蔽字,它規定了當前要阻塞遞送到該進程的訊號集。

1.設定

函數:sigpromask、sigpending

sigpromask:檢測或更改進程的訊號屏蔽字

sigpending:返回對於調用進程被阻塞不能遞送和當前未決定的訊號集

格式:

#include <signal.h>int sigpromask(int how, const sigset_t *set, sigset_t *oset);int sigpending(sigset_t *set);

2.實現

函數:sigaction

sigaction:檢查或修改與指定訊號相關聯的處理動作。

格式:

#include <signal.h>int sigaction(int signo, const struct sigaction *act, struct sigaction *oact);//結構體原型如下struct sigaction{    void (*sa_handler)();//addr of signal handler, or SIG_IGN, or SIG_DFL    sigset_t sa_mask;//additional to block    int sa_flag;//signal options};

【相關操作】:

函數:sigsetjmp和siglongjmp、sigsuspend、abort、system、sleep

  sigsetjmp和siglongjmp:非局部轉移訊號處理

sigsuspend:在一個原子操作中實現恢複訊號屏蔽字,然後使進程睡眠。

abort:使程式異常終止

system:執行系統命令

sleep:掛起調用中的進程,直到過了預定時間或收到一個訊號並從訊號處理常式返回。可以由alarm函數實現。

格式:

#include <setjmp.h>int sigsetjmp(sigjmp_buf env, int savemask);void siglongjmp(sigjmp_buf env, int val);//#include <signal.h>int sigsuspend(const sigset_t *sigmask);//#include <stdlib.h>void abort(void);int system(const char *string);//#include <unistd.h>unsigned int sleep(unsigned int second);

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.