标签:
#include <signal.h>
void (*signal(int sig, void (*func)(int)))(int);
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
void ouch(int sig)
{
printf("\nOUCH! - I got signal %d\n", sig);
//恢复终端中断信号SIGINT的默认行为
(void) signal(SIGINT, SIG_DFL);
}
int main()
{
//改变终端中断信号SIGINT的默认行为,使之执行ouch函数
//而不是终止程序的执行
(void) signal(SIGINT, ouch);
while(1)
{
printf("Hello World!\n");
sleep(1);
}
return 0;
}
#include <signal.h>
int sigaction(int sig, const struct sigaction *act, struct sigaction *oact);
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
void ouch(int sig)
{
printf("\nOUCH! - I got signal %d\n", sig);
}
int main()
{
struct sigaction act;
act.sa_handler = ouch;
//创建空的信号屏蔽字,即不屏蔽任何信息
sigemptyset(&act.sa_mask);
//使sigaction函数重置为默认行为
act.sa_flags = SA_RESETHAND;
sigaction(SIGINT, &act, 0);
while(1)
{
printf("Hello World!\n");
sleep(1);
}
return 0;
}
#include <sys/types.h>
#include <signal.h>
int kill(pid_t pid, int sig);
#include <unistd.h>
unsigned int alarm(unsigned int seconds);
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
static int alarm_fired = 0;
void ouch(int sig)
{
alarm_fired = 1;
}
int main()
{
pid_t pid;
pid = fork();
switch(pid)
{
case -1:
perror("fork failed\n");
exit(1);
case 0:
//子进程
sleep(5);
//向父进程发送信号
kill(getppid(), SIGALRM);
exit(0);
default:;
}
//设置处理函数
signal(SIGALRM, ouch);
while(!alarm_fired)
{
printf("Hello World!\n");
sleep(1);
}
if(alarm_fired)
printf("\nI got a signal %d\n", SIGALRM);
exit(0);
}
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
static int alarm_fired = 0;
void ouch(int sig)
{
alarm_fired = 1;
}
int main()
{
//关联信号处理函数
signal(SIGALRM, ouch);
//调用alarm函数,5秒后发送信号SIGALRM
alarm(5);
//挂起进程
pause();
//接收到信号后,恢复正常执行
if(alarm_fired == 1)
printf("Receive a signal %d\n", SIGALRM);
exit(0);
}
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
void handler(int sig)
{
printf("Handle the signal %d\n", sig);
}
int main()
{
sigset_t sigset;//用于记录屏蔽字
sigset_t ign;//用于记录被阻塞的信号集
struct sigaction act;
//清空信号集
sigemptyset(&sigset);
sigemptyset(&ign);
//向信号集中添加信号SIGINT
sigaddset(&sigset, SIGINT);
//设置处理函数和信号集
act.sa_handler = handler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGINT, &act, 0);
printf("Wait the signal SIGINT...\n");
pause();//挂起进程,等待信号
//设置进程屏蔽字,在本例中为屏蔽SIGINT
sigprocmask(SIG_SETMASK, &sigset, 0);
printf("Please press Ctrl+c in 10 seconds...\n");
sleep(10);
//测试SIGINT是否被屏蔽
sigpending(&ign);
if(sigismember(&ign, SIGINT))
printf("The SIGINT signal has ignored\n");
//在信号集中删除信号SIGINT
sigdelset(&sigset, SIGINT);
printf("Wait the signal SIGINT...\n");
//将进程的屏蔽字重新设置,即取消对SIGINT的屏蔽
//并挂起进程
sigsuspend(&sigset);
printf("The app will exit in 5 seconds!\n");
sleep(5);
exit(0);
}
标签:
原文地址:http://www.cnblogs.com/wuchanming/p/4381574.html