-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfork.cpp
More file actions
44 lines (43 loc) · 1.32 KB
/
Copy pathfork.cpp
File metadata and controls
44 lines (43 loc) · 1.32 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
#include <sys/types.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int main() {
int x = 10;
int y = 20;
cout << "I am still only one process." << endl;
pid_t returnedValue = fork();
if(returnedValue < 0){
// still only one process
perror("error forking"); // report the error
return -1;
}
else if (returnedValue > 0) {
// this must be the parent process
//cout << "I am the parent process; my child’s ID is " << returnedValue << "." << endl;
cout << "I am the parent process. y is " << x << endl;
sleep(1);
}
else if (returnedValue == 0){
// this must be the child process
pid_t returnedValueChild = fork(); //forking child process to break parent and child
if(returnedValueChild < 0){
perror("error forking");
return -1;
}
else if(returnedValueChild > 0){
//parent of child process
cout << "I am the parent of child process. y is " << y << endl;
sleep(1); // wait a second before repeating
}
else{
//child of child process
cout << "I am the child of child process. y^2 is " << y*y << endl;
sleep(1);
}
}
return 0;
}