Linux SIGINT捕获

test.c

#include <stdio.h>
#include <signal.h>

void int_handler(void);

int main(int argc, char ** argv)
{
    sigset( SIGINT, int_handler);
    printf( "Process_pid=[%d]\n", getpid() );
    while(1);
    return 0;
}

void int_handler(void)
{
    printf("Get a SIGINT signal!\n"); 
}

SIGINT: 程序中止信号,在用户键入INTR字符(通常是Ctrl+C)时发出。

gcc  test.c -o test

运行程序:

./test

按下Ctrl+C键(or kill -2 1234,kill -SIGINT 1234),程序并不退出,而是打印输出 Get a SIGINT signal!。
可以使用Ctrl+\ 来退出(SIGQUIT)。
//
signal函数

#include <signal.h>
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);

第一个参数signum:指明了所要处理的信号类型,它可以取除了SIGKILL和SIGSTOP外的任何一种信号。  
第二个参数handler:描述了与信号关联的动作,它可以取以下三种值:
(1)SIG_IGN   
这个符号表示忽略该信号。

#include <stdio.h>
#include <signal.h>
int main(int argc, char *argv[]) 
{
    signal(SIGINT, SIG_IGN);
    while(1);
    return 0;
}

按下Ctrl+C,由于信号忽略了,程序并不退出。
(2)SIG_DFL   
这个符号表示恢复对信号的系统默认处理。不写此处理函数默认也是执行系统默认操作。

#include <stdio.h>
#include <signal.h>
int main(int argc, char *argv[])
 {
    signal(SIGINT, SIG_DFL);
    while(1);
    return 0;
}

这时就可以按下CTRL +C 来终止该进程了。把signal(SIGINT, SIG_DFL);这句去掉,效果是一样的
(3)sighandler_t类型的函数指针

如上。
//

   kill - send a signal to a process

EXAMPLES
kill -9 -1
Kill all processes you can kill.

   kill -l 11
          Translate number 11 into a signal name.

   kill -L //kill -l ???
          List the available signal choices in a nice table.

   kill 123 543 2341 3453
          Send the default signal, SIGTERM, to all those processes.

发送信号SIGTERM给进程 4589,方法:
kill -s TERM 4589
kill -s 15 4589
kill -n TERM 4589
kill -n 15 4589
kill -15 4589 //建议
kill -TERM 4589 //建议
kill -SIGTERM 4589

在进程中,SIGTSTP(信号量20: Ctrl+Z)和SIGKILL(信号量9)信号是不可以被捕获或者忽略的。除此之外,其他的各种信号都可以被进程捕获和处理。


ps -aux
ps -ef
ps -ef | grep test
通过名字杀死进程:

killall test
pkill test
killall和pkill是相似的,不过如果给出的进程名不完整,killall会报错。pkill只要给出进程名的一部分就可以终止进程。
pkill tes :ok
killall tes :error

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
主要功能: 1.读取配置文件程序 2.启动进程 3.监控进程,查看进程是否退出或者崩溃 4.若进程退出或者崩溃,重启程序。 5.支持sleep功能 6.进程若连续崩溃NUM_MAX次就进行相应的睡眠周期struct proc_struct proc: struct proc_ struct [mp: if(array) return 0 ∥切换到目录rse chdirldiri ifdp= opendir(dir}=NuLL}开日录/proc,矢败返回0,成功把描述指针返回给d return o 〃将φpro文件夹的描述符指针传递给re&epsilon;ddir,读取文件夹内容,循环赋值给结构体di while ((dirp= readdir(dp))= NULLY char data 301 ∥取文件名称赋值给数组daa(其中包含有进程的名称(pid sprintf(data, "s", dirp->d_name); ∥是否是由字符09组成的字符串,即得到所有进程的pid f((IsDigit(data)) prac =(struct proc_struct )4 malloc(sizeof(struct proc_struct) tmp proc: prac->pid =a: oi(dirp->d_name): It(proc tind( proc. array)) free( tmp); closedir(dp cturn proc_find 两个参数分别是两个进程描述的结构体指针 李比较两个进程pd是否相等 李*相等返回1,不相等返回0 幸率球事容球家草事家事球峰率享事球摩率球享享溶事*事卷寒球套事塞容寒/ int proc find( struct prcc_struct* src, struct proc- struct* dest) char buffer[40%6]. ps cmd[20] It fd. I sprintf(buffer, "ed/star", sre->pid); fd = open(butter, O_RDONLY) if(fd==-1) rerurn 0 memset(buffer, wO, sizeof(buffer)) len= read(fd, bufter, sizeof(bufter )-1) close(ld) if(l return 0: p= butter: p= strrchr(p, C) narq=strrchr(p, )) n=q-p-1 if (len >= sizeof, srt->name)) len= sizeof(src->name)-1 p+ l, len src->namelen]=0; =日 turn(strcmp( src->name, dest dest->name)==0)? 1: 0- 条善参数aay:让程结构体指针;参数sie进程列表数组aray的大小ie:配置文件路径 从配置文件得到指定的程序列表,将对应进程的信息填充到aray数组中 羋执行成功返回进程个数,执行失败返回0 int get_ proc( struct proc_struct array, int size, char file intnRet=o if(! array I‖(si 0)l‖fhle myprinttf"invalid parameterin retun o char line[4096]; FILE fp= fopen(file, T"); if(fp) printf("open file cs fail\n", file) return U memset(line, 0, 4095); while(fgets(lire, 4095, tp)&& nRet size) memcpy(void s)[(&arraylnRet )->cmdline), (void")line, strlen(line)-2 ) tmp= strrchr(line, / ) Lmp += I: memcpy((&array inRet))->name, tmp, strlen(tmp)- 2) nRet++ ); return(nReL); 康棒串串浓凉率旅浓串底率卖毒志着旅浓浓准溶房表 装 startProc *卷参数proc:要启动的进的结构体描述指针 启动程序 执行成功返回1,子进程退出 宗塞家康家家家家家家家家宋家家聚家苯家球察塞家塞家家容家塞家家家家室家家察家家家聚家聚寒撑家装家掌建察家家室事 int startProc (struct proc_ struct* proc, struct proc _struct*must_run_ proc int mIst_run_size static inti=d if( proc)return 0 if(strlen(proc->cmdline I<=0) return 0; int pid= forko: 〃进程内部返回值为0,返回给父进程的为自己的pid inta〓 if(pid pid= fork( ifpd≡0 execl(char")proc->cmdline,(char")prDc->name,NULL); ∥exit: It(o): sleep42片 waiL(NULL) sleep( I: if(i== must run size -1) if(check proc(&must run proc[i])==0) startProc( &mtust_run_proeli], must_run_prce, must_run_size); el if(i== must run size-11 i= else 1++ start Proc( &must_run_proclil, must_run_ proc, mustrun_ _size); !**幸幸串率幸米幸*家*幸毕零*幸幸半字幸字华米*幸半孝率非幸零幸学幸幸车 3a*8*daemon init 幸*启动配置文件当中的需要守护的程序 执行成功返回1,中途出错,返回-1 长界零家墨军零家零率家三哮零座零率零零容岸军零罕型率零零零零牢察座察零零零零季球军零容零 int moniter_ run(struct proc_struct"must_run_proc, int proc_ size) nti=0: for(i=0; i< must_run_size: i ++) ∥监控程序是否正在运行 if(check_ proc(&(must un_ proc[il))<=o) ∥厘新片动程序 startProc(&' must run procli]), must run proc, proc size return I: 幸*事率事率率**率**字幸学摩*率*幸幸学幸半*率幸字****幸中*幸学幸 春*着*信号处理函数 exit_proc 翥安全结束监控的程序 4来没有返回值 告参毒萨响幸帝称昨嗜幸古称索点响卷南都南请南幸难布际本啪昨青市南动南香请非市赤南本 void exit_ proc(int ar InL I struct proc struct proc for(i=0; i< must run_ Size: i++) proc=&(must_run_proc[i]): kill(proc->pid, SIGTERM); exit flag=I exit(o): void exit_proc(int pid 要main L.获取程序列表2启动进程3.监控进程,若程序岀或崩溃,重新启动程序4.收到退 出信号,安全结束监控程序 成功返回1,失败返回0 零牢容容家容字家容容察*禁容容字哮零常字容容容家察容牢容零容容容容容牢字家客字容牢容零容*字容客字容容字容家容容字岩 static void run moniter( void data) 读取程序列表 must_ run _size get proc(must_run_proc, sIZE, data if(rmust run Sizc <=1) return o struct sigaction act, oldact act,sa handler= exit_proc act. sa flags =SA ONESHOT sigaction(SIGTERM, &act, NULL) sigaction(SIGINT, &act, NULL) sigaction(sIGHUP, &act, NULL); 检测并启动未启动的程序 moniter_ run(must run proc, must run slze) eturn null int creat and run moniter(char * file) 开线程: pthread_t moniter_ thread if(pthread_create(&moniter_thread, NULL, run_moniter, file)==0) printf("thread create Ok, check thread start \n") return printf( thread check create Errin"): return -I 零零零零享享事职增零半非寥零享半容零摩率率零享剩率容半半享零半率零半率零率辱寒零享 要 IsDigit 参茶爹数a字符串的地址 *判断字符串是否有纯数字字符组成 春客是纯数字返回1,不是纯数字返回0 喜非要串思率串串串串家串润串串串串串串毒毒喜串串最率毒串串踪串率串串非球毒串妆串串毒串串影零串串毒事串 static int IsDigit[char aD) Int size ∥得到当前字符串的长度 size= strlen(a: ∥若字符串长度为0,则直接返回0:即宇符串为空则返回0: if(size ==0) return 0; ∥循环遍历整个字符串 forli=0; i< Size; i++) ∥如果字符小于字符0,或者大于字符9,则返回0 if(ai]<ol ai>9) retum ∥走到这一步说明字符串由字符09组成,返回1 return l; 主进程源文件:man,c main.c #include"process, h Include <stdio. h> include <stdlib h> 甲 include< unistd. I> 甲 nclude< signal> 却 nclude <sys/ ypes,h include <sys/stat. h> 甲 include< tenth> int main(void) creat_and_run_moniter("proclistini") while(l) sleep(D) turn 以上内容是程序全部源文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值