学习《Linux高性能服务器编程》第十一章定时器,里面介绍了各种网络程序中的定时事件,为了印象深刻一些,多动手多实践,所以记下这个笔记。这一篇主要记录Linux中SIGALRM信号触发的定时器。
由于alarm和setitimer函数设置的实时闹钟一旦超时,将触发SIGALRM信号。因此,我们可以利用该信号的信号处理函数来处理定时任务。但是,如果要处理多个定时任务,我们就需要不断地触发SIGALRM信号,并在其信号处理函数中执行到期的任务。
一般而言,SIGALRM信号按照固定的频率生成,即由alarm或setitimer函数设置的定时周期T保持不变。如果某个定时任务的超时时间不是T的整数倍,那么它实际被执行的时间和预期的时间将略有偏差。因此定时周期T反映了定时的精度。
#include
unsigned int alarm(unsigned int seconds);
alarm定时发送 SIGALRM 给当前进程(需要注意的是alarm调用只会引起一次调用)。
seconds参数表示经过seconds秒数后发送SIGALRM 给目前的进程
alarm返回上次定时剩余时间。
如果设置alarm(0)则表示取消闹钟
我们举个小例子,结合前面的信号一起写下
#include
#include
#include
#include
void sig_alarm(int a)
{
printf("hello world\n");
}
int main(int argc, char *argv[])
{
int i;
alarm(3); // 3秒后发送信号
signal(SIGALRM, sig_alarm); //设置信号对应的处理函数
while (true)
{
printf("------------------\n");
sleep(1);
}
return 0;
}

setitimer相比alarm,提供了更为精细的参数选择
#include
int getitimer(int which, struct itimerval *curr_value);
int setitimer(int which, const struct itimerval *new_value, struct itimerval *old_value);
which指计时器采用那种类型的计时方法
| 类型 | 介绍 |
|---|---|
ITIMER_REAL | 以系统真实的时间来计算,它送出SIGALRM信号。 |
ITIMER_VIRTUAL | 以该进程用户空间下花费的时间来计算,它送出SIGVTALRM信号。 |
ITIMER_PROF | 以该进程在用户空间下和内核下所费的时间来计算,它送出SIGPROF信号。 |
new_value和old_value都是itimerval类型的结构体
struct itimerval {
struct timeval it_interval; /* Interval for periodic timer */
struct timeval it_value; /* Time until next expiration */
};
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
timeval结构体中成员很简单,tv_sec设置秒,tv_usec设置微妙。
itimerval结构体中成员it_interval为计时间隔,it_value为延时时长。比如:我想3s后,以每次5s的时间间隔打印hello world,那么就需要设置it_value为3s,设置it_interval为5s(3s后第一次打印,此后每次以5s为间隔打印)。
其中的new_value参数用来对计时器进行设置。
old_value参数,通常用不上,设置为NULL,它是用来存储上一次setitimer调用时设置的new_value值。
函数调用成功返回0,失败返回-1,并且设置errno。
假如it_value为0是不会触发信号的,所以要能触发信号,it_value得大于0;如果it_interval为0,只会延时,不会定时(也就是说只会触发一次信号)。
下面就写一个延时3s后,以5s为间隔打印hello world
#include
#include
#include
#include
void sig_alarm(int signo)
{
printf("hello world\n");
}
int main(int argc, char *argv[])
{
struct itimerval it, oldit;
signal(SIGALRM, sig_alarm); //注册SIGALRM信号的捕捉处理函数。
it.it_value.tv_sec = 3; //设置延时3s
it.it_value.tv_usec = 0;
it.it_interval.tv_sec = 5; //设置时间间隔5s
it.it_interval.tv_usec = 0;
if (setitimer(ITIMER_REAL, &it, &oldit) == -1)
{
perror("setitimer error");
return -1;
}
while (1)
{
printf("------------------\n");
sleep(1);
};
return 0;
}

socket选项SO_RCVTIMEO和SO_SNDTIMEO,它们分别用来设置socket 接收数据超时时间和发送数据超时时间。因此,这两个选项仅对与数据接收和发送相关的socket专用系统调用( socket专用的系统调用指的是5.2~5.11节介绍的那些socketAPI)有效,这些系统调用包括send、sendmsg、recv、recvmsg、accept和 connect。将选项SO_RCVTIMEO和SO_SNDTIMEO对这些系统调用的影响总结于表中(来源Linux高性能服务器编程)。

这里举书上的代码例子,比较简单 明了
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int timeout_connect(const char *ip, int port, int time)
{
int ret = 0;
struct sockaddr_in address;
bzero(&address, sizeof(address));
address.sin_family = AF_INET;
inet_pton(AF_INET, ip, &address.sin_addr);
address.sin_port = htons(port);
int sockfd = socket(PF_INET, SOCK_STREAM, 0);
assert(sockfd >= 0);
struct timeval timeout;
timeout.tv_sec = time;
timeout.tv_usec = 0;
socklen_t len = sizeof(timeout);
ret = setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &timeout, len);
assert(ret != -1);
ret = connect(sockfd, (struct sockaddr *)&address, sizeof(address));
if (ret == -1)
{
if (errno == EINPROGRESS)
{
printf("connecting timeout\n");
return -1;
}
printf("error occur when connecting to server\n");
return -1;
}
return sockfd;
}
int main(int argc, char *argv[])
{
if (argc <= 2)
{
printf("usage: %s ip_address port_number\n", basename(argv[0]));
return 1;
}
const char *ip = argv[1];
int port = atoi(argv[2]);
int sockfd = timeout_connect(ip, port, 10);
if (sockfd < 0)
{
return 1;
}
return 0;
}