mirror of
https://github.com/riba2534/TCP-IP-NetworkNote.git
synced 2026-07-11 15:35:44 +08:00
P169 用 signal 函数进行信号处理
This commit is contained in:
29
ch10/signal.c
Normal file
29
ch10/signal.c
Normal file
@@ -0,0 +1,29 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
|
||||
void timeout(int sig) //信号处理器
|
||||
{
|
||||
if (sig == SIGALRM)
|
||||
puts("Time out!");
|
||||
alarm(2); //为了每隔 2 秒重复产生 SIGALRM 信号,在信号处理器中调用 alarm 函数
|
||||
}
|
||||
void keycontrol(int sig) //信号处理器
|
||||
{
|
||||
if (sig == SIGINT)
|
||||
puts("CTRL+C pressed");
|
||||
}
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int i;
|
||||
signal(SIGALRM, timeout); //注册信号及相应处理器
|
||||
signal(SIGINT, keycontrol);
|
||||
alarm(2); //预约 2 秒候发生 SIGALRM 信号
|
||||
|
||||
for (i = 0; i < 3; i++)
|
||||
{
|
||||
puts("wait...");
|
||||
sleep(100);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user