-
Notifications
You must be signed in to change notification settings - Fork 0
/
fork.c
63 lines (48 loc) · 1.33 KB
/
fork.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
int i = 0;
int j = 0;
void doSomeWork(char*);
int main () {
pid_t proccessId = getpid();
printf("I am : %d \n",proccessId);
sleep(2);
pid_t pid = fork();
srand((int)pid);
printf("Fork returned: %d \n",(int)pid);
if(pid < 0) {
perror("Fork Failed !!");
}
if (pid == 0) {
printf("I am child with pid: %d\n",(int)getpid());
doSomeWork("CHILD");
exit(42);
} else if (pid > 0) {
printf("I am parrent and waiting child to end:\n");
sleep(30);
doSomeWork("PARENT");
int status = 0;
pid_t child_PID = wait(&status);
printf("Parent Know child %d finished with status %d...|\n",(int) child_PID,status);
int child_return_value = WEXITSTATUS(status);
printf(" \n Return value was -> %d \n",child_return_value);
}
return 0;
}
void doSomeWork (char* thread_name) {
const int NUM_TIMES = 2;
if (strcmp(thread_name,"PARENT") == 0) {
for (i = 0; i < NUM_TIMES; i++) {
sleep(rand()% 4);
printf("Done pass %d for %s \n",i,thread_name);
}
} else if (strcmp(thread_name,"CHILD") == 0) {
for (j = 0; j < NUM_TIMES; j++) {
sleep(rand()% 4);
printf("Done pass %d for %s \n",j,thread_name);
}
}
}