fork() and vfork() getppid's result

简介:
fork和vfork的描述和区别参考本文最后的部分.
下面来看看用fork或vfork创建的子进程, 子进程看到的ppid(父进程是多少).
fork : 
[root@db-172-16-3-150 zzz]# cat a.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>

const int MAX_PROCS = 5;

int main() {
  fprintf(stdout, "this is parent process, fork start.pid:%i, ppid:%i\n", (int)(getpid()), (int)(getppid()));
  int i;
  pid_t p;
  for(i=0;i<MAX_PROCS;i++) {
    //p = vfork();
    p = fork();
    if(!p) {
      break;
    }
  }
  if(!p) {
    fprintf(stdout, "i:%i, this is child process. parent process not exit. ppid:%i\n", i, (int)(getppid()));
    sleep(10);
    fprintf(stdout, "i:%i, this is child process. parent process exited. ppid:%i\n", i, (int)(getppid()));
    return 0;
  }
  fprintf(stdout, "i:%i, this is parent process, fork end.pid:%i, ppid:%i\n", i, (int)(getpid()), (int)(getppid()));
  sleep(5);
  return 0;
}
结果 : 
[root@db-172-16-3-150 zzz]# gcc -O3 -Wall -Werror -Wextra -g ./a.c -o abcd && ./abcd
this is parent process, fork start.pid:6094, ppid:12826
i:0, this is child process. parent process not exit. ppid:6094
i:1, this is child process. parent process not exit. ppid:6094
i:2, this is child process. parent process not exit. ppid:6094
i:3, this is child process. parent process not exit. ppid:6094
i:4, this is child process. parent process not exit. ppid:6094
i:5, this is parent process, fork end.pid:6094, ppid:12826
[root@db-172-16-3-150 zzz]# 
// 当主进程退出后, 子进程的ppid会变成1. 也就是init会接管这些子进程.
[root@db-172-16-3-150 zzz]# 
i:0, this is child process. parent process exited. ppid:1
i:1, this is child process. parent process exited. ppid:1
i:2, this is child process. parent process exited. ppid:1
i:3, this is child process. parent process exited. ppid:1
i:4, this is child process. parent process exited. ppid:1


接下来看看环境变量会不会也变掉 . 
从结果来看, 环境变量会继承下去, 不会因为init接管而改变.
[root@db-172-16-3-150 zzz]# cat a.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

const int MAX_PROCS = 5;

int main() {
  fprintf(stdout, "this is parent process, fork start.pid:%i, ppid:%i\n", (int)(getpid()), (int)(getppid()));
  fprintf(stdout, "getenv(TEST):%s\n", getenv("TEST"));
  int i;
  pid_t p;
  for(i=0;i<MAX_PROCS;i++) {
    //p = vfork();
    p = fork();
    if(!p) {
      break;
    }
  }
  if(!p) {
    fprintf(stdout, "getenv(TEST):%s\n", getenv("TEST"));
    fprintf(stdout, "i:%i, this is child process. parent process not exit. ppid:%i\n", i, (int)(getppid()));
    sleep(10);
    fprintf(stdout, "i:%i, this is child process. parent process exited. ppid:%i\n", i, (int)(getppid()));
    fprintf(stdout, "getenv(TEST):%s\n", getenv("TEST"));
    return 0;
  }
  fprintf(stdout, "i:%i, this is parent process, fork end.pid:%i, ppid:%i\n", i, (int)(getpid()), (int)(getppid()));
  sleep(5);
  return 0;
}
结果 : 
[root@db-172-16-3-150 zzz]# export TEST=abc
[root@db-172-16-3-150 zzz]# gcc -O3 -Wall -Werror -Wextra -g ./a.c -o abcd && ./abcd
this is parent process, fork start.pid:6348, ppid:12826
getenv(TEST):abc
getenv(TEST):abc
i:0, this is child process. parent process not exit. ppid:6348
getenv(TEST):abc
i:1, this is child process. parent process not exit. ppid:6348
getenv(TEST):abc
i:2, this is child process. parent process not exit. ppid:6348
getenv(TEST):abc
i:3, this is child process. parent process not exit. ppid:6348
getenv(TEST):abc
i:4, this is child process. parent process not exit. ppid:6348
i:5, this is parent process, fork end.pid:6348, ppid:12826
[root@db-172-16-3-150 zzz]# i:0, this is child process. parent process exited. ppid:1
getenv(TEST):abc
i:1, this is child process. parent process exited. ppid:1
getenv(TEST):abc
i:2, this is child process. parent process exited. ppid:1
getenv(TEST):abc
i:3, this is child process. parent process exited. ppid:1
getenv(TEST):abc
i:4, this is child process. parent process exited. ppid:1
getenv(TEST):abc

    原因是这些进程是主进程fork出来的, 还记得前几天写的一篇《fork and page sharing》, 本例中主进程的内存空间不会因为主进程退出而马上释放掉, 因为子进程还需要用到共享的内存部分.  环境变量也存储在主进程的某些内存空间里面, 具体是哪块空间目前还不清楚 . 
    vfork()创建的子进程, 父进程号是多少呢? 我想不说也知道了, 就是创建子进程的进程的进程号. 因为vfork创建子进程, 父进程要等待子进程执行完成. 就好像默认用了waitpid.

另外要提一下, 为什么fork()的返回值类型是pid_t, 包括还有其他一些系统函数返回值并不是C语言的基本类型(int, char, short, long, float,或者double). 因为在不同的操作系统中, 用来存储进程号的数据类型可能不一样, 如有的系统可能用short, 也有的可能用int.
如果你写程序的时候使用short来存储fork()的返回值, 那么当这个程序要在另一个操作系统用INT来表述进程号的环境下编译运行就可能出现问题.
使用pid_t很好的规避了这个问题.

【参考】

fork : 
NAME
       fork - create a child process

SYNOPSIS
       #include <sys/types.h>
       #include <unistd.h>

       pid_t fork(void);

DESCRIPTION
       fork()  creates  a child process that differs from the parent process only in its PID and PPID, and in the fact
       that resource utilizations are set to 0.  File locks and pending signals are not inherited.

       Under Linux, fork() is implemented using copy-on-write pages, so the only penalty that it incurs  is  the  time
       and memory required to duplicate the parent’s page tables, and to create a unique task structure for the child.


vfork : 
NAME
       vfork - create a child process and block parent

SYNOPSIS
       #include <sys/types.h>
       #include <unistd.h>

       pid_t vfork(void);
LINUX DESCRIPTION
       vfork(), just like fork(2), creates a child process of the calling process.  For details and return  value  and
       errors, see fork(2).

       vfork()  is  a special case of clone(2).  It is used to create new processes without copying the page tables of
       the parent process.  It may be useful in performance sensitive applications where a child will be created which
       then immediately issues an execve().

       vfork()  differs  from  fork()  in  that  the  parent is suspended until the child makes a call to execve(2) or
       _exit(2).  The child shares all memory with its parent, including the stack, until execve() is  issued  by  the
       child.  The child must not return from the current function or call exit(), but may call _exit().

       Signal  handlers are inherited, but not shared.  Signals to the parent arrive after the child releases the par-
       ent’s memory.

getpid, getppid : 
NAME
       getpid, getppid - get process identification

SYNOPSIS
       #include <sys/types.h>
       #include <unistd.h>

       pid_t getpid(void);
       pid_t getppid(void);

DESCRIPTION
       getpid()  returns  the process ID of the current process.  (This is often used by routines that generate unique
       temporary filenames.)

       getppid() returns the process ID of the parent of the current process.

目录
相关文章
|
4天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
15天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1313 5
|
2天前
|
监控 JavaScript Java
基于大模型技术的反欺诈知识问答系统
随着互联网与金融科技发展,网络欺诈频发,构建高效反欺诈平台成为迫切需求。本文基于Java、Vue.js、Spring Boot与MySQL技术,设计实现集欺诈识别、宣传教育、用户互动于一体的反欺诈系统,提升公众防范意识,助力企业合规与用户权益保护。
|
14天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1356 87
|
2天前
|
JavaScript Java 大数据
基于JavaWeb的销售管理系统设计系统
本系统基于Java、MySQL、Spring Boot与Vue.js技术,构建高效、可扩展的销售管理平台,实现客户、订单、数据可视化等全流程自动化管理,提升企业运营效率与决策能力。
|
4天前
|
弹性计算 安全 数据安全/隐私保护
2025年阿里云域名备案流程(新手图文详细流程)
本文图文详解阿里云账号注册、服务器租赁、域名购买及备案全流程,涵盖企业实名认证、信息模板创建、域名备案提交与管局审核等关键步骤,助您快速完成网站上线前的准备工作。
194 82
2025年阿里云域名备案流程(新手图文详细流程)