alarm()函数的使用总结

alarm()函数说明

1.引用头文件:#include <unistd.h>;

2.函数标准式:unsigned int alarm(unsigned int seconds);

3.功能与作用:alarm()函数的主要功能是设置信号传送闹钟,即用来设置信号SIGALRM在经过参数seconds秒数后发送给目前的进程。如果未设置信号SIGALARM的处理函数,那么alarm()默认处理终止进程。

4.函数返回值:如果在seconds秒内再次调用了alarm函数设置了新的闹钟,则后面定时器的设置将覆盖前面的设置,即之前设置的秒数被新的闹钟时间取代;当参数seconds为0时,之前设置的定时器闹钟将被取消,并将剩下的时间返回。

alarm()测试1.1
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

void sig_alarm() 
{ 
  exit(0); 
}
int main(int argc, char *argv[]) 
{ 
  signal(SIGALRM, sig_alarm); 
  alarm(10); 
  sleep(15); 
  printf("Hello World!\n"); 
  return 0; 
}

 

备注:这样就可以使用alarm函数来实现server和client之间的定时通信,比如说我想在一个小时后发送xxx给xxx

猜你喜欢

转载自www.cnblogs.com/wuyepeng/p/9788919.html