⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 killzombie.c

📁 linux下创建多进程的程序。并且干净的杀死僵尸进程。
💻 C
字号:
/*------------------------------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>          /* netbd.h is needed for struct hostent =) */

#define PROCESSNUM	25

int main(int argc, char * argv[])
{
		int nRunProcessNum;
		pid_t pid;
		int nWaitRet;
		int status;
		int nWaitRetStatus;

		nRunProcessNum = 0;
		
		int i;
		
		for (i = 0; i < PROCESSNUM; i++) 
		{
			nRunProcessNum++;
			
				pid = fork();
				if (pid == 0 || pid == -1)
						break;
		}
		
		if (pid == -1) 
		{
				printf("fork() Error !!!\n");
		} 
		else if (pid == 0) 
		{
				// child process.
				printf("This is child process, pid = [%d], parent pid = [%d], nRunProcessNum = [%d].\n", getpid(), getppid(), nRunProcessNum);
				exit(0);
		}
		else 
		{ 
				//parent process 
				
				for (;;)
		    {
		        nWaitRet = waitpid(-1, &status, 0);
		        if( WIFEXITED(status) )
		        {
		        		nWaitRetStatus = WEXITSTATUS(status);
		        		printf("waitpid() Normal Exit, nWaitRet = [%d], status = [%d].\n", nWaitRet, nWaitRetStatus);
		        }
		        else
		        {
								printf("waitpid() unNormal Exit, nWaitRet = [%d].\n", nWaitRet);
				    }
				    
		    		if ( !nWaitRet )
		        {
		        		printf("waitpid() !nWaitRet = [%d].\n", nWaitRet);
		            break;
		        }
		        if (nWaitRet == -1)
		        {
		        		printf("waitpid() nWaitRet == -1, errno = [%d].\n", errno);
		            break;
		        }
		    }
		}
		
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -