2016-04-18 20 views
0

你知道我在哪裏可以看到不能與sleep()命令一起使用的信號和函數列表嗎?Linux:通過信號梳理睡眠()

例如,你可以看到這樣的代碼:

// this program presents how to block signal SIGINT 
// while running in critical region 
#include <signal.h> 
#include  <stdio.h> 
#include  <unistd.h> 
#include  <stdlib.h> 
#include  <stdio.h> 

static void sig_int(int); 

int main(void) //6 
{ 
    sigset_t newmask, oldmask, zeromask; 

    if (signal(SIGINT, sig_int) == SIG_ERR) 
    fprintf(stderr,"signal(SIGINT) error"); 

    sigemptyset(&zeromask); 

    sigemptyset(&newmask); 
    sigaddset(&newmask, SIGINT); 

    /* block SIGINT and save current signal mask */ 
    if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) 
    fprintf(stderr,"SIG_BLOCK error"); 

    /* critical region of code */ 
    printf("In critical region: SIGINT will be blocked for 3 sec.\n"); 
    printf("Type Ctrl-C in first 3 secs and see what happens.\n"); 
    printf("Then run this program again and type Ctrl-C when 3 secs elapsed.\n"); 
    fflush(stdout); 


    sleep(3); 

    printf("end sleep"); 
    /* allow all signals and pause */ 
    if (sigsuspend(&zeromask) != -1) 
    fprintf(stderr,"sigsuspend error"); 

    printf("after return from sigsuspend: "); 

    /* reset signal mask which unblocks SIGINT */ 
    if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) 
    fprintf(stderr,"SIG_SETMASK error"); 

    /* and continue processing ... */ 
    exit(0); 
} 

static void sig_int(int signo) 
{ 
    printf("\nIn sig_int: SIGINT\n"); fflush(stdout); 
    return; 
} 

睡眠後該程序不會醒來()。你知道爲什麼嗎?

+0

你確定它睡得更久嗎?它可能掛在fflush(stdout)上,例如如果磁盤已滿或其他因素阻止fflush完成。 – dww

+1

你怎麼知道它「沒有醒來」?你可能會[忍受緩衝](http://stackoverflow.com/q/2160469/132382),並且如果你是通過線緩衝運行的,則需要在printf()(或fflush())中使用換行符終奌站。 – pilcrow

+0

@pilcrow,你說得對!添加一個換行符DID可以解決這個問題。你能給我一個關於「線路緩衝終端」的解釋的鏈接嗎? – CrazySynthax

回答

0

如果您使用strace的,你可以看到你的程序實際上不會

strace的./my-sig-program

如果睡眠永遠不會返回,我想這任務已經收到SIGSTOP(這可以不被攔截)或SIGTSTP(這一個可以用信號處理程序攔截),導致操作系統停止整個過程,直到接收到SIGCONT。