在Ubuntu系統中,僵尸進程(Zombie Process)是指已經結束運行但尚未被其父進程回收資源的進程。僵尸進程的產生通常有以下幾種原因:
當一個子進程結束時,操作系統會將其狀態設置為EXIT_ZOMBIE
,并通知其父進程。父進程需要調用wait()
或waitpid()
系統調用來讀取子進程的退出狀態,并回收其資源。如果父進程沒有這樣做,子進程就會變成僵尸進程。
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
} else if (pid == 0) {
// 子進程
printf("Child process exiting...\n");
exit(EXIT_SUCCESS);
} else {
// 父進程
int status;
printf("Parent process waiting for child...\n");
waitpid(pid, &status, 0); // 等待子進程結束并回收資源
printf("Child process has been reaped.\n");
}
return 0;
}
如果父進程在子進程結束之前退出,子進程可能會變成僵尸進程,因為沒有父進程來回收它的資源。這種情況下,init進程(PID為1)會自動成為這些僵尸進程的父進程,并最終回收它們。
如果父進程在調用wait()
或waitpid()
時被信號中斷,它可能會忽略子進程的退出狀態,導致子進程變成僵尸進程。為了避免這種情況,可以使用waitpid()
的WNOHANG
選項來非阻塞地檢查子進程狀態。
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
volatile sig_atomic_t stop = 0;
void signal_handler(int signum) {
stop = 1;
}
int main() {
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
} else if (pid == 0) {
// 子進程
printf("Child process exiting...\n");
exit(EXIT_SUCCESS);
} else {
// 父進程
struct sigaction sa;
sa.sa_handler = signal_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGINT, &sa, NULL);
while (!stop) {
int status;
pid_t result = waitpid(pid, &status, WNOHANG);
if (result == -1) {
perror("waitpid");
break;
} else if (result == 0) {
// 沒有子進程退出
sleep(1);
} else {
// 子進程已退出
printf("Child process has exited.\n");
break;
}
}
}
return 0;
}
在多線程程序中,如果主線程創建了子線程,并且主線程在子線程結束之前退出,子線程可能會變成僵尸進程。為了避免這種情況,可以使用pthread_join()
函數來等待子線程結束并回收其資源。
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void* thread_func(void* arg) {
printf("Thread is running...\n");
return NULL;
}
int main() {
pthread_t thread;
int status;
if (pthread_create(&thread, NULL, thread_func, NULL) != 0) {
perror("pthread_create");
exit(EXIT_FAILURE);
}
printf("Main thread waiting for child thread...\n");
pthread_join(thread, &status); // 等待子線程結束并回收資源
printf("Child thread has been reaped.\n");
return 0;
}
通過以上方法,可以有效地避免和處理Ubuntu系統中的僵尸進程問題。