-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfork.c
More file actions
45 lines (43 loc) · 891 Bytes
/
fork.c
File metadata and controls
45 lines (43 loc) · 891 Bytes
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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
pid_t pid;
pid = fork();
if(pid == 0)
{
int i = 0;
while(1)
{
printf("this is child process i = %d\n", i++);
printf("this is child process, my pid = %d\n", getpid());
printf("this is child process, my ppid = %d\n", getppid());
sleep(1);
if(i == 10)
{
return 0;
}
}
}
else if(pid > 0)
{
int i = 0;
while(1)
{
printf("this is parent process i = %d\n", i++);
printf("this is parent process, my pid = %d\n", getpid());
sleep(1);
if(i == 15)
{
return 0;
}
}
}
else
{
perror("fork");
exit(1);
}
return 0;
}