Linux System Programming 01

1 fork function

1.1 Introduction to the usage of fork function

Prototype of function: pid_t fork (void) pid_t is a returned identifier, which may be defined differently on different platforms, may be int or long type, etc. pid_t is defined in sys / types.h

	pid_t pid = fork();
	if (pid == 0) {//子进程
	}
	if(pid>0){ //父进程,此时pid的值是子进程的id号

	}
	if(pid==-1){  //此时创建子进程失败

	}

Reasons why the process of fork creation fails: the number of processes exceeds the limit or the memory space is insufficient

1.2 fork creates process space sharing problem

In the beginning, the child process created by fork is different from the parent process except for pcb.

1.3 The problem of zombie and orphan processes caused by creating child processes

Zombie process: After the child process exits before the parent process, the child process PCB needs its parent process to release, but the parent process does not release the child process PCB. Such a child process is called a zombie process. The zombie process is actually a Dead process, but still occupies memory space

Orphan process: A parent process exits while one or more of its child processes are still running, then those child processes will become orphan processes. The orphan process will be adopted by the init process (process number 1), and the init process will complete the status collection work for them, and will not occupy the ID like a zombie process, which will damage the running system.

Ways to solve the zombie process:

  • Kill the parent process, the child process resources will be recovered by the init process, but generally the parent process is a server program, it is impossible to use this violent way
  • Use wait or waitpid function in the parent process, use wait function to reclaim resources will cause the parent process to block, reasonable use of waitpid function will not cause the parent process to block
  • Using signal processing, when the child process exits, it will send the SIGCHLD signal like the parent process, so we can set the method of processing this signal at the beginning of the program, but when multiple signals arrive at the same time, it may cause the signal to be lost.

2 wait function and waitpid function

2.1 Introduction

When a process terminates, it will close all file descriptors and release the memory allocated in user space, but its PCB still retains it. The kernel saves some information in it: if it is terminated normally, the exit status is saved, if it is terminated abnormally Then it is saved which signal caused the process to terminate. The parent process of this process can call wait or waitpid to obtain this information, and then completely clear the process

2.1 wait

This function has three functions:

  • Block waiting for the child process to exit
  • Reclaim residual resources of subprocesses
  • Get the end status of the child process (exit reason).

Once wait is called, it will block here until a child process exits

Function prototype:

pid_t wait(int *status);   //如果不关心程序退出的状态,可以传入NULL

成功:清理掉的子进程ID;失败:-1 (没有子进程)

Use the wait function to pass the parameter status to save the exit status of the process (normal termination → exit value; abnormal termination → termination signal). With the help of macro functions to further determine the specific cause of the process termination, the macro functions refer to the man command in Linux

2.2 waitpid

Function prototype:

pid_t waitpid(pid_t pid,int *status,int options);

//第一个参数
pid<-1  等待进程组号为pid绝对值的任何子进程
pid=-1  等待任何子进程,此时的waitpid()函数就退化成了普通的wait()函数。
pid=0   等待进程组号与目前进程相同的任何子进程,也就是说任何和调用waitpid()函数的进程在同一个进程组的进程。
pid>0   等待进程号为pid的子进程。

//第二个参数需要搭配具体的宏函数来获得其状态,如果不关心具体的状态则传入NULL

//第三个参数,其参数可以用 “|” 运算符连接起来使用。
WNOHANG  如果pid指定的子进程没有结束,则waitpid()函数立即返回0,而不是阻塞在这个函数上等待;如果结束了,则返回该子进程的进程号。
WUNTRACED	如果子进程进入暂停状态,则马上返回。

If the waitpid () function is successfully executed, the process number of the child process is returned; if an error occurs, -1 is returned, and the cause of the failure is stored in the errno variable.

The main reasons for failure are: no child process (errno is set to ECHILD), the call is interrupted by a signal (errno is set to EINTR) or the option parameter is invalid (errno is set to EINVAL)

waitpid (-1, status, 0) Degenerate equivalent to wait (status)

3 Exec function cluster

Reference blog: https://blog.csdn.net/amoscykl/article/details/80354052

 

Published 123 original articles · praised 31 · 90,000 views +

Guess you like

Origin blog.csdn.net/qq_40794602/article/details/104646846