另一个带有 sigtimedwait 的信号
Another signal with sigtimedwait
此代码是在 100 秒内处理 SIGINT 信号,或者如果 SIGINT 未到达则打印超时。
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int main (int argc, char *argv[])
{
sigset_t mask;
sigset_t orig_mask;
struct timespec timeout;
sigemptyset (&mask);
sigaddset (&mask, SIGINT);
if (sigprocmask(SIG_BLOCK, &mask, &orig_mask) < 0) {
perror ("sigprocmask");
return 1;
}
timeout.tv_sec = 100;
timeout.tv_nsec = 0;
int v =sigtimedwait(&mask, NULL, &timeout);
if (errno == EAGAIN) {
printf ("Timeout\n");
return -1;
}
if(v== SIGINT){
printf("SIGINT\n");
return 1;
}
return 0;
}
当代码处于 sigtimedwait
时,如果除 SIGINT 之外的另一个信号将到达,代码是否会继续?或者 sigtimedwait
只有在 SIGINT 到达时才会完成?
此外,如果在此代码之前我将注册到另一个信号,如 signal(SIGUSR1, handle_sig);
,当 sigtimedwait
和 SIGUSR1
中的代码到达时,handle_sig
将被调用?否则它会被阻止?
int main (int argc, char *argv[])
{
signal(SIGUSR1, handle_sig);//
sigset_t mask;
sigset_t orig_mask;
struct timespec timeout;
sigemptyset (&mask);
sigaddset (&mask, SIGINT);
if (sigprocmask(SIG_BLOCK, &mask, &orig_mask) < 0) {
perror ("sigprocmask");
return 1;
}
timeout.tv_sec = 100;
timeout.tv_nsec = 0;
int v =sigtimedwait(&mask, NULL, &timeout);
if (errno == EAGAIN) {
printf ("Timeout\n");
return -1;
}
if(v== SIGINT){
printf("SIGINT\n");
return 1;
}
return 0;
}
(code 1) When code is in sigtimedwait if another signal than SIGINT will arrive, is the code will continue ?
如果SIGKILL
, SIGTERM
, SIGUSR1
, SIGUSR2
....任何其他具有默认动作终止进程的信号都会到达,进程将终止。如果信号 SIGCHLD
或 SIGURG
到达,它们将被忽略 - 代码将“继续”为 in sigtimedwait
(因为没有处理程序为这些信号执行)。如果有停止信号(SIGSTOP、SIGTSTP、SIGTTIN、SIGTTOU),进程将被停止。在 SIGSTOP
之后我们恢复进程(通过发送 SIGCONT
),然后 sigtimedwait
将 return -1
并且 errno
将设置为 EINTR
。有关详细信息,请参阅 man 7 signal
and posix sigtimedwait and posix signal concepts。
(code 2) when the code in sigtimedwait and SIGUSR1 will arrived, will handle_sig be called?
是的。
or it will blocked ?
没有。信号处理程序将被执行,然后 sigtimedwait
将 return -1
和 errno
将设置为 EINTR
.
(TBH 为什么你不直接测试代码?在信号处理程序、编译器和 运行 进程中放置一些 write(1
并向其发送 SIGUSR1 信号并查看结果看起来微不足道发生)
maybe it's depending on architecture
它不是 - 行为不依赖于体系结构,而是依赖于操作系统的行为 - 依赖于负责将信号转发和处理到进程并管理它们的底层软件。 POSIX 兼容的操作系统必须执行 POSIX 指定的操作,独立于它 运行 正在使用的架构。
此代码是在 100 秒内处理 SIGINT 信号,或者如果 SIGINT 未到达则打印超时。
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int main (int argc, char *argv[])
{
sigset_t mask;
sigset_t orig_mask;
struct timespec timeout;
sigemptyset (&mask);
sigaddset (&mask, SIGINT);
if (sigprocmask(SIG_BLOCK, &mask, &orig_mask) < 0) {
perror ("sigprocmask");
return 1;
}
timeout.tv_sec = 100;
timeout.tv_nsec = 0;
int v =sigtimedwait(&mask, NULL, &timeout);
if (errno == EAGAIN) {
printf ("Timeout\n");
return -1;
}
if(v== SIGINT){
printf("SIGINT\n");
return 1;
}
return 0;
}
当代码处于 sigtimedwait
时,如果除 SIGINT 之外的另一个信号将到达,代码是否会继续?或者 sigtimedwait
只有在 SIGINT 到达时才会完成?
此外,如果在此代码之前我将注册到另一个信号,如 signal(SIGUSR1, handle_sig);
,当 sigtimedwait
和 SIGUSR1
中的代码到达时,handle_sig
将被调用?否则它会被阻止?
int main (int argc, char *argv[])
{
signal(SIGUSR1, handle_sig);//
sigset_t mask;
sigset_t orig_mask;
struct timespec timeout;
sigemptyset (&mask);
sigaddset (&mask, SIGINT);
if (sigprocmask(SIG_BLOCK, &mask, &orig_mask) < 0) {
perror ("sigprocmask");
return 1;
}
timeout.tv_sec = 100;
timeout.tv_nsec = 0;
int v =sigtimedwait(&mask, NULL, &timeout);
if (errno == EAGAIN) {
printf ("Timeout\n");
return -1;
}
if(v== SIGINT){
printf("SIGINT\n");
return 1;
}
return 0;
}
(code 1) When code is in sigtimedwait if another signal than SIGINT will arrive, is the code will continue ?
如果SIGKILL
, SIGTERM
, SIGUSR1
, SIGUSR2
....任何其他具有默认动作终止进程的信号都会到达,进程将终止。如果信号 SIGCHLD
或 SIGURG
到达,它们将被忽略 - 代码将“继续”为 in sigtimedwait
(因为没有处理程序为这些信号执行)。如果有停止信号(SIGSTOP、SIGTSTP、SIGTTIN、SIGTTOU),进程将被停止。在 SIGSTOP
之后我们恢复进程(通过发送 SIGCONT
),然后 sigtimedwait
将 return -1
并且 errno
将设置为 EINTR
。有关详细信息,请参阅 man 7 signal
and posix sigtimedwait and posix signal concepts。
(code 2) when the code in sigtimedwait and SIGUSR1 will arrived, will handle_sig be called?
是的。
or it will blocked ?
没有。信号处理程序将被执行,然后 sigtimedwait
将 return -1
和 errno
将设置为 EINTR
.
(TBH 为什么你不直接测试代码?在信号处理程序、编译器和 运行 进程中放置一些 write(1
并向其发送 SIGUSR1 信号并查看结果看起来微不足道发生)
maybe it's depending on architecture
它不是 - 行为不依赖于体系结构,而是依赖于操作系统的行为 - 依赖于负责将信号转发和处理到进程并管理它们的底层软件。 POSIX 兼容的操作系统必须执行 POSIX 指定的操作,独立于它 运行 正在使用的架构。