OS第2次实验报告:创建进程

  • 姓名 那宝龙
  • 学号 201821121054
  • 班级 计算1812

1. 编写程序

在服务器上用VIM编辑器编写一个程序:一个进程创建(fork)两个子进程。给出源代码:

#include<sys/types.h>
#include<unistd.h>
#include<stdio.h>

int main()
{
pid_t ppid;
int i;
for(i=0;i<2;i++)
{
ppid=fork();
if(ppid==0 || ppid<0){
break;
}
}
ifp(ppid<0)
printf("error in fork!");
else if(ppid==0){
printf("Child process,my process id is %d\n",getpid());
}
else{
printf("Parent process,my process id is %d\n",getpid());
}
sleep(100);
return 0;
}

 

 

 2. 打印进程树

打印1所创建的进程树结构,给出带有自己名字的截图。

 

 

提示:

  • 使用unsigned sleep(unsigned seconds)挂起进程,以便打印进程树
  • 打印进程树命令pstree -p pid

3. 解读进程相关信息

(1) ps -ef

使用ps -ef给出所创建进程的信息,并分析每个字段的含义。

UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 Mar26 ?        00:00:34 /sbin/init
root         2     0  0 Mar26 ?        00:00:00 [kthreadd]
root         4     2  0 Mar26 ?        00:00:00 [kworker/0:0H]
root         6     2  0 Mar26 ?        00:00:00 [mm_percpu_wq]
root         7     2  0 Mar26 ?        00:00:35 [ksoftirqd/0]
root         8     2  0 Mar26 ?        00:01:00 [rcu_sched]
root         9     2  0 Mar26 ?        00:00:00 [rcu_bh]
root        10     2  0 Mar26 ?        00:00:00 [migration/0]
root        11     2  0 Mar26 ?        00:00:01 [watchdog/0]
root        12     2  0 Mar26 ?        00:00:00 [cpuhp/0]
root        13     2  0 Mar26 ?        00:00:00 [kdevtmpfs]
root        14     2  0 Mar26 ?        00:00:00 [netns]
root        15     2  0 Mar26 ?        00:00:00 [rcu_tasks_kthre]
root        16     2  0 Mar26 ?        00:00:00 [kauditd]
root        17     2  0 Mar26 ?        00:00:00 [khungtaskd]
root        18     2  0 Mar26 ?        00:00:00 [oom_reaper]
root        19     2  0 Mar26 ?        00:00:00 [writeback]
root        20     2  0 Mar26 ?        00:00:00 [kcompactd0]
root        21     2  0 Mar26 ?        00:00:00 [ksmd]
root        22     2  0 Mar26 ?        00:00:00 [khugepaged]
root        23     2  0 Mar26 ?        00:00:00 [crypto]
root        24     2  0 Mar26 ?        00:00:00 [kintegrityd]
root        25     2  0 Mar26 ?        00:00:00 [kblockd]
root        26     2  0 Mar26 ?        00:00:00 [ata_sff]
root        27     2  0 Mar26 ?        00:00:00 [md]
root        28     2  0 Mar26 ?        00:00:00 [edac-poller]
root        29     2  0 Mar26 ?        00:00:00 [devfreq_wq]
root        30     2  0 Mar26 ?        00:00:00 [watchdogd]
root        34     2  0 Mar26 ?        00:00:02 [kswapd0]
root        35     2  0 Mar26 ?        00:00:00 [kworker/u3:0]
root        36     2  0 Mar26 ?        00:00:00 [ecryptfs-kthrea]
root        78     2  0 Mar26 ?        00:00:00 [kthrotld]
root        79     2  0 Mar26 ?        00:00:00 [acpi_thermal_pm]
root        80     2  0 Mar26 ?        00:00:00 [scsi_eh_0]
root        81     2  0 Mar26 ?        00:00:00 [scsi_tmf_0]
root        82     2  0 Mar26 ?        00:00:00 [scsi_eh_1]
root        83     2  0 Mar26 ?        00:00:00 [scsi_tmf_1]
root        89     2  0 Mar26 ?        00:00:00 [ipv6_addrconf]
root        98     2  0 Mar26 ?        00:00:00 [kstrp]
root       115     2  0 Mar26 ?        00:00:00 [charger_manager]
root       156     2  0 Mar26 ?        00:00:10 [kworker/0:1H]
root       169     2  0 Mar26 ?        00:00:00 [ttm_swap]
root       272     2  0 Mar26 ?        00:00:00 [raid5wq]
root       327     2  0 Mar26 ?        00:00:14 [jbd2/vda1-8]
root       328     2  0 Mar26 ?        00:00:00 [ext4-rsv-conver]
root       394     1  0 Mar26 ?        00:03:08 /lib/systemd/systemd-journald
root       410     2  0 Mar26 ?        00:00:00 [iscsi_eh]
root       411     1  0 Mar26 ?        00:00:00 /sbin/lvmetad -f
root       421     2  0 Mar26 ?        00:00:00 [ib-comp-wq]
root       422     2  0 Mar26 ?        00:00:00 [ib_mcast]
root       423     2  0 Mar26 ?        00:00:00 [ib_nl_sa_wq]
root       434     2  0 Mar26 ?        00:00:00 [rdma_cm]
root       436     1  0 Mar26 ?        00:00:01 /lib/systemd/systemd-udevd
systemd+   803     1  0 Mar26 ?        00:00:00 /lib/systemd/systemd-networkd
systemd+   825     1  0 Mar26 ?        00:00:11 /lib/systemd/systemd-resolved
root       909     1  0 Mar26 ?        00:00:01 /lib/systemd/systemd-logind
root       918     1  0 Mar26 ?        00:00:03 /usr/bin/lxcfs /var/lib/lxcfs/
syslog     919     1  0 Mar26 ?        00:00:40 /usr/sbin/rsyslogd -n
daemon     925     1  0 Mar26 ?        00:00:00 /usr/sbin/atd -f
root       929     1  0 Mar26 ?        00:00:00 /usr/bin/python3 /usr/bin/networkd-dispatcher --run-startup-triggers
root       933     1  0 Mar26 ?        00:00:45 /usr/lib/accountsservice/accounts-daemon
message+   936     1  0 Mar26 ?        00:00:41 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --
root       978     1  0 Mar26 ?        00:00:03 /usr/sbin/cron -f
root       994     1  0 Mar26 ?        00:00:00 /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutd
root      1011     1  0 Mar26 ?        00:00:22 /usr/lib/policykit-1/polkitd --no-debug
ntp       1012     1  0 Mar26 ?        00:00:26 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 111:115
root      1013     1  0 Mar26 ?        00:00:59 /usr/sbin/sshd -D
root      1157     1  0 Mar26 ttyS0    00:00:00 /sbin/agetty -o -p -- \u --keep-baud 115200,38400,9600 ttyS0 vt220
root      1159     1  0 Mar26 ?        00:31:34 /usr/local/qcloud/YunJing/YDEyes/YDService
root      1365     1  0 Mar26 ?        00:00:05 /usr/local/qcloud/stargate/sgagent -d
root      1380     1  0 Mar26 ?        00:00:07 barad_agent
root      1381  1380  0 Mar26 ?        00:07:40 barad_agent
root      1382  1380  0 Mar26 ?        00:37:29 barad_agent
root      1446     1  0 Mar26 ?        00:01:04 /usr/local/qcloud/YunJing/YDLive/YDLive
root      2550     2  0 10:37 ?        00:00:00 [kworker/u2:0]
root      8937     2  0 11:14 ?        00:00:00 [kworker/u2:2]
nabaolo+  9946     1  0 11:19 ?        00:00:00 /lib/systemd/systemd --user
nabaolo+  9947  9946  0 11:19 ?        00:00:00 (sd-pam)
root     10547  1013  0 11:22 ?        00:00:00 sshd: nabaolong [priv]
nabaolo+ 10641 10547  0 11:22 ?        00:00:00 sshd: nabaolong@pts/0
nabaolo+ 10642 10641  0 11:22 pts/0    00:00:00 -bash
nabaolo+ 10674 10642  0 11:22 pts/0    00:00:00 vim myos2.c
root     11987     2  0 11:30 ?        00:00:00 [kworker/u2:1]
root     11989     2  0 11:30 ?        00:00:00 [kworker/u2:3]
root     12018  1013  0 11:30 ?        00:00:00 sshd: zhangyiming [priv]
zhangyi+ 12032     1  0 11:30 ?        00:00:00 /lib/systemd/systemd --user
zhangyi+ 12033 12032  0 11:30 ?        00:00:00 (sd-pam)
zhangyi+ 12120 12018  0 11:30 ?        00:00:00 sshd: zhangyiming@pts/2
zhangyi+ 12122 12120  0 11:30 pts/2    00:00:00 -bash
zhangyi+ 12361 12122  0 11:32 pts/2    00:00:00 vim /C:/fork/ffork.c
root     12405  1013  0 11:32 ?        00:00:00 sshd: nabaolong [priv]
nabaolo+ 12499 12405  0 11:32 ?        00:00:00 sshd: nabaolong@pts/3
nabaolo+ 12500 12499  0 11:32 pts/3    00:00:00 -bash
nabaolo+ 12946 12500  0 11:35 pts/3    00:00:00 ps -ef
root     15733     1  0 Apr02 tty2     00:00:00 /sbin/agetty -o -p -- \u --noclear tty2 linux
root     17340     2  0 01:15 ?        00:00:00 [kworker/0:1]
root     22819     1  0 Apr03 tty1     00:00:00 /sbin/agetty -o -p -- \u --noclear tty1 linux
root     28757     2  0 02:41 ?        00:00:02 [kworker/0:0]

UID:用户ID

PID:该进程的ID

PPID:进程的父进程ID

C:进程cpu占有率

STIME:进程启动时间

TTY:该进程终端名称

TIME:进程执行时间

CMD:下达的指令

(2) ps -aux

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.5  78484  9484 ?        Ss   Mar26   0:34 /sbin/init
root         2  0.0  0.0      0     0 ?        S    Mar26   0:00 [kthreadd]
root         4  0.0  0.0      0     0 ?        I<   Mar26   0:00 [kworker/0:0H]
root         6  0.0  0.0      0     0 ?        I<   Mar26   0:00 [mm_percpu_wq]
root         7  0.0  0.0      0     0 ?        S    Mar26   0:35 [ksoftirqd/0]
root         8  0.0  0.0      0     0 ?        I    Mar26   1:00 [rcu_sched]
root         9  0.0  0.0      0     0 ?        I    Mar26   0:00 [rcu_bh]
root        10  0.0  0.0      0     0 ?        S    Mar26   0:00 [migration/0]
root        11  0.0  0.0      0     0 ?        S    Mar26   0:01 [watchdog/0]
root        12  0.0  0.0      0     0 ?        S    Mar26   0:00 [cpuhp/0]
root        13  0.0  0.0      0     0 ?        S    Mar26   0:00 [kdevtmpfs]
root        14  0.0  0.0      0     0 ?        I<   Mar26   0:00 [netns]
root        15  0.0  0.0      0     0 ?        S    Mar26   0:00 [rcu_tasks_kthre]
root        16  0.0  0.0      0     0 ?        S    Mar26   0:00 [kauditd]
root        17  0.0  0.0      0     0 ?        S    Mar26   0:00 [khungtaskd]
root        18  0.0  0.0      0     0 ?        S    Mar26   0:00 [oom_reaper]
root        19  0.0  0.0      0     0 ?        I<   Mar26   0:00 [writeback]
root        20  0.0  0.0      0     0 ?        S    Mar26   0:00 [kcompactd0]
root        21  0.0  0.0      0     0 ?        SN   Mar26   0:00 [ksmd]
root        22  0.0  0.0      0     0 ?        SN   Mar26   0:00 [khugepaged]
root        23  0.0  0.0      0     0 ?        I<   Mar26   0:00 [crypto]
root        24  0.0  0.0      0     0 ?        I<   Mar26   0:00 [kintegrityd]
root        25  0.0  0.0      0     0 ?        I<   Mar26   0:00 [kblockd]
root        26  0.0  0.0      0     0 ?        I<   Mar26   0:00 [ata_sff]
root        27  0.0  0.0      0     0 ?        I<   Mar26   0:00 [md]
root        28  0.0  0.0      0     0 ?        I<   Mar26   0:00 [edac-poller]
root        29  0.0  0.0      0     0 ?        I<   Mar26   0:00 [devfreq_wq]
root        30  0.0  0.0      0     0 ?        I<   Mar26   0:00 [watchdogd]
root        34  0.0  0.0      0     0 ?        S    Mar26   0:02 [kswapd0]
root        35  0.0  0.0      0     0 ?        I<   Mar26   0:00 [kworker/u3:0]
root        36  0.0  0.0      0     0 ?        S    Mar26   0:00 [ecryptfs-kthrea]
root        78  0.0  0.0      0     0 ?        I<   Mar26   0:00 [kthrotld]
root        79  0.0  0.0      0     0 ?        I<   Mar26   0:00 [acpi_thermal_pm]
root        80  0.0  0.0      0     0 ?        S    Mar26   0:00 [scsi_eh_0]
root        81  0.0  0.0      0     0 ?        I<   Mar26   0:00 [scsi_tmf_0]
root        82  0.0  0.0      0     0 ?        S    Mar26   0:00 [scsi_eh_1]
root        83  0.0  0.0      0     0 ?        I<   Mar26   0:00 [scsi_tmf_1]
root        89  0.0  0.0      0     0 ?        I<   Mar26   0:00 [ipv6_addrconf]
root        98  0.0  0.0      0     0 ?        I<   Mar26   0:00 [kstrp]
root       115  0.0  0.0      0     0 ?        I<   Mar26   0:00 [charger_manager]
root       156  0.0  0.0      0     0 ?        I<   Mar26   0:10 [kworker/0:1H]
root       169  0.0  0.0      0     0 ?        I<   Mar26   0:00 [ttm_swap]
root       272  0.0  0.0      0     0 ?        I<   Mar26   0:00 [raid5wq]
root       327  0.0  0.0      0     0 ?        S    Mar26   0:14 [jbd2/vda1-8]
root       328  0.0  0.0      0     0 ?        I<   Mar26   0:00 [ext4-rsv-conver]
root       394  0.0  4.2 200704 80032 ?        S<s  Mar26   3:09 /lib/systemd/systemd-journald
root       410  0.0  0.0      0     0 ?        I<   Mar26   0:00 [iscsi_eh]
root       411  0.0  0.0  97708  1680 ?        Ss   Mar26   0:00 /sbin/lvmetad -f
root       421  0.0  0.0      0     0 ?        I<   Mar26   0:00 [ib-comp-wq]
root       422  0.0  0.0      0     0 ?        I<   Mar26   0:00 [ib_mcast]
root       423  0.0  0.0      0     0 ?        I<   Mar26   0:00 [ib_nl_sa_wq]
root       434  0.0  0.0      0     0 ?        I<   Mar26   0:00 [rdma_cm]
root       436  0.0  0.2  45464  3920 ?        Ss   Mar26   0:01 /lib/systemd/systemd-udevd
systemd+   803  0.0  0.2  80040  4636 ?        Ss   Mar26   0:00 /lib/systemd/systemd-networkd
systemd+   825  0.0  0.2  70624  4788 ?        Ss   Mar26   0:11 /lib/systemd/systemd-resolved
root       909  0.0  0.3  70700  6000 ?        Ss   Mar26   0:01 /lib/systemd/systemd-logind
root       918  0.0  0.1 636612  2776 ?        Ssl  Mar26   0:03 /usr/bin/lxcfs /var/lib/lxcfs/
syslog     919  0.0  0.2 263036  4504 ?        Ssl  Mar26   0:40 /usr/sbin/rsyslogd -n
daemon     925  0.0  0.1  28332  2276 ?        Ss   Mar26   0:00 /usr/sbin/atd -f
root       929  0.0  0.8 169784 15056 ?        Ssl  Mar26   0:00 /usr/bin/python3 /usr/bin/networkd-dispatcher --run-sta
root       933  0.0  0.3 287456  5784 ?        Ssl  Mar26   0:45 /usr/lib/accountsservice/accounts-daemon
message+   936  0.0  0.2  50176  4256 ?        Ss   Mar26   0:41 /usr/bin/dbus-daemon --system --address=systemd: --nofo
root       978  0.0  0.1  30664  2968 ?        Ss   Mar26   0:03 /usr/sbin/cron -f
root       994  0.0  0.9 186632 18124 ?        Ssl  Mar26   0:00 /usr/bin/python3 /usr/share/unattended-upgrades/unatten
root      1011  0.0  0.2 288880  4864 ?        Ssl  Mar26   0:22 /usr/lib/policykit-1/polkitd --no-debug
ntp       1012  0.0  0.1 107776  3432 ?        Ssl  Mar26   0:26 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 111:115
root      1013  0.0  0.3  72296  6068 ?        Ss   Mar26   0:59 /usr/sbin/sshd -D
root      1157  0.0  0.1  15300  2132 ttyS0    Ss+  Mar26   0:00 /sbin/agetty -o -p -- \u --keep-baud 115200,38400,9600
root      1159  0.2  1.8 165940 34040 ?        Ssl  Mar26  31:35 /usr/local/qcloud/YunJing/YDEyes/YDService
root      1365  0.0  0.1  98828  3688 ?        Sl   Mar26   0:05 /usr/local/qcloud/stargate/sgagent -d
root      1380  0.0  0.4  54888  8976 ?        S    Mar26   0:07 barad_agent
root      1381  0.0  0.5  57864 11140 ?        S    Mar26   7:40 barad_agent
root      1382  0.2  0.7 508748 14656 ?        Sl   Mar26  37:31 barad_agent
root      1446  0.0  0.2 103744  5172 ?        Ssl  Mar26   1:04 /usr/local/qcloud/YunJing/YDLive/YDLive
root      2550  0.0  0.0      0     0 ?        I    10:37   0:00 [kworker/u2:0]
nabaolo+  9946  0.0  0.4  76616  7548 ?        Ss   11:19   0:00 /lib/systemd/systemd --user
nabaolo+  9947  0.0  0.1 112468  3100 ?        S    11:19   0:00 (sd-pam)
root     10547  0.0  0.3 105684  7180 ?        Ss   11:22   0:00 sshd: nabaolong [priv]
nabaolo+ 10641  0.0  0.2 107984  5396 ?        S    11:22   0:00 sshd: nabaolong@pts/0
nabaolo+ 10642  0.0  0.2  22020  5156 pts/0    Ss   11:22   0:00 -bash
nabaolo+ 10674  0.0  0.4  54896  9312 pts/0    S+   11:22   0:00 vim myos2.c
root     11987  0.0  0.0      0     0 ?        I    11:30   0:00 [kworker/u2:1]
root     11989  0.0  0.0      0     0 ?        I    11:30   0:00 [kworker/u2:3]
root     12405  0.0  0.3 105684  7168 ?        Ss   11:32   0:00 sshd: nabaolong [priv]
nabaolo+ 12499  0.0  0.3 107984  5640 ?        S    11:32   0:00 sshd: nabaolong@pts/3
nabaolo+ 12500  0.0  0.2  22020  5044 pts/3    Ss   11:32   0:00 -bash
root     13167  0.0  0.3 105684  7100 ?        Ss   11:36   0:00 sshd: zhangyiming [priv]
zhangyi+ 13184  0.0  0.4  76616  7592 ?        Ss   11:37   0:00 /lib/systemd/systemd --user
zhangyi+ 13185  0.0  0.1 112468  3100 ?        S    11:37   0:00 (sd-pam)
zhangyi+ 13272  0.0  0.2 107984  5508 ?        S    11:37   0:00 sshd: zhangyiming@pts/1
zhangyi+ 13279  0.0  0.2  22020  5124 pts/1    Ss   11:37   0:00 -bash
zhangyi+ 13355  0.0  0.5  54896  9420 pts/1    S+   11:37   0:00 vim test.c
nabaolo+ 14020  0.0  0.1  39008  3612 pts/3    R+   11:42   0:00 ps -aux
root     15733  0.0  0.1  15524  1952 tty2     Ss+  Apr02   0:00 /sbin/agetty -o -p -- \u --noclear tty2 linux
root     17340  0.0  0.0      0     0 ?        I    01:15   0:00 [kworker/0:1]
root     22819  0.0  0.1  15524  1924 tty1     Ss+  Apr03   0:00 /sbin/agetty -o -p -- \u --noclear tty1 linux
root     28757  0.0  0.0      0     0 ?        I    02:41   0:02 [kworker/0:0]

USER:用户名 
%CPU:进程的占用的CPU百分比 
%MEM:占用的内存的百分比 
VSZ;该进程的使用的虚拟內存量 
RSS:该进程的占用的固定內存量
STAT:该进程的状态 
START/该进程的被触发的启动时间 

 

 

posted @ 2020-04-04 11:45  baolong22  阅读(368)  评论(0编辑  收藏  举报