
Linux系统编程45 信号 - kill() raise() alarm() pause()
发布日期:2021-05-07 13:25:52
浏览次数:18
分类:原创文章
本文共 4989 字,大约阅读时间需要 16 分钟。
kill()
man 2 killNAME kill - send signal to a process SYNOPSIS #include <sys/types.h> #include <signal.h> int kill(pid_t pid, int sig); Feature Test Macro Requirements for glibc (see feature_test_macros(7)): kill(): _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCEDESCRIPTION The kill() system call can be used to send any signal to any process group or process.pid>0 给指定进程发送信号 If pid is positive, then signal sig is sent to the process with the ID specified by pid.pid==0 组内广播,即给所有同组进程发送信号 If pid equals 0, then sig is sent to every process in the process group of the calling process.pid==-1 全局广播,给所有自己有权限发送信号的进程 发送信号,除init进程外,不包括init进程。一般就是init进程会发全局广播。 If pid equals -1, then sig is sent to every process for which the calling process has permission to send signals, except for process 1 (init), but see below.pid<-1 发送给指定进程组。将信号发送给 进程组ID绝对值 == pid绝对值的 进程组中的每个进程 If pid is less than -1, then sig is sent to every process in the process group whose ID is -pid.sig == 0 不会发送任何信号,用于检测一个进程或者进程组 是否存在。 If sig is 0, then no signal is sent, but error checking is still performed; this can be used to check for the existence of a process ID or process group ID.RETURN VALUE 0成功 -1失败 On success (at least one signal was sent), zero is returned. On error, -1 is returned, and errno is set appropriately.ERRORS 如果失败,查看errno 如下:失败原因 EINVAL An invalid signal was specified. 表示目标信号无效 EPERM The process does not have permission to send the signal to any of the target processes. 表示 进程没有向任何目标进程发送信号的权限,即表示 目标进程或进程组存在,但是你没有发送信号的权限。 ESRCH The pid or process group does not exist. Note that an existing process might be a zombie, a process which already committed termination, but has not yet been wait(2)ed for. pid或进程组不存在。注意,现有的进程可能是僵死进程,即已经提交了终止请求,但还没有被等待(2)的进程。
raise()
man 3 raise 标准库函数 NAME raise - send a signal to the caller 给当前进程发送信号,自己给自己发信号SYNOPSIS #include <signal.h> int raise(int sig);DESCRIPTION给当前进程或线程发送信号 The raise() function sends a signal to the calling process or thread. In a single-threaded program it is equivalent to kill(getpid(), sig); //给当前进程发送信号 In a multithreaded program it is equivalent to pthread_kill(pthread_self(), sig);//给当前线程发送信号 If the signal causes a handler to be called, raise() will return only after the signal handler has returned.RETURN VALUE raise() returns 0 on success, and nonzero for failure.
alarm()
NAME alarm - set an alarm clock for delivery of a signal 设置一个发送信号的闹钟SYNOPSIS #include <unistd.h> unsigned int alarm(unsigned int seconds);DESCRIPTION alarm() arranges for a SIGALRM signal to be delivered to the calling process in seconds seconds. alarm()以秒为单位安排将一个SIGALRM信号发送给调用进程。 SIGALRM 也是终止信号 If seconds is zero, any pending alarm is canceled. In any event any previously set alarm() is canceled.RETURN VALUE alarm() returns the number of seconds remaining until any previously scheduled alarm was due to be delivered, or zero if there was no previously scheduled alarm.
实验1 :alarm的基本用法
#include <stdio.h>#include <stdlib.h>#include <unistd.h>int main(){ alarm(5); while(1); exit(0);}mhr@ubuntu:~/Desktop/xitongbiancheng/parallel/signal/test$ gcc alarm.c mhr@ubuntu:~/Desktop/xitongbiancheng/parallel/signal/test$ ./a.out Alarm clockmhr@ubuntu:~/Desktop/xitongbiancheng/parallel/signal/test$ 延迟5秒
实验2 :alarm的连续设置 最有一个alarm有效
#include <stdio.h>#include <stdlib.h>#include <unistd.h>int main(){ alarm(5); alarm(10); alarm(1); while(1); exit(0);}mhr@ubuntu:~/Desktop/xitongbiancheng/parallel/signal/test$ gcc alarm.c mhr@ubuntu:~/Desktop/xitongbiancheng/parallel/signal/test$ ./a.out Alarm clock只延迟1秒mhr@ubuntu:~/Desktop/xitongbiancheng/parallel/signal/test$
实验3 signal+ alarm
signal+ alarm 配合使用 signal 一定要写在 alarm前面,如果alarm写在前面,如下: alarm(5); ... 中间程序 经过了5秒signal(SIGALRM,alarm_handler);
这种情况 中间程序 做了5秒以上的工作,那么5秒后 当此时信号来了,程序还没有执行到 signal() 也就看不到 给信号注册的新的行为:alarm_handler,那么就会沿用这个信号默认的行为,即终止程序。
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <signal.h>static int loop = 1;static void alarm_handler(int s){ loop = 0;}int main(){ int64_t count = 0;// signal 一定要写在 alarm前面signal(SIGALRM,alarm_handler); alarm(5); while(loop) count++; printf("%ld\n",count); exit(0);}mhr@ubuntu:~/Desktop/xitongbiancheng/parallel/signal/test$ gcc alarm.c mhr@ubuntu:~/Desktop/xitongbiancheng/parallel/signal/test$ time ./a.out 2629709698real 0m5.002suser 0m4.936ssys 0m0.000smhr@ubuntu:~/Desktop/xitongbiancheng/parallel/signal/test$
pause()
我们专门做出来的一个用来被打断的 阻塞的系统调用
NAME pause - wait for signal 等待一个信号,即人为做出来的一个阻塞的系统调用SYNOPSIS #include <unistd.h> int pause(void);
pause()使调用进程(或线程)处于休眠状态,直到发出终止进程或调用信号捕捉函数的信号
在有些环境下 sleep() 是由 alarm() + pause()封装的。有些是用nanosleep()封装的。
所以 不建议使用 sleep(),理由是在 sleep() 是由 alarm() + pause()封装的环境中,当你程序中同时使用 sleep() 和 alarm()的时候,必然有覆盖一个 alarm,即 alarm() 和 sleep()当中必然会有一个失效。
发表评论
最新留言
网站不错 人气很旺了 加油
[***.192.178.218]2025年04月08日 02时42分33秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
常用的IDC函数
2019-03-04
BUUCTF 新年快乐 内涵的软件 Java逆向解密 刮开有奖
2019-03-04
angr学习笔记(7)(malloc地址单元符号化)
2019-03-04
angr学习笔记(9)(添加约束)
2019-03-04
angr学习笔记(13)(static_binary)
2019-03-04
windows环境利用start命令实现微信多开
2019-03-04
「CF149D」括号涂色 区间DP好题
2019-03-04
树状数组 模板总结
2019-03-04
「NOI2015」程序自动分析 并查集题解
2019-03-04
[JSOI2008]Blue Mary的战役地图 Hash题解
2019-03-04
Ubuntu修改终端上显示的用户名和主机名(详细步骤)
2019-03-04
教你写一手漂亮的伪代码(详细规则&简单实例)
2019-03-04
结构型设计在工作中的一些经验总结
2019-03-04
如何提升员工体验 助力企业业务增长?这个棘手的问题终于被解决了!
2019-03-04
全球首个!阿里云开源批流一体机器学习平台Alink……
2019-03-04
红点中国、红杉中国联合领投,WakeData惟客数据完成1000万美元B轮融资
2019-03-04
OpenStack发布Ussuri版本 实现智能开源基础设施的自动化
2019-03-04
2020 AI 产业图谱启动,勾勒中国 AI 技术与行业生态
2019-03-04
“编程能力差,90%输在了数学上!”CTO:多数程序员都是瞎努力!
2019-03-04
我是程序员,我用这种方式铭记历史
2019-03-04