#include <stdio.h>
#include <signal.h>
#include <stdarg.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#if defined(__Linux__)&&!defined(SI_KERNEL)
#define SI_KERNEL 0x80
#endif
int my_printf(const char *fmt,...);
void sighandler(int signumber,siginfo_t *info,void *extre);
void continuehandler(int signumber,siginfo_t *info,void *extre);
char buffer[200];
int main(void)
{
struct sigaction act;
sigset_t blockset,pending;
int pendingcount;
strcpy(buffer,"None\n");
sigemptyset(&blockset);
act.sa_mask=blockset;
act.sa_flags=SA_SIGINFO;
act.sa_sigaction=&sighandler;
if(sigaction(SIGTERM,&act,NULL)==-1)
{
my_printf("Could not register signal for SIGINT.\n");
}
act.sa_sigaction=&continuehandler;
if(sigaction(SIGCONT,&act,NULL)==-1)
{
my_printf("Could not register signal for SIGCONT.\n");
}
sigaddset(&blockset,SIGTERM);
sigaddset(&blockset,SIGINT);
for(;;)
{
sigprocmask(SIG_BLOCK,&blockset,NULL);
fgets(buffer,sizeof(buffer),stdin);
printf("Input:%s",buffer);
sigpending(&pending);
pendingcount=0;
if(sigismember(&pending,SIGINT))
pendingcount++;
if(sigismember(&pending,SIGTERM))
pendingcount++;
if(pendingcount)
{
my_printf("There are %d signals pending.\n",pendingcount);
}
sigprocmask(SIG_UNBLOCK,&blockset,NULL);
}
return 0;
}
int my_printf(const char *fmt,...)
{
va_list args;
struct tm a;
struct tm *tstruct =&a;
time_t tsec;
tsec=time(NULL);
tstruct=localtime(&tsec);
printf("%02d:%02d:%02d %05d\n",tstruct->tm_hour,tstruct->tm_min,tstruct->tm_sec,getpid());
va_start(args,fmt);
return vprintf(fmt,args);
}
void sighandler(int signumber,siginfo_t *info,void *extre)
{
my_printf("Caught signal %d from",signumber);
switch(info->si_code)
{
case SI_USER: printf("a user process \n");
break;
case SI_KERNEL: printf("the kernel \n");
break;
default: printf("something,strange \n");
}
}
void continuehandler(int signumber,siginfo_t *info,void *extre)
{
my_printf("Continuing.\n");
my_printf("Your last input was: %s",buffer);
}
相关文章
- linux系统编程之fcntl函数设置非阻塞
- linux系统编程之stat函数
- linux查看wifi信号命令_详细解析:Linux系统的无线网络命令
- Linux系统编程-进程暂停函数pause的使用,以及程序中止信号SIGINT的处理,signal信号注册
- Linux系统编程之无名管道
- 【Linux系统】信号:信号保存 / 信号处理、内核态 / 用户态、操作系统运行原理(中断)-两种信号默认处理
- 【Linux系统编程】第四十七弹---深入探索:POSIX信号量与基于环形队列的生产消费模型实现
- 【Linux系统编程】第三十九弹---探索信号处理的奥秘:阻塞信号与sigset_t的深入剖析及实战
- linux系统编程之进程(八):守护进程详解及创建,daemon()使用
- linux strace-跟踪进程的系统调用或是信号产生情况,lstrace-跟踪己丑年调用库函数情况,进程跟踪调试命令