博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
fork() and vfork() getppid's result
阅读量:7025 次
发布时间:2019-06-28

本文共 3461 字,大约阅读时间需要 11 分钟。

fork和vfork的描述和区别参考本文最后的部分.
下面来看看用fork或vfork创建的子进程, 子进程看到的ppid(父进程是多少).
fork : 
[root@db-172-16-3-150 zzz]# cat a.c#include 
#include
#include
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
接下来看看环境变量会不会也变掉 . 
从结果来看, 环境变量会继承下去, 不会因为init接管而改变.
[root@db-172-16-3-150 zzz]# cat a.c#include 
#include
#include
#include
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
    原因是这些进程是主进程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 processSYNOPSIS       #include 
#include
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 parentSYNOPSIS       #include 
#include
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 identificationSYNOPSIS       #include 
#include
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.

转载地址:http://cjmxl.baihongyu.com/

你可能感兴趣的文章
12篇学通C#网络编程——第四篇 TCP应用编程
查看>>
ASP.NET MVC的DropDownList
查看>>
B-tree vs hash_我思故我在_百度空间
查看>>
spring.net 结合简单三层实例
查看>>
phpcms(v9)添加模型
查看>>
LINQ 图解
查看>>
状态目标bfs+哈希表 + 三杯水
查看>>
MVC系统过滤器、自定义过滤器
查看>>
ASP.NET MVC+EF框架+EasyUI实现权限管理系列(20)-多条件模糊查询和回收站还原的实现...
查看>>
管理管理器深度探索QT窗口系统---布局篇
查看>>
Jekyll – 基于纯文本的开源静态网站 & 博客系统
查看>>
HTML <font> 标签
查看>>
NSString / NSMutableString 字符串处理
查看>>
3、C语言中一般类型的指针变量细解
查看>>
zookeeper
查看>>
IEnumerable和IEnumerator 详解 (转)
查看>>
web service接口测试工具选型
查看>>
Stage3d 由浅到深理解AGAL的管线vertex shader和fragment shader || 简易教程 学习心得 AGAL 非常非常好的入门文章...
查看>>
vi编辑器的使用方式
查看>>
JOIN与EXISTS(子查询)的效率研究
查看>>