-
Notifications
You must be signed in to change notification settings - Fork 0
/
t1.cpp
55 lines (46 loc) · 1.1 KB
/
t1.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>
struct TFoo {
int vMain;
int vChild;
};
int main(int argc, char **argv) {
printf("--beginning of program\n");
TFoo *myGlob = (TFoo*) mmap(NULL, sizeof *myGlob, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
myGlob->vMain = 0;
myGlob->vChild = 0;
int counter = 0;
pid_t pid = fork();
if (pid == 0)
{
// child process
int i = 0;
for (; i < 5; ++i)
{
myGlob->vChild++;
printf("child process: counter=%d vChild=%d, vMain=%d\n", ++counter, myGlob->vChild, myGlob->vMain );
usleep(50);
}
}
else if (pid > 0)
{
// parent process
int j = 0;
for (; j < 5; ++j)
{
myGlob->vMain++;
printf("parent process: counter=%d vChild=%d, vMain=%d\n", ++counter, myGlob->vChild, myGlob->vMain );
usleep(50);
}
}
else
{
// fork failed
printf("fork() failed!\n");
return 1;
}
printf("--end of program--\n");
return 0;
}