了vfork()系统调用 [英] vfork() system call

查看:99
本文介绍了了vfork()系统调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了新的过程中使用了vfork()系统调用的执行过程中的父进程的地址空间,直到子线程一个线程调用亘​​古不退出()或者exec()系统调用,父被阻止创建。于是,我用写了一个程序的vfork()系统调用

I read that the new process created using vfork() system call executes as a thread in the parent's address space and until the child thread doesnot calls exit() or exec() system call, the parent is blocked. So I wrote a program using vfork() system call

#include <stdio.h>  
#include <unistd.h>

int main()  
 {  
      pid_t pid;  
      printf("Parent\n");  
      pid = vfork();  
      if(pid==0)  
      {  
          printf("Child\n");  
      }  
      return 0;  
  }

我得到的输出如下:

I got the output as follows:

 Parent  
 Child  
 Parent  
 Child  
 Parent  
 Child  
 ....  
 ....  
 ....

我假定return语句必须在内部调用的exit()系统调用,所以我期待的输出,因为只有

I was assuming that the return statement must be calling the exit() system call internally so I was expecting the output as only

Parent  
Child

有人能解释我为什么实际上它没有停止和无限循环连续打印。

Can somebody explain me why actually it is not stopping and continuously printing for infinite loop.

推荐答案

您应该阅读的 的vfork 非常小心:

You should read the man page for vfork very carefully:

的的vfork()函数具有作为叉相同的效果(2),所不同的是该行为是未定义如果()由vfork的创建的过程修改比用于从存储返回值类型将为pid_t的变量之外的任何数据了vfork(),或从函数返回其中的vfork()被调用,或者调用任何其他函数之前调用成功_exit(2)或某个exec(3)家庭的功能。

The vfork() function has the same effect as fork(2), except that the behavior is undefined if the process created by vfork() either modifies any data other than a variable of type pid_t used to store the return value from vfork(), or returns from the function in which vfork() was called, or calls any other function before successfully calling _exit(2) or one of the exec(3) family of functions.

(以上是从该名男子页的POSIX一部分,因此适用(可能)到其他环境比Linux)。

(above is from the POSIX part of the man page, so applies (potentially) to other environments than Linux).

您正在呼叫的printf 和孩子回来,所以你的程序的行为是不确定的。

You're calling printf and returning from the child, so the behavior of your program is undefined.

这篇关于了vfork()系统调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆