zombies 和 init process

作者: perryn | 来源:发表于2019-06-16 14:57 被阅读1次

进程关系

zombies进程

  • linux中的pid号和file的fd号的规则不同,pid的增长是逐次递增使用,如果达到上限再来一次从小到大的周期,寻找可用的pid号。如果没有就会出现系统错误。
  • linux fork后子进程先于父进程退出,这个进程子进程就是zombies的进程,zombies的进程占用的是pcb块资源以及pid资源,zombies的进程最终由1号(init process接管),统一由init process回收这些zombies进程,但是这个回收zombies的进程是异步的

孤儿进程

  • linux fork之后父进程退出,子进程存活,这些子进程是孤儿进程,孤儿进程是由init process接管。这些孤儿进程最开始得父进程是fork出来这些子进程的父进程,之后父进程退出后,孤儿进程的父进程就是1号进程(init process)

zombies 代码例子

image.png
/*************************************************************************
  > File Name: fork0.c
  > Author:perrynzhou 
  > Mail:perrynzhou@gmail.com 
  > Created Time: Sun 16 Jun 2019 01:55:54 PM CST
 ************************************************************************/

#include <stdio.h>
#include <getopt.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int is_num(const char *s)
{
  if (NULL == s)
  {
    return -1;
  }
  size_t len = strlen(s);
  for (int i = 0; i < len; i++)
  {
    if (isdigit(s[i]) == 0)
    {
      return -1;
    }
  }
  return 0;
}
int main(int argc,char *argv[])
{
  int pn, fn;
  int default_pn = 2;
  const char *cmd_line = "p:";
  char ch;
  while ((ch = getopt(argc, argv, cmd_line)) != -1)
  {
    int flag = is_num(optarg);
    switch(ch)
    {
    case 'p':
      pn = (flag == 0) ? atoi(optarg) : default_pn;
      break;
    default:
      break;
    }
  }
  fprintf(stdout,"process number:%d\n",pn);
  fflush(NULL);
  for (int i = 0; i < pn; i++)
  {
    fflush(NULL);
    pid_t pid = fork();
    if (pid == -1)
    {
      perror("fork()");
      exit(1);
    }
    if (pid == 0)
    {
      fprintf(stdout, "child process %ld,parent process %ld\n", getpid(), getppid());
      //sleep(100000); 
      exit(0);
    }
  }
  sleep(10000);
  exit(0);
  return 0;
}
孤儿进程
image.png
/*************************************************************************
  > File Name: fork0.c
  > Author:perrynzhou 
  > Mail:perrynzhou@gmail.com 
  > Created Time: Sun 16 Jun 2019 01:55:54 PM CST
 ************************************************************************/

#include <stdio.h>
#include <getopt.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int is_num(const char *s)
{
  if (NULL == s)
  {
    return -1;
  }
  size_t len = strlen(s);
  for (int i = 0; i < len; i++)
  {
    if (isdigit(s[i]) == 0)
    {
      return -1;
    }
  }
  return 0;
}
int main(int argc,char *argv[])
{
  int pn, fn;
  int default_pn = 2;
  const char *cmd_line = "p:";
  char ch;
  while ((ch = getopt(argc, argv, cmd_line)) != -1)
  {
    int flag = is_num(optarg);
    switch(ch)
    {
    case 'p':
      pn = (flag == 0) ? atoi(optarg) : default_pn;
      break;
    default:
      break;
    }
  }
  fprintf(stdout,"process number:%d\n",pn);
  fflush(NULL);
  for (int i = 0; i < pn; i++)
  {
    fflush(NULL);
    pid_t pid = fork();
    if (pid == -1)
    {
      perror("fork()");
      exit(1);
    }
    if (pid == 0)
    {
      fprintf(stdout, "child process %ld,parent process %ld\n", getpid(), getppid());
      sleep(100000); 
      exit(0);
    }
  }
 // sleep(10000);
  exit(0);
  return 0;
}

相关文章

网友评论

    本文标题:zombies 和 init process

    本文链接:https://www.haomeiwen.com/subject/hgkgfctx.html