黑马《linux系统编程》学习笔记(从11到15)

十一. fork复习

十二. ps和kill命令

[root@VM_0_15_centos 5Day]# ls
a.out        execlp.c  homework     orphan.c  test_fork.c  zombie.c
demo_exec.c  fork.c    loop_f       temp      wait.c
execl.c      hello     loop_fork.c  test_f    waitpid.c
[root@VM_0_15_centos 5Day]# ^C
[root@VM_0_15_centos 5Day]# ^C
[root@VM_0_15_centos 5Day]# vim heelo.c

gcc: error: unrecognized command line option ‘-0’
[root@VM_0_15_centos 5Day]# gcc heelo.c -o myhello
[root@VM_0_15_centos 5Day]# ls
a.out        execlp.c  hello     loop_fork.c  temp         wait.c
demo_exec.c  fork.c    homework  myhello      test_f       waitpid.c
execl.c      heelo.c   loop_f    orphan.c     test_fork.c  zombie.c
[root@VM_0_15_centos 5Day]# ./myhello
hello,world
hello,world
hello,world
hello,world
^C
[root@VM_0_15_centos 5Day]# ps
  PID TTY          TIME CMD
11892 pts/0    00:00:00 bash
13306 pts/0    00:00:00 ps



[root@VM_0_15_centos 5Day]# ps a
  PID TTY      STAT   TIME COMMAND
 1046 ttyS0    Ss+    0:00 /sbin/agetty --keep-baud 115200 38400 9600 ttyS0 vt22
 1047 tty1     Ss+    0:00 /sbin/agetty --noclear tty1 linux
11892 pts/0    Ss     0:00 -bash
13354 pts/0    R+     0:00 ps a
[root@VM_0_15_centos 5Day]# ps au
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      1046  0.0  0.0 110088   840 ttyS0    Ss+  Dec14   0:00 /sbin/agetty --
root      1047  0.0  0.0 110088   820 tty1     Ss+  Dec14   0:00 /sbin/agetty --
root     11892  0.0  0.1 116804  3596 pts/0    Ss   21:34   0:00 -bash
root     13388  0.0  0.0 151108  1856 pts/0    R+   21:47   0:00 ps au
[root@VM_0_15_centos 5Day]# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1  43252  3032 ?        Ss   Dec14   0:53 /usr/lib/system
root         2  0.0  0.0      0     0 ?        S    Dec14   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        S    Dec14   0:09 [ksoftirqd/0]
root         5  0.0  0.0      0     0 ?        S<   Dec14   0:00 [kworker/0:0H]
root         7  0.0  0.0      0     0 ?        S    Dec14   0:00 [migration/0]
root         8  0.0  0.0      0     0 ?        S    Dec14   0:00 [rcu_bh]
root         9  0.0  0.0      0     0 ?        R    Dec14   0:56 [rcu_sched]
root        10  0.0  0.0      0     0 ?        S    Dec14   0:05 [watchdog/0]
root        12  0.0  0.0      0     0 ?        S    Dec14   0:00 [kdevtmpfs]
root        13  0.0  0.0      0     0 ?        S<   Dec14   0:00 [netns]
root        14  0.0  0.0      0     0 ?        S    Dec14   0:00 [khungtaskd]
root        15  0.0  0.0      0     0 ?        S<   Dec14   0:00 [writeback]
root        16  0.0  0.0      0     0 ?        S<   Dec14   0:00 [kintegrityd]
root        17  0.0  0.0      0     0 ?        S<   Dec14   0:00 [bioset]
root        18  0.0  0.0      0     0 ?        S<   Dec14   0:00 [kblockd]
root        19  0.0  0.0      0     0 ?        S<   Dec14   0:00 [md]
root        25  0.0  0.0      0     0 ?        S    Dec14   0:02 [kswapd0]
root        26  0.0  0.0      0     0 ?        SN   Dec14   0:00 [ksmd]
root        27  0.0  0.0      0     0 ?        SN   Dec14   0:02 [khugepaged]
root        28  0.0  0.0      0     0 ?        S<   Dec14   0:00 [crypto]
root        36  0.0  0.0      0     0 ?        S<   Dec14   0:00 [kthrotld]
root        38  0.0  0.0      0     0 ?        S<   Dec14   0:00 [kmpath_rdacd]
root        39  0.0  0.0      0     0 ?        S<   Dec14   0:00 [kpsmoused]
root        40  0.0  0.0      0     0 ?        S<   Dec14   0:00 [ipv6_addrconf]
root        59  0.0  0.0      0     0 ?        S<   Dec14   0:00 [deferwq]
root        98  0.0  0.0      0     0 ?        S    Dec14   0:02 [kauditd]
root       226  0.0  0.0      0     0 ?        S<   Dec14   0:00 [ata_sff]
root       230  0.0  0.0      0     0 ?        S    Dec14   0:00 [scsi_eh_0]
root       232  0.0  0.0      0     0 ?        S<   Dec14   0:00 [scsi_tmf_0]
root       235  0.0  0.0      0     0 ?        S    Dec14   0:00 [scsi_eh_1]
root       237  0.0  0.0      0     0 ?        S<   Dec14   0:00 [scsi_tmf_1]
root       241  0.0  0.0      0     0 ?        S<   Dec14   0:00 [ttm_swap]
root       258  0.0  0.0      0     0 ?        S    Dec14   0:21 [jbd2/vda1-8]
root       259  0.0  0.0      0     0 ?        S<   Dec14   0:00 [ext4-rsv-conve
root       333  0.0  4.6 139536 88236 ?        Ss   Dec14   1:17 /usr/lib/system
root       355  0.0  0.0 118916  1052 ?        Ss   Dec14   0:00 /usr/sbin/lvmet
root       359  0.0  0.0      0     0 ?        S<   Dec14   0:10 [kworker/0:1H]
root       366  0.0  0.0  44084  1672 ?        Ss   Dec14   0:00 /usr/lib/system
root       450  0.0  0.0      0     0 ?        S<   Dec14   0:00 [edac-poller]
root       486  0.0  0.0  55496  1076 ?        S<sl Dec14   0:15 /sbin/auditd
polkitd    511  0.0  0.5 536252 10576 ?        Ssl  Dec14   0:20 /usr/lib/polkit
root       513  0.0  2.8 734860 53224 ?        Ssl  Dec14   1:11 /usr/sbin/rsysl
root       517  0.0  0.0  24248  1584 ?        Ss   Dec14   0:30 /usr/lib/system
libstor+   518  0.0  0.0   8576   764 ?        Ss   Dec14   0:01 /usr/bin/lsmd -
dbus       519  0.0  0.0  26536  1660 ?        Ss   Dec14   0:55 /bin/dbus-daemo
root       521  0.0  0.0   4384   492 ?        Ss   Dec14   0:00 /usr/sbin/acpid
ntp        542  0.0  0.1  47252  2216 ?        Ss   Dec14   0:00 /usr/sbin/ntpd
root       676  0.0  0.0 126280  1600 ?        Ss   Dec14   0:05 /usr/sbin/crond
root       678  0.0  0.0  25900   940 ?        Ss   Dec14   0:00 /usr/sbin/atd -
root       818  0.0  0.6 113412 12588 ?        Ss   Dec14   0:00 /sbin/dhclient
root       898  0.0  0.2 106040  4140 ?        Ss   Dec14   0:15 /usr/sbin/sshd
root       900  0.0  0.7 562432 13264 ?        Ssl  Dec14   1:40 /usr/bin/python
mysql      977  0.0  0.0 113308  1520 ?        Ss   Dec14   0:00 /bin/sh /usr/bi
root      1046  0.0  0.0 110088   840 ttyS0    Ss+  Dec14   0:00 /sbin/agetty --
root      1047  0.0  0.0 110088   820 tty1     Ss+  Dec14   0:00 /sbin/agetty --
mysql     1257  0.0  6.2 961288 117588 ?       Sl   Dec14   5:23 /usr/sbin/mysql
root      1407  0.0  0.0  97336  1716 ?        Sl   Dec14   0:07 /usr/local/qclo
root      1419  0.0  0.3 155260  7504 ?        S    Dec14   0:14 barad_agent
root      1425  0.0  0.4 158160  9028 ?        S    Dec14   9:40 barad_agent
root      1426  0.3  0.5 610308 11184 ?        Sl   Dec14  48:24 barad_agent
root      1470  0.0  0.1 100988  2000 ?        Ssl  Dec14   2:19 /usr/local/qclo
root     11865  0.0  0.2 147832  5260 ?        Ss   21:34   0:00 sshd: root@pts/
root     11892  0.0  0.1 116804  3596 pts/0    Ss   21:34   0:00 -bash
root     12084  0.0  0.0      0     0 ?        S    21:36   0:00 [kworker/0:1]
root     12437  0.0  0.0      0     0 ?        S    09:50   0:01 [kworker/u2:1]
root     12515  0.0  0.0      0     0 ?        S    21:41   0:00 [kworker/0:2]
root     13182  0.0  0.0      0     0 ?        R    21:46   0:00 [kworker/0:0]
root     13487  0.0  0.2 106040  4088 ?        Ss   21:48   0:00 sshd: [accepted
sshd     13488  0.0  0.1 106040  1924 ?        S    21:48   0:00 sshd: [net]
root     13489  0.0  0.2 145628  4804 ?        Ss   21:48   0:00 sshd: root [pri
sshd     13490  0.0  0.1 106040  2180 ?        S    21:48   0:00 sshd: root [net
root     13491  0.0  0.0 151108  1852 pts/0    R+   21:48   0:00 ps aux
root     16041  0.0  0.0      0     0 ?        S    04:19   0:00 [kworker/u2:2]
root     26009  0.1  0.4 571352  8156 ?        Ssl  06:08   1:44 /usr/local/qclo


//这里用了管道来进行查询
[root@VM_0_15_centos 5Day]# ps aux | grep myhello
root     14264  0.0  0.0 112708   976 pts/0    R+   21:54   0:00 grep --color=auto myhello

kill命令

[root@VM_0_15_centos 5Day]# kill -l
 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP
 6) SIGABRT      7) SIGBUS       8) SIGFPE       9) SIGKILL     10) SIGUSR1
11) SIGSEGV     12) SIGUSR2     13) SIGPIPE     14) SIGALRM     15) SIGTERM
16) SIGSTKFLT   17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO       30) SIGPWR
31) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX
[root@VM_0_15_centos 5Day]# kill -9
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]

kill -9 强杀进程

十三. 进程间数据共享

这里当我们修改父进程的num时,会额外在物理内存中,给父进程的num提供一块区域

 接下来,如果我们在子进程的num里也进行修改,那么也会为子进程的num,在物理内存里提供num

 也因此各个进程里的用户区的数据,在fork后,就完全独立了。

总结起来,就是“读时共享,写时复制”。

 十四. 验证进程间是否共享全局变量

先写了一个loop_fork.c的程序

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>

int counter = 100;

int main(int argc, const char* argv[])
{
    pid_t pid;

    int i=0;
    for(i=0; i<3; i++)
    {
        pid = fork();
        if(pid == 0)
        {
            break;
        }
    }

    // 父进程
    if(i == 3)
    {
        counter += 100;
        printf("parent process, pid = %d, ppid = %d, %d\n", getpid(), getppid(), counter);
        //        sleep(1);
    }
    // 子进程
    else if(i == 0)
    {
        // 1th
        counter += 200;
        printf("child process, pid = %d, ppid = %d, %d\n", getpid(), getppid(), counter);
    }
    else if(i == 1)
    {
        // 2th
        counter += 300;
        printf("child process, pid = %d, ppid = %d, %d\n", getpid(), getppid(), counter);
    }
    else if(i == 2)
    {
        // 3th
        counter += 400;
        printf("child process, pid = %d, ppid = %d, %d\n", getpid(), getppid(), counter);
    }


    return 0;
}

运行结果如下: 

[root@VM_0_15_centos 5Day]# ls
a.out        execlp.c  hello     loop_fork.c  temp         wait.c
demo_exec.c  fork.c    homework  myhello      test_f       waitpid.c
execl.c      heelo.c   loop_f    orphan.c     test_fork.c  zombie.c
[root@VM_0_15_centos 5Day]# ./loop_f
parent process, pid = 22533, ppid = 22447, 200
[root@VM_0_15_centos 5Day]# child process, pid = 22534, ppid = 1, 300
child process, pid = 22535, ppid = 1, 400
child process, pid = 22536, ppid = 1, 500

这里说明了,父子进程之间,并不能使用全局变量进行通信

十五. exec函数族功能介绍

 

exec族的作用 

 

猜你喜欢

转载自blog.csdn.net/garrulousabyss/article/details/85220174