UNIX進階環境編程-訊號相關函數筆記__PHP

來源:互聯網
上載者:User

1.如何查看訊號相關函數

man signal

查看SEE ALSO(我的os: ubuntu 14.04 64位)

SEE ALSO       kill(1), alarm(2), kill(2),  killpg(2),  pause(2),  sigaction(2),  sig‐       nalfd(2),  sigpending(2), sigprocmask(2), sigsuspend(2), bsd_signal(3),       raise(3),  siginterrupt(3),   sigqueue(3),   sigsetops(3),   sigvec(3),       sysv_signal(3), signal(7)

當然,接下來的筆記以UNIX進階環境編程為主,其他函數大家可以自行man查閱


2.函數備忘

NAME       kill - send signal to a process  //發送訊號給進程或者進程組,還有一個同名的kill命令SYNOPSIS       #include <sys/types.h>       #include <signal.h>       int kill(pid_t pid, int sig);NAME       raise - send a signal to the caller //進程向自身發送訊號,等價於kill(getpid(),signo)SYNOPSIS       #include <signal.h>       int raise(int sig); NAME       alarm - set an alarm clock for delivery of a signal #include <unistd.h>       unsigned int alarm(unsigned int seconds);  //seconds設定為0時表示取消以前的鬧鐘,其餘留值為傳回值 NAME       pause - wait for signal    //pause使得調用進程掛起直至捕捉到一個訊號SYNOPSIS       #include <unistd.h>       int pause(void);  //只有執行了一個訊號處理常式並返回,pause才返回。傳回值:-1,errno設定為EINTR1. NAME       sigprocmask - examine and change blocked signals  //檢測或更改當前訊號屏蔽字,注意,此函數僅為單線程進程定義SYNOPSIS       #include <signal.h>       int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);記住最後一句,在調用sigprocmask後如果有任何未決的 不在阻塞的訊號,則在sigprocmask返回前,至少將其中一個遞送給該進程 多線程的進程中,線程必須使用pthread_sigmask

NAME       sigpending - examine pending signals  //sigpending函數返回在送往進程的時候被阻塞掛起的訊號集合SYNOPSIS       #include <signal.h>       int sigpending(sigset_t *set);

sigaction詳解:

作用:檢查或修改與指定訊號相關聯的處理動作(可同時兩種操作)

int sigaction(int signo,const struct sigaction *restrict act,

              struct sigaction *restrict oact);

signo:當然就是指定訊號

act:新的訊號處理相關

oact:獲得老的訊號處理相關


sigaction結構體內容:

 struct sigaction{  void (*sa_handler)(int);   sigset_t sa_mask;  int sa_flag;  void (*sa_sigaction)(int,siginfo_t *,void *);};

sa_handler欄位包含一個訊號捕捉函數的地址

sa_mask欄位說明了一個訊號集,在調用該訊號捕捉函數之前,這一訊號集要加進進程的訊號屏蔽字中。僅當從訊號捕捉函數返回時再將進程的訊號屏蔽字複位為原先值。(注意,這是重點)

sa_flag是一個選項,主要理解兩個SA_INTERRUPT 由此訊號中斷的系統調用不會自動重啟SA_RESTART 由此訊號中斷的系統調用會自動重啟SA_SIGINFO 提供附加資訊,一個指向siginfo結構的指標以及一個指向進程上下文標識符的指標最後一個參數對於排隊訊號功能很有用,具體參見下面的訊號排隊相關知識

#include<signal.h>int sigsuspend(const sigset_t *sigmask);
參見unix進階環境編程286頁,講解了為什麼會出現sigsuspend,最主要的就是其原子操作(先恢複訊號屏蔽字,然後使得進程休眠(準確來說是掛起)) 進程的訊號屏蔽字設定為sigmask指向的值。 返回條件 訊號發生,並且訊號是非屏蔽訊號 訊號必須要處理,而且處理函數返回後sigsuspend才返回 返回後sigsuspend設定失效,該進程的訊號屏蔽字設定為調用sigsuspend之前的值 此函數返回調用者時,總是返回-1,並將errno設定為EINTR(表示被中斷的系統調用)

#include<stdlib.h>void abort(void);         //使得程式異常終止,將SIGABRT訊號發送給調用進程

訊號排隊相關知識: 使用sigaction函數安裝訊號處理常式時指定SA_SIGINFO標誌。 在sigaciton結構的sa_sigaction成員中(而不是通常的sa_handler欄位)提供訊號處理函數。 使用sigqueue函數發送訊號


#include<signal.h>int sigqueue(pid_t pid,int signo,const union sigval value);
把signo訊號發送給pid進程,使用value參數向訊號處理函數傳遞整數值和指標值 訊號隊列有大小限制,不能被無限排隊,超過限制,errno被設定為EAGAIN


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.