linux学习--进程管理

查看进程的几种方式

ps

1 ps
查看当前shell的运行的程序,

1
2
3
4
[root@localhost ~]# ps
PID TTY TIME CMD
4619 pts/1 00:00:00 bash
4771 pts/1 00:00:00 ps

2 ps aux
列出当前系统所有进程,只是静态的

1
2
3
4
5
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root 1 0.1 0.1 59724 7140 ? Ss 10:42 0:02 /usr/lib/system
root 2 0.0 0.0 0 0 ? S 10:42 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S 10:42 0:00 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< 10:42 0:00 [kworker/0:0H]

top

可以动态的显示出进程的状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@localhost ~]# top

top - 11:33:30 up 51 min, 4 users, load average: 0.23, 0.13, 0.13
Tasks: 213 total, 1 running, 212 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.9 us, 0.8 sy, 0.0 ni, 98.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 3579208 total, 2199488 free, 523764 used, 855956 buff/cache
KiB Swap: 3817468 total, 3817468 free, 0 used. 2670476 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3703 root 20 0 784792 23868 14084 S 2.0 0.7 0:08.43 gnome-term+
1618 root 20 0 264952 25048 13596 S 1.3 0.7 0:14.69 Xorg
3382 root 20 0 1948172 194408 36856 S 1.0 5.4 0:44.14 gnome-shell
726 root 20 0 208012 7600 2744 S 0.7 0.2 0:00.18 rsyslogd
891 root 20 0 529076 8852 6600 S 0.7 0.2 0:00.97 NetworkMan+
1 root 20 0 59724 7140 3952 S 0.3 0.2 0:02.19 systemd
524 root 20 0 43016 3604 3232 S 0.3 0.1 0:00.58 systemd-jo+
731 root 20 0 211912 4436 3116 S 0.3 0.1 0:00.18 abrt-watch+
...
输入大写的`M` 会以内存的消耗来排列
输入大写的`C` 会以CPU的消耗来排列

pstree

以树型的形式来展现进程的结构关系
pstree

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
systemd─┬─ModemManager───2*[{ModemManager}]
├─NetworkManager─┬─dhclient
│ └─3*[{NetworkManager}]
├─2*[abrt-watch-log]
├─abrtd
├─accounts-daemon───2*[{accounts-daemon}]
├─alsactl
├─at-spi-bus-laun─┬─dbus-daemon───{dbus-daemon}
│ └─3*[{at-spi-bus-laun}]
├─at-spi2-registr───{at-spi2-registr}
├─atd
├─auditd─┬─audispd─┬─sedispatch
│ │ └─{audispd}
│ └─{auditd}
├─avahi-daemon───avahi-daemon

对进程的控制

强制杀死进程

kill -9 进程的PID号
可以直接强制地杀死进程

将前台的程序后台暂停状态

ctrl + z

将后台暂停的程序恢复到前台

首先使用jobs命令 查看暂停的程序
(1) fg %1 将后台停止的程序变成前台继续执行。1为程序的标号(jobs查看)
(2) bg %1 将后台停止的程序变成后台继续执行。

守护进程

守护进程的概念

linux为了向外部提供稳定的服务,所以需要服务的进程能够安全持久的运行。
由此设计除了守护进程

redhat7下管理服务

1. system V init

在 redhat 7 之前的版本,我们针对服务的管理是采用 system V 的 init 方案,该方案依靠
划分不同的运行级别,启动不同的服务集合,服务依靠脚本控制,并且是顺序执行的。
它有着如下的优缺点:
(1) 优点:原理简单,易于理解。依靠 shell 脚本控制,别写服务脚本门槛比较低。
(2) 缺点:服务顺序启动,启动过程比较慢。不能做到动态的启动服务。

2 systemd

system V init 启动在以前看来并不是影响服务运行的关键,以前 linux 操作系统的
服务常年也难得重启一次,但是随着移动互联网的兴起。许多安卓设备也是基于 linux
内核的,移动设备的启动比较频繁,如果每次启动服务都是顺序启动,客户是难以忍受
的。所以 systemd 采用的是并行的方案。
要想采用并行的方案,一定要解决好服务之间的依赖性。systemd 采用的是一种类
似缓冲池的方案。加入服务 1 需要服务 2 提供的信息,加入服务 2 没有准备好。服务 1
预先留下这个需求继续向下执行。直到服务 2 准备好后和服务 1 进行通信。这样就实现
了并行的服务启动,大大加快了服务的开启。
systemd 机制下对于服务的管理使用 systemctl 命令。下面对 systemctl 的使用方法
进行介绍。

3 systemctl

(1)服务的开启,重启,关闭
systemctl start network
systemctl restart network
systemctl stop network
(2)查看服务运行的详细状态
systemctl status httpd
(3)设置服务的开机自启动,查看是否是自启动
systemctl is-enable vsftpd 查看是否时开机自启动
systemctl enable vsftpd 设置开机自启动
systemctl disable vsftpd 关闭开机自启动
(4)重新载入进程的配置,不用关闭服务
systemctl reload vsftpd
(5)禁用某种服务,使用mask命令

1
2
3
4
[root@localhost ~]# systemctl mask vsftpd
ln -s '/dev/null' '/etc/systemd/system/vsftpd.service'
[root@localhost ~]# systemctl start vsftpd
Failed to issue method call: Unit vsftpd.service is masked.

这种情况下,已经开启的服务可以被关闭,但是没有开的的服务就无法开启

创建一个僵尸进程zombie

僵尸进程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv)
{
pid_t pid = 0;

if((pid = fork()) < 0){ //创建子程序失败
fprintf(stderr, "the fork is error1n");
exit(1);
}else if(pid == 0){ //子进程操作
printf("the child pid is %d\n",getpid());

}else {
printf("the parent pid is %d\n",getpid());
while(1){
printf("the parent is still alive\n");
sleep(1);
}
}
return 0;
}

编译
gcc zombie.c -o zombie
执行
.\zombie

1
2
3
4
5
6
7
8
9
[root@localhost test]# vim zombie.c
[root@localhost test]# gcc zombie.c -o zombie
[root@localhost test]# ./zombie
the parent pid is 9118
the parent is still alive
the child pid is 9119
the parent is still alive
the parent is still alive
the parent is still alive

当执行程序后,使用top命令
可以发现1 zombie
kill -9 9118杀掉进程

Contents
  1. 1. 查看进程的几种方式
    1. 1.1. ps
    2. 1.2. top
    3. 1.3. pstree
  2. 2. 对进程的控制
    1. 2.1. 强制杀死进程
    2. 2.2. 将前台的程序后台暂停状态
    3. 2.3. 将后台暂停的程序恢复到前台
  3. 3. 守护进程
    1. 3.1. 守护进程的概念
    2. 3.2. redhat7下管理服务
      1. 3.2.1. 1. system V init
      2. 3.2.2. 2 systemd
      3. 3.2.3. 3 systemctl
  4. 4. 创建一个僵尸进程zombie
,