-
Notifications
You must be signed in to change notification settings - Fork 1
/
process_generator.c
204 lines (145 loc) · 5.5 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
/******************************************************************************
*
* Module: process_generator
*
* File Name: process_generator.c
*
* Description: C file for the process generator
*
*This file handles all the requirments of the process generator (refer to the PDF)
*
*******************************************************************************/
#include "headers.h"
#include"process_generator.h"
/*******************************************************************************
* Functions definitions *
*******************************************************************************/
/*
* Description : Function to clear all resources
*/
void clearResources(int signum)
{
//TODO Clears all resources in case of interruption
msgctl(msgq_id, IPC_RMID, (struct msqid_ds *)0);
//To Do : call the Destructor of the process queue
Queue_destroy(&process_queue);
}
/*******************************************************************************
* main *
*******************************************************************************/
//
int main(int argc, char * argv[])
{
//initialization of the msg queue
key_t key_id;
int send_val;
key_id = ftok("keyfile", 65);
msgq_id = msgget(key_id, 0666 | IPC_CREAT);
if (msgq_id == -1)
{
perror("Error in create");
exit(-1);
}
int stat_loc;
signal(SIGINT, clearResources);
// TODO Initialization
// 1. Read the input files.
FILE *input_file;
input_file = fopen("processes.txt","r");
if (!input_file) {
printf("couldn't open the input file \"processes.txt\"\n");
exit(-1);
}
// skipping the first line
fscanf(input_file,"%*[^\n]\n");
process_par p;
processNumbers=0;
// printf("%d %d %d %d",p.processNumber,p.arrival_time,p.runtime,p.priority);
while (fscanf(input_file, "%d %d %d %d", &p.processNumber, &p.arrival_time, &p.runtime, &p.priority) == 4){
process_par *ptr=malloc(sizeof(process_par));
*ptr=p;
Queue_push(&process_queue,(void*)ptr);
printf("\n");
processNumbers++;
}
fclose(input_file);
// 2. Ask the user for the chosen scheduling algorithm and its parameters, if there are any.
printf("Choose one of the following algorithms to use:\n"
"0.Non-preemptive Highest Priority First (HPF).\n"
"1.Shortest Remaining time Next (SRTN).\n"
"2.Round Robin (RR).\n");
SCHEDULING_ALGORITHM scheduling_algorithm = 0;
int x;
scanf("%d",&x);
scheduling_algorithm=x;
int time_chunk = -1;
if (scheduling_algorithm == RR) {
printf("Enter the time chunk for each process to run RR algorithm:");
scanf("%d",&time_chunk);
}
// 3. Initiate and create the scheduler and clock processes.
printf("Number of proccess =%d\nAlgorithm :%d\n",processNumbers,scheduling_algorithm);
int Clock_pid=fork();
if(Clock_pid<0){
perror("Error in forking the Clock Process\n");
exit(1);
}
if (Clock_pid == 0) { // child process
char *Clock_file[] = {"./clk", NULL}; // arguments for execv
execv(Clock_file[0], Clock_file); // execute the child process
fprintf(stderr, "Exec failed\n"); // execv only returns if it fails
exit(1);
}
int Scheduler_pid=fork();
if(Scheduler_pid<0){
perror("Error in forking the Scheduler Process\n");
exit(1);
}
if (Scheduler_pid == 0) { // child process
char* buff=malloc(sizeof(char));//we need to free these memory
char* len=malloc(sizeof(char));
char* timeChunk=malloc(sizeof(char));
int x=scheduling_algorithm;
snprintf(buff,sizeof(buff),"%d",x);
snprintf(len,sizeof(len),"%d",processNumbers);
snprintf(timeChunk,sizeof(timeChunk),"%d",time_chunk);
char *Scheduler_file[] = {"./scheduler",buff,len,timeChunk,NULL}; // arguments for execv
execv(Scheduler_file[0], Scheduler_file); // execute the child process
fprintf(stderr, "Exec failed\n"); // execv only returns if it fails
exit(1);
}
// 4. Use this function after creating the clock process to initialize clock
initClk();
// To get time use this
// int x = getClk();
// printf("current time is %d\n", x);
// TODO Generation Main Loop
int lastClk=-1;
while (!isEmpty(&process_queue)) {
// if(lastClk==getClk()){
// continue;
// }
int cur_time = getClk();
p = *((process_par*)Queue_peek(&process_queue));
// 6. Send the information to the scheduler at the appropriate time.
if (cur_time >= p.arrival_time) {
// send process to scheduler
// // use message queue don't forget the flag !IPC_NOWAIT
msgbuff sentProcess;
sentProcess.process=p;
sentProcess.mtype = 7; /* arbitrary value */
send_val = msgsnd(msgq_id, &sentProcess, sizeof(sentProcess.process), !IPC_NOWAIT);
if (send_val == -1)
perror("Errror in send");
Queue_pop(&process_queue);
}else{
lastClk=cur_time;
}
}
// 7. Clear clock resources
/*Don't uncomment the destroyclk there is an exception thrown*/
/*To Do: need to hndle the Exception */
// destroyClk(true);
waitpid(Scheduler_pid,&stat_loc,WUNTRACED);
//TODO clear the resources
}