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;
}
睡眠后该程序不会醒来()。你知道为什么吗?
你确定它睡得更久吗?它可能挂在fflush(stdout)上,例如如果磁盘已满或其他因素阻止fflush完成。 – dww
你怎么知道它“没有醒来”?你可能会[忍受缓冲](http://stackoverflow.com/q/2160469/132382),并且如果你是通过线缓冲运行的,则需要在printf()(或fflush())中使用换行符终奌站。 – pilcrow
@pilcrow,你说得对!添加一个换行符DID可以解决这个问题。你能给我一个关于“线路缓冲终端”的解释的链接吗? – CrazySynthax