首页 > 代码库 > fork,defuct僵尸示例
fork,defuct僵尸示例
#define __USE_LARGEFILE64
#define _LARGEFILE64_SOURCE
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>
#include<string.h>
#include <pthread.h>
#include <sys/wait.h>
#include <signal.h>
int main (int argc, char *argv[])
{
int flag = 1;
pid_t pid;
pid = fork();
while (1)
{
if(1 == flag)
{
flag = 0;
if (pid == 0)
{
printf("in child,pid:%d,ppid:%d,then exit child...\n",getpid(),getppid());
exit (0);
}
else
{
printf("in parent,pid:%d,ppid:%d,then waitpid...\n",getpid(),getppid());
#if 1
/*when the 3rd para is 0 WUNTRACED WSTOPPED WCONTINUED,child process won‘t defunct*/
waitpid(pid, NULL, 0);
#else
/*when the 3rd para is WNOHANG WEXITED WNOWAIT,child process will defunct*/
waitpid(pid, NULL, WNOHANG);
#endif
}
}
}
}
#define _LARGEFILE64_SOURCE
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>
#include<string.h>
#include <pthread.h>
#include <sys/wait.h>
#include <signal.h>
int main (int argc, char *argv[])
{
int flag = 1;
pid_t pid;
pid = fork();
while (1)
{
if(1 == flag)
{
flag = 0;
if (pid == 0)
{
printf("in child,pid:%d,ppid:%d,then exit child...\n",getpid(),getppid());
exit (0);
}
else
{
printf("in parent,pid:%d,ppid:%d,then waitpid...\n",getpid(),getppid());
#if 1
/*when the 3rd para is 0 WUNTRACED WSTOPPED WCONTINUED,child process won‘t defunct*/
waitpid(pid, NULL, 0);
#else
/*when the 3rd para is WNOHANG WEXITED WNOWAIT,child process will defunct*/
waitpid(pid, NULL, WNOHANG);
#endif
}
}
}
}
fork,defuct僵尸示例
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。