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 :
vfork :
getpid, getppid :
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.