-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_generator.c
197 lines (151 loc) · 4.94 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
#include "headers.h"
void clearResources(int);
int clk_pid;
int scheduler_pid;
int stat_loc;
void forkClkAndScheduler(){
clk_pid = fork();
if(clk_pid < 0){
printf("Error while forking \n");
fflush(stdout);
exit(1);
}
// Child(clock proc)
else if(clk_pid == 0){
execl("clk.out","clk.out",(char *)NULL);
}
// Parent(process generator)
else{
scheduler_pid = fork();
if(scheduler_pid < 0){
printf("Error while forking \n");
fflush(stdout);
exit(1);
}
// Child(Scheduler proc)
else if (scheduler_pid == 0){
char selectedAlgorithmChar[5];
sprintf(selectedAlgorithmChar, "%d", selected_algo);
execl("scheduler.out","scheduler.out",selectedAlgorithmChar,(char *)NULL);
}
}
}
void getAlgorithmFromUser(){
printf("Enter the required algorithm \n");
printf("1. SJF\n2. HPF\n3. RR\n4. MLFQ\n");
fflush(stdout);
do{
scanf("%d",&selected_algo);
if(selected_algo < 1 || selected_algo > 4){
printf("Invalid choice! Please enter a number from 1 to 4\n");
fflush(stdout);
}
} while(selected_algo < 1 || selected_algo > 4);
if(selected_algo == 1) SJFflag = 1;
}
int main(int argc, char *argv[])
{
signal(SIGINT, clearResources);
// TODO Initialization
// 1. Read the input files.
FILE *file = fopen("processes.txt", "r");
char line[30];
int size;
int d;
int a;
// read the number of generated processes
fscanf(file, "%d", &size);
process *processes[size];
// ignore the header line
fscanf(file, "%s %s %s %s %s", line, line, line, line, line);
// read each process
int next_process = 0;
while (next_process < size)
{
int id, arrival, runtime, priority, memSize; // used in reading the input file
fscanf(file, "%d %d %d %d %d", &id, &arrival, &runtime, &priority, &memSize);
process *new_process = (process *)malloc(sizeof(process));
*new_process = (process) {
.arrival = arrival,
.id = id,
.runtime = runtime,
.remainingtime = runtime, // initially, remaining time = needed run time
.waitingtime = 0, // initially, waiting time = 0
.priority = priority,
.StartedBefore = 0, // initially, process has not started before
.status = 3,
.memSize = memSize,
};
processes[next_process] = new_process;
next_process++;
}
// TODO: parse the cmd line arguments
// 2. Ask the user for the chosen scheduling algorithm and its parameters, if there are any.
// 3. Initiate and create the scheduler and clock processes.
getAlgorithmFromUser();
//system("./scheduler & "); // TODO: You can either pass args or utilize IPCs
// 4. Send the information to the scheduler at the appropriate time.
// create the message queue
key_t key_id;
int msgq_id, send_val;
key_id = ftok("keyfile", 65);
msgq_id = msgget(key_id, 0666 | IPC_CREAT);
if (msgq_id == -1)
{
perror("Error in create msgq_id");
exit(-1);
}
forkClkAndScheduler() ;
initClk();
printf("Message Queue ID = %d\n", msgq_id);
fflush(stdout);
// end of message queue initialization
int next_scheduled = 0; // next process to schedule
while (true)
{
int x = getClk();
if (next_scheduled == size)
{
// system("fg");
break;
}
;
while (next_scheduled < size)
{
// printf("%d", processes[next_scheduled].arrival);
if (processes[next_scheduled]->arrival == x)
{
// send to the queue
// char str[] = "\nMessage from sender to receiver..";
msgBuf message;
message.mtype = 1; /* arbitrary value */
// strcpy(message.mtext, str);
message.proc = *processes[next_scheduled];
send_val = msgsnd(msgq_id, &message, sizeof(message.proc), IPC_NOWAIT);
if (send_val == -1)
perror("Errror in send send_val");
// end of send
next_scheduled++;
}
else
break;
}
printf("current time is %d\n", x);
sleep(1);
}
printf("Waiting for scheduler to finish\n");
fflush(stdout);
int temp_pid = wait(&stat_loc);
// 5. Clear clock resources
destroyClk(true);
}
void clearResources(int signum)
{
destroyClk(true);
key_t key_id;
key_id = ftok("keyfile", 65);
int msgq_id = msgget(key_id, 0666 | IPC_CREAT);
msgctl(msgq_id, IPC_RMID, (struct msqid_ds *)0);
exit(0);
// TODO Clears all resources in case of interruption
}