您的位置:网站首页 > 电器维修资料网 > 正文 >
FORK()函数的理解
来源: 日期:2013-11-29 9:19:25 人气:标签:
返回值:
负数:如果出错,则fork()返回-1,此时没有创建新的进程。 初的进程仍然运行。
零:在子进程中,fork()返回0
正数:在负进程中,fork()返回正的子进程的pid
其次我们来看下如何利用fork创建子进程。
创建子进程的样板代码如下所示:
pid_t child;
if((child = fork())<0)
/*错误处理*/
else if(child == 0)
/*这是新进程*/
else
/*这是 初的父进程*/
fock函数调用一次却返回两次;向父进程返回子进程的id,向子进程中返回0,
这是因为父进程可能存在很多过子进程,所以必须通过这个返回的子进程id来跟踪子进程,
而子进程只有一个父进程,他的id可以通过getppid取得。
下面我们来对比一下两个例子:
第一个:
#include
#include
int main()
{
pid_t pid;
int count=0;
pid = fork();
printf( "this is first time, pid = %dn", pid );
printf( "this is second time, pid = %dn", pid );
count++;
printf( "count = %dn", count );
if ( pid>0 )
{
printf( "this is the parent process,the child has the pid:%dn", pid );
}
else if ( !pid )
{
printf( "this is the child process.n")
}
else
{
printf( "fork failed.n" );
}
printf( "this is third time, pid = %dn", pid );
printf( "this is fouth time, pid = %dn", pid );
return 0;
}
运行结果如下:
问题:
这个结果很奇怪了,为什么printf的语句执行两次,而那句“count++;”的语句却只执行了一次
接着看:
#include
#include
int main(void)
{
pid_t pid;
int count=0;
pid = fork();
printf( "now, the pid returned by calling fork() is %dn", pid );
if ( pid>0 )
{
printf( "this is the parent process,the child has the pid:%dn", pid );
printf( "in the parent process,count = %dn", count );
}
else if ( !pid )
{
printf( "this is the child process.n");
printf( "do your own things here.n" );
count ++;
printf( "in the child process, count = %dn", count );
}
else
{
printf( "fork failed.n" );
}
return 0;
}
运行结果如下:
现在来解释上面提出的问题。
看这个程序的时候,头脑中必须首先了解一个概念:在语句pid=fork()之前,只有一个进程在执行这段代码,但在这条语句之后,就变成两个进程在执行了,这两个进程的代码部分完全相同,将要执行的下一条语句都是if ( pid>0 )……。
两个进程中,原先就存在的那个被称作“父进程”,新出现的那个被称作“子进程”。父子进程的区别除了进程标志符(process id)不同外,变量pid的值也不相同,pid存放的是fork的返回值。fork调用的一个奇妙之处就是它仅仅被调用一次,却能够返回两次,它可能有三种不同的返回值:
1. 在父进程中,fork返回新创建子进程的进程id;
2.在子进程中,fork返回0;
3.如果出现错误,fork返回一个负值;
fork出错可能有两种原因:(1)当前的进程数已经达到了系统规定的上限,这时errno的值被设置为eagain。(2)系统内存不足,这时errno的值被设置为enomem。
接下来我们来看看apue2中对fork的说明:
the new process created by fork is called the child process. this function is called once but returns twice. the only difference in the returns is that the return value in the child is 0, whereas the return value in the parent is the process id of the new child. the reason the child's process id is returned to the parent is that a process can have more than one child, and there is no function that allows a process to o^ain the process ids of its children. the reason fork returns 0 to the child is that a process can have only a single parent, and the child can always call getppid to o^ain the process id of its parent. (process id 0 is reserved for use by the kernel, so it's not possible for 0 to be the process id of a child.)
被fork创建的新进程叫做自进程。fork函数被调用一次,却两次返回。返回值唯一的区别是在子进程中返回0,而在父进程中返回子进程的pid。在父进程中要返回子进程的pid的原因是父进程可能有不止一个子进程,而一个进程又没有任何函数可以得到他的子进程的pid。
both the child and the parent continue executing with the instruction that follows the call to fork. the child is a copy of the parent. for example, the child gets a copy of the parent's data space, heap, and stack. note that this is a copy for the child; the parent and the child do not share these portions of memory. the parent and the child share the text segment (section 7.6).
- 1
- 2
- 下一页
【看看这篇文章在百度的收录情况】
相关文章
- 上一篇: 光纤活动连接器的种类
- 下一篇: PADS中BGA Fanout扇出教程