linux c之sigsuspend 進程阻塞

來源:互聯網
上載者:User

函數原型:

#include <signal.h>

int sigsuspend(const sigset_t *mask);

作用:

用於在接收到某個訊號之前,臨時用mask替換進程的訊號掩碼,並暫停進程執行,直到收到訊號為止。

The sigsuspend() function replaces the current signal mask of the calling thread with the set of signals pointed to by sigmask and then suspends the thread until delivery of a signal whose action is either to execute a signal-catching function or to terminate the process. This will not cause any other signals that may have been pending on the process to become pending on the thread.

If the action is to terminate the process then sigsuspend() will never return. If the action is to execute a signal-catching function, thensigsuspend() will return after the signal- catching function returns, with the signal mask restored to the set that existed prior to thesigsuspend() call.

It is not possible to block signals that cannot be ignored. This is enforced by the system without causing an error to be indicated.

也就是說,sigsuspend後,進程 就掛在那裡,等待著開放的訊號的喚醒。系統在接收到訊號後,馬上就把現在的訊號集還原為原來的,然後調 用處理函數。

傳回值:

sigsuspend返回後將恢複調用之前的的訊號掩碼。訊號處理函數完成後,進 程將繼續執行。該系統調用始終返回-1,並將errno設定為EINTR.

Since sigsuspend() suspends process execution indefinitely, there is no successful completion return value. If a return occurs, -1 is returned and errno is set to indicate the error.

The sigsuspend() function will fail if:

[EINTR]

A signal is caught by the calling process and control is returned from the signal-catching function.

也就是說,sigsuspend後,進程就掛在那裡,等待著開放的訊號的喚醒。 系統在接受到訊號後,馬上就把現在的訊號集還原為原來的,然後調用處理函數。

Stevens在《Unix環 境進階編程》一書中是如是回答的“If a signal is caught and if the signal handler returns, then sigsuspend returns and the signal mask of the process is set to its value before the call to sigsuspend.”,由於sigsuspend是原子操作,所以這句給人的感覺就是先調用signal handler先返回,然後 sigsuspend再返回。

int main(void) {     sigset_t   newmask, oldmask, zeromask;           if (signal(SIGINT, sig_int) == SIG_ERR)        err_sys("signal(SIGINT) error");           sigemptyset(&zeromask);           sigemptyset(&newmask);     sigaddset(&newmask, SIGINT);     /* block SIGINT and save current signal mask */   if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)        err_sys("SIG_BLOCK error");           /* critical region of code */   pr_mask("in critical region: ");           /* allow all signals and pause */   if (sigsuspend(&zeromask) != -1)        err_sys("sigsuspend error");     pr_mask("after return from sigsuspend: ");           /* reset signal mask which unblocks SIGINT */   if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)        err_sys("SIG_SETMASK error");           /* and continue processing ... */   exit(0);  }        static void sig_int(int signo) {     pr_mask("\nin sig_int: ");     return;  }

結果:

$a.outin critical region: SIGINT  ^C  in sig_int: SIGINT  after return from sigsuspend: SIGINT

相關文章

聯繫我們

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