fork()函数-创建子进程
2011-04-04 11:29
295 查看
fork():
The fork()
system call will spawn a new child process which
is an identical process to the parent except that has a new
system process ID. The process is copied in memory from the parent and a new
process structure is assigned by the kernel. The return value of the
function is which discriminates the two threads of execution. A zero is
returned by the fork function in the child's process.
The environment, resource limits, umask, controlling terminal, current working
directory, root directory, signal masks and other process resources are also
duplicated from the parent in the forked child process.
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 = fork();
10
11 if( pID == 0 ) {
12 //Code only executed by child process
13 sIdentifier = "Child Process: ";
14 globalVariable++;
15 iStackVariable++;
16 } else if ( pID < 0 ) {
17 printf( "Failed to fork!/n" );
18 exit(1);
19 //Throw exception
20 } else {
21 //Code only executed by parent process
22 sIdentifier = "Parent Process: ";
23 }
24
25 //Code executed by both parent and child.
26 printf( " %s/n", sIdentifier );
27 printf( " Global variable: %d/n", globalVariable );
28 printf( " Stack variable: %d/n", iStackVariable );
29 }
explore@ubuntu:~/Documents/Linux C Programming/Process/fork$ gcc fork.c
explore@ubuntu:~/Documents/Linux C Programming/Process/fork$ ./a.out
Parent Process:
Global variable: 2
Stack variable: 20
Child Process:
Global variable: 3
Stack variable: 21
[Potential Pitfall]
: Some memory duplicated by
a forked process such as file pointers, will cause intermixed output from
both processes.
Use the wait()
function so that the processes do not access the file
at the same time or open unique file descriptors. Some like stdout or stderr
will be shared unless synchronized using wait()
or some other mechanism.
The file close on exit is another gotcha. A terminating process will close
files before exiting.
File locks set by the parent process are not inherited by the child process.
[Potential Pitfall]
: Race conditions can be
created due to the unpredictability of when the kernel scheduler runs
portions or time slices of the process. One can use wait()
.
the use of sleep()
does not guarentee reliability of execution
on a heavily loaded system as the scheduler behavior is not predictable by the
application.
Note on exit()
vs _exit()
:
The C library function exit()
calls the kernel system call
_exit()
internally.
The kernel system call _exit()
will cause the kernel to close descriptors,
free memory, and perform the kernel terminating process clean-up.
The C library function exit()
call will flush I/O buffers and
perform aditional clean-up before calling _exit()
internally.
The function exit(status
)
causes the executable to return
"status" as the return code for main()
.
When exit(status
)
is called by a child process,
it allows the parent process to examine the terminating status of the child
(if it terminates first). Without this call (or a call from main()
to return()
)
and specifying the status argument, the process will not return a value.
Man Pages:
fork
- create a child process
The fork()
system call will spawn a new child process which
is an identical process to the parent except that has a new
system process ID. The process is copied in memory from the parent and a new
process structure is assigned by the kernel. The return value of the
function is which discriminates the two threads of execution. A zero is
returned by the fork function in the child's process.
The environment, resource limits, umask, controlling terminal, current working
directory, root directory, signal masks and other process resources are also
duplicated from the parent in the forked child process.
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 = fork();
10
11 if( pID == 0 ) {
12 //Code only executed by child process
13 sIdentifier = "Child Process: ";
14 globalVariable++;
15 iStackVariable++;
16 } else if ( pID < 0 ) {
17 printf( "Failed to fork!/n" );
18 exit(1);
19 //Throw exception
20 } else {
21 //Code only executed by parent process
22 sIdentifier = "Parent Process: ";
23 }
24
25 //Code executed by both parent and child.
26 printf( " %s/n", sIdentifier );
27 printf( " Global variable: %d/n", globalVariable );
28 printf( " Stack variable: %d/n", iStackVariable );
29 }
explore@ubuntu:~/Documents/Linux C Programming/Process/fork$ gcc fork.c
explore@ubuntu:~/Documents/Linux C Programming/Process/fork$ ./a.out
Parent Process:
Global variable: 2
Stack variable: 20
Child Process:
Global variable: 3
Stack variable: 21
[Potential Pitfall]
: Some memory duplicated by
a forked process such as file pointers, will cause intermixed output from
both processes.
Use the wait()
function so that the processes do not access the file
at the same time or open unique file descriptors. Some like stdout or stderr
will be shared unless synchronized using wait()
or some other mechanism.
The file close on exit is another gotcha. A terminating process will close
files before exiting.
File locks set by the parent process are not inherited by the child process.
[Potential Pitfall]
: Race conditions can be
created due to the unpredictability of when the kernel scheduler runs
portions or time slices of the process. One can use wait()
.
the use of sleep()
does not guarentee reliability of execution
on a heavily loaded system as the scheduler behavior is not predictable by the
application.
Note on exit()
vs _exit()
:
The C library function exit()
calls the kernel system call
_exit()
internally.
The kernel system call _exit()
will cause the kernel to close descriptors,
free memory, and perform the kernel terminating process clean-up.
The C library function exit()
call will flush I/O buffers and
perform aditional clean-up before calling _exit()
internally.
The function exit(status
)
causes the executable to return
"status" as the return code for main()
.
When exit(status
)
is called by a child process,
it allows the parent process to examine the terminating status of the child
(if it terminates first). Without this call (or a call from main()
to return()
)
and specifying the status argument, the process will not return a value.
#include <stdlib.h> void exit(int status ); | #include <unistd.h> void _exit(int status ); |
fork
- create a child process
相关文章推荐
- wait函数在fork创建子进程后的使用
- wait函数在fork创建子进程后的使用
- 《深入理解计算机系统》第八章 (一)关于子进程创建函数fork的理解
- fork函数创建子进程,exec函数执行命令
- 父进程利用fork()函数创建子进程并且利用shared_memory进行通信的实例
- wait函数在fork创建子进程后的使用
- wait函数在fork创建子进程后的使用
- wait函数在fork创建子进程后的使用
- linux系统如何使用fork函数创建子进程
- wait函数在fork创建子进程后的使用
- Linux 内核--fork()函数创建进程
- Linux0.11进程创建函数fork()
- Linux基础学习系列:对于fork()函数的学习,及进程创建相关知识
- 有意思的进程创建函数fork()的问题
- fork()创建子进程步骤、函数用法及常见考点(内附fork()过程图)
- Linux创建进程的fork()函数
- fork函数创建进程
- Linux基础学习系列:对于fork()函数的学习,及进程创建相关知识
- 进程创建,fork()函数讲解
- Linux进程创建fork()函数