vfork()系统调用-创建子进程,阻塞父进程
2011-04-04 11:47
483 查看
vfork():
The vfork()
function is the same as fork()
except that
it does not make a copy of the address space. The memory is shared
reducing the overhead of spawning a new process with a unique copy of all
the memory.
This is typically used
when using fork()
to exec()
a process and terminate.
The vfork()
function also executes the child process first and
resumes the parent process when the child terminates.
Example:
1 #include<stdio.h>
2 #include<unistd.h>
3 #include<stdlib.h>
4 int globalVariable = 2;
5 int main(){
6 char *sIdentifier;
7 int iStackVariable = 20;
8
9 pid_t pID = vfork();
10
11 if( pID == 0 ) {
12 //Code only executed by child process
13 sIdentifier = "Child Process: ";
14 globalVariable++;
15 iStackVariable++;
16 printf( " %s ", sIdentifier );
17 printf( " Global variable: %d", globalVariable );
18
19 printf( " Stack variable: %d/n", iStackVariable );
20
21 _exit(0);
22
23 } else if ( pID < 0 ) {
24 printf( "Failed to fork!/n" );
25 exit(1);
26 //Throw exception
27 } else {
28 //Code only executed by parent process
29 sIdentifier = "Parent Process: ";
30 }
31
32 //Code executed by parent.
33 printf( " %s ", sIdentifier );
34 printf( " Global variable: %d ", globalVariable );
35 printf( " Stack variable: %d/n", iStackVariable );
36 }
explore@ubuntu:~/Documents/Linux C Programming/Process/vfork$ gcc vfork.c
explore@ubuntu:~/Documents/Linux C Programming/Process/vfork$ ./a.out
Child Process: Global variable: 3 Stack variable: 21
Parent Process: Global variable: 3 Stack variable: 21
Note: The child process executed first, updated the variables which are shared
between the processes and NOT
unique, and then the parent process
executes using variables which the child has updated.
[Potential Pitfall]
: A deadlock condition may occur
if the child process does not terminate, the parent process will not proceed.
Man Pages:
vfork
- create a child process and block parent
_exit
- - terminate the current process
The vfork()
function is the same as fork()
except that
it does not make a copy of the address space. The memory is shared
reducing the overhead of spawning a new process with a unique copy of all
the memory.
This is typically used
when using fork()
to exec()
a process and terminate.
The vfork()
function also executes the child process first and
resumes the parent process when the child terminates.
Example:
1 #include<stdio.h>
2 #include<unistd.h>
3 #include<stdlib.h>
4 int globalVariable = 2;
5 int main(){
6 char *sIdentifier;
7 int iStackVariable = 20;
8
9 pid_t pID = vfork();
10
11 if( pID == 0 ) {
12 //Code only executed by child process
13 sIdentifier = "Child Process: ";
14 globalVariable++;
15 iStackVariable++;
16 printf( " %s ", sIdentifier );
17 printf( " Global variable: %d", globalVariable );
18
19 printf( " Stack variable: %d/n", iStackVariable );
20
21 _exit(0);
22
23 } else if ( pID < 0 ) {
24 printf( "Failed to fork!/n" );
25 exit(1);
26 //Throw exception
27 } else {
28 //Code only executed by parent process
29 sIdentifier = "Parent Process: ";
30 }
31
32 //Code executed by parent.
33 printf( " %s ", sIdentifier );
34 printf( " Global variable: %d ", globalVariable );
35 printf( " Stack variable: %d/n", iStackVariable );
36 }
explore@ubuntu:~/Documents/Linux C Programming/Process/vfork$ gcc vfork.c
explore@ubuntu:~/Documents/Linux C Programming/Process/vfork$ ./a.out
Child Process: Global variable: 3 Stack variable: 21
Parent Process: Global variable: 3 Stack variable: 21
Note: The child process executed first, updated the variables which are shared
between the processes and NOT
unique, and then the parent process
executes using variables which the child has updated.
[Potential Pitfall]
: A deadlock condition may occur
if the child process does not terminate, the parent process will not proceed.
Man Pages:
vfork
- create a child process and block parent
_exit
- - terminate the current process
相关文章推荐
- 线程系统调用阻塞是否导致进程阻塞的问题
- 在web程序里面调用系统进程(创建pop3帐户)
- 在创建守护进程时优先选用系统调用daemon()
- windows下调用系统API实现进程创建和文件读写
- 系统调用-进程创建函数基本用法1
- fork(),clone()系统调用进行进程创建的过程
- Linux 系统调用之 fork()——进程的创建
- 系统调用-进程创建函数glibc部分2
- 进程调度之3:系统调用fork、vfork与clone
- 【进程管理】系统调用fork(),vfork()与clone()
- 从系统调用fork,到进程的创建与终止
- Linux 2.6 劫持系统调用 隐藏进程
- Linux进程间通讯所使用的系统调用函数
- 内存分配的原理__进程分配内存有两种方式,分别由两个系统调用完成:brk和mmap(不考虑共享内存)。
- Linux内核2.4.18创建硬链接的系统调用sys_link
- 应用调试之自制系统调用、编写进程查看器(二)
- 进程相关的系统调用3
- shell监控linux系统进程创建脚本分享
- Android系统中的进程管理:进程的创建
- c#关闭系统进程以及如何调用cmd并执行命令开发者在线 Builder.com.cn 更新时间:2008-08-05作者:乔毅 来源:IT168