信号警报失效得太早
signal alarm fails too soon
我有一个 signal
,我在其中设置回调处理程序,然后在我的函数中调用 alarm(1)
,这样我的程序将在 1 秒后超时。超时后,我希望它重试相同的阻塞调用 recvfrom()
直到集合 MAX_ATTEMPTS
为 5。
问题:
退出程序前只重试2次。知道可能出了什么问题吗?
/* declate signal for setting alarm */
signal(SIGALRM, timeout_hdler);
int attempts = 0;
while(1) {
if(attempts > MAX_ATTEMPTS) {
printf("Connection is not working, EXITING program");
s.curr_state = CLOSED;
/* we assume connection is failing so we shut down */
exit(-1);
}
if (s.curr_state == CLOSED) {
printf("Sending SYN_packet with seqnum: %d...\n", SYN_packet->seqnum);
if (sendto(sockfd, SYN_packet, sizeof(SYN_packet), 0, server, socklen) == -1) {
perror("Sendto error");
exit(-1);
}
s.curr_state = SYN_SENT;
printf("Current state SYN_SENT: %d\n", s.curr_state);
}
if (s.curr_state == SYN_SENT) {
alarm(1);
attempts++;
printf("\nAttempt number: %d\n", attempts);
printf("Waiting for SYNACK_packet...\n");
if (recvfrom(
sockfd, SYNACK_packet, sizeof(*SYNACK_packet), 0, (struct sockaddr *) &server, &socklen) == -1)
{
if (errno != EINTR) {
perror("Recvfrom SYNACK_packet error\n");
s.curr_state = CLOSED;
exit(-1);
}
}
if ((SYNACK_packet->type == SYNACK) && (validate_packet(SYNACK_packet) == 1)) {
printf("SYNACK_packet received\n");
s.address = *(struct sockaddr *) &server;
s.sock_len = socklen;
s.curr_state = ESTABLISHED;
s.seq_num = SYNACK_packet->seqnum;
printf("Current state ESTABLISHED: %d\n", s.curr_state);
return sockfd;
}
}
}
处理程序(除了打印外什么都不做):
void timeout_hdler(int signum) {
printf("TIMEOUT has occured with signum: %d", signum);
}
这是我的控制台中的输出(来自 printf
语句):
In Connect() with socket: 4, server: 2, socklen: 16
Sending SYN_packet with seqnum: 67...
Current state SYN_SENT: 1
Attempt number: 1
Waiting for SYNACK_packet...
TIMEOUT has occured with signum: 14
Attempt number: 2
Waiting for SYNACK_packet...
Alarm clock
为什么只试了2次就退出了?理想情况下,我希望它在关闭连接之前重试 5 次(这是使用 UDP 的 Go Back N 实现)
更新
我通过在我的处理程序中重新安装 signal
: signal(SIGALRM, timeout_hdler);
解决了这个问题。但这是为什么呢?我做错了吗?
void timeout_hdler(int signum) {
printf("TIMEOUT has occured with signum: %d", signum);
signal(SIGALRM, timeout_hdler);
}
signal
的语义取决于 OS 和 libc。但是根据您在特定情况下的输出,信号处理程序处理程序在第一次调用处理程序函数后会重置为默认值。如果再次触发信号,则默认值会导致程序退出,即您在输出中看到的 Alarm clock
和退出。
来自the documentation of signal in Linux:
The only portable use of signal() is to set a signal's disposition to
SIG_DFL or SIG_IGN. ....
In the original UNIX systems, when a handler that was established
using signal() was invoked by the delivery of a signal, the disposition of the signal would be reset to SIG_DFL, and the system did not
block delivery of further instances of the signal. ...
换句话说:不要使用 signal
。相反,您应该使用 sigaction
:
POSIX.1 solved the portability mess by specifying sigaction(2), which
provides explicit control of the semantics when a signal handler is
invoked; use that interface instead of signal().
我有一个 signal
,我在其中设置回调处理程序,然后在我的函数中调用 alarm(1)
,这样我的程序将在 1 秒后超时。超时后,我希望它重试相同的阻塞调用 recvfrom()
直到集合 MAX_ATTEMPTS
为 5。
问题:
退出程序前只重试2次。知道可能出了什么问题吗?
/* declate signal for setting alarm */
signal(SIGALRM, timeout_hdler);
int attempts = 0;
while(1) {
if(attempts > MAX_ATTEMPTS) {
printf("Connection is not working, EXITING program");
s.curr_state = CLOSED;
/* we assume connection is failing so we shut down */
exit(-1);
}
if (s.curr_state == CLOSED) {
printf("Sending SYN_packet with seqnum: %d...\n", SYN_packet->seqnum);
if (sendto(sockfd, SYN_packet, sizeof(SYN_packet), 0, server, socklen) == -1) {
perror("Sendto error");
exit(-1);
}
s.curr_state = SYN_SENT;
printf("Current state SYN_SENT: %d\n", s.curr_state);
}
if (s.curr_state == SYN_SENT) {
alarm(1);
attempts++;
printf("\nAttempt number: %d\n", attempts);
printf("Waiting for SYNACK_packet...\n");
if (recvfrom(
sockfd, SYNACK_packet, sizeof(*SYNACK_packet), 0, (struct sockaddr *) &server, &socklen) == -1)
{
if (errno != EINTR) {
perror("Recvfrom SYNACK_packet error\n");
s.curr_state = CLOSED;
exit(-1);
}
}
if ((SYNACK_packet->type == SYNACK) && (validate_packet(SYNACK_packet) == 1)) {
printf("SYNACK_packet received\n");
s.address = *(struct sockaddr *) &server;
s.sock_len = socklen;
s.curr_state = ESTABLISHED;
s.seq_num = SYNACK_packet->seqnum;
printf("Current state ESTABLISHED: %d\n", s.curr_state);
return sockfd;
}
}
}
处理程序(除了打印外什么都不做):
void timeout_hdler(int signum) {
printf("TIMEOUT has occured with signum: %d", signum);
}
这是我的控制台中的输出(来自 printf
语句):
In Connect() with socket: 4, server: 2, socklen: 16
Sending SYN_packet with seqnum: 67...
Current state SYN_SENT: 1
Attempt number: 1
Waiting for SYNACK_packet...
TIMEOUT has occured with signum: 14
Attempt number: 2
Waiting for SYNACK_packet...
Alarm clock
为什么只试了2次就退出了?理想情况下,我希望它在关闭连接之前重试 5 次(这是使用 UDP 的 Go Back N 实现)
更新
我通过在我的处理程序中重新安装 signal
: signal(SIGALRM, timeout_hdler);
解决了这个问题。但这是为什么呢?我做错了吗?
void timeout_hdler(int signum) {
printf("TIMEOUT has occured with signum: %d", signum);
signal(SIGALRM, timeout_hdler);
}
signal
的语义取决于 OS 和 libc。但是根据您在特定情况下的输出,信号处理程序处理程序在第一次调用处理程序函数后会重置为默认值。如果再次触发信号,则默认值会导致程序退出,即您在输出中看到的 Alarm clock
和退出。
来自the documentation of signal in Linux:
The only portable use of signal() is to set a signal's disposition to SIG_DFL or SIG_IGN. ....
In the original UNIX systems, when a handler that was established using signal() was invoked by the delivery of a signal, the disposition of the signal would be reset to SIG_DFL, and the system did not block delivery of further instances of the signal. ...
换句话说:不要使用 signal
。相反,您应该使用 sigaction
:
POSIX.1 solved the portability mess by specifying sigaction(2), which provides explicit control of the semantics when a signal handler is invoked; use that interface instead of signal().