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); }
Is this of use ? Note the caveats surrounding modification of variables.