ImageVerifierCode 换一换
格式:DOCX , 页数:8 ,大小:74.77KB ,
资源ID:9199262      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/9199262.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Fork函数理解.docx)为本站会员(b****8)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Fork函数理解.docx

1、Fork函数理解Fork函数理解对于刚刚接触Unix/Linux操作系统,在Linux下编写多进程的人来说,fork是最难理解的概念之一:它执行一次却返回两个值。首先我们来看下fork函数的原型:i nclude i nclude pid_t fork(void);返回值:负数:如果出错,则fork()返回-1,此时没有创建新的进程。最初的进程仍然运行。零:在子进程中,fork()返回0正数:在负进程中,fork()返回正的子进程的PID其次我们来看下如何利用fork创建子进程。创建子进程的样板代码如下所示:pid_t child;if(child = fork()0)/*错误处理*/else

2、 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 tim

3、e, pid = %dn, pid );count+;printf( count = %dn, count );if ( pid0 )printf( This is the parent process,the child has the pid:%dn, pid );else if ( !pid )printf( This is the child Process.n)elseprintf( fork failed.n );printf( This is third time, pid = %dn, pid );printf( This is fouth time, pid = %dn, p

4、id );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 ( pid0 )printf( This is the parent procESS,the child has the pid:%dn, pid );printf( In the p

5、arent 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 );elseprintf( fork failed.n );return 0;运行结果如下:现在来解释上面提出的问题。看这个程序的时候,头脑中必须首先了解一个概念:在语句pid=fork()之前,只有一个进程在执行这段代码,但在这条语句之

6、后,就变成两个进程在执行了,这两个进程的代码部分完全相同,将要执行的下一条语句都是if ( pid0 )。两个进程中,原先就存在的那个被称作“父进程”,新出现的那个被称作“子进程”。父子进程的区别除了进程标志符(process ID)不同外,变量pid的值也不相同,pid存放的是fork的返回值。fork调用的一个奇妙之处就是它仅仅被调用一次,却能够返回两次,它可能有三种不同的返回值:1. 在父进程中,fork返回新创建子进程的进程ID;2.在子进程中,fork返回0;3.如果出现错误,fork返回一个负值;fork出错可能有两种原因:(1)当前的进程数已经达到了系统规定的上限,这时errno

7、的值被设置为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

8、process ID of the new child. The reason the childs 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 oain the process IDs of its children. The reason fork returns 0 to the child is that a process can have only a sin

9、gle parent, and the child can always call getppid to oain the process ID of its parent. (Process ID 0 is reserved for use by the kernel, so its not possible for 0 to be the process ID of a child.)被fork创建的新进程叫做自进程。fork函数被调用一次,却两次返回。返回值唯一的区别是在子进程中返回0,而在父进程中返回子进程的pid。在父进程中要返回子进程的pid的原因是父进程可能有不止一个子进程,而一

10、个进程又没有任何函数可以得到他的子进程的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 parents data space, heap, and stack. Note that this is a copy for the child; the parent and the ch

11、ild do not share these portions of memory. The parent and the child share the text segment (Section 7.6).子进程和父进程都执行在fork函数调用之后的代码,子进程是父进程的一个拷贝。例如,父进程的数据空间、堆栈空间都会给子进程一个拷贝,而不是共享这些内存。Current implementations dont perform. a complete copy of the parents data, stack, and heap, since a fork is often follow

12、ed by an exec. Instead, a technique called copy-on-write (COW) is used. These regions are shared by the parent and the child and have their protection changed by the kernel to read-only. If either process tries to modify these regions, the kernel then makes a copy of that piece of memory only, typic

13、ally a page in a virtual memory system. Section 9.2 of Bach 1986 and Sections 5.6 and 5.7 of McKusick et al. 1996 provide more detail on this feature.我们来给出详细的注释#include #include int main(void)pid_t pid;int count=0;/*此处,执行fork调用,创建了一个新的进程, 这个进程共享父进程的数据和堆栈空间等,这之后的代码指令为子进程创建了一个拷贝。 fock 调用是一个复制进程,fock 不

14、象线程需提供一个函数做为入口, fock调用后,新进程的入口就在 fock的下一条语句。*/pid = fork();/*此处的pid的值,可以说明fork调用后,目前执行的是父进程还是子进程*/printf( Now, the pid returned by calling fork() is %dn, pid );if ( pid0 )/*当fork在子进程中返回后,fork调用又向父进程中返回子进程的pid, 如是该段代码被执行,但是注意的事,count仍然为0, 因为父进程中的count始终没有被重新赋值, 这里就可以看出子进程的数据和堆栈空间和父进程是独立的,而不是共享数据*/pri

15、ntf( This is the parent process,the child has the pid:%dn, pid );printf( In the parent process,count = %dn, count );else if ( !pid ) /*在子进程中对count进行自加1的操作,但是并没有影响到父进程中的count值,父进程中的count值仍然为0*/printf( This is the child process.n);printf( Do your own things here.n );count+;printf( In the child process

16、, count = %dn, count );elseprintf( fork failed.n );return 0;也就是说,在Linux下一个进程在内存里有三部分的数据,就是代码段、堆栈段和数据段。代码段,顾名思义,就是存放了程序代码的数据,假如机器中有数个进程运行相同的一个程序,那么它们就可以使用相同的代码段。堆栈段存放的就是子程序的返回地址、子程序的参数以及程序的局部变量。而数据段则存放程序的全局变量,常数以及动态数据分配的数据空间(比如用malloc之类的函数取得的空间)。系统如果同时运行数个相同的程序,它们之间就不能使用同一个堆栈段和数据段。仔细分析后,我们就可以知道:一个程序一

17、旦调用fork函数,系统就为一个新的进程准备了前述三个段,首先,系统让新的进程与旧的进程使用同一个代码段,因为它们的程序还是相同的,对于数据段和堆栈段,系统则复制一份给新的进程,这样,父进程的所有数据都可以留给子进程,但是,子进程一旦开始运行,虽然它继承了父进程的一切数据,但实际上数据却已经分开,相互之间不再有影响了,也就是说,它们之间不再共享任何数据了。fork()不仅创建出与父进程代码相同的子进程,而且父进程在fork执行点的所有上下文场景也被自动复制到子进程中,包括:全局和局部变量打开的文件句柄共享内存、消息等同步对象而如果两个进程要共享什么数据的话,就要使用另一套函数(shmget,shmat,shmdt等)来操作。现在,已经是两个进程了,对于父进程,fork函数返回了子程序的进程号,而对于子程序,fork函数则返回零,这样,对于程序,只要判断fork函数的返回值,就知道自己是处于父进程还是子进程中。“本文由华清远见http:/www.embedu.org/index.htm提供”

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1