新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > FORK()函数的理解

FORK()函数的理解

作者:时间:2012-08-06来源:网络收藏

对于刚刚接触Unix/Linux操作系统,在Linux下编写多进程的人来说,fork是最难的概念之一:它执行一次却返回两个值。

本文引用地址:http://www.eepw.com.cn/article/148649.htm

首先我们来看下fork的原型:

#i nclude

#i nclude

pid_t fork(void);

返回值:

负数:如果出错,则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).

pid控制相关文章:pid控制原理



上一页 1 2 下一页

关键词: 理解 函数 FORK

评论


相关推荐

技术专区

关闭