Repository navigation
Expand file tree
/
Copy pathfork.cpp
More file actions
executable file
·55 lines (47 loc) · 1.57 KB
/
Copy pathfork.cpp
File metadata and controls
executable file
·55 lines (47 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// fork() — create a child process that is an (almost) identical copy of the
// parent. The code after fork() runs in BOTH processes; we tell them apart by
// the return value:
// pID > 0 -> parent (the value is the child's PID)
// pID == 0 -> child
// pID < 0 -> fork failed
//
// Each process has its own address space, so changes a child makes to globals
// or stack variables are NOT visible to the parent.
#include <iostream>
#include <stdlib.h> // exit()
#include <string>
#include <sys/types.h>
#include <sys/wait.h> // wait()
#include <unistd.h> // fork(), getpid()
int globalVariable = 2;
int main() {
std::cout << "[pid " << getpid() << "] before fork(), runs only in parent"
<< std::endl;
std::string sIdentifier;
int iStackVariable = 20;
pid_t pID = fork();
// From here on, the code below runs in BOTH parent and child.
// We check pID to decide which branch each process should take.
if (pID == 0) {
// child
sIdentifier = "Child Process: ";
globalVariable++;
iStackVariable++;
} else if (pID < 0) {
// fork failed
std::cerr << "Failed to fork" << std::endl;
exit(1);
} else {
// parent
sIdentifier = "Parent Process:";
}
// Code executed by both parent and child.
std::cout << "[pid " << getpid() << "] " << sIdentifier
<< " global=" << globalVariable
<< " stack=" << iStackVariable << std::endl;
// Parent waits for the child so its output isn't interleaved with the shell
// prompt and so we don't leave a zombie behind.
if (pID > 0) {
wait(nullptr);
}
}