/*
* File: SigAction.c
* Author: 云守护
*/
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include "DieWithMessage.h"
//中断信号处理函数
void InterruptSignalHandler(int signalType);
int main(int argc, char** argv) {
//信号处理器
struct sigaction handler;
//设置信号处理函数,指派函数处理信号
handler.sa_handler=InterruptSignalHandler;
//填充信号掩码
if(sigfillset(&handler.sa_mask)<0)
DieWithSystemMessage("sigfillset failed!");
handler.sa_flags=0;
//为中断信号设置信号处理者
if(sigaction(SIGINT,&handler,0)<0)
DieWithSystemMessage("sigaction failed for SIGINT");
for(;;)
{
//死循环直到收到 SIGINT信号
handler.sa_flags=0;
printf("hehe");
puts("hehe:puts");
pause();//线程挂起直到收到信号
}
return (EXIT_SUCCESS);
}
void InterruptSignalHandler(int signalType)
{
puts("Interrupt Received .exiting Program");
exit(1);
}
/*
* File: SigAction.c
* Author: 云守护
*/
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <syscall.h>
#include <pthread.h>
pid_t gettid(void)
{
return syscall(SYS_gettid);
}
void DieWithSystemMessage(const char* msg)
{
printf("%s",msg);
printf("\n");
exit(0);
}
//中断信号处理函数
void InterruptSignalHandler(int signalType);
//gcc test_signal002.c -lpthread
int main(int argc, char** argv) {
//信号处理器
struct sigaction handler;
//设置信号处理函数,指派函数处理信号
handler.sa_handler=InterruptSignalHandler;
//填充信号掩码
sigfillset(&handler.sa_mask);
handler.sa_flags=0;
//为中断信号设置信号处理者
sigaction(SIGINT,&handler,0);
sigaction(SIGUSR2,&handler,0);
for(;;)
{
//死循环直到收到 SIGINT信号
// handler.sa_flags=0;
printf("hehe");
puts("hehe:puts");
kill(getpid(),SIGUSR2);
puts("gettid");
kill(gettid(),SIGUSR2);
pthread_kill(pthread_self(),SIGUSR2);
raise(SIGUSR2);
pause();//线程挂起直到收到信号
}
return (EXIT_SUCCESS);
}
void InterruptSignalHandler(int signalType)
{
printf("InterruptSignalHandler=%d\n",signalType);
if(signalType ==SIGINT)
{
puts("Interrupt Received .exiting Program\n");
exit(1);
}else if(signalType ==SIGUSR2)
{
puts("Received SIGUSR2\n");
}
}