0
であなたは知っていますか?例えば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;
}
プログラムは、(スリープ後に目を覚ますしません)。なぜなのかご存知ですか?あなたはstraceのを使用している場合
あなたはそれが眠るまで来ると確信していますか? fflush(stdout)にぶら下がっている可能性があります。ディスクがいっぱいになったり、他の要因がfflushの完了を妨げている場合 – dww
「目を覚まさない」ことはどうやって知っていますか?あなたはおそらく[バッファリングに苦しんでいる](http://stackoverflow.com/q/2160469/132382)、これをラインバッファリングされたものから実行しているなら、あなたのprintf()(またはfflush())に改行が必要ですターミナル。 – pilcrow
@pilcrow、そうですよ!改行文字DIDの追加は、この問題を解決します。 「回線バッファ型端末」についての説明にリンクできますか? – CrazySynthax