forked from seeditsolution/cprogram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorphanprocess.c
40 lines (35 loc) · 859 Bytes
/
orphanprocess.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
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
// fork() Create a child process
int pid = fork();
if (pid > 0)
{
//getpid() returns process id
// while getppid() will return parent process id
printf("Parent process\n");
printf("ID : %d\n\n",getpid());
}
else if (pid == 0)
{
printf("Child process\n");
// getpid() will return process id of child process
printf("ID: %d\n",getpid());
// getppid() will return parent process id of child process
printf("Parent -ID: %d\n\n",getppid());
sleep(10);
// At this time parent process has finished.
// So if u will check parent process id
// it will show different process id
printf("\nChild process \n");
printf("ID: %d\n",getpid());
printf("Parent -ID: %d\n",getppid());
}
else
{
printf("Failed to create child process");
}
return 0;
}