linux系统编程之信号(四):alarm和可重入函数

一,香港虚拟主机,alarm()

在将可重入函数之前我们先来了解下alarm()函数使用:

#include <unistd.h>

unsigned int alarm(unsigned int seconds)

系统调用alarm安排内核为调用进程在指定的seconds秒后发出一个SIGALRM的信号。如果指定的参数seconds为0,则不再发送 SIGALRM信号。后一次设定将取消前一次的设定。该调用返回值为上次定时调用到发送之间剩余的时间,或者因为没有前一次定时调用而返回0。

注意,在使用时,香港服务器,alarm只设定为发送一次信号,服务器空间,如果要多次发送,就要多次使用alarm调用。

man帮助说明:

DESCRIPTION        alarm()  arranges  for  a SIGALRM signal to be delivered to the calling        process in seconds seconds.

       If seconds is zero, no new alarm() is scheduled.

       In any event any previously set alarm() is canceled.

RETURN VALUE        alarm() returns the number of seconds remaining  until  any  previously        scheduled alarm was due to be delivered, or zero if there was no previ-        ously scheduled alarm.

示例:

#include <unistd.h>#include <sys/stat.h>#include <sys/wait.h>#include <sys/types.h>#include <fcntl.h>#include <stdlib.h>#include <stdio.h>#include <errno.h>#include <string.h>#include <signal.h>#define ERR_EXIT(m) \do \{ \perror(m); \exit(EXIT_FAILURE); \} while(0)void handler(int sig);int main(int argc, char *argv[]){if (signal(SIGALRM, handler) == SIG_ERR)ERR_EXIT();alarm(1);for (;;)pause();return 0;}void handler(int sig){printf(, sig);alarm(1);}伟人之所以伟大,是因为他与别人共处逆境时,

linux系统编程之信号(四):alarm和可重入函数

相关文章:

你感兴趣的文章:

标签云: