-
Notifications
You must be signed in to change notification settings - Fork 0
/
08_Sleep_In_Process.c
60 lines (48 loc) · 1.19 KB
/
08_Sleep_In_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
// Q. Create a child process using fork() and then use sleep() in parent 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)
{
printf("This is Child Process!\n");
printf("Child: Child Process pid: %d\n", getpid());
printf("Child: Parent Process pid: %d\n", getppid());
}
// Parent process
else
{
// Parent process will be in sleep for 5 secs
// in the meantime execution of the child process will be completed
sleep(5);
printf("This is Parent Process!\n");
printf("Parent: Parent Process pid: %d\n", getpid());
printf("Parent: Child Process pid: %d\n", f);
}
// This part will be executed by both child & parent
printf("This is common!\n");
}
/*
Output:
s4shibam@SHIBAM:~/OS$ gcc 8_Sleep_In_Process.c
s4shibam@SHIBAM:~/OS$ ./a.out
Before fork!
This is Child Process!
Child: Child Process pid: 2154
Child: Parent Process pid: 2153
This is common!
This is Parent Process!
Parent: Parent Process pid: 2153
Parent: Child Process pid: 2154
This is common!
s4shibam@SHIBAM:~/OS$
*/