Linux进程管理使用信号控制进程

简介: 信号全称为软中断信号,也有人称作软中断,信号机制是进程之间相互传递消息的一种方法。

一、信号概念


 信号全称为软中断信号,也有人称作软中断,信号机制是进程之间相互传递消息的一种方法。


1、信号的种类


      kill,killall:都能发送信号。

       kill 只能接进程号

       killall 能接进程名称


kill  -l   可查看所有支持信号


10b49b733a1d4631bb5af2fdba40f7d8.png


编号为1-31的为不可靠信号,34-64的为可靠信号。连续不间断发送信号,信号会丢失的为不可靠信号,连续不间断发送信号,信号不会丢失的为可靠信号。


简单介绍几个信号的功能


1)SIGHUP    重新加载配置

2)SIGINT       键盘中断Ctrl+C

3)SIGQUIT     键盘退出Ctrl+\,类似SIGINT

9)SIGKILL       强制终止,无条件

15)SIGTERM    终止(正常结束),缺省信号

18)SIGONT      继续

19)SIGSTOP     暂停

20)SIGTSTP     键盘停止Ctrl+Z


语法:kill  命令编号   进程id


1) SIGHUP 重新加载配置,信号发出之后,主进程的进程号不会发生变化,子进程进程号会发生变化。

[root@localhost ~]# ps -ef|grep kthreadd
root          2      0  0 08:55 ?        00:00:00 [kthreadd]
root       6395   4863  0 13:45 pts/0    00:00:00 grep --color=auto kthreadd
[root@localhost ~]# kill -1  2
[root@localhost ~]# ps -ef|grep kthreadd
root          2      0  0 08:55 ?        00:00:00 [kthreadd]
root       6420   4863  0 13:46 pts/0    00:00:00 grep --color=auto kthreadd


kthread的子进程号由6395变为6420


2、测试9,15信号


创建两个文件file1,file2


打开一个终端,查看当前终端(命令tty),用vim进行编辑file1


[root@localhost ~]# tty            //查看当前终端
/dev/pts/0
[root@localhost ~]# vim /test/file1


打开另一个终端,查看当前终端(命令tty),用vim编辑file2


[root@localhost ~]# tty                //查看当前终端
/dev/pts/1
[root@localhost ~]# vim /test/file2


打开多个终端方法如图:

ba1b0780a02e4ecfa7a67b3c35259188.png

c332fb5a8e284bfda729eb061d6110e1.png


打开第三个终端查看vim进程

[root@localhost ~]# ps aux |grep  vim                //查看vim进程
root       6784  0.2  0.5 149808  5528 pts/0    S+   14:08   0:00 vim /test/file1
root       6803  0.6  0.5 149800  5512 pts/1    S+   14:08   0:00 vim /test/file2
root       6805  0.0  0.0 112824   976 pts/2    R+   14:08   0:00 grep --color=auto vim


使用9号信号对vim对file1编辑的进程进行操作

[root@localhost ~]# kill -9 6784        //vim对file1的编辑进程号为6484,使用9号信号进行操作


 vim对file1的操作进程被杀死


7f4544a852c347159176f5c2d67d2d31.png


使用15号信号对vim对file2编辑的进程进行操作(15号为默认的信号可以直接使用kill  进程号直接操作)

[root@localhost ~]# kill -15 6803(kill  6803)      //使用15号信号对进程6803进行操作


vim对file2的编辑进程被终止


7bc8c894463c469c98e5dd75b66f5665.png


注意:一般不推荐使用9号信号,在进程僵死的时候可以使用9号信号,平时我们都使用15号信号关掉进程


killall   vim                              //杀死所有vim进程


3、测试18,19信号

[root@localhost ~]# ps aux |grep nginx
root      27854  0.0  0.1  48112  1144 ?        Ss   14:29   0:00 nginx: master process /opt/data/nginx/sbin/nginx -c /opt/data/nginx/conf/nginx.conf
nginx     27855  0.0  0.2  48492  2168 ?        S    14:29   0:00 nginx: worker process
root      27878  0.0  0.0 112728   972 pts/1    S+   14:29   0:00 grep --color=auto nginx
[root@localhost ~]# kill -19 27854
[root@localhost ~]# ps aux |grep nginx
root      27854  0.0  0.1  48112  1144 ?        Ts   14:30   0:00 nginx: master process /opt/data/nginx/sbin/nginx -c /opt/data/nginx/conf/nginx.conf
nginx     27855  0.0  0.2  48492  2168 ?        S    14:30   0:00 nginx: worker process
root      27880  0.0  0.0 112728   972 pts/1    R+   14:30   0:00 grep --color=auto nginx
[root@localhost ~]# kill -18 27854
[root@localhost ~]# ps aux |grep nginx
root      27854  0.0  0.1  48112  1144 ?        Ss   14:30   0:00 nginx: master process /opt/data/nginx/sbin/nginx -c /opt/data/nginx/conf/nginx.conf
nginx     27855  0.0  0.2  48492  2168 ?        S    14:30   0:00 nginx: worker process
root      27882  0.0  0.0 112728   972 pts/1    S+   14:30   0:00 grep --color=auto nginx













相关文章
|
6天前
|
Ubuntu Linux
【Linux】详解信号产生的方式
【Linux】详解信号产生的方式
|
6天前
|
Unix Linux
【Linux】详解信号的分类&&如何自定义信号的作用
【Linux】详解信号的分类&&如何自定义信号的作用
|
6天前
|
Linux 数据库
linux守护进程介绍 | Linux的热拔插UDEV机制
linux守护进程介绍 | Linux的热拔插UDEV机制
linux守护进程介绍 | Linux的热拔插UDEV机制
|
6天前
|
Unix Linux 调度
linux线程与进程的区别及线程的优势
linux线程与进程的区别及线程的优势
|
6天前
|
Unix Linux C语言
|
6天前
|
Linux 调度 C语言
|
6天前
|
安全 Linux
【Linux】详解用户态和内核态&&内核中信号被处理的时机&&sigaction信号自定义处理方法
【Linux】详解用户态和内核态&&内核中信号被处理的时机&&sigaction信号自定义处理方法
|
6天前
|
存储 Linux C++
【Linux】详解信号的保存&&信号屏蔽字的设置
【Linux】详解信号的保存&&信号屏蔽字的设置
|
6天前
|
存储 Linux
【Linux】对信号产生的内核级理解
【Linux】对信号产生的内核级理解