使用信号实现超时
2008-10-30 16:57
337 查看
信号是软件中断,能够提供一种处理异步事件的方法。
这些信号被定义在signal.h中,列表如下:
#define SIGHUP 1 /* Hangup (POSIX). */
#define SIGINT 2 /* Interrupt (ANSI). */
#define SIGQUIT 3 /* Quit (POSIX). */
#define SIGILL 4 /* Illegal instruction (ANSI). */
#define SIGTRAP 5 /* Trace trap (POSIX). */
#define SIGABRT 6 /* Abort (ANSI). */
#define SIGIOT 6 /* IOT trap (4.2 BSD). */
#define SIGBUS 7 /* BUS error (4.2 BSD). */
#define SIGFPE 8 /* Floating-point exception (ANSI). */
#define SIGKILL 9 /* Kill, unblockable (POSIX). */
#define SIGUSR1 10 /* User-defined signal 1 (POSIX). */
#define SIGSEGV 11 /* Segmentation violation (ANSI). */
#define SIGUSR2 12 /* User-defined signal 2 (POSIX). */
#define SIGPIPE 13 /* Broken pipe (POSIX). */
#define SIGALRM 14 /* Alarm clock (POSIX). */
#define SIGTERM 15 /* Termination (ANSI). */
#define SIGSTKFLT 16 /* Stack fault. */
#define SIGCLD SIGCHLD /* Same as SIGCHLD (System V). */
#define SIGCHLD 17 /* Child status has changed (POSIX). */
#define SIGCONT 18 /* Continue (POSIX). */
#define SIGSTOP 19 /* Stop, unblockable (POSIX). */
#define SIGTSTP 20 /* Keyboard stop (POSIX). */
#define SIGTTIN 21 /* Background read from tty (POSIX). */
#define SIGTTOU 22 /* Background write to tty (POSIX). */
#define SIGURG 23 /* Urgent condition on socket (4.2 BSD). */
#define SIGXCPU 24 /* CPU limit exceeded (4.2 BSD). */
#define SIGXFSZ 25 /* File size limit exceeded (4.2 BSD). */
#define SIGVTALRM 26 /* Virtual alarm clock (4.2 BSD). */
#define SIGPROF 27 /* Profiling alarm clock (4.2 BSD). */
#define SIGWINCH 28 /* Window size change (4.3 BSD, Sun). */
#define SIGPOLL SIGIO /* Pollable event occurred (System V). */
#define SIGIO 29 /* I/O now possible (4.2 BSD). */
#define SIGPWR 30 /* Power failure restart (System V). */
#define SIGSYS 31 /* Bad system call. */
#define SIGUNUSED 31
其中,有一个信号是闹钟信号SIGALRM,在程序中,我们可以调用alarm函数设置一个超时数值,当超过这个值之后内核将会向该进程发送SIGALRM信号。按POSIX的说明,如果不捕获或者忽略该信号,默认的动作是终止该进程。但事实上我们一般不会使用默认的动作,而是进行自己的处理。
alarm函数的原型是 unsigned int alarm(unsigned int seconds)
它的参数seconds是超时的秒数,它有返回值。如果上一次使用了alarm函数,并且还没有到超时时间又使用了alarm函数,它会将上一次设置的seconds返回以便程序对其进行处理。
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
static short int is_run = 1;
void sig_alarm(int);
int main(int argc, char *argv[])
{
if(signal(SIGALRM, sig_alarm) == SIG_ERR)
{
perror("signal()");
return 0;
}
alarm(5);
printf("process will stop after 5 seconds!/n");
while(is_run);
printf("process stop!/n");
return 0;
}
void sig_alarm(int seconds)
{
is_run = 0;
}
程序中有一个空循环,条件是is_run。而闹钟信号监听程序将is_run设置为0从而终止了该循环。其结果为:
process will stop after 5 seconds!
process stop!
其中第二句是五秒后才输出的。
这些信号被定义在signal.h中,列表如下:
#define SIGHUP 1 /* Hangup (POSIX). */
#define SIGINT 2 /* Interrupt (ANSI). */
#define SIGQUIT 3 /* Quit (POSIX). */
#define SIGILL 4 /* Illegal instruction (ANSI). */
#define SIGTRAP 5 /* Trace trap (POSIX). */
#define SIGABRT 6 /* Abort (ANSI). */
#define SIGIOT 6 /* IOT trap (4.2 BSD). */
#define SIGBUS 7 /* BUS error (4.2 BSD). */
#define SIGFPE 8 /* Floating-point exception (ANSI). */
#define SIGKILL 9 /* Kill, unblockable (POSIX). */
#define SIGUSR1 10 /* User-defined signal 1 (POSIX). */
#define SIGSEGV 11 /* Segmentation violation (ANSI). */
#define SIGUSR2 12 /* User-defined signal 2 (POSIX). */
#define SIGPIPE 13 /* Broken pipe (POSIX). */
#define SIGALRM 14 /* Alarm clock (POSIX). */
#define SIGTERM 15 /* Termination (ANSI). */
#define SIGSTKFLT 16 /* Stack fault. */
#define SIGCLD SIGCHLD /* Same as SIGCHLD (System V). */
#define SIGCHLD 17 /* Child status has changed (POSIX). */
#define SIGCONT 18 /* Continue (POSIX). */
#define SIGSTOP 19 /* Stop, unblockable (POSIX). */
#define SIGTSTP 20 /* Keyboard stop (POSIX). */
#define SIGTTIN 21 /* Background read from tty (POSIX). */
#define SIGTTOU 22 /* Background write to tty (POSIX). */
#define SIGURG 23 /* Urgent condition on socket (4.2 BSD). */
#define SIGXCPU 24 /* CPU limit exceeded (4.2 BSD). */
#define SIGXFSZ 25 /* File size limit exceeded (4.2 BSD). */
#define SIGVTALRM 26 /* Virtual alarm clock (4.2 BSD). */
#define SIGPROF 27 /* Profiling alarm clock (4.2 BSD). */
#define SIGWINCH 28 /* Window size change (4.3 BSD, Sun). */
#define SIGPOLL SIGIO /* Pollable event occurred (System V). */
#define SIGIO 29 /* I/O now possible (4.2 BSD). */
#define SIGPWR 30 /* Power failure restart (System V). */
#define SIGSYS 31 /* Bad system call. */
#define SIGUNUSED 31
其中,有一个信号是闹钟信号SIGALRM,在程序中,我们可以调用alarm函数设置一个超时数值,当超过这个值之后内核将会向该进程发送SIGALRM信号。按POSIX的说明,如果不捕获或者忽略该信号,默认的动作是终止该进程。但事实上我们一般不会使用默认的动作,而是进行自己的处理。
alarm函数的原型是 unsigned int alarm(unsigned int seconds)
它的参数seconds是超时的秒数,它有返回值。如果上一次使用了alarm函数,并且还没有到超时时间又使用了alarm函数,它会将上一次设置的seconds返回以便程序对其进行处理。
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
static short int is_run = 1;
void sig_alarm(int);
int main(int argc, char *argv[])
{
if(signal(SIGALRM, sig_alarm) == SIG_ERR)
{
perror("signal()");
return 0;
}
alarm(5);
printf("process will stop after 5 seconds!/n");
while(is_run);
printf("process stop!/n");
return 0;
}
void sig_alarm(int seconds)
{
is_run = 0;
}
程序中有一个空循环,条件是is_run。而闹钟信号监听程序将is_run设置为0从而终止了该循环。其结果为:
process will stop after 5 seconds!
process stop!
其中第二句是五秒后才输出的。
相关文章推荐
- 使用 select 实现 goroutine 超时
- 使用共享内存实现进程间通信 -使用信号实现同步
- 使用SIGALRM信号为阻塞操作设置超时
- 使用矩阵运算替代 for 循环实现信号的DTFT
- Linux定时器处理之实时信号使用,消息队列阻塞模型,避免超时等待
- 如何用信号来实现超时的读写机制
- golang 使用os/exec配合context实现的超时机制
- 使用SIGALRM信号为阻塞操作设置超时
- C# .NET使用SerialPort类实现接受信号和发送信号
- 我们每天使用互联网,你是否想过,它是如何实现的? 全世界几十亿台电脑,连接在一起,两两通信。上海的某一块网卡送出信号,洛杉矶的另一块网卡居然就收到了,两者实际上根本不知道对方的物理位置,你不觉得这是
- 第二十五篇:使用 sigaction 函数实现可靠信号
- UNIX网络编程——使用select 实现套接字I/O超时
- 使用信号实现异步通知机制的例子
- 使用 sigaction 函数实现可靠信号
- 使用信号实现线程同步
- ES transport client底层是netty实现,netty本质上是异步方式,但是netty自身可以使用sync或者await(future超时机制)来实现类似同步调用!因此,ES transport client可以同步调用也可以异步(不过底层的socket必然是异步实现)
- 使用boost和stl分别实现超时功能
- 使用信号signal,实现进程之间的同步
- 使用两个context实现CLOSE包的超时等待
- Qt的事件模型(5种使用办法,通常重新实现event handler即可。只有定义控件才需要管理信号的发射)