进程控制(上)

文章目录

    • [1. 进程创建](#1. 进程创建)
      • [1.1 写时拷贝](#1.1 写时拷贝)
      • [1.2 fork常规用法](#1.2 fork常规用法)
      • [1.3 fork调用失败的原因](#1.3 fork调用失败的原因)
    • [2. 进程终止](#2. 进程终止)
    • [3. 进程等待](#3. 进程等待)

1. 进程创建

1.1 写时拷贝

1.2 fork常规用法

  • ⼀个父进程希望复制自己,使父子进程同时执行不同的代码段。例如,父进程等待客户端请求,生成子进程来处理请求。
  • ⼀个进程要执行一个不同的程序。例如子进程从fork返回后,调用exec函数。

1.3 fork调用失败的原因

  • 系统中有太多的进程
  • 实际用户的进程数超过了限制

2. 进程终止

c 复制代码
[gsm@VM-4-3-centos lesson15]$ ll
total 0
[gsm@VM-4-3-centos lesson15]$ touch process.cc
[gsm@VM-4-3-centos lesson15]$ ls > Makefile
[gsm@VM-4-3-centos lesson15]$ ll
total 4
-rw-rw-r-- 1 gsm gsm 20 Nov 22 11:54 Makefile
-rw-rw-r-- 1 gsm gsm  0 Nov 22 11:53 process.cc
[gsm@VM-4-3-centos lesson15]$ vim Makefile 
[gsm@VM-4-3-centos lesson15]$ cat Makefile 
process:process.cc
	g++ -o $@ $^ -std=c++11

.PHONY:clean
clean:
	rm -f process
[gsm@VM-4-3-centos lesson15]$ vim process.cc 
[gsm@VM-4-3-centos lesson15]$ cat process.cc 
#include <iostream>
#include <string>


int main()
{
    std::cout << "hello bit" << std::endl;


    return 10;
}
[gsm@VM-4-3-centos lesson15]$ make
g++ -o process process.cc -std=c++11
[gsm@VM-4-3-centos lesson15]$ ls
Makefile  process  process.cc
[gsm@VM-4-3-centos lesson15]$ ./process 
hello bit
[gsm@VM-4-3-centos lesson15]$ echo $?
10
[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 9024 Nov 22 11:59 process
-rw-rw-r-- 1 gsm gsm  115 Nov 22 11:59 process.cc
[gsm@VM-4-3-centos lesson15]$ ./process 
hello bit
[gsm@VM-4-3-centos lesson15]$ echo $?
10
[gsm@VM-4-3-centos lesson15]$ echo $?
0
c 复制代码
[gsm@VM-4-3-centos lesson15]$ man perror
[gsm@VM-4-3-centos lesson15]$ man strerror
[gsm@VM-4-3-centos lesson15]$ man errno
[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 9024 Nov 22 11:59 process
-rw-rw-r-- 1 gsm gsm  115 Nov 22 11:59 process.cc
[gsm@VM-4-3-centos lesson15]$ make clean
rm -f process
[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>

int main()
{
    printf("before: errno : %d, errstring : %s\n", errno, strerror(errno));
    
    FILE* fp = fopen("./log.txt", "r");

    if (fp == nullptr)
    {
        printf("after: errno : %d, errstring : %s\n", errno, strerror(errno));
        return errno;
    }

    return 10;
}
[gsm@VM-4-3-centos lesson15]$ make
g++ -o process process.cc -std=c++11
[gsm@VM-4-3-centos lesson15]$ ls
Makefile  process  process.cc
[gsm@VM-4-3-centos lesson15]$ ./process 
before: errno : 0, errstring : Success
after: errno : 2, errstring : No such file or directory
[gsm@VM-4-3-centos lesson15]$ echo $?
2
c 复制代码
[gsm@VM-4-3-centos lesson15]$ vim process.cc
[gsm@VM-4-3-centos lesson15]$ head -12 process.cc
#include <iostream>
#include <string>
#include <cstdio>
#include <string.h>
#include <errno.h>

int main()
{
    for (int i = 0; i < 200; i++)
    {
        std::cout << "code :" << i << ", errstring: " << strerror(i) << std::endl;
    }
[gsm@VM-4-3-centos lesson15]$ make
g++ -o process process.cc -std=c++11
[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 9128 Nov 22 12:43 process
-rw-rw-r-- 1 gsm gsm  531 Nov 22 12:40 process.cc
[gsm@VM-4-3-centos lesson15]$ ./process 
code :0, errstring: Success
code :1, errstring: Operation not permitted
code :2, errstring: No such file or directory
code :3, errstring: No such process
code :4, errstring: Interrupted system call
code :5, errstring: Input/output error
code :6, errstring: No such device or address
code :7, errstring: Argument list too long
code :8, errstring: Exec format error
code :9, errstring: Bad file descriptor
code :10, errstring: No child processes
code :11, errstring: Resource temporarily unavailable
code :12, errstring: Cannot allocate memory
code :13, errstring: Permission denied
code :14, errstring: Bad address
code :15, errstring: Block device required
code :16, errstring: Device or resource busy
code :17, errstring: File exists
code :18, errstring: Invalid cross-device link
code :19, errstring: No such device
code :20, errstring: Not a directory
code :21, errstring: Is a directory
code :22, errstring: Invalid argument
code :23, errstring: Too many open files in system
code :24, errstring: Too many open files
code :25, errstring: Inappropriate ioctl for device
code :26, errstring: Text file busy
code :27, errstring: File too large
code :28, errstring: No space left on device
code :29, errstring: Illegal seek
code :30, errstring: Read-only file system
code :31, errstring: Too many links
code :32, errstring: Broken pipe
code :33, errstring: Numerical argument out of domain
code :34, errstring: Numerical result out of range
code :35, errstring: Resource deadlock avoided
code :36, errstring: File name too long
code :37, errstring: No locks available
code :38, errstring: Function not implemented
code :39, errstring: Directory not empty
code :40, errstring: Too many levels of symbolic links
code :41, errstring: Unknown error 41
code :42, errstring: No message of desired type
code :43, errstring: Identifier removed
code :44, errstring: Channel number out of range
code :45, errstring: Level 2 not synchronized
code :46, errstring: Level 3 halted
code :47, errstring: Level 3 reset
code :48, errstring: Link number out of range
code :49, errstring: Protocol driver not attached
code :50, errstring: No CSI structure available
code :51, errstring: Level 2 halted
code :52, errstring: Invalid exchange
code :53, errstring: Invalid request descriptor
code :54, errstring: Exchange full
code :55, errstring: No anode
code :56, errstring: Invalid request code
code :57, errstring: Invalid slot
code :58, errstring: Unknown error 58
code :59, errstring: Bad font file format
code :60, errstring: Device not a stream
code :61, errstring: No data available
code :62, errstring: Timer expired
code :63, errstring: Out of streams resources
code :64, errstring: Machine is not on the network
code :65, errstring: Package not installed
code :66, errstring: Object is remote
code :67, errstring: Link has been severed
code :68, errstring: Advertise error
code :69, errstring: Srmount error
code :70, errstring: Communication error on send
code :71, errstring: Protocol error
code :72, errstring: Multihop attempted
code :73, errstring: RFS specific error
code :74, errstring: Bad message
code :75, errstring: Value too large for defined data type
code :76, errstring: Name not unique on network
code :77, errstring: File descriptor in bad state
code :78, errstring: Remote address changed
code :79, errstring: Can not access a needed shared library
code :80, errstring: Accessing a corrupted shared library
code :81, errstring: .lib section in a.out corrupted
code :82, errstring: Attempting to link in too many shared libraries
code :83, errstring: Cannot exec a shared library directly
code :84, errstring: Invalid or incomplete multibyte or wide character
code :85, errstring: Interrupted system call should be restarted
code :86, errstring: Streams pipe error
code :87, errstring: Too many users
code :88, errstring: Socket operation on non-socket
code :89, errstring: Destination address required
code :90, errstring: Message too long
code :91, errstring: Protocol wrong type for socket
code :92, errstring: Protocol not available
code :93, errstring: Protocol not supported
code :94, errstring: Socket type not supported
code :95, errstring: Operation not supported
code :96, errstring: Protocol family not supported
code :97, errstring: Address family not supported by protocol
code :98, errstring: Address already in use
code :99, errstring: Cannot assign requested address
code :100, errstring: Network is down
code :101, errstring: Network is unreachable
code :102, errstring: Network dropped connection on reset
code :103, errstring: Software caused connection abort
code :104, errstring: Connection reset by peer
code :105, errstring: No buffer space available
code :106, errstring: Transport endpoint is already connected
code :107, errstring: Transport endpoint is not connected
code :108, errstring: Cannot send after transport endpoint shutdown
code :109, errstring: Too many references: cannot splice
code :110, errstring: Connection timed out
code :111, errstring: Connection refused
code :112, errstring: Host is down
code :113, errstring: No route to host
code :114, errstring: Operation already in progress
code :115, errstring: Operation now in progress
code :116, errstring: Stale file handle
code :117, errstring: Structure needs cleaning
code :118, errstring: Not a XENIX named type file
code :119, errstring: No XENIX semaphores available
code :120, errstring: Is a named type file
code :121, errstring: Remote I/O error
code :122, errstring: Disk quota exceeded
code :123, errstring: No medium found
code :124, errstring: Wrong medium type
code :125, errstring: Operation canceled
code :126, errstring: Required key not available
code :127, errstring: Key has expired
code :128, errstring: Key has been revoked
code :129, errstring: Key was rejected by service
code :130, errstring: Owner died
code :131, errstring: State not recoverable
code :132, errstring: Operation not possible due to RF-kill
code :133, errstring: Memory page has hardware error
code :134, errstring: Unknown error 134
code :135, errstring: Unknown error 135
code :136, errstring: Unknown error 136
code :137, errstring: Unknown error 137
code :138, errstring: Unknown error 138
code :139, errstring: Unknown error 139
code :140, errstring: Unknown error 140
code :141, errstring: Unknown error 141
code :142, errstring: Unknown error 142
code :143, errstring: Unknown error 143
code :144, errstring: Unknown error 144
code :145, errstring: Unknown error 145
code :146, errstring: Unknown error 146
code :147, errstring: Unknown error 147
code :148, errstring: Unknown error 148
code :149, errstring: Unknown error 149
code :150, errstring: Unknown error 150
code :151, errstring: Unknown error 151
code :152, errstring: Unknown error 152
code :153, errstring: Unknown error 153
code :154, errstring: Unknown error 154
code :155, errstring: Unknown error 155
code :156, errstring: Unknown error 156
code :157, errstring: Unknown error 157
code :158, errstring: Unknown error 158
code :159, errstring: Unknown error 159
code :160, errstring: Unknown error 160
code :161, errstring: Unknown error 161
code :162, errstring: Unknown error 162
code :163, errstring: Unknown error 163
code :164, errstring: Unknown error 164
code :165, errstring: Unknown error 165
code :166, errstring: Unknown error 166
code :167, errstring: Unknown error 167
code :168, errstring: Unknown error 168
code :169, errstring: Unknown error 169
code :170, errstring: Unknown error 170
code :171, errstring: Unknown error 171
code :172, errstring: Unknown error 172
code :173, errstring: Unknown error 173
code :174, errstring: Unknown error 174
code :175, errstring: Unknown error 175
code :176, errstring: Unknown error 176
code :177, errstring: Unknown error 177
code :178, errstring: Unknown error 178
code :179, errstring: Unknown error 179
code :180, errstring: Unknown error 180
code :181, errstring: Unknown error 181
code :182, errstring: Unknown error 182
code :183, errstring: Unknown error 183
code :184, errstring: Unknown error 184
code :185, errstring: Unknown error 185
code :186, errstring: Unknown error 186
code :187, errstring: Unknown error 187
code :188, errstring: Unknown error 188
code :189, errstring: Unknown error 189
code :190, errstring: Unknown error 190
code :191, errstring: Unknown error 191
code :192, errstring: Unknown error 192
code :193, errstring: Unknown error 193
code :194, errstring: Unknown error 194
code :195, errstring: Unknown error 195
code :196, errstring: Unknown error 196
code :197, errstring: Unknown error 197
code :198, errstring: Unknown error 198
code :199, errstring: Unknown error 199
[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 9128 Nov 22 12:43 process
-rw-rw-r-- 1 gsm gsm  531 Nov 22 12:40 process.cc
[gsm@VM-4-3-centos lesson15]$ ls
Makefile  process  process.cc
[gsm@VM-4-3-centos lesson15]$ echo $?
0
[gsm@VM-4-3-centos lesson15]$ ls aaaaa
ls: cannot access aaaaa: No such file or directory
[gsm@VM-4-3-centos lesson15]$ echo $?
2
[gsm@VM-4-3-centos lesson15]$ kill -9 888888
-bash: kill: (888888) - No such process
[gsm@VM-4-3-centos lesson15]$ echo $?
1

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 9128 Nov 22 12:43 process
-rw-rw-r-- 1 gsm gsm  531 Nov 22 12:40 process.cc
[gsm@VM-4-3-centos lesson15]$ vim process.cc
[gsm@VM-4-3-centos lesson15]$ head -18 process.cc
#include <iostream>
#include <string>
#include <cstdio>
#include <string.h>
#include <errno.h>
#include <cstdlib>

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

    exit(100);
}

int main()
{
    fun();
    std::cout << "进程正常退出" << std::endl;
[gsm@VM-4-3-centos lesson15]$ make
g++ -o process process.cc -std=c++11
[gsm@VM-4-3-centos lesson15]$ ./process 
hello world
[gsm@VM-4-3-centos lesson15]$ echo $?
100
[gsm@VM-4-3-centos lesson15]$ vim process.cc
[gsm@VM-4-3-centos lesson15]$ head -19 process.cc
#include <iostream>
#include <string>
#include <cstdio>
#include <string.h>
#include <errno.h>
#include <cstdlib>

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

    return 100;
    //exit(100);
}

int main()
{
    fun();
    std::cout << "进程正常退出" << std::endl;
[gsm@VM-4-3-centos lesson15]$ make
g++ -o process process.cc -std=c++11
[gsm@VM-4-3-centos lesson15]$ ./process 
hello world
进程正常退出
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 716 Nov 22 15:23 process.cc
[gsm@VM-4-3-centos lesson15]$ man _exit
[gsm@VM-4-3-centos lesson15]$ vim process.cc 
[gsm@VM-4-3-centos lesson15]$ head -21 process.cc
#include <iostream>
#include <string>
#include <cstdio>
#include <string.h>
#include <errno.h>
#include <cstdlib>
#include <unistd.h>

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

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

int main()
{
    fun();
    std::cout << "进程正常退出" << std::endl;
[gsm@VM-4-3-centos lesson15]$ make 
g++ -o process process.cc -std=c++11
[gsm@VM-4-3-centos lesson15]$ ./process 
hello world
[gsm@VM-4-3-centos lesson15]$ echo $?
23

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 753 Nov 22 15:29 process.cc
[gsm@VM-4-3-centos lesson15]$ vim process.cc 
[gsm@VM-4-3-centos lesson15]$ head -24 process.cc 
#include <iostream>
#include <string>
#include <cstdio>
#include <string.h>
#include <errno.h>
#include <cstdlib>
#include <unistd.h>

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

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

int main()
{
    printf("进程运行结束!\n");
    sleep(2);
    exit(5);

    sleep(2);
[gsm@VM-4-3-centos lesson15]$ make
g++ -o process process.cc -std=c++11
[gsm@VM-4-3-centos lesson15]$ ./process 
进程运行结束!
[gsm@VM-4-3-centos lesson15]$ echo $?
5
[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 9240 Nov 22 15:35 process
-rw-rw-r-- 1 gsm gsm  771 Nov 22 15:35 process.cc
[gsm@VM-4-3-centos lesson15]$ vim process.cc
[gsm@VM-4-3-centos lesson15]$ head -24 process.cc
#include <iostream>
#include <string>
#include <cstdio>
#include <string.h>
#include <errno.h>
#include <cstdlib>
#include <unistd.h>

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

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

int main()
{
    printf("进程运行结束!");
    sleep(2);
    exit(5);

    sleep(2);
[gsm@VM-4-3-centos lesson15]$ make
g++ -o process process.cc -std=c++11
[gsm@VM-4-3-centos lesson15]$ ./process 
进程运行结束![gsm@VM-4-3-centos lesson15]$ echo $?
5
[gsm@VM-4-3-centos lesson15]$ vim process.cc
[gsm@VM-4-3-centos lesson15]$ head -24 process.cc
#include <iostream>
#include <string>
#include <cstdio>
#include <string.h>
#include <errno.h>
#include <cstdlib>
#include <unistd.h>

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

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

int main()
{
    printf("进程运行结束!");
    sleep(2);
    _exit(5);

    sleep(2);
[gsm@VM-4-3-centos lesson15]$ make
g++ -o process process.cc -std=c++11
[gsm@VM-4-3-centos lesson15]$ ./process 
[gsm@VM-4-3-centos lesson15]$ echo $?
5
[gsm@VM-4-3-centos lesson15]$ vim process.cc
[gsm@VM-4-3-centos lesson15]$ head -24 process.cc
#include <iostream>
#include <string>
#include <cstdio>
#include <string.h>
#include <errno.h>
#include <cstdlib>
#include <unistd.h>

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

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

int main()
{
    printf("进程运行结束!\n");
    sleep(2);
    _exit(5);

    sleep(2);
[gsm@VM-4-3-centos lesson15]$ make
g++ -o process process.cc -std=c++11
[gsm@VM-4-3-centos lesson15]$ ./process 
进程运行结束!

3. 进程等待

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 22 15:51 process
-rw-rw-r-- 1 gsm gsm  786 Nov 23 13:11 process.cc
[gsm@VM-4-3-centos lesson15]$ man fork
[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>

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 = 10;

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

       exit(0);
   }
   else
   {
       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
[gsm@VM-4-3-centos lesson15]$ ./process 
我是父进程: pid: 27920
子进程运行中, pid: 27921
我是父进程: pid: 27920
子进程运行中, pid: 27921
我是父进程: pid: 27920
子进程运行中, pid: 27921
我是父进程: pid: 27920
子进程运行中, pid: 27921
我是父进程: pid: 27920
子进程运行中, pid: 27921
我是父进程: pid: 27920
子进程运行中, pid: 27921
我是父进程: pid: 27920
子进程运行中, pid: 27921
子进程运行中, pid: 27921
我是父进程: pid: 27920
子进程运行中, pid: 27921
我是父进程: pid: 27920
我是父进程: pid: 27920
子进程运行中, pid: 27921
我是父进程: pid: 27920
我是父进程: pid: 27920
我是父进程: pid: 27920
我是父进程: pid: 27920
^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 27894 27893 27065 pts/1    27893 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
27065 27905 27904 27065 pts/1    27904 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
27065 27914 27913 27065 pts/1    27913 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 27920 27920 23159 pts/0    27920 S+    1001   0:00 ./process
27920 27921 27920 23159 pts/0    27920 S+    1001   0:00 ./process
27065 27925 27924 27065 pts/1    27924 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 27920 27920 23159 pts/0    27920 S+    1001   0:00 ./process
27920 27921 27920 23159 pts/0    27920 S+    1001   0:00 ./process
27065 27931 27930 27065 pts/1    27930 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 27920 27920 23159 pts/0    27920 S+    1001   0:00 ./process
27920 27921 27920 23159 pts/0    27920 S+    1001   0:00 ./process
27065 27939 27938 27065 pts/1    27938 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 27920 27920 23159 pts/0    27920 S+    1001   0:00 ./process
27920 27921 27920 23159 pts/0    27920 S+    1001   0:00 ./process
27065 27950 27949 27065 pts/1    27949 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 27920 27920 23159 pts/0    27920 S+    1001   0:00 ./process
27920 27921 27920 23159 pts/0    27920 S+    1001   0:00 ./process
27065 27956 27955 27065 pts/1    27955 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 27920 27920 23159 pts/0    27920 S+    1001   0:00 ./process
27920 27921 27920 23159 pts/0    27920 S+    1001   0:00 ./process
27065 27961 27960 27065 pts/1    27960 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 27920 27920 23159 pts/0    27920 S+    1001   0:00 ./process
27920 27921 27920 23159 pts/0    27920 S+    1001   0:00 ./process
27065 27966 27965 27065 pts/1    27965 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 27920 27920 23159 pts/0    27920 S+    1001   0:00 ./process
27920 27921 27920 23159 pts/0    27920 S+    1001   0:00 ./process
27065 27973 27972 27065 pts/1    27972 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 27920 27920 23159 pts/0    27920 S+    1001   0:00 ./process
27920 27921 27920 23159 pts/0    27920 S+    1001   0:00 ./process
27065 27984 27983 27065 pts/1    27983 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 27920 27920 23159 pts/0    27920 S+    1001   0:00 ./process
27920 27921 27920 23159 pts/0    27920 S+    1001   0:00 ./process
27065 27993 27992 27065 pts/1    27992 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 27920 27920 23159 pts/0    27920 S+    1001   0:00 ./process
27920 27921 27920 23159 pts/0    27920 Z+    1001   0:00 [process] <defunct>
27065 28002 28001 27065 pts/1    28001 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 27920 27920 23159 pts/0    27920 S+    1001   0:00 ./process
27920 27921 27920 23159 pts/0    27920 Z+    1001   0:00 [process] <defunct>
27065 28008 28007 27065 pts/1    28007 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 27920 27920 23159 pts/0    27920 S+    1001   0:00 ./process
27920 27921 27920 23159 pts/0    27920 Z+    1001   0:00 [process] <defunct>
27065 28017 28016 27065 pts/1    28016 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 27920 27920 23159 pts/0    27920 S+    1001   0:00 ./process
27920 27921 27920 23159 pts/0    27920 Z+    1001   0:00 [process] <defunct>
27065 28028 28027 27065 pts/1    28027 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
27065 28034 28033 27065 pts/1    28033 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
27065 28050 28049 27065 pts/1    28049 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 13552 Nov 23 13:28 process
-rw-rw-r-- 1 gsm gsm  1267 Nov 23 13:28 process.cc
[gsm@VM-4-3-centos lesson15]$ man 2 wait
[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 = 10;

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

       exit(0);
   }
   else
   {
       pid_t rid = wait(nullptr);

       if (rid > 0)
       {
           printf("wait sub process success, rid: %d\n", rid);
       }

       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
[gsm@VM-4-3-centos lesson15]$ ./process 
子进程运行中, pid: 28379
子进程运行中, pid: 28379
子进程运行中, pid: 28379
子进程运行中, pid: 28379
子进程运行中, pid: 28379
子进程运行中, pid: 28379
子进程运行中, pid: 28379
子进程运行中, pid: 28379
子进程运行中, pid: 28379
子进程运行中, pid: 28379
wait sub process success, rid: 28379
我是父进程: pid: 28378
我是父进程: pid: 28378
我是父进程: pid: 28378
我是父进程: pid: 28378
^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 28366 28365 27065 pts/1    28365 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
27065 28375 28374 27065 pts/1    28374 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 28378 28378 23159 pts/0    28378 S+    1001   0:00 ./process
28378 28379 28378 23159 pts/0    28378 S+    1001   0:00 ./process
27065 28383 28382 27065 pts/1    28382 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 28378 28378 23159 pts/0    28378 S+    1001   0:00 ./process
28378 28379 28378 23159 pts/0    28378 S+    1001   0:00 ./process
27065 28391 28390 27065 pts/1    28390 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 28378 28378 23159 pts/0    28378 S+    1001   0:00 ./process
28378 28379 28378 23159 pts/0    28378 S+    1001   0:00 ./process
27065 28402 28401 27065 pts/1    28401 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 28378 28378 23159 pts/0    28378 S+    1001   0:00 ./process
28378 28379 28378 23159 pts/0    28378 S+    1001   0:00 ./process
27065 28408 28407 27065 pts/1    28407 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 28378 28378 23159 pts/0    28378 S+    1001   0:00 ./process
28378 28379 28378 23159 pts/0    28378 S+    1001   0:00 ./process
27065 28413 28412 27065 pts/1    28412 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 28378 28378 23159 pts/0    28378 S+    1001   0:00 ./process
28378 28379 28378 23159 pts/0    28378 S+    1001   0:00 ./process
27065 28418 28417 27065 pts/1    28417 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 28378 28378 23159 pts/0    28378 S+    1001   0:00 ./process
28378 28379 28378 23159 pts/0    28378 S+    1001   0:00 ./process
27065 28425 28424 27065 pts/1    28424 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 28378 28378 23159 pts/0    28378 S+    1001   0:00 ./process
28378 28379 28378 23159 pts/0    28378 S+    1001   0:00 ./process
27065 28439 28438 27065 pts/1    28438 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 28378 28378 23159 pts/0    28378 S+    1001   0:00 ./process
28378 28379 28378 23159 pts/0    28378 S+    1001   0:00 ./process
27065 28449 28448 27065 pts/1    28448 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 28378 28378 23159 pts/0    28378 S+    1001   0:00 ./process
28378 28379 28378 23159 pts/0    28378 S+    1001   0:00 ./process
27065 28455 28454 27065 pts/1    28454 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 28378 28378 23159 pts/0    28378 S+    1001   0:00 ./process
27065 28463 28462 27065 pts/1    28462 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 28378 28378 23159 pts/0    28378 S+    1001   0:00 ./process
27065 28474 28473 27065 pts/1    28473 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 28378 28378 23159 pts/0    28378 S+    1001   0:00 ./process
27065 28480 28479 27065 pts/1    28479 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
27065 28485 28484 27065 pts/1    28484 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 13600 Nov 23 16:08 process
-rw-rw-r-- 1 gsm gsm  1449 Nov 23 16:08 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);
       }

       exit(0);
   }
   else
   {
       sleep(10);
       pid_t rid = wait(nullptr);

       if (rid > 0)
       {
           printf("wait sub process success, rid: %d\n", rid);
       }

       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
[gsm@VM-4-3-centos lesson15]$ ./process 
子进程运行中, pid: 29484
子进程运行中, pid: 29484
子进程运行中, pid: 29484
子进程运行中, pid: 29484
子进程运行中, pid: 29484
wait sub process success, rid: 29484
我是父进程: pid: 29483
我是父进程: pid: 29483
我是父进程: pid: 29483
^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 29462 29461 27065 pts/1    29461 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
27065 29471 29470 27065 pts/1    29470 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
27065 29480 29479 27065 pts/1    29479 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 29483 29483 23159 pts/0    29483 S+    1001   0:00 ./process
29483 29484 29483 23159 pts/0    29483 S+    1001   0:00 ./process
27065 29488 29487 27065 pts/1    29487 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 29483 29483 23159 pts/0    29483 S+    1001   0:00 ./process
29483 29484 29483 23159 pts/0    29483 S+    1001   0:00 ./process
27065 29508 29507 27065 pts/1    29507 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 29483 29483 23159 pts/0    29483 S+    1001   0:00 ./process
29483 29484 29483 23159 pts/0    29483 S+    1001   0:00 ./process
27065 29519 29518 27065 pts/1    29518 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 29483 29483 23159 pts/0    29483 S+    1001   0:00 ./process
29483 29484 29483 23159 pts/0    29483 S+    1001   0:00 ./process
27065 29525 29524 27065 pts/1    29524 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 29483 29483 23159 pts/0    29483 S+    1001   0:00 ./process
29483 29484 29483 23159 pts/0    29483 S+    1001   0:00 ./process
27065 29530 29529 27065 pts/1    29529 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 29483 29483 23159 pts/0    29483 S+    1001   0:00 ./process
29483 29484 29483 23159 pts/0    29483 Z+    1001   0:00 [process] <defunct>
27065 29536 29535 27065 pts/1    29535 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 29483 29483 23159 pts/0    29483 S+    1001   0:00 ./process
29483 29484 29483 23159 pts/0    29483 Z+    1001   0:00 [process] <defunct>
27065 29544 29543 27065 pts/1    29543 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 29483 29483 23159 pts/0    29483 S+    1001   0:00 ./process
29483 29484 29483 23159 pts/0    29483 Z+    1001   0:00 [process] <defunct>
27065 29555 29554 27065 pts/1    29554 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 29483 29483 23159 pts/0    29483 S+    1001   0:00 ./process
29483 29484 29483 23159 pts/0    29483 Z+    1001   0:00 [process] <defunct>
27065 29564 29563 27065 pts/1    29563 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 29483 29483 23159 pts/0    29483 S+    1001   0:00 ./process
29483 29484 29483 23159 pts/0    29483 Z+    1001   0:00 [process] <defunct>
27065 29573 29572 27065 pts/1    29572 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 29483 29483 23159 pts/0    29483 S+    1001   0:00 ./process
27065 29580 29579 27065 pts/1    29579 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 29483 29483 23159 pts/0    29483 S+    1001   0:00 ./process
27065 29588 29587 27065 pts/1    29587 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159 29483 29483 23159 pts/0    29483 S+    1001   0:00 ./process
27065 29599 29598 27065 pts/1    29598 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
27065 29606 29605 27065 pts/1    29605 S+    1001   0:00 grep --color=auto process
^C

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 1466 Nov 23 16:18 process.cc
[gsm@VM-4-3-centos lesson15]$ man waitpid
[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);
       }

       exit(0);
   }
   else
   {
       sleep(10);
       //pid_t rid = wait(nullptr);
       pid_t rid = waitpid(-1, nullptr, 0); // == wait

       if (rid > 0)
       {
           printf("wait sub process success, rid: %d\n", rid);
       }

       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
[gsm@VM-4-3-centos lesson15]$ ./process 
子进程运行中, pid: 789
子进程运行中, pid: 789
子进程运行中, pid: 789
子进程运行中, pid: 789
子进程运行中, pid: 789
wait sub process success, rid: 789
我是父进程: pid: 788
我是父进程: pid: 788
我是父进程: pid: 788
^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   771   770 27065 pts/1      770 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
27065   782   781 27065 pts/1      781 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159   788   788 23159 pts/0      788 S+    1001   0:00 ./process
  788   789   788 23159 pts/0      788 S+    1001   0:00 ./process
27065   795   794 27065 pts/1      794 R+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159   788   788 23159 pts/0      788 S+    1001   0:00 ./process
  788   789   788 23159 pts/0      788 S+    1001   0:00 ./process
27065   803   802 27065 pts/1      802 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159   788   788 23159 pts/0      788 S+    1001   0:00 ./process
  788   789   788 23159 pts/0      788 S+    1001   0:00 ./process
27065   812   811 27065 pts/1      811 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159   788   788 23159 pts/0      788 S+    1001   0:00 ./process
  788   789   788 23159 pts/0      788 S+    1001   0:00 ./process
27065   823   822 27065 pts/1      822 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159   788   788 23159 pts/0      788 S+    1001   0:00 ./process
  788   789   788 23159 pts/0      788 S+    1001   0:00 ./process
27065   829   828 27065 pts/1      828 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159   788   788 23159 pts/0      788 S+    1001   0:00 ./process
  788   789   788 23159 pts/0      788 Z+    1001   0:00 [process] <defunct>
27065   834   833 27065 pts/1      833 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159   788   788 23159 pts/0      788 S+    1001   0:00 ./process
  788   789   788 23159 pts/0      788 Z+    1001   0:00 [process] <defunct>
27065   863   862 27065 pts/1      862 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159   788   788 23159 pts/0      788 S+    1001   0:00 ./process
  788   789   788 23159 pts/0      788 Z+    1001   0:00 [process] <defunct>
27065   870   869 27065 pts/1      869 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159   788   788 23159 pts/0      788 S+    1001   0:00 ./process
  788   789   788 23159 pts/0      788 Z+    1001   0:00 [process] <defunct>
27065   881   880 27065 pts/1      880 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159   788   788 23159 pts/0      788 S+    1001   0:00 ./process
27065   890   889 27065 pts/1      889 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159   788   788 23159 pts/0      788 S+    1001   0:00 ./process
27065   899   898 27065 pts/1      898 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159   788   788 23159 pts/0      788 S+    1001   0:00 ./process
27065   905   904 27065 pts/1      904 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
27065   913   912 27065 pts/1      912 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 13608 Nov 23 16:29 process
-rw-rw-r-- 1 gsm gsm  1523 Nov 23 16:29 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);
       }

       exit(0);
   }
   else
   {
       sleep(10);
       //pid_t rid = wait(nullptr);
       pid_t rid = waitpid(id, nullptr, 0); // == wait

       if (rid > 0)
       {
           printf("wait sub process success, rid: %d\n", rid);
       }

       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
[gsm@VM-4-3-centos lesson15]$ ./process 
子进程运行中, pid: 1748
子进程运行中, pid: 1748
子进程运行中, pid: 1748
子进程运行中, pid: 1748
子进程运行中, pid: 1748
wait sub process success, rid: 1748
我是父进程: pid: 1747
我是父进程: pid: 1747
我是父进程: pid: 1747
^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  1743  1742 27065 pts/1     1742 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159  1747  1747 23159 pts/0     1747 S+    1001   0:00 ./process
 1747  1748  1747 23159 pts/0     1747 S+    1001   0:00 ./process
27065  1752  1751 27065 pts/1     1751 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159  1747  1747 23159 pts/0     1747 S+    1001   0:00 ./process
 1747  1748  1747 23159 pts/0     1747 S+    1001   0:00 ./process
27065  1763  1762 27065 pts/1     1762 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159  1747  1747 23159 pts/0     1747 S+    1001   0:00 ./process
 1747  1748  1747 23159 pts/0     1747 S+    1001   0:00 ./process
27065  1773  1772 27065 pts/1     1772 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159  1747  1747 23159 pts/0     1747 S+    1001   0:00 ./process
 1747  1748  1747 23159 pts/0     1747 S+    1001   0:00 ./process
27065  1782  1781 27065 pts/1     1781 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159  1747  1747 23159 pts/0     1747 S+    1001   0:00 ./process
 1747  1748  1747 23159 pts/0     1747 S+    1001   0:00 ./process
27065  1788  1787 27065 pts/1     1787 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159  1747  1747 23159 pts/0     1747 S+    1001   0:00 ./process
 1747  1748  1747 23159 pts/0     1747 Z+    1001   0:00 [process] <defunct>
27065  1796  1795 27065 pts/1     1795 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159  1747  1747 23159 pts/0     1747 S+    1001   0:00 ./process
 1747  1748  1747 23159 pts/0     1747 Z+    1001   0:00 [process] <defunct>
27065  1807  1806 27065 pts/1     1806 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159  1747  1747 23159 pts/0     1747 S+    1001   0:00 ./process
 1747  1748  1747 23159 pts/0     1747 Z+    1001   0:00 [process] <defunct>
27065  1813  1812 27065 pts/1     1812 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159  1747  1747 23159 pts/0     1747 S+    1001   0:00 ./process
 1747  1748  1747 23159 pts/0     1747 Z+    1001   0:00 [process] <defunct>
27065  1818  1817 27065 pts/1     1817 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159  1747  1747 23159 pts/0     1747 S+    1001   0:00 ./process
 1747  1748  1747 23159 pts/0     1747 Z+    1001   0:00 [process] <defunct>
27065  1825  1824 27065 pts/1     1824 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159  1747  1747 23159 pts/0     1747 S+    1001   0:00 ./process
27065  1832  1831 27065 pts/1     1831 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159  1747  1747 23159 pts/0     1747 S+    1001   0:00 ./process
27065  1850  1849 27065 pts/1     1849 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
27065  1860  1859 27065 pts/1     1859 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
27065  1869  1868 27065 pts/1     1868 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 13608 Nov 23 16:32 process
-rw-rw-r-- 1 gsm gsm  1523 Nov 23 16:32 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);
       }

       exit(0);
   }
   else
   {
       sleep(10);
       //pid_t rid = wait(nullptr);
       pid_t rid = waitpid(id + 1, nullptr, 0); // == wait

       if (rid > 0)
       {
           printf("wait sub process success, rid: %d\n", rid);
       }
       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
[gsm@VM-4-3-centos lesson15]$ ./process 
子进程运行中, pid: 3072
子进程运行中, pid: 3072
子进程运行中, pid: 3072
子进程运行中, pid: 3072
子进程运行中, pid: 3072
waitpid: No child processes
我是父进程: pid: 3071
我是父进程: pid: 3071
我是父进程: pid: 3071
^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 16:37 process
-rw-rw-r-- 1 gsm gsm  1587 Nov 23 16:37 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);
       }

       exit(1);
   }
   else
   {
       sleep(10);
       //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); // ??
       }
       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
[gsm@VM-4-3-centos lesson15]$ ./process 
子进程运行中, pid: 5779
子进程运行中, pid: 5779
子进程运行中, pid: 5779
子进程运行中, pid: 5779
子进程运行中, pid: 5779
wait sub process success, rid: 5779, status code: 256
我是父进程: pid: 5778
我是父进程: pid: 5778
我是父进程: pid: 5778
^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  5770  5769 27065 pts/1     5769 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159  5778  5778 23159 pts/0     5778 S+    1001   0:00 ./process
 5778  5779  5778 23159 pts/0     5778 S+    1001   0:00 ./process
27065  5784  5783 27065 pts/1     5783 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159  5778  5778 23159 pts/0     5778 S+    1001   0:00 ./process
 5778  5779  5778 23159 pts/0     5778 S+    1001   0:00 ./process
27065  5790  5789 27065 pts/1     5789 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159  5778  5778 23159 pts/0     5778 S+    1001   0:00 ./process
 5778  5779  5778 23159 pts/0     5778 S+    1001   0:00 ./process
27065  5799  5798 27065 pts/1     5798 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159  5778  5778 23159 pts/0     5778 S+    1001   0:00 ./process
 5778  5779  5778 23159 pts/0     5778 S+    1001   0:00 ./process
27065  5810  5809 27065 pts/1     5809 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159  5778  5778 23159 pts/0     5778 S+    1001   0:00 ./process
 5778  5779  5778 23159 pts/0     5778 S+    1001   0:00 ./process
27065  5816  5815 27065 pts/1     5815 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159  5778  5778 23159 pts/0     5778 S+    1001   0:00 ./process
 5778  5779  5778 23159 pts/0     5778 Z+    1001   0:00 [process] <defunct>
27065  5821  5820 27065 pts/1     5820 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159  5778  5778 23159 pts/0     5778 S+    1001   0:00 ./process
 5778  5779  5778 23159 pts/0     5778 Z+    1001   0:00 [process] <defunct>
27065  5826  5825 27065 pts/1     5825 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159  5778  5778 23159 pts/0     5778 S+    1001   0:00 ./process
 5778  5779  5778 23159 pts/0     5778 Z+    1001   0:00 [process] <defunct>
27065  5833  5832 27065 pts/1     5832 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159  5778  5778 23159 pts/0     5778 S+    1001   0:00 ./process
 5778  5779  5778 23159 pts/0     5778 Z+    1001   0:00 [process] <defunct>
27065  5844  5843 27065 pts/1     5843 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159  5778  5778 23159 pts/0     5778 S+    1001   0:00 ./process
 5778  5779  5778 23159 pts/0     5778 Z+    1001   0:00 [process] <defunct>
27065  5853  5852 27065 pts/1     5852 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159  5778  5778 23159 pts/0     5778 S+    1001   0:00 ./process
27065  5862  5861 27065 pts/1     5861 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
23159  5778  5778 23159 pts/0     5778 S+    1001   0:00 ./process
27065  5868  5867 27065 pts/1     5867 S+    1001   0:00 grep --color=auto process
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
27065  5876  5875 27065 pts/1     5875 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 16:51 process
-rw-rw-r-- 1 gsm gsm  1632 Nov 23 16:51 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);
       }

       exit(1);
   }
   else
   {
       sleep(10);
       //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
[gsm@VM-4-3-centos lesson15]$ ./process 
子进程运行中, pid: 11741
子进程运行中, pid: 11741
子进程运行中, pid: 11741
子进程运行中, pid: 11741
子进程运行中, pid: 11741
wait sub process success, rid: 11741, status code: 1
我是父进程: pid: 11740
我是父进程: pid: 11740
我是父进程: pid: 11740
^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 19:59 process
-rw-rw-r-- 1 gsm gsm  1640 Nov 23 19:59 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);
       }
       
       // 我们可不可以使用全局变量,来获取子进程的退出码呢? 不可以
       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
[gsm@VM-4-3-centos lesson15]$ ./process 
子进程运行中, pid: 12277
子进程运行中, pid: 12277
子进程运行中, pid: 12277
子进程运行中, pid: 12277
子进程运行中, pid: 12277
wait sub process success, rid: 12277, status code: 123
我是父进程: pid: 12276
我是父进程: pid: 12276
我是父进程: pid: 12276
^C

(接着进程控制(中)继续看)

相关推荐
程序员三明治1 小时前
【Java】synchronized关键字详解:从字节码到对象头与锁升级
java·开发语言·juc·synchronized··锁升级
Mr.Ja1 小时前
【Docker 从入门到实战】——解决跨环境部署痛点的完整指南
运维·docker·容器·dockerfile·dockerimage
q***87601 小时前
Nginx 常用安全头
运维·nginx·安全
youxiao_901 小时前
LVS负载均衡集群与LVS+Keepalived集群
运维·负载均衡·lvs
y***54881 小时前
Rust在嵌入式中的实时操作系统
开发语言·后端·rust
老虎06271 小时前
Java基础面试题(11)—Java(泛型)
java·开发语言·windows
SweerItTer1 小时前
RK3566 泰山派 IMX415驱动移植+设备树修改+iq文件复制
linux·csdn·泰山派·imx415·rk356x·驱动移植
i***t9191 小时前
Nginx 之Rewrite 使用详解
运维·nginx
Bigan(安)1 小时前
【奶茶Beta专项】【LVGL9.4源码分析】03-显示框架-图层管理
linux·c语言·mcu·arm·unix