-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcowtest.c
More file actions
50 lines (43 loc) · 1.26 KB
/
cowtest.c
File metadata and controls
50 lines (43 loc) · 1.26 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
#include "types.h"
#include "stat.h"
#include "user.h"
void
cow_demo()
{
int pid = getpid();
int *x = (int *)malloc(sizeof(int));
int *grade = (int*)malloc(sizeof(int));
*grade = 100;
*x = 0;
int *y = (int *)malloc(sizeof(int));
*y = 100; // GRADE?!#!##$
printf(1, "pid : %d x: %d y: %d grade: %d \n\n", pid, *x, *y, *grade);
printf(1, "Fater Process <procdump> Before Forking \n\n");
//output will be from page 1 and we can see that there is a readonly page.
procdump();
//child
if (forkcow() == 0)
{
printf(1, "CHILD before changing X same address -copied\n");
printf(1,"pid is : %d x: %d y: %d, grade: %d \n\n", getpid(), *x, *y, *grade);
procdump();
*x=2;
printf(1, "CHILD after changing X same address\n");
printf(1,"pid is : %d x: %d y: %d, grade: %d \n\n", getpid(), *x, *y, *grade);
procdump();
// // now we can see that before changing x it was a shared memory
// printf(1,"pid is : %d x: %d , y: %d, grade: %d \n\n\n\n", getpid(), *x, *y, *grade);
} else {
waitcow();
printf(1, "Fater Process <procdump> After Cow_Forking \n\n");
procdump();
}
exit();
}
int
main(int argc, char *argv[])
{
printf(1,"COW demonstration:\n\n");
cow_demo();
exit();
}