-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask2_2_3.c
166 lines (126 loc) · 4.37 KB
/
task2_2_3.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
#define ERR(source) \
(fprintf(stderr, "%s:%d\n", __FILE__, __LINE__), perror(source), kill(0, SIGKILL), exit(EXIT_FAILURE))
volatile int *sig_count = NULL; // Signal counters
volatile pid_t *child_pids = NULL; // Array of child PIDs
volatile int num_students = 0;
void sethandler(void (*f)(int, siginfo_t *, void *), int sigNo) {
struct sigaction act;
memset(&act, 0, sizeof(struct sigaction));
act.sa_sigaction = f; // Use sa_sigaction for more detailed signal handling
act.sa_flags = SA_SIGINFO | SA_RESTART; // Set SA_SIGINFO flag for additional info
if (-1 == sigaction(sigNo, &act, NULL)) {
ERR("sigaction");
}
}
int get_student_index(pid_t pid) {
for (int i = 0; i < num_students; i++) {
if (child_pids[i] == pid) {
return i;
}
}
return -1;
}
void sig1_handler(int sig, siginfo_t *info, void *context) {
pid_t sender_pid = info->si_pid;
int student_index = get_student_index(sender_pid);
if (student_index >= 0) {
sig_count[student_index]++;
}
}
void student_work(int index, int prob, int p, int t, int n) {
pid_t pid = getpid();
srand(getpid());
for(int i = 0; i < p; i++)
{
struct timespec work_time = {0, t * 100000000};
struct timespec err_time = {0, 50000000};
int issue_count = 0;
int r = rand() % 100;
if (i == 0)
printf("Student[%d,%d] has started doing task!\n", index, pid);
printf("Student[%d, %d] is starting doing part %d of %d!\n", index, pid, i + 1, p);
nanosleep(&work_time, NULL);
while(r < prob)
{
nanosleep(&err_time, NULL);
printf("Student[%d,%d] has issue (%d) doing task!\n", index, pid, issue_count);
r = rand() % 100;
issue_count++;
}
if(kill(getppid(), SIGUSR1))
ERR("SIGUSR1");
printf("Student[%d, %d] has finished part %d of %d!\n", index, pid, i+1, p);
}
}
void teacher_work(int n, int p) {
int completed_students = 0;
int *processed_signals = calloc(num_students, sizeof(int));
if (!processed_signals) ERR("calloc");
while (completed_students < n) {
for (int i = 0; i < num_students; i++) {
if (sig_count[i] > processed_signals[i]) {
//printf("Teacher received SIGUSR1 from student[%d, PID=%d]. Total signals: %d\n",
//i, child_pids[i], sig_count[i]);
processed_signals[i] = sig_count[i];
}
}
pid_t pid = waitpid(-1, NULL, WNOHANG);
if (pid > 0) {
printf("Teacher has accepted solution of student [%d]\n", pid);
completed_students++;
} else if (pid < 0 && errno != ECHILD) {
ERR("waitpid");
}
}
}
void create_students(int n, int *probability, int p, int t) {
pid_t s;
for (int i = 0; i < n; i++) {
if (( s = fork()) < 0)
ERR("fork");
if (!s) {
student_work(i, probability[i], p, t, n);
exit(EXIT_SUCCESS);
}
child_pids[i] = s;
}
}
void usage(char *name) {
fprintf(stderr, "USAGE: %s 0<n\n", name);
exit(EXIT_FAILURE);
}
int main(int argc, char **argv) {
if (argc < 4)
usage(argv[0]);
int p = atoi(argv[1]);
int t = atoi(argv[2]);
if (p < 1 || p > 10 || t < 1 || t > 10) {
fprintf(stderr, "Error: p must be between 1 and 10, t must be between 1 and 10.\n");
return 1;
}
int n = argc - 3;
num_students = argc - 3;
int *probability = (int *)malloc(n * sizeof(int));
for(int i = 0; i < n; i++){
probability[i] = atoi(argv[i+3]);
}
child_pids = malloc(n * sizeof(pid_t));
if (!child_pids) ERR("malloc");
sig_count = malloc(n * sizeof(int));
if (!sig_count) ERR("malloc");
sethandler(sig1_handler, SIGUSR1);
create_students(n, probability, p, t);
teacher_work(n, p);
free(probability);
free((void *)child_pids);
return EXIT_SUCCESS;
}