Zombie process and daemon of Linux c/c + + process

1. Zombie process

1.1 definition of zombie process:

The parent process creates a child process, and the parent process ends before the child process. If the resources of the child process are not released, it will become a zombie process and continue to occupy system resources

1.2 solutions to zombie process

Before ending, the child process will send SIGCHLD signal to the parent process. After receiving the signal, the parent process will recycle the child process resources first, and then the parent process will end itself

To prevent zombie processes, you can write a wait function (or waitpid function) in the parent process to wait for the SIGCHLD signal sent by the child process

Without wait() function:

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(){
	if(fork()){
		printf("The child process is created!\n");
	}else{
		for(int i = 0;i<5;i++){
			printf("Subprocess execution:%d\n",i+1);
			sleep(1);
		}
	}
	printf("End of parent process!\n\n");
	return 0;
}

Operation results:

 

As can be seen from the above output results, without the wait() function, the parent process will not wait for the child process to finish before executing "parent process ends!" In this sentence, if the parent process is forced to end before the child process ends, the zombie process will occur

With wait() function:

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(){
	if(fork()){
		printf("The child process is created!\n");
		wait(0);
	}else{
		for(int i = 0;i<5;i++){
			printf("Subprocess execution:%d\n",i+1);
			sleep(1);
		}
	}
	printf("End of parent process!\n\n");
	return 0;
}

Operation results:

 

When there is a wait() function, the parent process will wait for the SIGCHLD signal sent by the child process. When the parent process receives the signal, the child process resources will be recovered first, and then the parent process will end

2. Daemon

2.1 # daemon concept

Record the status of other processes independently, and record the operating system log, etc

2.2 viewing method of daemon

 ps  -axj

TPGID of - 1 is a daemon

  

2.3 creation of daemon

2.3.1 the function implementation of the daemon is the same as that of other processes

2.3.2 steps to turn a process into a daemon

  • Kill its parent process
  • Get rid of terminal control (close 0 1 2 file descriptor and redirect the io operation of the current process to / dev/NULL (black hole device))
  • Get rid of the original session and create a new session under the control of the process group

2.4 create daemon programming model

2.4.1 create daemon mode 1

  1. Create a new session: setsid() function
  2. Change the current working directory and use the chdir() function
  3. Reset the permissions of the current file and use the {umask() function
  4. Close file() function

2.4.2 create daemon mode 2

  1. Reset file permissions - umask() function
  2. Create subprocess {fork() function
  3. End parent process
  4. Create new session {setsid()
  5. Prevent the child process from becoming a zombie process. The sigcld # SIGHUP signal() function ignores the sigcld # SIGHUP signal
  6. Change the current working directory and use the chdir() function
  7. Redirection file description symbol {open() function} dup2() function
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>

int main(){
	// 1. Reset file permissions
	umask(0);
	// 2. Create sub process
	int ret = fork();
	if(ret < 0) printf("Failed to create process:%m"),exit(-1);
	if(ret > 0) printf("End of parent process:%m\n"),exit(0); 	// 3. Let the parent process end
	if(0 == ret){
		printf("pid:%d\n",getpid());
		// 4. Create a new session
		setsid();
		// 5. Prevent the child process from becoming a zombie process and ignore the SIGCHLD SIGHUP signal
		signal(SIGCHLD,SIG_IGN);
		signal(SIGHUP,SIG_IGN);
		// 6. Change the current working directory
		chdir("/");
		// 7. Redirect file descriptor
		int fd = open("/dev/NULL",O_RDWR);
		dup2(fd,0);
		dup2(fd,1);
	}
	
	while(1){
		//Do something (simulate daemon work)
		sleep(1);
	}

	return 0;
}

Results:

 

Shut down daemons:

 

It can be seen that the daemon with pid 3469 created above has been closed

2.5 organization form of process

Multiple processes form a process group

Multiple process groups form a session

Note: the leader of the process group is the session process

2.6 shutdown of daemon

Directly use kill 2 pid to kill the specified process

2.7 terminal standard I / O equipment

  • Standard input device
  • 1. Standard output equipment
  • 2. Standard error output device

Keywords: C C++ Linux

Added by DaPrince on Sat, 05 Mar 2022 17:22:58 +0200