-
Notifications
You must be signed in to change notification settings - Fork 3
/
process_generator.c
142 lines (114 loc) · 3.25 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
#include "headers.h"
#include "datastructure.h"
void clearResources(int);
void readProcesses();
void readAlgo();
void createClkChild();
void createSchChild();
void sendProccessToSchd();
ALGO_mem CURR_ALGO_mem;
struct PQueue *queue;
enum ALGO CURR_ALGO;
int Quantum = 1;
int msgq_id;
pid_t clk_pid, schedule_pid;
int main(int argc, char *argv[])
{
signal(SIGINT, clearResources);
// TODO Initialization
// 1. Read the input files.
readProcesses(argv[1]);
for(int i =2;i <= 6;i+= 2) {
if(i >= argc) break;
if(strcmp(argv[i] ,"-sch") == 0)
CURR_ALGO = atoi(argv[i+1]);
else if(strcmp(argv[i] ,"-mem") == 0)
CURR_ALGO_mem = atoi(argv[i+1]);
else
Quantum = atoi(argv[i+1]);
}
//printf("======= %i %i %i =======\n",argc, CURR_ALGO, CURR_ALGO_mem);
// 3. Initiate and create the scheduler and clock processes.
createSchChild();
createClkChild();
// 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.
msgq_id = msgget(MSGQKEY, 0666 | IPC_CREAT); // create message queue for scheduler
if (msgq_id == -1)
{
perror("generator: Error in create queue");
exit(-1);
}
// 6. Send the information to the scheduler at the appropriate time.
processIn process_ending ;
process_ending.id = -1;
process_ending.arrival_time = 0;
struct PNode * endingNode = CreateNode(process_ending);
enqueuProcess(endingNode, queue);
while (true)
sendProccessToSchd();
raise(SIGINT);
}
void clearResources(int signum)
{
destroyClk(true); // kill clock + deattach shared mem + send Int to all
int lock;
wait(&lock); //wait for clock
wait(&lock); // wait for scheduler
msgctl(msgq_id, IPC_RMID, (struct msqid_ds *)0); // clear msg queue of scheduler
//DEBUG
raise(SIGKILL); // kill himself
}
/**/
void readProcesses(char * file_name)
{
queue = createQueue();
FILE *fptr = fopen(file_name, "r");
fseek(fptr, 37, SEEK_SET);
while (readAddProccess(fptr, queue))
;
}
void createClkChild()
{
clk_pid = fork();
if (clk_pid == 0)
execl("clk.out", "", NULL);
if (clk_pid < 0)
{
printf("error in fork() clk\n");
exit(-1);
}
}
void createSchChild()
{
schedule_pid = fork();
if (schedule_pid == 0)
{
char algo[5];
char algomem[5];
char q[5];
sprintf(algo, "%i", (int)CURR_ALGO);
sprintf(algomem, "%i", (int)CURR_ALGO_mem);
sprintf(q, "%i", Quantum);
execl("scheduler.out", "scheduler.out", algo,algomem, q, NULL);
}
if (schedule_pid < 0)
{
printf("error in fork() scheduler\n");
exit(-1);
}
}
void sendProccessToSchd()
{
if (queue->head == NULL)
return;
int curr_time = getClk();
while (queue->head != NULL && queue->head->proccess.arrival_time <= curr_time)
{
msgsnd(msgq_id, &(queue->head->proccess), sizeof(processIn), !IPC_NOWAIT);
dequeuProcess(queue);
}
}