pause()
之前一直以为pause就是将进程暂停,至于是怎么个暂停法,一直没有去深究。今天man了一下,才发现原来是wait for signal。
#include <unistd.h>
int pause(void);
DESCRIPTION
The pause() library function causes the invoking process (or thread) to sleep until a signal is received that either terminates it or causes it
to call a signal-catching function.
RETURN VALUE
The pause() function only returns when a signal was caught and the signal-catching function returned. In this case pause() returns -1, and errno
is set to EINTR.
ERRORS
EINTR a signal was caught and the signal-catching function returned.
pause函数使调用进程挂起直到捕捉到一个信号,只有执行了一个信号处理程序并从其返回时,pause才返回。在这种情况下,pause返回-1,并将errno设置为EINTR。
1 void sig_usr(int signo) 2 { 3 if (signo == SIGUSR1) 4 printf("received SIGUSR1\n"); 5 else if (signo == SIGUSR2) 6 printf("received SIGUSR2\n"); 7 else 8 printf("received signal %d\n", signo); 9 } 10 11 static void sig_quit(int signo) 12 { 13 printf("111caught SIGQUIT\n"); 14 if (signal(SIGQUIT, SIG_DFL)==SIG_ERR) 15 { 16 printf("can't reset SIGQUIT\n"); 17 } 18 } 19 20 static void sig_alrm(int signo) 21 { 22 printf("caught alarm...\n"); 23 }
1 int main(int argc, char** argv) 2 { 3 signal(SIGUSR1, sig_usr); 4 signal(SIGQUIT, sig_quit); 5 signal(SIGALRM, sig_alrm); 6 7 printf("before pause......\n"); 8 //raise(SIGQUIT); 9 alarm(5); 10 //pause(); 11 printf("after pause:%d\n",pause()); 12 printf("errno:%s\n", strerror(errno)); 13 //signal(SIGQUIT, SIG_DFL); 14 15 return 0; 16 }
运行结果为: