使用vfork()时为什么会出错?

时间:2022-03-24 22:50:38

This is my code... I don't know why I'm get an error segment... could somebody explain the reason to me?

这是我的代码......我不知道为什么我会得到一个错误段......有人可以向我解释原因吗?

#include <iostream>

#include <string>

// Required by for routine
#include <sys/types.h>
#include <unistd.h>

using namespace std;


int globalVariable = 2;

main()
{
   string sIdentifier;
   int    iStackVariable = 20;

   pid_t pID = vfork();
   if (pID == 0)                // child
   {
      // Code only executed by child process

      sIdentifier = "Child Process: ";
      globalVariable++;
      iStackVariable++;
      cout << "PROCESO NUMERO"<<getpid()<<sIdentifier;
//          printf("Proceso hijo: PID %d - PPID %d\n", getpid(), getppid());
      cout << " Global variable: " << globalVariable;
      cout << " Stack variable: "  << iStackVariable << endl;
      return (0);
    }
    else if (pID < 0)            // failed to fork
    {
        cerr << "Failed to fork" << endl;
        return (1);
        // Throw exception
    }
    else                                   // parent
    {
      // Code only executed by parent process

      sIdentifier = "Parent Process:";
    }

    // executed only by parent

    cout << sIdentifier;
    cout << " Global variable: " << globalVariable;
    cout << " Stack variable: "  << iStackVariable << endl;
    return (0);
}

2 个解决方案

#1


Is this of use ? Note the caveats surrounding modification of variables.

这有用吗?请注意有关变量修改的警告。

The vfork() function has the same effect as fork(), except that the behaviour 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() or one of the exec family of functions.

vfork()函数与fork()具有相同的效果,除非如果vfork()创建的进程修改除了用于存储vfork()的返回值的pid_t类型的变量之外的任何数据,则行为未定义,或者从调用vfork()的函数返回,或者在成功调用_exit()或exec函数系列之前调用任何其他函数。

#2


If you vfork() both processes are sharing an address space. You should probably only use vfork() if you are going to exec another process pretty much immediately in the child. The reason the system call was created was to avoid the overhead of copying every page in the parent process's address space only to have all those mappings discarded when the child exec's. For your case, use fork() instead.

如果你vfork()两个进程共享一个地址空间。你应该只使用vfork(),如果你要在孩子中立即执行另一个进程。创建系统调用的原因是为了避免复制父进程的地址空间中的每个页面的开销,只是为了在子进程的时候丢弃所有这些映射。对于您的情况,请改用fork()。

#1


Is this of use ? Note the caveats surrounding modification of variables.

这有用吗?请注意有关变量修改的警告。

The vfork() function has the same effect as fork(), except that the behaviour 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() or one of the exec family of functions.

vfork()函数与fork()具有相同的效果,除非如果vfork()创建的进程修改除了用于存储vfork()的返回值的pid_t类型的变量之外的任何数据,则行为未定义,或者从调用vfork()的函数返回,或者在成功调用_exit()或exec函数系列之前调用任何其他函数。

#2


If you vfork() both processes are sharing an address space. You should probably only use vfork() if you are going to exec another process pretty much immediately in the child. The reason the system call was created was to avoid the overhead of copying every page in the parent process's address space only to have all those mappings discarded when the child exec's. For your case, use fork() instead.

如果你vfork()两个进程共享一个地址空间。你应该只使用vfork(),如果你要在孩子中立即执行另一个进程。创建系统调用的原因是为了避免复制父进程的地址空间中的每个页面的开销,只是为了在子进程的时候丢弃所有这些映射。对于您的情况,请改用fork()。