在CentOS系統中,僵尸進程是指子進程已經結束,但其父進程沒有正確回收其資源,導致子進程的進程描述符仍然保留在系統中。以下是修復CentOS僵尸進程的幾種方法:
wait() 或 waitpid()在父進程中,確保在子進程結束后調用 wait() 或 waitpid() 來回收子進程的資源。以下是一個簡單的示例代碼:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
pid_t pid = fork();
if (pid < 0) {
perror("fork failed");
exit(1);
} else if (pid == 0) { // 子進程
printf("Child process is running
");
sleep(2);
printf("Child process is exiting
");
exit(0);
} else { // 父進程
printf("Parent process is waiting for child
");
wait(NULL); // 等待子進程結束
printf("Parent process is exiting
");
}
return 0;
}
如果父進程無法立即調用 wait(),可以通過設置信號處理函數來捕獲 SIGCHLD 信號,并在信號處理函數中調用 wait()。以下是一個示例:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
void sigchld_handler(int signo) {
pid_t pid;
int status;
while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
printf("Child process %d terminated
", pid);
}
}
int main() {
signal(SIGCHLD, sigchld_handler);
pid_t pid = fork();
if (pid < 0) {
perror("fork failed");
exit(1);
} else if (pid == 0) { // 子進程
printf("Child process is running
");
sleep(2);
printf("Child process is exiting
");
exit(0);
} else { // 父進程
printf("Parent process is running
");
while (1) {
sleep(1);
}
}
return 0;
}
如果父進程已經無法正常工作,或者你無法修改父進程的代碼,可以考慮殺死父進程。當父進程被殺死后,僵尸進程會被 init 進程(PID為1)接管并回收。
kill -9 <父進程ID>
lsof 命令查找和清理僵尸進程lsof 命令可以用來查找和清理僵尸進程。以下是具體步驟:
安裝 lsof(如果尚未安裝):
yum install lsof
查找僵尸進程:
lsof -n | grep deleted
殺死僵尸進程:
kill -9 $(lsof -n | grep deleted | awk '{print $2}')
如果系統中有多個僵尸進程,可以使用以下命令批量清理:
ps -A -o stat,ppid,pid,cmd | grep -e '[Zz]' | awk '{print $2}' | xargs kill -9