进程控制(中)

文章目录

    • [4. 进程程序替换](#4. 进程程序替换)
c 复制代码
[gsm@VM-4-3-centos lesson15]$ ll
total 8
-rw-rw-r-- 1 gsm gsm   80 Nov 22 11:56 Makefile
-rw-rw-r-- 1 gsm gsm 1657 Nov 23 20:14 process.cc
[gsm@VM-4-3-centos lesson15]$ vim process.cc 
[gsm@VM-4-3-centos lesson15]$ cat process.cc 
#include <iostream>
#include <string>
#include <cstdio>
#include <string.h>
#include <errno.h>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int fun()
{
    std::cout << "hello world" << std::endl;

    _exit(23);
    //return 100;
    //exit(100);
}

int main()
{
   pid_t id = fork(); 

   if (id < 0)
   {
       printf("errno : %d, errstring : %s\n", errno, strerror(errno));
       return errno;
   }
   else if (id == 0)
   {
       int cnt = 5;

       while (cnt)
       {
           printf("子进程运行中, pid: %d\n", getpid());
           cnt--;
           sleep(1);
           int a = 1/0;
       }

       exit(123);
   }
   else
   {
       sleep(6);
       //pid_t rid = wait(nullptr);
       int status = 0;
       pid_t rid = waitpid(id, &status, 0); // == wait

       if (rid > 0)
       {
           printf("wait sub process success, rid: %d, status code: %d\n", rid, (status >> 8) & 0xFF);
       }
       else
       {
           perror("waitpid");
       }

       while (true)
       {
           printf("我是父进程: pid: %d\n", getpid());
           sleep(1);
       }
   }

    return 0;
}
[gsm@VM-4-3-centos lesson15]$ make
g++ -o process process.cc -std=c++11
process.cc: In function 'int main()':
process.cc:38:22: warning: division by zero [-Wdiv-by-zero]
            int a = 1/0;
                      ^
[gsm@VM-4-3-centos lesson15]$ ./process 
子进程运行中, pid: 15997
wait sub process success, rid: 15997, status code: 0
我是父进程: pid: 15996
我是父进程: pid: 15996
我是父进程: pid: 15996
^C
c 复制代码
[gsm@VM-4-3-centos ~]$ while :; do ps ajx | head -1 && ps ajx | grep process; sleep 1; done
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
27065 15988 15987 27065 pts/1    15987 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 15996 15996 23159 pts/0    15996 S+    1001   0:00 ./process
15996 15997 15996 23159 pts/0    15996 S+    1001   0:00 ./process
27065 16001 16000 27065 pts/1    16000 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 15996 15996 23159 pts/0    15996 S+    1001   0:00 ./process
15996 15997 15996 23159 pts/0    15996 Z+    1001   0:00 [process] <defunct>
27065 16007 16006 27065 pts/1    16006 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 15996 15996 23159 pts/0    15996 S+    1001   0:00 ./process
15996 15997 15996 23159 pts/0    15996 Z+    1001   0:00 [process] <defunct>
27065 16012 16011 27065 pts/1    16011 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 15996 15996 23159 pts/0    15996 S+    1001   0:00 ./process
15996 15997 15996 23159 pts/0    15996 Z+    1001   0:00 [process] <defunct>
27065 16018 16017 27065 pts/1    16017 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 15996 15996 23159 pts/0    15996 S+    1001   0:00 ./process
15996 15997 15996 23159 pts/0    15996 Z+    1001   0:00 [process] <defunct>
27065 16026 16025 27065 pts/1    16025 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 15996 15996 23159 pts/0    15996 S+    1001   0:00 ./process
15996 15997 15996 23159 pts/0    15996 Z+    1001   0:00 [process] <defunct>
27065 16037 16036 27065 pts/1    16036 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 15996 15996 23159 pts/0    15996 S+    1001   0:00 ./process
27065 16058 16057 27065 pts/1    16057 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 15996 15996 23159 pts/0    15996 S+    1001   0:00 ./process
27065 16067 16066 27065 pts/1    16066 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 15996 15996 23159 pts/0    15996 S+    1001   0:00 ./process
27065 16074 16073 27065 pts/1    16073 S+    1001   0:00 grep --color=auto process
^C

c 复制代码
[gsm@VM-4-3-centos lesson15]$ ll
total 24
-rw-rw-r-- 1 gsm gsm    80 Nov 22 11:56 Makefile
-rwxrwxr-x 1 gsm gsm 13656 Nov 23 20:17 process
-rw-rw-r-- 1 gsm gsm  1665 Nov 23 20:16 process.cc
[gsm@VM-4-3-centos lesson15]$ vim process.cc
[gsm@VM-4-3-centos lesson15]$ head -67 process.cc
#include <iostream>
#include <string>
#include <cstdio>
#include <string.h>
#include <errno.h>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int fun()
{
    std::cout << "hello world" << std::endl;

    _exit(23);
    //return 100;
    //exit(100);
}

int main()
{
    char* p = nullptr;
    *p = 'a';
[gsm@VM-4-3-centos lesson15]$ make
g++ -o process process.cc -std=c++11
[gsm@VM-4-3-centos lesson15]$ ./process 
Segmentation fault

c 复制代码
[gsm@VM-4-3-centos lesson15]$ vim process.cc
[gsm@VM-4-3-centos lesson15]$ head -70 process.cc
#include <iostream>
#include <string>
#include <cstdio>
#include <string.h>
#include <errno.h>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int fun()
{
    std::cout << "hello world" << std::endl;

    _exit(23);
    //return 100;
    //exit(100);
}

int main()
{
    while (true)
    {
        printf("hello world\n");
        sleep(1);
    }
[gsm@VM-4-3-centos lesson15]$ make
g++ -o process process.cc -std=c++11
[gsm@VM-4-3-centos lesson15]$ ./process 
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
Segmentation fault
[gsm@VM-4-3-centos lesson15]$ ./process 
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
Floating point exception
c 复制代码
[gsm@VM-4-3-centos ~]$ 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	
[gsm@VM-4-3-centos ~]$ ps ajx | grep process
23159 18683 18683 23159 pts/0    18683 S+    1001   0:00 ./process
27065 18696 18695 27065 pts/1    18695 S+    1001   0:00 grep --color=auto process
[gsm@VM-4-3-centos ~]$ kill -11 18683
[gsm@VM-4-3-centos ~]$ ps ajx | grep process
23159 19443 19443 23159 pts/0    19443 S+    1001   0:00 ./process
27065 19450 19449 27065 pts/1    19449 S+    1001   0:00 grep --color=auto process
[gsm@VM-4-3-centos ~]$ kill -8 19443

c 复制代码
[gsm@VM-4-3-centos lesson15]$ ll
total 20
-rw-rw-r-- 1 gsm gsm   80 Nov 22 11:56 Makefile
-rwxrwxr-x 1 gsm gsm 9208 Nov 23 20:29 process
-rw-rw-r-- 1 gsm gsm 1862 Nov 23 20:29 process.cc
[gsm@VM-4-3-centos lesson15]$ vim process.cc
[gsm@VM-4-3-centos lesson15]$ head -71 process.cc
#include <iostream>
#include <string>
#include <cstdio>
#include <string.h>
#include <errno.h>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int fun()
{
    std::cout << "hello world" << std::endl;

    _exit(23);
    //return 100;
    //exit(100);
}

int main()
{
    while (true)
    {
        int a = 1 / 0;
        printf("hello world\n");
        sleep(1);
    }
[gsm@VM-4-3-centos lesson15]$ make
g++ -o process process.cc -std=c++11
process.cc: In function 'int main()':
process.cc:68:21: warning: division by zero [-Wdiv-by-zero]
         int a = 1 / 0;
                     ^
[gsm@VM-4-3-centos lesson15]$ ./process 
Floating point exception

c 复制代码
[gsm@VM-4-3-centos lesson15]$ ll
total 20
-rw-rw-r-- 1 gsm gsm   80 Nov 22 11:56 Makefile
-rwxrwxr-x 1 gsm gsm 9208 Nov 23 20:37 process
-rw-rw-r-- 1 gsm gsm 1885 Nov 23 20:37 process.cc
[gsm@VM-4-3-centos lesson15]$ vim process.cc
[gsm@VM-4-3-centos lesson15]$ head -63 process.cc
#include <iostream>
#include <string>
#include <cstdio>
#include <string.h>
#include <errno.h>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int fun()
{
    std::cout << "hello world" << std::endl;

    _exit(23);
    //return 100;
    //exit(100);
}

int main()
{
   pid_t id = fork(); 

   if (id < 0)
   {
       printf("errno : %d, errstring : %s\n", errno, strerror(errno));
       return errno;
   }
   else if (id == 0)
   {
       int cnt = 5;

       while (cnt)
       {
           printf("子进程运行中, pid: %d\n", getpid());
           cnt--;
           sleep(1);
       }

       exit(123);
   }
   else
   {
       sleep(6);
       //pid_t rid = wait(nullptr);
       int status = 0;
       pid_t rid = waitpid(id, &status, 0); // == wait

       if (rid > 0)
       {
           printf("wait sub process success, rid: %d, status code: %d, exit signal: %d\n", rid, (status >> 8) & 0xFF, status & 0x7F);
       }
       else
       {
           perror("waitpid");
       }

       while (true)
       {
           printf("我是父进程: pid: %d\n", getpid());
           sleep(1);
       }
   }
[gsm@VM-4-3-centos lesson15]$ make
g++ -o process process.cc -std=c++11
[gsm@VM-4-3-centos lesson15]$ ./process 
子进程运行中, pid: 24150
子进程运行中, pid: 24150
子进程运行中, pid: 24150
子进程运行中, pid: 24150
子进程运行中, pid: 24150
wait sub process success, rid: 24150, status code: 123, exit signal: 0
我是父进程: pid: 24149
我是父进程: pid: 24149
我是父进程: pid: 24149
^C

c 复制代码
[gsm@VM-4-3-centos lesson15]$ ll
total 24
-rw-rw-r-- 1 gsm gsm    80 Nov 22 11:56 Makefile
-rwxrwxr-x 1 gsm gsm 13656 Nov 23 23:33 process
-rw-rw-r-- 1 gsm gsm  1834 Nov 23 23:33 process.cc
[gsm@VM-4-3-centos lesson15]$ vim process.cc
[gsm@VM-4-3-centos lesson15]$ head -66 process.cc
#include <iostream>
#include <string>
#include <cstdio>
#include <string.h>
#include <errno.h>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int fun()
{
    std::cout << "hello world" << std::endl;

    _exit(23);
    //return 100;
    //exit(100);
}

int main()
{
   pid_t id = fork(); 

   if (id < 0)
   {
       printf("errno : %d, errstring : %s\n", errno, strerror(errno));
       return errno;
   }
   else if (id == 0)
   {
       int cnt = 5;

       while (cnt)
       {
           printf("子进程运行中, pid: %d\n", getpid());
           cnt--;
           sleep(1);
       }

       int* p = nullptr;
       *p = 100;

       exit(123);
   }
   else
   {
       sleep(6);
       //pid_t rid = wait(nullptr);
       int status = 0;
       pid_t rid = waitpid(id, &status, 0); // == wait

       if (rid > 0)
       {
           printf("wait sub process success, rid: %d, status code: %d, exit signal: %d\n", rid, (status >> 8) & 0xFF, status & 0x7F);
       }
       else
       {
           perror("waitpid");
       }

       while (true)
       {
           printf("我是父进程: pid: %d\n", getpid());
           sleep(1);
       }
   }
[gsm@VM-4-3-centos lesson15]$ make
g++ -o process process.cc -std=c++11
[gsm@VM-4-3-centos lesson15]$ ./process 
子进程运行中, pid: 24886
子进程运行中, pid: 24886
子进程运行中, pid: 24886
子进程运行中, pid: 24886
子进程运行中, pid: 24886
wait sub process success, rid: 24886, status code: 0, exit signal: 11
我是父进程: pid: 24885
^C
[gsm@VM-4-3-centos lesson15]$ 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	

c 复制代码
[gsm@VM-4-3-centos lesson15]$ ll
total 24
-rw-rw-r-- 1 gsm gsm    80 Nov 22 11:56 Makefile
-rwxrwxr-x 1 gsm gsm 13656 Nov 23 23:36 process
-rw-rw-r-- 1 gsm gsm  1877 Nov 23 23:36 process.cc
[gsm@VM-4-3-centos lesson15]$ vim process.cc
[gsm@VM-4-3-centos lesson15]$ head -65 process.cc
#include <iostream>
#include <string>
#include <cstdio>
#include <string.h>
#include <errno.h>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int fun()
{
    std::cout << "hello world" << std::endl;

    _exit(23);
    //return 100;
    //exit(100);
}

int main()
{
   pid_t id = fork(); 

   if (id < 0)
   {
       printf("errno : %d, errstring : %s\n", errno, strerror(errno));
       return errno;
   }
   else if (id == 0)
   {
       int cnt = 5;

       while (true)
       {
           printf("子进程运行中, pid: %d\n", getpid());
           cnt--;
           sleep(1);
       }

       //int* p = nullptr;
       //*p = 100;

       exit(123);
   }
   else
   {
       //pid_t rid = wait(nullptr);
       int status = 0;
       pid_t rid = waitpid(id, &status, 0); // == wait

       if (rid > 0)
       {
           printf("wait sub process success, rid: %d, status code: %d, exit signal: %d\n", rid, (status >> 8) & 0xFF, status & 0x7F);
       }
       else
       {
           perror("waitpid");
       }

       while (true)
       {
           printf("我是父进程: pid: %d\n", getpid());
           sleep(1);
       }
   }
[gsm@VM-4-3-centos lesson15]$ make
g++ -o process process.cc -std=c++11
[gsm@VM-4-3-centos lesson15]$ ./process 
子进程运行中, pid: 26138
子进程运行中, pid: 26138
子进程运行中, pid: 26138
子进程运行中, pid: 26138
子进程运行中, pid: 26138
子进程运行中, pid: 26138
子进程运行中, pid: 26138
子进程运行中, pid: 26138
子进程运行中, pid: 26138
子进程运行中, pid: 26138
子进程运行中, pid: 26138
子进程运行中, pid: 26138
子进程运行中, pid: 26138
子进程运行中, pid: 26138
子进程运行中, pid: 26138
子进程运行中, pid: 26138
wait sub process success, rid: 26138, status code: 0, exit signal: 9
我是父进程: pid: 26137
我是父进程: pid: 26137
我是父进程: pid: 26137
^C
c 复制代码
[gsm@VM-4-3-centos ~]$ ps ajx | grep process
23159 26137 26137 23159 pts/0    26137 S+    1001   0:00 ./process
26137 26138 26137 23159 pts/0    26137 S+    1001   0:00 ./process
27065 26148 26147 27065 pts/1    26147 S+    1001   0:00 grep --color=auto process
[gsm@VM-4-3-centos ~]$ kill -9 26138

WIFEXITED(status): 若为正常终止子进程返回的状态,则为真。(查看进程是否是正常退出)
WEXITSTATUS(status): 若WIFEXITED非零,提取子进程退出码。(查看进程的退出码)

c 复制代码
[gsm@VM-4-3-centos lesson15]$ ll
total 24
-rw-rw-r-- 1 gsm gsm    80 Nov 22 11:56 Makefile
-rwxrwxr-x 1 gsm gsm 13624 Nov 23 23:39 process
-rw-rw-r-- 1 gsm gsm  1865 Nov 23 23:39 process.cc
[gsm@VM-4-3-centos lesson15]$ vim process.cc
[gsm@VM-4-3-centos lesson15]$ head -72 process.cc
#include <iostream>
#include <string>
#include <cstdio>
#include <string.h>
#include <errno.h>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int fun()
{
    std::cout << "hello world" << std::endl;

    _exit(23);
    //return 100;
    //exit(100);
}

int main()
{
   pid_t id = fork(); 

   if (id < 0)
   {
       printf("errno : %d, errstring : %s\n", errno, strerror(errno));
       return errno;
   }
   else if (id == 0)
   {
       int cnt = 5;

       while (true)
       {
           printf("子进程运行中, pid: %d\n", getpid());
           cnt--;
           sleep(1);
       }

       //int* p = nullptr;
       //*p = 100;

       exit(123);
   }
   else
   {
       //pid_t rid = wait(nullptr);
       int status = 0;
       pid_t rid = waitpid(id, &status, 0); // == wait

       if (rid > 0)
       {
           if (WIFEXITED(status))
           {
               printf("wait sub process success, rid: %d, status code: %d\n", rid, WEXITSTATUS(status));
           }
           else
           {
               printf("child process quit error!\n");
           }
       }
       else
       {
           perror("waitpid");
       }

       while (true)
       {
           printf("我是父进程: pid: %d\n", getpid());
           sleep(1);
       }
   }
[gsm@VM-4-3-centos lesson15]$ make
g++ -o process process.cc -std=c++11
[gsm@VM-4-3-centos lesson15]$ ./process 
子进程运行中, pid: 29862
子进程运行中, pid: 29862
子进程运行中, pid: 29862
子进程运行中, pid: 29862
子进程运行中, pid: 29862
子进程运行中, pid: 29862
子进程运行中, pid: 29862
子进程运行中, pid: 29862
子进程运行中, pid: 29862
子进程运行中, pid: 29862
子进程运行中, pid: 29862
child process quit error!
我是父进程: pid: 29861
我是父进程: pid: 29861
我是父进程: pid: 29861
^C
c 复制代码
[gsm@VM-4-3-centos ~]$ ps ajx | grep process
23159 29861 29861 23159 pts/0    29861 S+    1001   0:00 ./process
29861 29862 29861 23159 pts/0    29861 S+    1001   0:00 ./process
27065 29868 29867 27065 pts/1    29867 S+    1001   0:00 grep --color=auto process
[gsm@VM-4-3-centos ~]$ kill -8 29862

c 复制代码
[gsm@VM-4-3-centos lesson15]$ ll
total 24
-rw-rw-r-- 1 gsm gsm    80 Nov 22 11:56 Makefile
-rwxrwxr-x 1 gsm gsm 13680 Nov 23 23:58 process
-rw-rw-r-- 1 gsm gsm  1992 Nov 23 23:57 process.cc
[gsm@VM-4-3-centos lesson15]$ vim process.cc
[gsm@VM-4-3-centos lesson15]$ head -72 process.cc
#include <iostream>
#include <string>
#include <cstdio>
#include <string.h>
#include <errno.h>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int fun()
{
    std::cout << "hello world" << std::endl;

    _exit(23);
    //return 100;
    //exit(100);
}

int main()
{
   pid_t id = fork(); 

   if (id < 0)
   {
       printf("errno : %d, errstring : %s\n", errno, strerror(errno));
       return errno;
   }
   else if (id == 0)
   {
       int cnt = 5;

       while (cnt)
       {
           printf("子进程运行中, pid: %d\n", getpid());
           cnt--;
           sleep(1);
       }

       //int* p = nullptr;
       //*p = 100;

       exit(123);
   }
   else
   {
       //pid_t rid = wait(nullptr);
       int status = 0;
       pid_t rid = waitpid(id, &status, 0); // == wait

       if (rid > 0)
       {
           if (WIFEXITED(status))
           {
               printf("wait sub process success, rid: %d, status code: %d\n", rid, WEXITSTATUS(status));
           }
           else
           {
               printf("child process quit error!\n");
           }
       }
       else
       {
           perror("waitpid");
       }

       while (true)
       {
           printf("我是父进程: pid: %d\n", getpid());
           sleep(1);
       }
   }
[gsm@VM-4-3-centos lesson15]$ make
g++ -o process process.cc -std=c++11
[gsm@VM-4-3-centos lesson15]$ ./process 
子进程运行中, pid: 30459
子进程运行中, pid: 30459
子进程运行中, pid: 30459
子进程运行中, pid: 30459
子进程运行中, pid: 30459
wait sub process success, rid: 30459, status code: 123
我是父进程: pid: 30458
我是父进程: pid: 30458
我是父进程: pid: 30458
^C

c 复制代码
[gsm@VM-4-3-centos lesson16]$ ll
total 8
-rw-rw-r-- 1 gsm gsm   80 Nov 24 09:38 Makefile
-rw-rw-r-- 1 gsm gsm 1991 Nov 24 09:38 process.cc
[gsm@VM-4-3-centos lesson16]$ > process.cc 
[gsm@VM-4-3-centos lesson16]$ vim process.cc 
[gsm@VM-4-3-centos lesson16]$ cat process.cc 
#include <iostream>
#include <vector>
#include <cstdio>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

enum
{
    OK = 0,
    OPEN_FILE_ERROR
};

const std::string gsep = " ";
std::vector<int> data;

int SaveBegin()
{
    std::string name = std::to_string(time(nullptr));
    name += ".backup";
    FILE* fp = fopen(name.c_str(), "w");

    if (fp == nullptr)
    {
        return OPEN_FILE_ERROR;
    }
    
    std::string dataStr;
    
    for (auto d : data)
    {
        dataStr += std::to_string(d);
        dataStr += gsep;
    }

    fputs(dataStr.c_str(), fp);
    fclose(fp);

    return OK;
}

void Save()
{
    pid_t id = fork();

    if (id == 0) // 子进程
    {
        int code = SaveBegin();
        exit(code);
    }

    int status = 0;
    pid_t rid = waitpid(id, &status, 0);

    if (rid > 0)
    {
        int code = WEXITSTATUS(status);

        if (code == 0)
        {
            printf("备份成功, exit code : %d\n", code);
        }
        else
        {
            printf("备份失败, exit code : %d\n", code);
        }
    }
    else
    {
        perror("waitpid");
    }
}

int main()
{
    int cnt = 1;

    while (true)
    {
        data.push_back(cnt++);
        sleep(1);

        if (cnt % 10 == 0)
        {
            Save();
        }
    }
}
[gsm@VM-4-3-centos lesson16]$ make
g++ -o process process.cc -std=c++11
[gsm@VM-4-3-centos lesson16]$ ./process 
备份成功, exit code : 0
备份成功, exit code : 0
备份成功, exit code : 0
^C
c 复制代码
[gsm@VM-4-3-centos lesson16]$ ll
total 44
-rw-rw-r-- 1 gsm gsm    18 Nov 24 11:12 1763953945.backup
-rw-rw-r-- 1 gsm gsm    80 Nov 24 09:38 Makefile
-rwxrwxr-x 1 gsm gsm 30128 Nov 24 11:12 process
-rw-rw-r-- 1 gsm gsm  1316 Nov 24 11:12 process.cc
[gsm@VM-4-3-centos lesson16]$ ll
total 48
-rw-rw-r-- 1 gsm gsm    18 Nov 24 11:12 1763953945.backup
-rw-rw-r-- 1 gsm gsm    48 Nov 24 11:12 1763953955.backup
-rw-rw-r-- 1 gsm gsm    80 Nov 24 09:38 Makefile
-rwxrwxr-x 1 gsm gsm 30128 Nov 24 11:12 process
-rw-rw-r-- 1 gsm gsm  1316 Nov 24 11:12 process.cc
[gsm@VM-4-3-centos lesson16]$ ll
total 52
-rw-rw-r-- 1 gsm gsm    18 Nov 24 11:12 1763953945.backup
-rw-rw-r-- 1 gsm gsm    48 Nov 24 11:12 1763953955.backup
-rw-rw-r-- 1 gsm gsm    78 Nov 24 11:12 1763953965.backup
-rw-rw-r-- 1 gsm gsm    80 Nov 24 09:38 Makefile
-rwxrwxr-x 1 gsm gsm 30128 Nov 24 11:12 process
-rw-rw-r-- 1 gsm gsm  1316 Nov 24 11:12 process.cc
[gsm@VM-4-3-centos lesson16]$ cat 1763953945.backup 
1 2 3 4 5 6 7 8 9 [gsm@VM-4-3-centos lesson16]$ cat 1763953955.backup 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 [gsm@VM-4-3-centos lesson16]$ cat 1763953965.backup 
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 

c 复制代码
[gsm@VM-4-3-centos lesson16]$ ll
total 8
-rw-rw-r-- 1 gsm gsm   80 Nov 24 09:38 Makefile
-rw-rw-r-- 1 gsm gsm 1316 Nov 24 11:12 process.cc
[gsm@VM-4-3-centos lesson16]$ man waitpid
[gsm@VM-4-3-centos lesson16]$ vim process.cc 
[gsm@VM-4-3-centos lesson16]$ head -46 process.cc 
#include <iostream>
#include <vector>
#include <cstdio>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    pid_t id = fork();

    if (id == 0)
    {
        // child
        while (true)
        {
            printf("我是子进程,pid : %d\n", getpid());
            sleep(1);
        }

        exit(0);
    }

    // father
    while (true)
    {
        sleep(1);
        pid_t rid = waitpid(id, nullptr, WNOHANG);

        if (rid > 0)
        {
            printf("等待子进程%d 成功\n", rid);
            break;
        }
        else if (rid < 0)
        {
            printf("等待子进程失败\n");
        }
        else
        {
            printf("子进程尚未退出\n");

            // 做自己的事情
        }
    }
}
[gsm@VM-4-3-centos lesson16]$ make
g++ -o process process.cc -std=c++11
[gsm@VM-4-3-centos lesson16]$ ./process 
我是子进程,pid : 18989
子进程尚未退出
我是子进程,pid : 18989
子进程尚未退出
我是子进程,pid : 18989
子进程尚未退出
我是子进程,pid : 18989
子进程尚未退出
我是子进程,pid : 18989
子进程尚未退出
我是子进程,pid : 18989
子进程尚未退出
我是子进程,pid : 18989
我是子进程,pid : 18989
子进程尚未退出
我是子进程,pid : 18989
子进程尚未退出
我是子进程,pid : 18989
子进程尚未退出
子进程尚未退出
我是子进程,pid : 18989
子进程尚未退出
我是子进程,pid : 18989
我是子进程,pid : 18989
子进程尚未退出
子进程尚未退出
我是子进程,pid : 18989
子进程尚未退出
我是子进程,pid : 18989
等待子进程18989 成功
c 复制代码
[gsm@VM-4-3-centos lesson16]$ ps ajx | grep process
17415 18988 18988 17415 pts/0    18988 S+    1001   0:00 ./process
18988 18989 18988 17415 pts/0    18988 S+    1001   0:00 ./process
 4799 18993 18992  4799 pts/1    18992 S+    1001   0:00 grep --color=auto process
[gsm@VM-4-3-centos lesson16]$ kill -9 18989

c 复制代码
[gsm@VM-4-3-centos lesson16]$ ll
total 20
-rw-rw-r-- 1 gsm gsm   80 Nov 24 09:38 Makefile
-rwxrwxr-x 1 gsm gsm 9016 Nov 24 17:33 process
-rw-rw-r-- 1 gsm gsm 2147 Nov 24 17:48 process.cc
[gsm@VM-4-3-centos lesson16]$ touch task.h
[gsm@VM-4-3-centos lesson16]$ vim task.h 
[gsm@VM-4-3-centos lesson16]$ cat task.h 
#pragma once
#include <iostream>

void PrintLog();
void Download();
void Backup();
[gsm@VM-4-3-centos lesson16]$ cp task.h task.cc
[gsm@VM-4-3-centos lesson16]$ vim task.cc
[gsm@VM-4-3-centos lesson16]$ cat task.cc
#include "task.h"

void PrintLog()
{
    std::cout << "Print log task" << std::endl;
}

void Download()
{
    std::cout << "DownLoad task" << std::endl;
}

void Backup()
{
    std::cout << "BackUp task" << std::endl;
}
[gsm@VM-4-3-centos lesson16]$ vim process.cc
[gsm@VM-4-3-centos lesson16]$ head -64 process.cc
#include <iostream>
#include <vector>
#include <cstdio>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <functional>
#include "task.h"

typedef std::function<void()> task_t;

void LoadTask(std::vector<task_t>& tasks)
{
    tasks.push_back(PrintLog);
    tasks.push_back(Download);
    tasks.push_back(Backup);
}

int main()
{
    std::vector<task_t> tasks;
    LoadTask(tasks);

    pid_t id = fork();

    if (id == 0)
    {
        // child
        while (true)
        {
            printf("我是子进程,pid : %d\n", getpid());
            sleep(1);
        }

        exit(0);
    }

    // father
    while (true)
    {
        sleep(1);
        pid_t rid = waitpid(id, nullptr, WNOHANG);

        if (rid > 0)
        {
            printf("等待子进程%d 成功\n", rid);
            break;
        }
        else if (rid < 0)
        {
            printf("等待子进程失败\n");
        }
        else
        {
            printf("子进程尚未退出\n");

            // 做自己的事情
            for (auto& task : tasks)
            {
                task();
            }
        }
    }
}
[gsm@VM-4-3-centos lesson16]$ vim Makefile 
[gsm@VM-4-3-centos lesson16]$ cat Makefile 
process:process.cc task.cc
	g++ -o $@ $^ -std=c++11

.PHONY:clean
clean:
	rm -f process
[gsm@VM-4-3-centos lesson16]$ make
g++ -o process process.cc task.cc -std=c++11
[gsm@VM-4-3-centos lesson16]$ ./process 
我是子进程,pid : 26320
子进程尚未退出
Print log task
DownLoad task
BackUp task
我是子进程,pid : 26320
子进程尚未退出
Print log task
我是子进程,pid : 26320
DownLoad task
BackUp task
我是子进程,pid : 26320
子进程尚未退出
Print log task
DownLoad task
BackUp task
^C

4. 进程程序替换

c 复制代码
[gsm@VM-4-3-centos lesson16]$ ll
total 16
-rw-rw-r-- 1 gsm gsm   88 Nov 24 18:07 Makefile
-rw-rw-r-- 1 gsm gsm 2506 Nov 24 18:06 process.cc
-rw-rw-r-- 1 gsm gsm  219 Nov 24 17:55 task.cc
-rw-rw-r-- 1 gsm gsm   83 Nov 24 17:52 task.h
[gsm@VM-4-3-centos lesson16]$ mkdir exec
[gsm@VM-4-3-centos lesson16]$ cd exec/
[gsm@VM-4-3-centos exec]$ ll
total 0
[gsm@VM-4-3-centos exec]$ touch myexec.cc
[gsm@VM-4-3-centos exec]$ cp ../Makefile .
[gsm@VM-4-3-centos exec]$ vim Makefile 
[gsm@VM-4-3-centos exec]$ cat Makefile 
myexec:myexec.cc
	g++ -o $@ $^ -std=c++11

.PHONY:clean
clean:
	rm -f myexec
[gsm@VM-4-3-centos exec]$ man execl
[gsm@VM-4-3-centos exec]$ man 3 printf
[gsm@VM-4-3-centos exec]$ vim myexec.cc 
[gsm@VM-4-3-centos exec]$ cat myexec.cc 
#include <iostream>
#include <cstdio>
#include <unistd.h>

int main()
{
    execl("/bin/ls", "ls", "-l", "-a", nullptr);

    return 0;
}
[gsm@VM-4-3-centos exec]$ make
g++ -o myexec myexec.cc -std=c++11
[gsm@VM-4-3-centos exec]$ ./myexec 
total 28
drwxrwxr-x 2 gsm gsm 4096 Nov 24 18:30 .
drwxrwxr-x 3 gsm gsm 4096 Nov 24 18:22 ..
-rw-rw-r-- 1 gsm gsm   77 Nov 24 18:24 Makefile
-rwxrwxr-x 1 gsm gsm 8760 Nov 24 18:30 myexec
-rw-rw-r-- 1 gsm gsm  138 Nov 24 18:30 myexec.cc
[gsm@VM-4-3-centos exec]$ vim myexec.cc
[gsm@VM-4-3-centos exec]$ cat myexec.cc
#include <iostream>
#include <cstdio>
#include <unistd.h>

int main()
{
    execl("/usr/bin/top", "top", nullptr);

    return 0;
}
[gsm@VM-4-3-centos exec]$ make
g++ -o myexec myexec.cc -std=c++11
[gsm@VM-4-3-centos exec]$ ./myexec 
top - 18:38:25 up 50 days,  3:21,  2 users,  load average: 0.00, 0.01, 0.05
Tasks:  97 total,   1 running,  96 sleeping,   0 stopped,   0 zombie
%Cpu(s):  1.0 us,  0.7 sy,  0.0 ni, 98.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  2046500 total,   275036 free,   275148 used,  1496316 buff/cache
KiB Swap:        0 total,        0 free,        0 used.  1573904 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                                                                                      
 1606 root      20   0  829760  17944   2844 S   2.3  0.9 668:01.03 barad_agent                                                                                                                                  
25273 root      20   0 1031572 113096  16552 S   0.7  5.5 504:56.21 YDService                                                                                                                                    
    1 root      20   0   43592   3640   2320 S   0.0  0.2   4:17.68 systemd                                                                                                                                      
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.76 kthreadd                                                                                                                                     
    4 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0H                                                                                                                                 
    6 root      20   0       0      0      0 S   0.0  0.0   0:40.95 ksoftirqd/0                                                                                                                                  
    7 root      rt   0       0      0      0 S   0.0  0.0   0:18.96 migration/0                                                                                                                                  
    8 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_bh                                                                                                                                       
    9 root      20   0       0      0      0 S   0.0  0.0   9:23.71 rcu_sched                                                                                                                                    
   10 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 lru-add-drain                                                                                                                                
   11 root      rt   0       0      0      0 S   0.0  0.0   0:10.14 watchdog/0                                                                                                                                   
   12 root      rt   0       0      0      0 S   0.0  0.0   0:08.23 watchdog/1                                                                                                                                   
   13 root      rt   0       0      0      0 S   0.0  0.0   0:19.24 migration/1                                                                                                                                  
   14 root      20   0       0      0      0 S   0.0  0.0   0:39.17 ksoftirqd/1                                                                                                                                  
   16 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/1:0H                                                                                                                                 
   18 root      20   0       0      0      0 S   0.0  0.0   0:00.00 kdevtmpfs                                                                                                                                    
   19 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 netns                                                                                                                                        
   20 root      20   0       0      0      0 S   0.0  0.0   0:00.83 khungtaskd                                                                                                                                   
   21 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 writeback                                                                                                                                    
   22 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kintegrityd                                                                                                                                  
   23 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 bioset                                                                                                                                       
   24 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 bioset                                                                                                                                       
   25 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 bioset                                                                                                                                       
   26 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kblockd                                                                                                                                      
   27 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 md                                                                                                                                           
   28 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 edac-poller                                                                                                                                  
   29 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 watchdogd                                                                                                                                    
   35 root      20   0       0      0      0 S   0.0  0.0   0:01.01 kswapd0                                                                                                                                      
   36 root      25   5       0      0      0 S   0.0  0.0   0:00.00 ksmd                                                                                                                                         
   37 root      39  19       0      0      0 S   0.0  0.0   0:04.23 khugepaged                                                                                                                                   
   38 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 crypto                                                                                                                                       
   46 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kthrotld                                                                                                                                     
   48 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kmpath_rdacd                                                                                                                                 
   49 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kaluad                                                                                                                                       
   50 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kpsmoused                                                                                                                                    
   51 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 ipv6_addrconf                                                                                                                                
   65 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 deferwq                                                                                                                                      
  108 root      20   0       0      0      0 S   0.0  0.0   0:12.12 kauditd                                                                                                                                      
  200 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 iscsi_eh                                                                                                                                     
  259 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 ata_sff                                                                                                                                      
  262 root      20   0       0      0      0 S   0.0  0.0   0:00.00 scsi_eh_0                                                                                                                                    
  263 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 scsi_tmf_0                                                                                                                                   

c 复制代码
[gsm@VM-4-3-centos exec]$ touch other.c
[gsm@VM-4-3-centos exec]$ vim other.c 
[gsm@VM-4-3-centos exec]$ cat other.c 
#include <stdio.h>
#include <unistd.h>

int main()
{
    printf("我是Other, pid: %d\n", getpid());

    return 0;
}
[gsm@VM-4-3-centos exec]$ gcc other.c -o other
[gsm@VM-4-3-centos exec]$ ./other 
我是Other, pid: 17287
[gsm@VM-4-3-centos exec]$ vim myexec.cc
[gsm@VM-4-3-centos exec]$ cat myexec.cc
#include <iostream>
#include <cstdio>
#include <unistd.h>

int main()
{
    printf("我是myexec, pid: %d\n", getpid());

    execl("./other", "other", nullptr);

    return 0;
}
[gsm@VM-4-3-centos exec]$ make
g++ -o myexec myexec.cc -std=c++11
[gsm@VM-4-3-centos exec]$ ./myexec 
我是myexec, pid: 18144
我是Other, pid: 18144

c 复制代码
[gsm@VM-4-3-centos exec]$ ll
total 36
-rw-rw-r-- 1 gsm gsm   77 Nov 24 18:24 Makefile
-rwxrwxr-x 1 gsm gsm 8864 Nov 25 12:00 myexec
-rw-rw-r-- 1 gsm gsm  325 Nov 25 12:00 myexec.cc
-rwxrwxr-x 1 gsm gsm 8408 Nov 25 11:51 other
-rw-rw-r-- 1 gsm gsm  118 Nov 25 11:48 other.c
[gsm@VM-4-3-centos exec]$ vim myexec.cc
[gsm@VM-4-3-centos exec]$ cat myexec.cc
#include <iostream>
#include <cstdio>
#include <unistd.h>

int main()
{
    printf("我是myexec, pid: %d\n", getpid());

    int n = execl("/bin/ls", "ls", "-l", "--color", "-a", nullptr);
    printf("execl return val: %d\n", n);

    return 0;
}
[gsm@VM-4-3-centos exec]$ make
g++ -o myexec myexec.cc -std=c++11
[gsm@VM-4-3-centos exec]$ ./myexec 
我是myexec, pid: 19772
total 44
drwxrwxr-x 2 gsm gsm 4096 Nov 25 12:02 .
drwxrwxr-x 3 gsm gsm 4096 Nov 24 18:22 ..
-rw-rw-r-- 1 gsm gsm   77 Nov 24 18:24 Makefile
-rwxrwxr-x 1 gsm gsm 8864 Nov 25 12:02 myexec
-rw-rw-r-- 1 gsm gsm  336 Nov 25 12:02 myexec.cc
-rwxrwxr-x 1 gsm gsm 8408 Nov 25 11:51 other
-rw-rw-r-- 1 gsm gsm  118 Nov 25 11:48 other.c
[gsm@VM-4-3-centos exec]$ vim myexec.cc
[gsm@VM-4-3-centos exec]$ cat myexec.cc
#include <iostream>
#include <cstdio>
#include <unistd.h>

int main()
{
    printf("我是myexec, pid: %d\n", getpid());

    int n = execl("/bin/lssss", "ssss", "-l", "--color", "-a", nullptr);
    printf("execl return val: %d\n", n);

    return 0;
}
[gsm@VM-4-3-centos exec]$ make
g++ -o myexec myexec.cc -std=c++11
[gsm@VM-4-3-centos exec]$ ./myexec 
我是myexec, pid: 20338
execl return val: -1

c 复制代码
[gsm@VM-4-3-centos exec]$ ll
total 36
-rw-rw-r-- 1 gsm gsm   77 Nov 24 18:24 Makefile
-rwxrwxr-x 1 gsm gsm 8864 Nov 25 12:04 myexec
-rw-rw-r-- 1 gsm gsm  341 Nov 25 12:04 myexec.cc
-rwxrwxr-x 1 gsm gsm 8408 Nov 25 11:51 other
-rw-rw-r-- 1 gsm gsm  118 Nov 25 11:48 other.c
[gsm@VM-4-3-centos exec]$ vim myexec.cc
[gsm@VM-4-3-centos exec]$ head -25 myexec.cc
#include <iostream>
#include <cstdio>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    pid_t id = fork();

    if (id == 0)
    {
        sleep(3);
        // child
        execl("/bin/ls", "ls", "-l", "--color", "-a", nullptr);
        exit(1);
    }

    // father
    pid_t rid = waitpid(id, nullptr, 0);

    if (rid > 0)
    {
        printf("等待子进程成功!\n");
    }
[gsm@VM-4-3-centos exec]$ make
g++ -o myexec myexec.cc -std=c++11
[gsm@VM-4-3-centos exec]$ ./myexec 
total 44
drwxrwxr-x 2 gsm gsm 4096 Nov 25 19:30 .
drwxrwxr-x 3 gsm gsm 4096 Nov 24 18:22 ..
-rw-rw-r-- 1 gsm gsm   77 Nov 24 18:24 Makefile
-rwxrwxr-x 1 gsm gsm 8992 Nov 25 19:30 myexec
-rw-rw-r-- 1 gsm gsm  628 Nov 25 19:30 myexec.cc
-rwxrwxr-x 1 gsm gsm 8408 Nov 25 11:51 other
-rw-rw-r-- 1 gsm gsm  118 Nov 25 11:48 other.c
等待子进程成功!
c 复制代码
[gsm@VM-4-3-centos ~]$ while :; do ps ajx | head -1 && ps ajx | grep myexec; sleep 1; done
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
14598 14767 14766 14598 pts/1    14766 S+    1001   0:00 grep --color=auto myexec
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
14598 14776 14775 14598 pts/1    14775 S+    1001   0:00 grep --color=auto myexec
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
11483 14778 14778 11483 pts/0    14778 S+    1001   0:00 ./myexec
14778 14779 14778 11483 pts/0    14778 S+    1001   0:00 ./myexec
14598 14787 14786 14598 pts/1    14786 S+    1001   0:00 grep --color=auto myexec
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
11483 14778 14778 11483 pts/0    14778 S+    1001   0:00 ./myexec
14778 14779 14778 11483 pts/0    14778 S+    1001   0:00 ./myexec
14598 14793 14792 14598 pts/1    14792 S+    1001   0:00 grep --color=auto myexec
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
11483 14778 14778 11483 pts/0    14778 S+    1001   0:00 ./myexec
14778 14779 14778 11483 pts/0    14778 S+    1001   0:00 ./myexec
14598 14801 14800 14598 pts/1    14800 S+    1001   0:00 grep --color=auto myexec
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
14598 14812 14811 14598 pts/1    14811 S+    1001   0:00 grep --color=auto myexec
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
14598 14818 14817 14598 pts/1    14817 S+    1001   0:00 grep --color=auto myexec
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
14598 14823 14822 14598 pts/1    14822 S+    1001   0:00 grep --color=auto myexec
^C

c 复制代码
[gsm@VM-4-3-centos exec]$ ll
total 36
-rw-rw-r-- 1 gsm gsm   77 Nov 24 18:24 Makefile
-rwxrwxr-x 1 gsm gsm 8992 Nov 25 19:30 myexec
-rw-rw-r-- 1 gsm gsm  628 Nov 25 19:30 myexec.cc
-rwxrwxr-x 1 gsm gsm 8408 Nov 25 11:51 other
-rw-rw-r-- 1 gsm gsm  118 Nov 25 11:48 other.c
[gsm@VM-4-3-centos exec]$ touch test.py
[gsm@VM-4-3-centos exec]$ vim test.py 
[gsm@VM-4-3-centos exec]$ cat test.py
#!/usr/bin/python

print ("hello python")
[gsm@VM-4-3-centos exec]$ python test.py
hello python
[gsm@VM-4-3-centos exec]$ touch test.sh
[gsm@VM-4-3-centos exec]$ vim test.sh
[gsm@VM-4-3-centos exec]$ cat test.sh
#!/bin/bash


echo "hello shell"
[gsm@VM-4-3-centos exec]$ bash test.sh
hello shell
[gsm@VM-4-3-centos exec]$ ls /usr/bin/bash -l
-rwxr-xr-x 1 root root 964536 Apr  1  2020 /usr/bin/bash
[gsm@VM-4-3-centos exec]$ ls /usr/bin/python -l
lrwxrwxrwx 1 root root 7 Jan  8  2024 /usr/bin/python -> python2
[gsm@VM-4-3-centos exec]$ vim myexec.cc
[gsm@VM-4-3-centos exec]$ head -26 myexec.cc
#include <iostream>
#include <cstdio>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    pid_t id = fork();

    if (id == 0)
    {
        sleep(3);
        // child
        //execl("/bin/ls", "ls", "-l", "--color", "-a", nullptr);
        execl("/usr/bin/python", "python", "test.py", nullptr);
        exit(1);
    }

    // father
    pid_t rid = waitpid(id, nullptr, 0);

    if (rid > 0)
    {
        printf("等待子进程成功!\n");
    }
[gsm@VM-4-3-centos exec]$ make
g++ -o myexec myexec.cc -std=c++11
[gsm@VM-4-3-centos exec]$ ./myexec 
hello python
等待子进程成功!
[gsm@VM-4-3-centos exec]$ vim myexec.cc
[gsm@VM-4-3-centos exec]$ head -27 myexec.cc
#include <iostream>
#include <cstdio>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    pid_t id = fork();

    if (id == 0)
    {
        sleep(3);
        // child
        //execl("/bin/ls", "ls", "-l", "--color", "-a", nullptr);
        //execl("/usr/bin/python", "python", "test.py", nullptr);
        execl("/usr/bin/bash", "bash", "test.sh", nullptr);
        exit(1);
    }

    // father
    pid_t rid = waitpid(id, nullptr, 0);

    if (rid > 0)
    {
        printf("等待子进程成功!\n");
    }
[gsm@VM-4-3-centos exec]$ make
g++ -o myexec myexec.cc -std=c++11
[gsm@VM-4-3-centos exec]$ ./myexec 
hello shell
等待子进程成功!

c 复制代码
[gsm@VM-4-3-centos exec]$ ll
total 32
-rw-rw-r-- 1 gsm gsm   77 Nov 24 18:24 Makefile
-rw-rw-r-- 1 gsm gsm  758 Nov 25 20:26 myexec.cc
-rwxrwxr-x 1 gsm gsm 8408 Nov 25 11:51 other
-rw-rw-r-- 1 gsm gsm  118 Nov 25 11:48 other.c
-rw-rw-r-- 1 gsm gsm   42 Nov 25 20:07 test.py
-rw-rw-r-- 1 gsm gsm   33 Nov 25 20:08 test.sh
[gsm@VM-4-3-centos exec]$ vim myexec.cc 
[gsm@VM-4-3-centos exec]$ head -35 myexec.cc 
#include <iostream>
#include <cstdio>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    pid_t id = fork();

    if (id == 0)
    {
        char* const argv[] = {
            (char*)"ls",
            (char*)"--color",
            (char*)"-a",
            (char*)"-l",
            nullptr
        };
        execv("/usr/bin/ls", argv);

        // child
        //execl("/bin/ls", "ls", "-l", "--color", "-a", nullptr);
        exit(1);
    }

    // father
    pid_t rid = waitpid(id, nullptr, 0);

    if (rid > 0)
    {
        printf("等待子进程成功!\n");
    }
[gsm@VM-4-3-centos exec]$ make
g++ -o myexec myexec.cc -std=c++11
[gsm@VM-4-3-centos exec]$ ./myexec 
total 52
drwxrwxr-x 2 gsm gsm 4096 Nov 25 20:44 .
drwxrwxr-x 3 gsm gsm 4096 Nov 24 18:22 ..
-rw-rw-r-- 1 gsm gsm   77 Nov 24 18:24 Makefile
-rwxrwxr-x 1 gsm gsm 8944 Nov 25 20:44 myexec
-rw-rw-r-- 1 gsm gsm  944 Nov 25 20:44 myexec.cc
-rwxrwxr-x 1 gsm gsm 8408 Nov 25 11:51 other
-rw-rw-r-- 1 gsm gsm  118 Nov 25 11:48 other.c
-rw-rw-r-- 1 gsm gsm   42 Nov 25 20:07 test.py
-rw-rw-r-- 1 gsm gsm   33 Nov 25 20:08 test.sh
等待子进程成功!

c 复制代码
[gsm@VM-4-3-centos exec]$ ll
total 44
-rw-rw-r-- 1 gsm gsm   77 Nov 24 18:24 Makefile
-rwxrwxr-x 1 gsm gsm 8944 Nov 25 20:44 myexec
-rw-rw-r-- 1 gsm gsm  944 Nov 25 20:44 myexec.cc
-rwxrwxr-x 1 gsm gsm 8408 Nov 25 11:51 other
-rw-rw-r-- 1 gsm gsm  118 Nov 25 11:48 other.c
-rw-rw-r-- 1 gsm gsm   42 Nov 25 20:07 test.py
-rw-rw-r-- 1 gsm gsm   33 Nov 25 20:08 test.sh
[gsm@VM-4-3-centos exec]$ vim myexec.cc
[gsm@VM-4-3-centos exec]$ head -38 myexec.cc
#include <iostream>
#include <cstdio>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    pid_t id = fork();

    if (id == 0)
    {
        execlp("ls", "ls", "--color", "-a", "-l", nullptr);
        
        // child
        //execl("/bin/ls", "ls", "-l", "--color", "-a", nullptr);
        exit(1);
    }

    // father
    pid_t rid = waitpid(id, nullptr, 0);

    if (rid > 0)
    {
        printf("等待子进程成功!\n");
    }
[gsm@VM-4-3-centos exec]$ make
g++ -o myexec myexec.cc -std=c++11
[gsm@VM-4-3-centos exec]$ ./myexec 
total 52
drwxrwxr-x 2 gsm gsm 4096 Nov 25 21:01 .
drwxrwxr-x 3 gsm gsm 4096 Nov 24 18:22 ..
-rw-rw-r-- 1 gsm gsm   77 Nov 24 18:24 Makefile
-rwxrwxr-x 1 gsm gsm 8944 Nov 25 21:01 myexec
-rw-rw-r-- 1 gsm gsm 1030 Nov 25 21:01 myexec.cc
-rwxrwxr-x 1 gsm gsm 8408 Nov 25 11:51 other
-rw-rw-r-- 1 gsm gsm  118 Nov 25 11:48 other.c
-rw-rw-r-- 1 gsm gsm   42 Nov 25 20:07 test.py
-rw-rw-r-- 1 gsm gsm   33 Nov 25 20:08 test.sh
等待子进程成功!

c 复制代码
[gsm@VM-4-3-centos exec]$ ll
total 44
-rw-rw-r-- 1 gsm gsm   77 Nov 24 18:24 Makefile
-rwxrwxr-x 1 gsm gsm 8944 Nov 25 21:10 myexec
-rw-rw-r-- 1 gsm gsm 1077 Nov 25 21:10 myexec.cc
-rwxrwxr-x 1 gsm gsm 8408 Nov 25 11:51 other
-rw-rw-r-- 1 gsm gsm  118 Nov 25 11:48 other.c
-rw-rw-r-- 1 gsm gsm   42 Nov 25 20:07 test.py
-rw-rw-r-- 1 gsm gsm   33 Nov 25 20:08 test.sh
[gsm@VM-4-3-centos exec]$ vim myexec.cc
[gsm@VM-4-3-centos exec]$ head -40 myexec.cc
#include <iostream>
#include <cstdio>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    pid_t id = fork();

    if (id == 0)
    {
        char* const argv[] = {
            (char*)"ls",
            (char*)"--color",
            (char*)"-a",
            (char*)"-l",
            nullptr
        };
        
        execvp("ls", argv);

        //execlp("ls", "ls", "--color", "-a", "-l", nullptr);
        exit(1);
    }

    // father
    pid_t rid = waitpid(id, nullptr, 0);

    if (rid > 0)
    {
        printf("等待子进程成功!\n");
    }
[gsm@VM-4-3-centos exec]$ make
g++ -o myexec myexec.cc -std=c++11
[gsm@VM-4-3-centos exec]$ ./myexec 
total 52
drwxrwxr-x 2 gsm gsm 4096 Nov 25 21:13 .
drwxrwxr-x 3 gsm gsm 4096 Nov 24 18:22 ..
-rw-rw-r-- 1 gsm gsm   77 Nov 24 18:24 Makefile
-rwxrwxr-x 1 gsm gsm 8944 Nov 25 21:13 myexec
-rw-rw-r-- 1 gsm gsm 1055 Nov 25 21:12 myexec.cc
-rwxrwxr-x 1 gsm gsm 8408 Nov 25 11:51 other
-rw-rw-r-- 1 gsm gsm  118 Nov 25 11:48 other.c
-rw-rw-r-- 1 gsm gsm   42 Nov 25 20:07 test.py
-rw-rw-r-- 1 gsm gsm   33 Nov 25 20:08 test.sh
等待子进程成功!

c 复制代码
[gsm@VM-4-3-centos exec]$ ll
total 32
-rw-rw-r-- 1 gsm gsm   77 Nov 24 18:24 Makefile
-rw-rw-r-- 1 gsm gsm 1055 Nov 25 21:12 myexec.cc
-rwxrwxr-x 1 gsm gsm 8408 Nov 25 11:51 other
-rw-rw-r-- 1 gsm gsm  118 Nov 25 11:48 other.c
-rw-rw-r-- 1 gsm gsm   42 Nov 25 20:07 test.py
-rw-rw-r-- 1 gsm gsm   33 Nov 25 20:08 test.sh
[gsm@VM-4-3-centos exec]$ vim myexec.cc 
[gsm@VM-4-3-centos exec]$ head -40 myexec.cc 
#include <iostream>
#include <cstdio>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    pid_t id = fork();

    if (id == 0)
    {
        char* const argv[] = {
            (char*)"ls",
            (char*)"--color",
            (char*)"-a",
            (char*)"-l",
            nullptr
        };
        
        execvp(argv[0], argv);

        //execlp("ls", "ls", "--color", "-a", "-l", nullptr);
        exit(1);
    }

    // father
    pid_t rid = waitpid(id, nullptr, 0);

    if (rid > 0)
    {
        printf("等待子进程成功!\n");
    }
[gsm@VM-4-3-centos exec]$ make
g++ -o myexec myexec.cc -std=c++11
[gsm@VM-4-3-centos exec]$ ./myexec 
total 52
drwxrwxr-x 2 gsm gsm 4096 Nov 25 21:15 .
drwxrwxr-x 3 gsm gsm 4096 Nov 24 18:22 ..
-rw-rw-r-- 1 gsm gsm   77 Nov 24 18:24 Makefile
-rwxrwxr-x 1 gsm gsm 8944 Nov 25 21:15 myexec
-rw-rw-r-- 1 gsm gsm 1058 Nov 25 21:15 myexec.cc
-rwxrwxr-x 1 gsm gsm 8408 Nov 25 11:51 other
-rw-rw-r-- 1 gsm gsm  118 Nov 25 11:48 other.c
-rw-rw-r-- 1 gsm gsm   42 Nov 25 20:07 test.py
-rw-rw-r-- 1 gsm gsm   33 Nov 25 20:08 test.sh
等待子进程成功!

c 复制代码
[gsm@VM-4-3-centos exec]$ vim other.c 
[gsm@VM-4-3-centos exec]$ cat other.c 
#include <stdio.h>

extern char** environ;

int main()
{
    for (int i = 0; environ[i]; i++)
    {
        printf("env[%d]: %s\n", i, environ[i]);
    }
}
[gsm@VM-4-3-centos exec]$ gcc other.c -o other -std=c99
[gsm@VM-4-3-centos exec]$ ./other 
env[0]: XDG_SESSION_ID=92184
env[1]: HOSTNAME=VM-4-3-centos
env[2]: TERM=xterm
env[3]: SHELL=/bin/bash
env[4]: HISTSIZE=3000
env[5]: SSH_CLIENT=114.84.3.78 31495 22
env[6]: SSH_TTY=/dev/pts/0
env[7]: USER=gsm
env[8]: LD_LIBRARY_PATH=:/home/gsm/.VimForCpp/vim/bundle/YCM.so/el7.x86_64
env[9]: LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:
env[10]: MAIL=/var/spool/mail/gsm
env[11]: PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/gsm/.local/bin:/home/gsm/bin
env[12]: PWD=/home/gsm/linux/112/lesson16/exec
env[13]: LANG=en_US.utf8
env[14]: SHLVL=1
env[15]: HOME=/home/gsm
env[16]: LOGNAME=gsm
env[17]: SSH_CONNECTION=114.84.3.78 31495 10.0.4.3 22
env[18]: LESSOPEN=||/usr/bin/lesspipe.sh %s
env[19]: PROMPT_COMMAND=history -a; history -a; printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"
env[20]: XDG_RUNTIME_DIR=/run/user/1001
env[21]: HISTTIMEFORMAT=%F %T 
env[22]: OLDPWD=/home/gsm/linux/112/lesson16
env[23]: _=./other
[gsm@VM-4-3-centos exec]$ vim myexec.cc 
[gsm@VM-4-3-centos exec]$ head -41 myexec.cc 
#include <iostream>
#include <cstdio>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    pid_t id = fork();

    if (id == 0)
    {
        execl("./other", "other", nullptr);
        exit(1);
    }

    // father
    pid_t rid = waitpid(id, nullptr, 0);

    if (rid > 0)
    {
        printf("等待子进程成功!\n");
    }
[gsm@VM-4-3-centos exec]$ make
g++ -o myexec myexec.cc -std=c++11
[gsm@VM-4-3-centos exec]$ ./myexec 
env[0]: XDG_SESSION_ID=92184
env[1]: HOSTNAME=VM-4-3-centos
env[2]: TERM=xterm
env[3]: SHELL=/bin/bash
env[4]: HISTSIZE=3000
env[5]: SSH_CLIENT=114.84.3.78 31495 22
env[6]: SSH_TTY=/dev/pts/0
env[7]: USER=gsm
env[8]: LD_LIBRARY_PATH=:/home/gsm/.VimForCpp/vim/bundle/YCM.so/el7.x86_64
env[9]: LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:
env[10]: MAIL=/var/spool/mail/gsm
env[11]: PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/gsm/.local/bin:/home/gsm/bin
env[12]: PWD=/home/gsm/linux/112/lesson16/exec
env[13]: LANG=en_US.utf8
env[14]: SHLVL=1
env[15]: HOME=/home/gsm
env[16]: LOGNAME=gsm
env[17]: SSH_CONNECTION=114.84.3.78 31495 10.0.4.3 22
env[18]: LESSOPEN=||/usr/bin/lesspipe.sh %s
env[19]: PROMPT_COMMAND=history -a; history -a; printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"
env[20]: XDG_RUNTIME_DIR=/run/user/1001
env[21]: HISTTIMEFORMAT=%F %T 
env[22]: OLDPWD=/home/gsm/linux/112/lesson16
env[23]: _=./myexec
等待子进程成功!
[gsm@VM-4-3-centos exec]$ vim myexec.cc
[gsm@VM-4-3-centos exec]$ head -48 myexec.cc
#include <iostream>
#include <cstdio>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    pid_t id = fork();

    if (id == 0)
    {
        char* const argv[] = {
            (char*)"other",
            nullptr
        };
        
        char* const env[] = {
            (char*)"HELLO=bite",
            (char*)"HELLO1=bite1",
            (char*)"HELLO2=bite2",
            (char*)"HELLO3=bite3",
            nullptr
        };

        execvpe("./other", argv, env);
        exit(1);
    }

    // father
    pid_t rid = waitpid(id, nullptr, 0);

    if (rid > 0)
    {
        printf("等待子进程成功!\n");
    }
[gsm@VM-4-3-centos exec]$ make
g++ -o myexec myexec.cc -std=c++11
[gsm@VM-4-3-centos exec]$ ./myexec 
env[0]: HELLO=bite
env[1]: HELLO1=bite1
env[2]: HELLO2=bite2
env[3]: HELLO3=bite3
等待子进程成功!

c 复制代码
[gsm@VM-4-3-centos exec]$ ll
total 32
-rw-rw-r-- 1 gsm gsm   77 Nov 24 18:24 Makefile
-rw-rw-r-- 1 gsm gsm 1269 Nov 25 21:57 myexec.cc
-rwxrwxr-x 1 gsm gsm 8432 Nov 25 21:35 other
-rw-rw-r-- 1 gsm gsm  156 Nov 25 21:35 other.c
-rw-rw-r-- 1 gsm gsm   42 Nov 25 20:07 test.py
-rw-rw-r-- 1 gsm gsm   33 Nov 25 20:08 test.sh
[gsm@VM-4-3-centos exec]$ man getenv
[gsm@VM-4-3-centos exec]$ man putenv
[gsm@VM-4-3-centos exec]$ vim myexec.cc 
[gsm@VM-4-3-centos exec]$ head -51 myexec.cc 
#include <iostream>
#include <cstdio>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

const std::string myenv = "HELLO=AAAAAAAAAAAAAAAAAAAA";

int main()
{
    putenv((char*)myenv.c_str());
    pid_t id = fork();

    if (id == 0)
    {
        execl("./other", "other", nullptr);
        exit(1);
    }

    // father
    pid_t rid = waitpid(id, nullptr, 0);

    if (rid > 0)
    {
        printf("等待子进程成功!\n");
    }
[gsm@VM-4-3-centos exec]$ make
g++ -o myexec myexec.cc -std=c++11
[gsm@VM-4-3-centos exec]$ ./myexec 
env[0]: XDG_SESSION_ID=92184
env[1]: HOSTNAME=VM-4-3-centos
env[2]: TERM=xterm
env[3]: SHELL=/bin/bash
env[4]: HISTSIZE=3000
env[5]: SSH_CLIENT=114.84.3.78 31495 22
env[6]: SSH_TTY=/dev/pts/0
env[7]: USER=gsm
env[8]: LD_LIBRARY_PATH=:/home/gsm/.VimForCpp/vim/bundle/YCM.so/el7.x86_64
env[9]: LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:
env[10]: MAIL=/var/spool/mail/gsm
env[11]: PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/gsm/.local/bin:/home/gsm/bin
env[12]: PWD=/home/gsm/linux/112/lesson16/exec
env[13]: LANG=en_US.utf8
env[14]: SHLVL=1
env[15]: HOME=/home/gsm
env[16]: LOGNAME=gsm
env[17]: SSH_CONNECTION=114.84.3.78 31495 10.0.4.3 22
env[18]: LESSOPEN=||/usr/bin/lesspipe.sh %s
env[19]: PROMPT_COMMAND=history -a; history -a; printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"
env[20]: XDG_RUNTIME_DIR=/run/user/1001
env[21]: HISTTIMEFORMAT=%F %T 
env[22]: OLDPWD=/home/gsm/linux/112/lesson16
env[23]: _=./myexec
env[24]: HELLO=AAAAAAAAAAAAAAAAAAAA
等待子进程成功!

c 复制代码
[gsm@VM-4-3-centos exec]$ ll
total 32
-rw-rw-r-- 1 gsm gsm   77 Nov 24 18:24 Makefile
-rw-rw-r-- 1 gsm gsm 1392 Nov 25 22:06 myexec.cc
-rwxrwxr-x 1 gsm gsm 8432 Nov 25 21:35 other
-rw-rw-r-- 1 gsm gsm  156 Nov 25 21:35 other.c
-rw-rw-r-- 1 gsm gsm   42 Nov 25 20:07 test.py
-rw-rw-r-- 1 gsm gsm   33 Nov 25 20:08 test.sh
[gsm@VM-4-3-centos exec]$ vim myexec.cc 
[gsm@VM-4-3-centos exec]$ head -53 myexec.cc 
#include <iostream>
#include <cstdio>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

const std::string myenv = "HELLO=AAAAAAAAAAAAAAAAAAAA";

extern char** environ;

int main()
{
    putenv((char*)myenv.c_str());
    pid_t id = fork();

    if (id == 0)
    {
        char* const argv[] = {
            (char*)"other",
            nullptr
        };
        
        execvpe("./other", argv, environ);
        exit(1);
    }

    // father
    pid_t rid = waitpid(id, nullptr, 0);

    if (rid > 0)
    {
        printf("等待子进程成功!\n");
    }
[gsm@VM-4-3-centos exec]$ make
g++ -o myexec myexec.cc -std=c++11
[gsm@VM-4-3-centos exec]$ ./myexec 
env[0]: XDG_SESSION_ID=92184
env[1]: HOSTNAME=VM-4-3-centos
env[2]: TERM=xterm
env[3]: SHELL=/bin/bash
env[4]: HISTSIZE=3000
env[5]: SSH_CLIENT=114.84.3.78 31495 22
env[6]: SSH_TTY=/dev/pts/0
env[7]: USER=gsm
env[8]: LD_LIBRARY_PATH=:/home/gsm/.VimForCpp/vim/bundle/YCM.so/el7.x86_64
env[9]: LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:
env[10]: MAIL=/var/spool/mail/gsm
env[11]: PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/gsm/.local/bin:/home/gsm/bin
env[12]: PWD=/home/gsm/linux/112/lesson16/exec
env[13]: LANG=en_US.utf8
env[14]: SHLVL=1
env[15]: HOME=/home/gsm
env[16]: LOGNAME=gsm
env[17]: SSH_CONNECTION=114.84.3.78 31495 10.0.4.3 22
env[18]: LESSOPEN=||/usr/bin/lesspipe.sh %s
env[19]: PROMPT_COMMAND=history -a; history -a; printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"
env[20]: XDG_RUNTIME_DIR=/run/user/1001
env[21]: HISTTIMEFORMAT=%F %T 
env[22]: OLDPWD=/home/gsm/linux/112/lesson16
env[23]: _=./myexec
env[24]: HELLO=AAAAAAAAAAAAAAAAAAAA
等待子进程成功!
相关推荐
0 0 03 小时前
CCF-CSP 36-3 缓存模拟(cache)【C++】
开发语言·c++·算法
wjs20243 小时前
C# 环境:深入解析与优化实践
开发语言
满天星83035773 小时前
【Linux】信号(上)
linux·运维·服务器·开发语言·c++
霸王大陆3 小时前
《零基础学 PHP:从入门到实战》模块十:从应用到精通——掌握PHP进阶技术与现代化开发实战-5
android·开发语言·php
科普瑞传感仪器3 小时前
航空航天领域青睐:复杂曲面机器人抛光为何必须采用六维力控?
运维·人工智能·机器人·自动化·无人机
博语小屋3 小时前
生产者消费者模型
linux·分布式·缓存
CIb0la3 小时前
在 ARM CPU 上运行 x86 应用的开源项目:FEX
linux·运维·生活
MarkHD3 小时前
车辆TBOX科普 第56次 从模块拼接到可靠交付的实战指南
java·开发语言
starvapour3 小时前
Ubuntu部署gitlab频繁出现500的问题
linux·ubuntu·gitlab