Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Chapter 3: analysis of the zombie (defunct) process

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Catalogue

1 preface

2 zombie process

2.1 introduction to the process

2.2 example of a zombie process

2.3 Zombie process hazards

3 handling zombie processes

3.1 kill command

3.2 kill parent process

3.3 reboot

3.4 magic sysrq key method

1 preface

There are occasional systemctl failures when centos7 runs Docker and K8s, as follows:

Failed to get properties...

Check the system process and find the zombie process (zombie/defunct):

Ps-ef | grep defunct

2 zombie process

2.1 introduction to the process

In linux, the parent process creates a child process through a fork call.

After the child process completes execution, the kernel releases the resources occupied by the child process, including open files, memory, etc., but still retains a slot in the process table to store the child process's file descriptors (such as process PID, process exit status, process run time, etc.) until the parent process sends a wait () or waitpid () call The kernel removes the child process file descriptor from the process table completely. If the parent process does not call wait () or waitpid () to clean up the child process, the child process will be in a zombie state.

But if the parent process ends before the child process, will it cause the child process to become a zombie process? The answer is no. Because every time the process ends, the system will scan all running processes to see if there is a child of the ending process. If so, the init process (or systemd process) will take over the child process, become the new parent of the child process, and automatically wait the child process to ensure that the child process will not become a zombie process in the future.

2.2 example of a zombie process

The following shows an example of a zombie process written in c language, in which the main process does not wait the child process and generates the file zombie.c:

# include

# include

# include

Int main (void) {

Int I = 60

Pid_t pid = fork ()

If (pid

< 0 ) { perror( "fork error." ); exit(1); } if ( pid == 0 ) { printf( "This is the child process. My PID is: %d. My PPID is: %d\n", getpid(), getppid() ); } if (pid >

0) {

Printf ("This is the parent process. My PID is% d.\ n", getpid ())

For (; I > 0; iMurray -) {

Sleep (1)

}

}

Return 0

}

Compile zombie.c and execute zombie:

Yum install gcc

Gcc zombie.c-o zombie

. / zombie

In the figure above, the main process PID:11552 and the child process PID:11553. Execute the following statement: it is found that the child process with a PID of 11553 happens to be in a defunct state, which is known by the program because the main process does not have a wait child process.

Ps aux | grep-I defunct

Analyze zombie.c and pay special attention to the fork () call. Before the pid_t pid = fork () statement, there is only one process, but after this statement is executed, it becomes two processes. The two processes are almost identical, and the next statement to be executed is if (pid).

< 0 )。 fork() 函数比较特殊,它被调用一次,却能够返回两次结果,它的返回值也根据进程的不同而不同: 1)在父进程中,fork 返回新创建子进程的 PID 2) 在子进程中,fork 返回 0 3)如果出现错误,则 fork 返回负值 2.3 僵尸进程危害 如果父进程没有 wait 子进程,子进程将变成僵尸状态,处于僵尸状态的进程将保留进程号(PID),众所周知,操作系统对进程号是有限制的,如果出现大量僵尸进程占用进程号,系统有可能无法创建新的进程。 3 处理僵尸进程 一般情况下处于僵尸状态的进程很难杀掉,当然你可以试着删除: 3.1 kill 命令 kill -9 PID 3.2 kill 父进程 kill -9 PPID 3.3 reboot 如果采用上面两种方式依然杀不掉,那么只能通过重启了。 reboot 如果重启也不生效,可以需要加选项 -nf reboot -nf 3.4 magic sysrq key 方法 有时执行 reboot 命令还是无法重启,可以执行 magic sysrq 方法来通过提供给用户的 proc 接口直接向 kernel 发底层命令。 重启命令如下: echo 1 >

/ proc/sys/kernel/sysrq

Echo b > / proc/sysrq-trigger

Forced shutdown command:

Echo 1 > / proc/sys/kernel/sysrq

Echo b > / proc/sysrq-trigger

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report