-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_generator.c
208 lines (188 loc) · 5.83 KB
/
process_generator.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "headers.h"
void clearResources(int);
struct processData
{
int id;
int arrival;
int runtime;
int priority;
int memsize;
};
struct Node
{
struct processData data;
struct Node *next;
};
struct msgbuff
{
long mtype;
struct processData data;
};
int ProcessQ;
void readFile(struct Node** head, struct Node** tail) {
FILE *fptr;
fptr = fopen("processes.txt", "r");
if (fptr == NULL) {
perror("Error in opening file");
exit(1);
}
int id, arrival, runtime, priority;
char line[100]; // Assuming each line has at most 100 characters
while (fgets(line, sizeof(line), fptr) != NULL) {
if (line[0] == '#') {
// Ignore comment lines
continue;
}
struct processData process;
if (sscanf(line, "%d %d %d %d %d", &process.id, &process.arrival, &process.runtime, &process.priority, &process.memsize) != 5) {
fprintf(stderr, "Error parsing line: %s\n", line);
continue;
}
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = process;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
*tail = newNode;
} else {
(*tail)->next = newNode;
*tail = newNode;
}
}
fclose(fptr);
}
void printProcesses(struct Node* head)
{
//print all processes
struct Node* current = head;
while (current != NULL)
{
printf("id: %d, arrival: %d, runtime: %d, priority: %d, memsize: %d\n", current->data.id, current->data.arrival, current->data.runtime, current->data.priority, current->data.memsize);
current = current->next;
}
}
int main(int argc, char * argv[])
{
signal(SIGINT, clearResources);
// TODO Initialization
// 1. Read the input files.
//read file processes.txt
struct Node* head = NULL;
struct Node* tail = NULL;
readFile(&head, &tail);
printProcesses(head);
// 2. Ask the user for the chosen scheduling algorithm and its parameters, if there are any.
printf("Please enter the chosen scheduling algorithm and its parameters\n");
printf("1. HPF\n");
printf("2. SRTN\n");
printf("3. RR \n");
int chosenAlgorithm, quantum;
quantum = 0;
scanf("%d", &chosenAlgorithm);
if (chosenAlgorithm == 3)
{
printf("Please enter the quantum\n");
scanf("%d", &quantum);
}
if (chosenAlgorithm != 1 && chosenAlgorithm != 2 && chosenAlgorithm != 3)
{
printf("Invalid input\n");
exit(1);
}
key_t key_id;
key_id = 17;
ProcessQ = msgget(key_id, 0666 | IPC_CREAT);
printf("ProcessQ: %d\n", ProcessQ);
// 3. Initiate and create the scheduler and clock processes.
int pidScheduler = fork();
int pidClk;
if (pidScheduler == -1)
{
perror("error in fork");
}
else if (pidScheduler == 0)
{
char algorithm [10];
sprintf(algorithm, "%d", chosenAlgorithm);
char quantumStr[10];
sprintf(quantumStr, "%d", quantum);
char* args[] = {"./scheduler", algorithm, quantumStr, NULL};
execvp(args[0], args);
}
else
{
pidClk = fork();
if (pidClk == -1)
{
perror("error in fork");
}
else if (pidClk == 0)
{
char * args[] = {"./clk", NULL};
execvp(args[0], args);
}
}
// 4. Use this function after creating the clock process to initialize clock
initClk();
// To get time use this
// TODO Generation Main Loop
// 5. Create a data structure for processes and provide it with its parameters.
int x = 0;
while(head != NULL)
{
if (getClk() > x)
{
struct msgbuff message = {1, head->data};
//printf("at Time %d Head ID: %d\n", getClk(), head->data.id);
x = getClk();
while(head != NULL && head->data.arrival == x)
{
printf("at Time %d Sent ID: %d\n", x, head->data.id);
struct processData process = head->data;
struct msgbuff message;
message.mtype = 1;
message.data = process;
int send_val = -1;
while (send_val == -1)
{
send_val = msgsnd(ProcessQ, &message, sizeof(message.data), !IPC_NOWAIT);
}
//int send_val = msgsnd(ProcessQ, &message, sizeof(message.data), !IPC_NOWAIT);
struct Node* temp = head;
head = head->next;
free(temp);
}
message.data.id = -1;
//printf("at Time %d Sent ID: %d\n", getClk(), -1);
int send_val = -1;
while (send_val == -1)
{
send_val = msgsnd(ProcessQ, &message, sizeof(message.data), !IPC_NOWAIT);
if(send_val != -1)
{
printf("at Time %d Sent ID: %d mem: %d\n", x, message.data.id, message.data.memsize);
}
}
}
}
struct msgbuff message = {1, {-2, -1, -1, -1, -1}};
int send_val = -1;
printf("Sent Last %d\n", getClk());
while (send_val == -1)
{
send_val = msgsnd(ProcessQ, &message, sizeof(message.data), !IPC_NOWAIT);
}
printf("All processes sent\n");
// 6. Send the information to the scheduler at the appropriate time.
// 7. Clear clock resources
waitpid(pidScheduler, NULL, 0);
msgctl(ProcessQ, IPC_RMID, (struct msqid_ds *)0);
destroyClk(true);
}
void clearResources(int signum)
{
//TODO Clears all resources in case of interruption
msgctl(ProcessQ, IPC_RMID, (struct msqid_ds *)0);
destroyClk(true);
exit(0);
}