• Linux:进程概念的引入和理解


    前面对操作系统有了一个基础的认知,从中得出的最重要的一个思想是,在认识一个新事物前要先描述,再组织,有了这样的思想也可以用于学习进程的概念

    进程的初步理解

    有了前面的思想,那么如何来描述一个进程?进程的概念是什么呢?
    在官方的解释中,将进程解释为运行起来的程序和在内存中的程序,严格意义来说这并不好理解,进程和程序很明显是不一样的,但是仅仅通过这样的概念是无法达到理解进程的程度,因此就需要进行其他方面的理解

    当程序在运行的时候,是从哪里开始的?这要由程序的存储位置决定,程序终究本质上是一个文件,既然是文件就必然是存储在了磁盘中,因此程序要运行起来就要从磁盘转移到内存,这个转移的过程就叫做加载,那么程序现在已经加载到内存中了,但是内存中只是一个一个单独的程序,如何将它们进行管理?因此就出现了前面提到的关键词—操作系统

    操作系统是一个纯粹的管理类的软件,同时也是在电脑运行的时候第一个启动的软件,因此不管什么时候将磁盘中的文件加载到内存中,此时内存中早已有一个软件被提前启动了,这个软件就是操作系统,那么操作系统就有了一个作用:用来管理这些磁盘中的程序,当它们进来的时候已经是进程了,那操作系统是不是要管理这些进程?那如何管理?就要先描述再组织

    如何描述进程?

    在日常生活中,想要管理人,就要先对人进行描述,比如定义一个结构体,里面存储的是人的各种属性,比如说学号,姓名,住址,手机号等等…有了这些信息就能把人的数据进行管理,能管理人的数据就能对人进行组织进行具体的实质性的管理

    那么对于进程来说也是这样,那如何描述进程?有很多描述的属性,比如说进程的id,进程的代码地址,进程的数据地址,进程的状态,进程的优先级,进程的链接字段,能不能把进程像链表一样链接起来可以帮助我们快速的找到进程?因此肯定会有指针的概念存在,前面说的这些属性就共同组成了进程的概念,那这个结构体的名字叫什么?它有一个官方的名字叫做进程的pcb,而通过这些进程的pcb,操作系统就可以像人管理人一样将进程也进行管理,这样就将操作系统和进程结合在了一起

    进程的实质理解

    那前面铺垫了很多,将进程和操作系统联系在了一起,那么说到头,进程到底是什么呢?

    进程=可执行程序+内核数据结构(PCB)

    由于我们的研究对象都是在Linux环境下进行研究,那么重点是讲述在Linux下的进程概念,在Linux操作系统下,进程的PCB被叫做是task_struct,也就是说,在Linux下描述进程的结构体不叫PCB,叫做task_struct,这是Linux内核中的一种数据结构,它会被装载到内存中,并且当中会包含着进程的信息,用下图来简答说明这段概念

    在这里插入图片描述
    图片很清晰的表现了上面内容的具体意义,磁盘上的可执行程序被加载到了内存中,而内存中早已加载好了操作系统等待着管理进程,当程序进入内存后,操作系统就为每一个进程创建了独属于它的task_struct,这个结构体是用来描述这个进程的,当进来的每一个进程都有一个独属于它的结构体后,就可以利用这些结构体的数据对进程进行管理,在内部操作系统通常是使用链表的形式对进程进行管理,当有进程被杀死后就删除,有新的进程进来就插入,采用的是双向链表能够提高一些效率等等,因此上面对于进程的概念就很好理解了,进程就是可执行程序和内核的数据结构

    下面对进程的理解进行一些其他的解析:

    进程的管理被建模成了数据结构

    数据结构是被抽象出来的概念,那么在实际应用中其实数据结构存在的意义就在于此处,通过数据结构可以很方便的对于数据的管理,以上图展示的进程为例,操作系统作为管理类的软件要进行数据的管理,那管理数据的方式就是借助数据结构,借助双向链表的这种数据结构,就可以把对于进程的管理建模成为对于双向链表的增删查改,有了这样的思维管理和处理数据就显得简单了许多

    进程的PCB可以存在于多个链表

    进程的PCB不一定一定是上图中画的这一条链表,从本质上来说链表中的这些节点只是链表中的一些数据而已,可以在很多条链表中同时存在这些数据

    进程不一定只能在链表中

    上面提到了,数据结构可以用来管理数据,对于上图的描述来说,只是讲数据放在了链表中进行管理,但是实际上并不意味着只能在链表中进行数据的管理,还可以放在队列等其他数据结构中进行建模管理

    小结

    从这一个章节结束后,对于进程的理解就已经初步搭建起来了,但这都是理论上的停留,对于进程的实践可以更好的帮助理解进程

    查看进程

    Linux中,如果想要查看进程,可以在/proc系统文件夹中查看

    在这里插入图片描述
    可以使用ps和top来获取进程信息

    # 查看进程的信息
    [test@VM-16-11-centos ~]$ ps aux
    USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
    root         1  0.0  0.1 191148  3812 ?        Ss   Jul15  11:50 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
    root         2  0.0  0.0      0     0 ?        S    Jul15   0:02 [kthreadd]
    root         4  0.0  0.0      0     0 ?        S<   Jul15   0:00 [kworker/0:0H]
    root         6  0.0  0.0      0     0 ?        S    Jul15   2:03 [ksoftirqd/0]
    root         7  0.0  0.0      0     0 ?        S    Jul15   0:26 [migration/0]
    root         8  0.0  0.0      0     0 ?        S    Jul15   0:00 [rcu_bh]
    root         9  0.0  0.0      0     0 ?        S    Jul15  21:54 [rcu_sched]
    root        10  0.0  0.0      0     0 ?        S<   Jul15   0:00 [lru-add-drain]
    root        11  0.0  0.0      0     0 ?        S    Jul15   0:14 [watchdog/0]
    root        12  0.0  0.0      0     0 ?        S    Jul15   0:12 [watchdog/1]
    root        13  0.0  0.0      0     0 ?        S    Jul15   0:27 [migration/1]
    root        14  0.0  0.0      0     0 ?        S    Jul15   1:51 [ksoftirqd/1]
    root        16  0.0  0.0      0     0 ?        S<   Jul15   0:00 [kworker/1:0H]
    root        18  0.0  0.0      0     0 ?        S    Jul15   0:00 [kdevtmpfs]
    root        19  0.0  0.0      0     0 ?        S<   Jul15   0:00 [netns]
    root        20  0.0  0.0      0     0 ?        S    Jul15   0:01 [khungtaskd]
    root        21  0.0  0.0      0     0 ?        S<   Jul15   0:00 [writeback]
    root        22  0.0  0.0      0     0 ?        S<   Jul15   0:00 [kintegrityd]
    root        23  0.0  0.0      0     0 ?        S<   Jul15   0:00 [bioset]
    root        24  0.0  0.0      0     0 ?        S<   Jul15   0:00 [bioset]
    root        25  0.0  0.0      0     0 ?        S<   Jul15   0:00 [bioset]
    root        26  0.0  0.0      0     0 ?        S<   Jul15   0:00 [kblockd]
    root        27  0.0  0.0      0     0 ?        S<   Jul15   0:00 [md]
    root        28  0.0  0.0      0     0 ?        S<   Jul15   0:00 [edac-poller]
    root        29  0.0  0.0      0     0 ?        S<   Jul15   0:00 [watchdogd]
    root        35  0.0  0.0      0     0 ?        S    Jul15   0:02 [kswapd0]
    root        36  0.0  0.0      0     0 ?        SN   Jul15   0:00 [ksmd]
    root        37  0.0  0.0      0     0 ?        SN   Jul15   0:07 [khugepaged]
    root        38  0.0  0.0      0     0 ?        S<   Jul15   0:00 [crypto]
    root        46  0.0  0.0      0     0 ?        S<   Jul15   0:00 [kthrotld]
    root        48  0.0  0.0      0     0 ?        S<   Jul15   0:00 [kmpath_rdacd]
    root        49  0.0  0.0      0     0 ?        S<   Jul15   0:00 [kaluad]
    root        51  0.0  0.0      0     0 ?        S<   Jul15   0:00 [kpsmoused]
    root        52  0.0  0.0      0     0 ?        S<   Jul15   0:00 [ipv6_addrconf]
    root        65  0.0  0.0      0     0 ?        S<   Jul15   0:00 [deferwq]
    root       106  0.0  0.0      0     0 ?        S    Jul15   1:42 [kauditd]
    root       203  0.0  0.0      0     0 ?        S<   Jul15   0:00 [iscsi_eh]
    root       253  0.0  0.0      0     0 ?        S<   Jul15   0:00 [ata_sff]
    root       262  0.0  0.0      0     0 ?        S    Jul15   0:00 [scsi_eh_0]
    root       267  0.0  0.0      0     0 ?        S<   Jul15   0:00 [scsi_tmf_0]
    root       268  0.0  0.0      0     0 ?        S    Jul15   0:00 [scsi_eh_1]
    root       269  0.0  0.0      0     0 ?        S<   Jul15   0:00 [scsi_tmf_1]
    root       272  0.0  0.0      0     0 ?        S<   Jul15   0:00 [ttm_swap]
    root       277  0.0  0.0      0     0 ?        S<   Jul15   0:31 [kworker/0:1H]
    root       292  0.0  0.0      0     0 ?        S    Jul15  11:01 [jbd2/vda1-8]
    root       293  0.0  0.0      0     0 ?        S<   Jul15   0:00 [ext4-rsv-conver]
    root       300  0.0  0.0      0     0 ?        S<   Jul15   0:20 [kworker/1:1H]
    root       381  0.0  3.0 134284 62172 ?        Ss   Jul15  15:18 /usr/lib/systemd/systemd-journald
    root       404  0.0  0.0 116644  1088 ?        Ss   Jul15   0:00 /usr/sbin/lvmetad -f
    root       409  0.0  0.0  45388  1744 ?        Ss   Jul15   0:00 /usr/lib/systemd/systemd-udevd
    root       501  0.0  0.0      0     0 ?        S<   Jul15   0:00 [nfit]
    root       515  0.0  0.0  55532  1064 ?        S<sl Jul15   3:11 /sbin/auditd
    libstor+   556  0.0  0.0   8580   796 ?        Ss   Jul15   0:13 /usr/bin/lsmd -d
    dbus       561  0.0  0.1  60312  2308 ?        Ss   Jul15  10:41 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --sys
    root       566  0.0  0.0   4388   520 ?        Ss   Jul15   0:00 /usr/sbin/acpid
    root       567  0.0  0.0  26384  1688 ?        Ss   Jul15   4:47 /usr/lib/systemd/systemd-logind
    polkitd    568  0.0  0.4 614324  9852 ?        Ssl  Jul15   3:20 /usr/lib/polkit-1/polkitd --no-debug
    root       612  0.0  0.0      0     0 ?        S<   Jul15   0:00 [ib-comp-wq]
    root       613  0.0  0.0      0     0 ?        S<   Jul15   0:00 [kworker/u5:0]
    root       614  0.0  0.0      0     0 ?        S<   Jul15   0:00 [ib-comp-unb-wq]
    root       615  0.0  0.0      0     0 ?        S<   Jul15   0:00 [ib_mcast]
    root       616  0.0  0.0      0     0 ?        S<   Jul15   0:00 [ib_nl_sa_wq]
    root       621  0.0  0.0      0     0 ?        S<   Jul15   0:00 [mlx5_ib_sigerr_]
    root      1005  0.0  0.1 103004  2200 ?        Ss   Jul15   0:00 /sbin/dhclient -q -lf /var/lib/dhclient/dhclient--eth0.lease -pf /var/run/d
    root      1070  0.0  0.4  61488 10140 ?        S<Ls Jul15   0:00 /sbin/iscsid -f
    root      1072  0.0  0.7 574288 16108 ?        Ssl  Jul15   7:35 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
    root      1075  0.0  0.0  50096   956 ?        Ssl  Jul15   0:00 /usr/sbin/rshim
    root      1180  0.0  1.5 731540 31080 ?        Ssl  Jul15   8:42 /usr/sbin/rsyslogd -n
    root      1188  0.0  0.0  25908   952 ?        Ss   Jul15   0:00 /usr/sbin/atd -f
    root      1195  0.0  0.0 126424  1656 ?        Ss   Jul15   0:42 /usr/sbin/crond -n
    root      1205  0.0  0.0 110208   832 ttyS0    Ss+  Jul15   0:00 /sbin/agetty --keep-baud 115200,38400,9600 ttyS0 vt220
    root      1206  0.0  0.0 110208   820 tty1     Ss+  Jul15   0:00 /sbin/agetty --noclear tty1 linux
    root      1318  0.0  0.1  91888  2220 ?        Ss   Jul15   0:16 /usr/libexec/postfix/master -w
    postfix   1328  0.0  0.1  92168  4052 ?        S    Jul15   0:03 qmgr -l -t unix -u
    root      1370  0.0  0.2 113000  4368 ?        Ss   Jul15   3:15 /usr/sbin/sshd -D
    root      1565  0.0  0.0  97492  1708 ?        Sl   Jul15   0:45 /usr/local/qcloud/stargate/bin/sgagent -d
    ntp       1800  0.0  0.1  32044  2076 ?        Ss   Jul15   0:07 /usr/sbin/ntpd -u ntp:ntp -g
    root      3243  0.0  0.0      0     0 ?        S    Sep21   0:00 [kworker/u4:0]
    root      4695  0.0  0.0      0     0 ?        S    14:26   0:00 [kworker/1:0]
    root      5449  0.0  0.8 988576 17532 ?        Sl   Jul15  26:46 /usr/local/qcloud/YunJing/YDLive/YDLive
    root     14438  0.0  0.0      0     0 ?        S    Sep19   0:01 [kworker/u4:2]
    root     14808  0.8  2.5 989324 52008 ?        Sl   Sep11 135:03 /usr/local/qcloud/YunJing/YDEyes/YDService
    root     14911  0.0  0.3 1026504 7044 ?        Sl   Sep11   3:00 /bin/sh -c sleep 100
    root     15264  0.0  0.3  29668  6356 ?        Sl   Aug30   4:58 /usr/local/qcloud/tat_agent/tat_agent
    root     20043  0.0  0.3 157820  7604 ?        S    Aug31   0:21 barad_agent
    root     20051  0.0  0.4 165176  9496 ?        S    Aug31  22:03 barad_agent
    root     20052  0.6  0.8 756260 16492 ?        Sl   Aug31 202:31 barad_agent
    test     20530  0.0  0.0  22244  1360 ?        Ssl  Sep12   0:00 /home/test/.VimForCpp/nvim test.s
    test     20543  0.0  0.2  20904  4192 ?        Ssl  Sep12   0:00 /home/test/.VimForCpp/vim/bundle/LanguageClient-neovim/bin/languageclient
    postfix  20942  0.0  0.2  91992  4108 ?        S    15:34   0:00 pickup -l -t unix -u
    root     24271  0.0  0.0      0     0 ?        S    15:48   0:00 [kworker/0:0]
    root     27220  0.0  0.0      0     0 ?        S    16:00   0:00 [kworker/1:1]
    root     27911  0.0  0.0      0     0 ?        R    16:03   0:00 [kworker/0:2]
    root     29238  0.0  0.2 156876  5400 ?        Ss   16:08   0:00 sshd: test [priv]
    test     29240  0.0  0.1 156876  2360 ?        D    16:08   0:00 sshd: test@pts/0
    test     29241  0.0  0.1 116992  3468 pts/0    Ss   16:08   0:00 -bash
    root     29450  0.0  0.0      0     0 ?        S    16:09   0:00 [kworker/0:1]
    root     29765  0.0  0.0      0     0 ?        S    16:10   0:00 [kworker/1:2]
    test     30118  0.0  0.0 155452  1872 pts/0    R+   16:11   0:00 ps aux
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102

    下面用实验证明进程的存在

    首先创建test.c文件

    #include 
    #include 
    
    int main()
    {
        while(1)
        {
            printf("正在打印进程\n");
            sleep(1);
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    创建Makefile文件

    cc=gcc
    src=test.c
    target=mybin
    
    $(target):$(src)
    	gcc $^ -o $@
    .PHONY:clean
    clean:
    	rm -f $(target)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    此时启动程序,启动的同时观察进程的变化情况

    # 启动程序
    [test@VM-16-11-centos test]$ ./mybin
    
    • 1
    • 2
    # 观察进程中mybin的情况
    [test@VM-16-11-centos test]$ ps aux |grep mybin
    test     16029  0.0  0.0   4216   352 pts/0    S+   17:24   0:00 ./mybin
    test     16056  0.0  0.0 112816   980 pts/1    S+   17:24   0:00 grep --color=auto mybin
    
    • 1
    • 2
    • 3
    • 4

    从中看出,mybin进程是确实存在的,并且其中还包含一个grep --color=auto mybin进程,因为调用grep管道指令也算一个进程

  • 相关阅读:
    2.JDBC必知必会
    动态内存分配
    信创优选,国产开源。Solon v2.5.3 发布
    (Java)中的数据类型和变量
    汇编学习(1)
    (框架)Deepracer 自动训练 框架的搭建
    SpringSecurity+JWT 登录授权过滤器
    基于ssm的智慧家政在线预约管理系统的设计与实现-计算机毕业设计源码
    Java反射--师承韩顺平
    MSTP+VRRP配置
  • 原文地址:https://blog.csdn.net/qq_73899585/article/details/133172516