I am new to C++. Is it possible to declare a variable for shared use between parent and child processes in fork()?
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;
int var;
int main(int argc, char * argv[])
{
pid_t child_pid;
int status;
var = 3;
if ((child_pid = fork()) < 0) {
perror("Error (fork failure)");
}
if (child_pid == 0) {
var = 10;
cout << "CHILD ASSIGNED var=" << var << endl;
}
else {
wait(NULL);
cout << "PARENT var=" << var << endl;
}
}
The current result I get is:
CHILD ASSIGNED var=10
PARENT var=3
What I want is
PARENT var=10