-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfork2.c
109 lines (78 loc) · 1.8 KB
/
fork2.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* fork example */
#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
int pids[10]; // pid array
int count;
int flag = 0;
void alrm_handler(int signo)
{
// send signal to child
int idx = count %10;
kill(pids[idx], SIGUSR1);
printf("At time : %d! send signal to %d\n", count, pids[idx]);
count++;
if (count >= 30) flag=1;
}
void child_handler(int signo)
{
printf("(%d) sigusr1 cnt: %d!\n", getpid(), count);
count++;
// send signal to child
if (count >= 3) flag=1;
}
int main()
{
int i = 0;
struct sigaction old_sa;
struct sigaction new_sa;
// set up SIGALRM handler
memset(&old_sa, 0, sizeof(old_sa));
memset(&new_sa, 0, sizeof(new_sa));
new_sa.sa_handler = alrm_handler;
sigaction(SIGALRM, &new_sa, &old_sa);
// fire!
struct itimerval new_timer;
struct itimerval old_timer;
memset(&new_timer, 0, sizeof(new_timer));
memset(&old_timer, 0, sizeof(old_timer));
new_timer.it_interval.tv_sec = 1;
new_timer.it_value.tv_sec = 1;
setitimer(ITIMER_REAL, &new_timer, &old_timer);
for (i = 0 ; i < 10 ; i++) {
int ipid;
int child_status;
ipid = fork();
if (ipid < 0) {
perror("fork error");
} else if (ipid == 0) {
// child
// set up SIGALRM handler
memset(&old_sa, 0, sizeof(old_sa));
memset(&new_sa, 0, sizeof(new_sa));
new_sa.sa_handler = child_handler;
sigaction(SIGUSR1, &new_sa, &old_sa);
count = 0;
flag = 0;
while (!flag) {
sleep(1);
}
printf("pid: %d, done. make sure you're not creating additional proc.\n", getpid());
return 0;
} else {
// parent
pids[i] = ipid;
//ipid = wait(&child_status);
//printf("(%d) proc %d completed.\n", getpid(), ipid);
}
}
while (!flag) {
sleep(1);
}
return 0;
}