-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain3.c
More file actions
53 lines (46 loc) · 1.12 KB
/
main3.c
File metadata and controls
53 lines (46 loc) · 1.12 KB
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
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#define MAX_COUNT 200
void ChildProcess(void); /* child process prototype */
void ParentProcess(void); /* parent process prototype */
void main(void)
{
pid_t pid;
int i;
for (i=0; i < 2; i++){
pid = fork();
if(pid < 0)
{
printf("Error");
}
if (pid == 0) {
ChildProcess();
}
else {
ParentProcess();
}
}
exit(0);
}
void ChildProcess(void)
{
srandom(time(NULL));
int sleep_time = random() % 10;
srandom(time(NULL));
int loop_num = random() % 30;
int i;
for(i=0; i < loop_num; i++){
printf("Child: %d is going to sleep!\n ", getpid());
sleep(sleep_time);
printf("Child Pid: is awake!\n Where is my Parent: %d\n ", getppid());
}
exit(0);
}
void ParentProcess(void)
{
int status;
pid_t pid = waitpid(-1, &status, 0);
printf("Child Pid:%d has completed\n ", pid);
printf("\n");
}