-
Notifications
You must be signed in to change notification settings - Fork 0
/
forkex.c
67 lines (53 loc) · 2 KB
/
forkex.c
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
57
58
59
60
61
62
63
64
65
66
67
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#define MAX_COUNT 107
void childProcess(void){
int i;
for(i = 0 ; i < MAX_COUNT ; i++){
printf("This is \x1b[33mchild\x1b[0m process, value is %d\n",i); //"child" in yellow color
}
printf(" ******Child Process is done****** \n");
}
void parentProcess(void){
int i;
for(i = 0; i < MAX_COUNT; i++)
printf("This is \x1b[32mParent\x1b[0m process, value is %d\n",i); //Parent in green color
printf(" ******Parent Process is done****** \n");
}
int main(){
int i=0;
pid_t pid;
/*if( (pid = fork() ) != 0){
printf("%d\n\n",pid);
printf("Error : child process creation unsuccessful\n");
///_exit(0);
}*/
pid=fork();
printf("Outside pid : %d\n\n",pid);
if(pid == 0){
printf("\n\n\n");
childProcess();
//sleep(10000000);
printf("Exiting from child\n");
_exit(0);
}
else{
printf("\n\n\n");
parentProcess();
//sleep(10000000);
printf("Exiting from parent\n");
_exit(0);
}
return 0;
}
//Conclusion: As we created child process using fork(), parent process got pid of child (some non negative non zero integer, I got
// 5926,6002 etc. So pid is not zero initially, (in parent process) , parent process run initially, but remember child
// has been created and is waiting in background for context switching. After spending one time quantum on parent process
// CPU switches the context and child process starts running (child process is exact copy of parent process). It return
// pid as 0 because in child process pid value is 0. After running for one time quantum, again context switching occurs
// and control returns to parent process. This back and forth operation happens till one of the process does not end
// execution. After ending of one process no context switching occurs and remained process runs till its end.
//
//Remember: We have to _exit(0) from both the processes. I was not exiting the parent process hence I had to end program using ctrl+c
// because program was in stand still mode.