-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfork.cpp
36 lines (32 loc) · 929 Bytes
/
fork.cpp
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
#include <unistd.h>
#include <wait.h>
#include <stdint.h>
#include <iostream>
#include <sys/mman.h>
#include "mmap_use.h"
using namespace std;
pair<void *, pid_t> mmap_fork(int len) {
pair<void *, pid_t> result;
result.first = shared_mmap(len);
result.second = fork();
return result;
}
void *fork_loop(int numForks, int len) {
void *shared_mem_pointer[numForks];
pid_t pid;
for (int i = 0; i < numForks; i++) {
pair<void *, pid_t> result = mmap_fork(len);
pid = result.second;
// decide if continue as child or parent process
if (pid == 0) {
// beginn child execution
return result.first;
} else if(pid > 0) {
// beginn parent execution
shared_mem_pointer[i] = result.first;
} else {
printf("Error creating the Process. The resulting PID is %d.\n",pid);
}
}
return NULL;
}