-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfork.c
More file actions
56 lines (47 loc) · 1.04 KB
/
Copy pathfork.c
File metadata and controls
56 lines (47 loc) · 1.04 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
56
#include<sys/types.h>
#include<stdio.h>
#include<unistd.h>
/*
fork() function creates a child process
Creating process is known as "Parent Process".
The newly creates process is known as "Child Process".
Both the process will use same
1. Program counter
2. Address Space(Clear from output of program)
*/
/* Although output addresses are same.
In reality actual physical address is different for both process.
However they are assigned same logical address(clear from output)
*/
int y=0;
int main()
{
int x=0;
pid_t pid;
pid = fork();
if(pid<0)
{
printf("Process Creation Failed");
}
else if(pid==0)
{
printf("Child Process\n");
x=x+1;
y=y+1;
printf("Local Variable: %d\n",x);
printf("Global Varible: %d\n",y);
printf("Address of Local Variable: %p\n",&x);
printf("Address of Global Varible: %p\n",&y);
}
else
{
printf("Parent Process\n");
x=x-1;
y=y-1;
printf("Local Variable: %d\n",x);
printf("Global Varible: %d\n",y);
printf("Address of Local Variable: %p\n",&x);
printf("Address of Global Varible: %p\n",&y);
}
return 0;
}