gpt4 book ai didi

c++ - 为什么在使用 vfork() 时会出现错误?

转载 作者:太空宇宙 更新时间:2023-11-04 15:28:57 26 4
gpt4 key购买 nike

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

#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);
}

最佳答案

this 使用 ?请注意变量修改的注意事项。

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.

关于c++ - 为什么在使用 vfork() 时会出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/731403/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com