-
Notifications
You must be signed in to change notification settings - Fork 0
/
09_Orphan_Process.c
97 lines (83 loc) · 2.27 KB
/
09_Orphan_Process.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Q. Create an orphan process (Use sleep() in child process)
/*
Note:
Orphan Process:
An orphan process is a process whose parent has finished.
Suppose P1 and P2 are two process such that P1 is the parent process and
P2 is the child process of P1. Now, if P1 finishes before P2 finishes,
then P2 becomes an orphan process.
The following programs we will see how to create an orphan process.
*/
#include <stdio.h>
#include <unistd.h>
int main()
{
int f;
printf("Before fork!\n");
f = fork();
// The creation of the process was unsuccessful
if (f < 0)
{
printf("Error occurred!\n");
}
// Child process
else if (f == 0)
{
// Child process will be in sleep for 5 secs
// in the meantime execution of the parent process will be completed.
// Thus child process will become Orphan (parent has finished already)
sleep(5);
printf("This is Child Process!\n");
printf("Child: Child Process pid: %d\n", getpid());
printf("Child: Parent Process pid: %d\n", getppid()); // It will print incorrect pid (parent process has terminated)
}
// Parent process
else
{
sleep(2); // Time to run "ps" command
printf("This is Parent Process!\n");
printf("Parent: Parent Process pid: %d\n", getpid());
printf("Parent: Child Process pid: %d\n", f);
}
}
/*
Output:
s4shibam@SHIBAM:~/OS$ gcc 9_Orphan_Process.c
s4shibam@SHIBAM:~/OS$ ./a.out
Before fork!
This is Parent Process!
Parent: Parent Process pid: 2025
Parent: Child Process pid: 2026
s4shibam@SHIBAM:~/OS$ This is Child Process!
Child: Child Process pid: 2026
Child: Parent Process pid: 1
^C
s4shibam@SHIBAM:~/OS$
*/
/*
Output: [using ps command]
s4shibam@SHIBAM:~/OS$ gcc 9_Orphan_Process.c
s4shibam@SHIBAM:~/OS$ ./a.out &
[1] 2342
s4shibam@SHIBAM:~/OS$ Before fork!
ps
PID TTY TIME CMD
2202 tty1 00:00:00 bash
2342 tty1 00:00:00 a.out
2343 tty1 00:00:00 a.out
2344 tty1 00:00:00 ps
s4shibam@SHIBAM:~/OS$ This is Parent Process!
Parent: Parent Process pid: 2342
Parent: Child Process pid: 2343
ps
PID TTY TIME CMD
2202 tty1 00:00:00 bash
2343 tty1 00:00:00 a.out
2345 tty1 00:00:00 ps
[1]+ Done ./a.out
s4shibam@SHIBAM:~/OS$ This is Child Process!
Child: Child Process pid: 2343
Child: Parent Process pid: 1
^C
s4shibam@SHIBAM:~/OS$
*/