• system函数实践1:system函数进程的爸爸是谁?


    前言

    有两个问题:

    system函数中修改了环境变量,会不会影响执行它的进程?

           不会。

            首先,这个我测过了,因为我原本想用它来传递参数。

            为什么?因为system函数执行的时候,会先创建一个shell,然后再这个shell中执行命令,在一个正在shell中修改环境变量,会影响另一个正在运行的shell的环境变量吗?不会。

    system函数执行命令实际上是创建一个进程,那么他的父进程是谁?

    这个要分两种情况来考虑。下面使用实例解答。

    一 简介

    1. #include
    2. int system(const char *command);

    函数描述: 

    DESCRIPTION
           The system() library function uses fork(2) to create a child process that executes the shell command specified in command using execl(3) as follows:

               execl("/bin/sh", "sh", "-c", command, (char *) 0);

           system() returns after the command has been completed.

           During  execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored, in the process that calls system() (these signals will be handled according to their defaults inside the child
           process that executes command).

           If command is NULL, then system() returns a status indicating whether a shell is available on the system

    返回值:

     RETURN VALUE
           The return value of system() is one of the following:

           *  If command is NULL, then a nonzero value if a shell is available, or 0 if no shell is available.

           *  If a child process could not be created, or its status could not be retrieved, the return value is -1.

           *  If a shell could not be executed in the child process, then the return value is as though the child shell terminated by calling _exit(2) with the status 127.

           *  If all system calls succeed, then the return value is the termination status of the child shell used to execute command.  (The termination status of a shell is the termination status of the last command  it executes.)

           In the last two cases, the return value is a "wait status" that can be examined using the macros described in waitpid(2).  (i.e., WIFEXITED() WEXITSTATUS() and so on).

           system() does not affect the wait status of any other children.

    二 实践 :

    实践1:让system函数进程成为systemd的子进程

    1. #include
    2. #include
    3. #include
    4. #include
    5. int main(int argc,char *argv[])
    6. {
    7. printf("pid=%d\n",getpid());
    8. system("sleep 100&");
    9. printf("ok\n");
    10. sleep(100);
    11. return 0;
    12. }

    测试:在后台执行a.out,进程号是64064,进程树导出到ps.txt文件

    1. csdn@ubuntu:~$ ./a.out &
    2. [1] 64064
    3. csdn@ubuntu:~$ pid=64064
    4. ok
    5. csdn@ubuntu:~$ pstree -p > ps.txt
    6. csdn@ubuntu:~$

     将进程树精简一下:如下所示system("sleep 100&");的进程号是64066,为什么不是64065,因为system函数的执行过程是,创建一个shell,然后再这个新shell中执行sleep。

    未解?   

        这中间都发生了什么?它为什么会在a..out活着的时候,变成了别人的儿子?是道德的沦丧还是人性的扭曲?亦或是妈妈的改嫁?它为什么不是systemd的孙子?

            有一种相对较合理的解释,那就是sleep是在后台执行的。它的附近是sh,sh执行它时,sleep是后台执行,所示sh无需等他结束,sh自己结束了,但是sleep的父进程是sh,sleep没有父进程了,linux是不允许没有父进程的子进程的。所以sleep被systemd接管了。所以sleep变成了systemd的子进程。

    1. csdn@ubuntu:~$ grep -A2 -E "grep|systemd|sleep|a.out" ps.txt
    2. systemd(1)-+-NetworkManager(949)-+-dnsmasq(1117)
    3. | |-{gdbus}(1014)
    4. | `-{gmain}(1006)
    5. --
    6. |-sleep(64066)
    7. |-smbd(1183)-+-smbd(1185)
    8. | `-smbd(1193)
    9. --
    10. | `-sshd(60610)---sshd(60639)---bash(60640)---su(62226)---bash(62227)-+-a.out(64064)
    11. | `-pstree(64074)
    12. |-systemd(2415)---(sd-pam)(2416)
    13. |-systemd-journal(385)
    14. |-systemd-logind(967)
    15. |-systemd-timesyn(671)---{sd-resolve}(684)
    16. |-systemd-udevd(411)
    17. |-udisksd(2841)-+-{cleanup}(2846)
    18. | |-{gdbus}(2844)
    19. csdn@ubuntu:~$

    重新执行a.out&,然后使用 ps -l,这可以直接看到父进程的ID:如下所示,sleep进程的父进程ID是1。就是systemd。

    1. csdn@ubuntu:~$ ps -l
    2. F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
    3. 4 S 1004 62227 62226 0 80 0 - 6232 wait pts/20 00:00:00 bash
    4. 0 S 1004 64102 62227 0 80 0 - 1088 hrtime pts/20 00:00:00 a.out
    5. 0 S 1004 64104 1 0 80 0 - 2257 hrtime pts/20 00:00:00 sleep
    6. 0 R 1004 64105 62227 0 80 0 - 7664 - pts/20 00:00:00 ps
    7. csdn@ubuntu:~$

    实践 2:让system函数进程成为执行者的孙子进程

    就是将system函数参数字符串后面的&去掉,修改前面的代码:

    1. //system("sleep 100&");
    2. system("sleep 100");

    然后重新编译:运行,测试进程的PID是64129,sleep的父进程是sh,sh的父进程是a.out。

    1. csdn@ubuntu:~$ ./a.out &
    2. [1] 64129
    3. csdn@ubuntu:~$ pid=64129
    4. csdn@ubuntu:~$ ps -l
    5. F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
    6. 4 S 1004 62227 62226 0 80 0 - 6232 wait pts/20 00:00:00 bash
    7. 0 S 1004 64129 62227 0 80 0 - 1088 wait pts/20 00:00:00 a.out
    8. 0 S 1004 64130 64129 0 80 0 - 1126 wait pts/20 00:00:00 sh
    9. 0 S 1004 64131 64130 0 80 0 - 2257 hrtime pts/20 00:00:00 sleep
    10. 0 R 1004 64132 62227 0 80 0 - 7664 - pts/20 00:00:00 ps
    11. csdn@ubuntu:~$

    查看进程树:

    1. csdn@ubuntu:~$ pstree -p > ps.txt
    2. csdn@ubuntu:~$
    1. systemd(1)-+-NetworkManager(949)-+-dnsmasq(1117)
    2. | |-{gdbus}(1014)
    3. | `-{gmain}(1006)
    4. --
    5. | `-sshd(60610)---sshd(60639)---bash(60640)---su(62226)---bash(62227)-+-a.out(64129)---sh(64130)---sleep(6413+
    6. | `-pstree(64140)
    7. |-systemd(2415)---(sd-pam)(2416)
    8. |-systemd-journal(385)
    9. |-systemd-logind(967)
    10. |-systemd-timesyn(671)---{sd-resolve}(684)
    11. |-systemd-udevd(411)
    12. |-udisksd(2841)-+-{cleanup}(2846)
    13. | |-{gdbus}(2844)
    14. csdn@ubuntu:~$

    进程树截图。 

     小结

  • 相关阅读:
    VAEGAN:理解 VAE 与 GAN【图像生成】
    创新与合规共舞 百望云铸就未来档案数字化管理之路
    【Java】已解决java.nio.channels.FileLockInterruptionException异常
    JavaEE——传输层_UDP
    RPC项目解析(1)
    《七月集训》第十三日——双向链表
    【Opencv入门到项目实战】(四):图像梯度计算|Sobel算子|Scharr算子|Laplacian算子
    springMVC入门
    十四、InnoDB的ACID事务
    《rust学习一》 fleet 配置rust环境
  • 原文地址:https://blog.csdn.net/yueni_zhao/article/details/128061192