-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbus.c
196 lines (169 loc) · 5.65 KB
/
bus.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
// #include <sys/wait.h>
// #include <sys/ipc.h>
// #include <sys/msg.h>
#include <string.h>
#define MAX_WORKERS_PER_DAY 10
#define MAX_WORKERS_IN_BUS 5
#define MAX_LINE_LENGTH 100
#define MAX_NAME_LENGTH 50
#define MAX_NAMES 100
// Structure for message queue
typedef struct {
long mtype; // Message type
int numWorkers; // Number of workers brought in
} Message;
// char** select(const char *filename, const char *day, int *count) {
// FILE *file = fopen(filename, "r");
// if (file == NULL) {
// printf("Failed to open the file.\n");
// return NULL;
// }
// char line[MAX_LINE_LENGTH];
// char **selectedNames = (char **)malloc(MAX_NAMES * sizeof(char *));
// *count = 0;
// while (fgets(line, sizeof(line), file) != NULL && *count < MAX_NAMES) {
// // Extract the name and days from the line
// char *name = strtok(line, " ");
// char *days = strtok(NULL, "\n");
// // Tokenize the days using space delimiter
// char *token = strtok(days, " ");
// while (token != NULL) {
// if (strcmp(token, day) == 0) {
// // Allocate memory for the name and store it in the selected names array
// selectedNames[*count] = (char *)malloc(MAX_NAME_LENGTH * sizeof(char));
// strcpy(selectedNames[*count], name);
// (*count)++;
// break;
// }
// token = strtok(NULL, " ");
// }
// }
// fclose(file);
// return selectedNames;
// }
int countWorkers(char *fileName, char *word)
{
FILE *file = fopen(fileName, "r");
if (file == NULL)
{
printf("Error: Unable to open file '%s' for reading.\n", fileName);
return -1;
}
int count = 0;
char line[100];
while (fgets(line, sizeof(line), file))
{
char *p = line;
while ((p = strstr(p, word)))
{
count++;
p += strlen(word);
}
}
fclose(file);
return count;
}
int StartDay(char day [100]){
const char *filename = "workers.txt";
int numbWorkers = countWorkers("workers.txt",day);
printf("%d\n", numbWorkers);
return numbWorkers ;
}
int getNumberOfBuses(int numbWorkers){
int numOfbuses ;
if (numbWorkers == 0){numOfbuses = 0;}
else if (numbWorkers <= MAX_WORKERS_IN_BUS){ numOfbuses = 1;}
else {numOfbuses= 2; }
return numOfbuses;
}
// Function for bus process
void busProcess(int busId, int pipeFd[], int msgQueueId, char **selectedNames, int numWorkers) {
close(pipeFd[1]); // Close write end of pipe
// Send signal to parent process
kill(getppid(), SIGUSR1);
// Receive worker list from parent process
read(pipeFd[0], selectedNames , sizeof(char**) * MAX_WORKERS_IN_BUS);
// // Display worker list
// int length = sizeof(selectedNames) / sizeof(selectedNames[0]);
// printf("Bus %d Worker List:\n", busId);
// if (selectedNames != NULL) {
// for (int i = 0; i < length; i++) {
// printf("%s\n", selectedNames[i]);
// free(selectedNames[i]); // Free allocated memory for each name
// }
// free(selectedNames); // Free the selected names array
// }
// Send summary message to parent process
Message msg;
msg.mtype = busId;
msg.numWorkers = MAX_WORKERS_IN_BUS;
msgsnd(msgQueueId, &msg, sizeof(Message) - sizeof(long), 0);
close(pipeFd[0]); // Close read end of pipe
exit(0);
}
// ==============MAIN=================
int main(){
printf("Welcome to the Spring Festival!\n");
printf("Please choose a Day to start:\n");
char day[100];
int count;
fgets(day, 100, stdin);
day[strcspn(day, "\n")] = 0;
int numberOfWorkers = StartDay(day);// number of workers in the seleced day
int numOfbuses = getNumberOfBuses(numberOfWorkers); //get number of buses needed
// char **selectedNames = select("workers.txt", day, &count); // get the names of the workers in the selected day
// Create message queue
key_t key = ftok(".", 'm');
int msgQueueId = msgget(key, IPC_CREAT | 0666);
// Create pipes
int pipeFd[2][2];
for (int i = 0; i < numOfbuses; i++) {
if (pipe(pipeFd[i]) == -1) {
perror("pipe");
return 1;
}
}
// Create buses
// Fork child processes (buses)
pid_t childPid;
for (int i = 0; i < numOfbusesBuses; i++) {
childPid = fork();
if (childPid == -1) {
perror("fork");
return 1;
} else if (childPid == 0) {
// Child process (bus)
busProcess(i + 1, pipeFd[i], msgQueueId, selectedNames, numberOfWorkers);
}
}
// Parent process
for (int i = 0; i < numOfbuses; i++) {
close(pipeFd[i][0]); // Close read end of pipe
}
// Wait for signal from child processes
for (int i = 0; i < numOfbuses; i++) {
pause();
}
// Send worker list to child processes
for (int i = 0; i < numOfbuses; i++) {
write(pipeFd[i][1], selectedNames, sizeof(char) * MAX_WORKERS_IN_BUS);
close(pipeFd[i][1]); // Close write end of pipe
}
// Receive summary messages from child processes
for (int i = 0; i < numOfbuses; i++) {
Message msg;
msgrcv(msgQueueId, &msg, sizeof(Message) - sizeof(long), i + 1, 0);
printf("Bus %ld brought in %d workers.\n", msg.mtype, msg.numWorkers);
}
// Clean up message queue
msgctl(msgQueueId, IPC_RMID, NULL);
// Wait for child processes to terminate
for (int i = 0; i < numOfbuses; i++) {
wait(NULL);
}
return 0;
}