-
Notifications
You must be signed in to change notification settings - Fork 3
/
srtn.c
167 lines (147 loc) · 5.78 KB
/
srtn.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
#include "PriorityQueue2.h"
#include "queue2.h"
#include "headers.h"
/* Modify this file as needed*/
// int remainingtime;
int msgq_id, rec_val;
void *shmAddrCnt;
Process *shmCurrProcess;
struct PriorityQueue2 currProcesses;
Process currProcess;
Process tempProcess;
struct Queue2 Processes;
struct Queue2 finishedProcesses;
int main(int agrc, char *argv[])
{
initClk();
currProcesses = create();
// finishedProcesses = createQueue();
currProcess.id = -1;
msgq_id = msgget(CONNKEY, 0666 | IPC_CREAT);
shmAddrCnt = shmat(CONNKEY, (void *)0, 0);
shmCurrProcess = shmat(CONNKEY + 1, (void *)0, 0);
if (shmCurrProcess == -1)
{
perror("Error in attach in writer");
exit(-1);
}
shmCurrProcess->id = -1;
while (1)
{
int currTime = getClk();
if (shmCurrProcess->id == -1)
{
if (currProcesses.count > 0)
{
currProcess = currProcesses.front->data;
*shmCurrProcess = currProcess;
dequeue(&currProcesses);
if (currProcess.realID == -1)
{
printf("LOOOOOL");
int Process_Id = fork();
if (Process_Id == 0)
{
system("gcc process.c -o process.out");
execl("process.out", "process.c", currProcess.remRunTime, NULL);
}
currProcess.realID = Process_Id;
}
else
{
// resume this procces by send signal to it by the pid
kill(SIGCONT, currProcess.realID);
}
}
}
else
{
if (shmCurrProcess->remRunTime == 0)
{
tempProcess.id = shmCurrProcess->id;
tempProcess.arrivalTime = shmCurrProcess->arrivalTime;
tempProcess.finishTime = shmCurrProcess->finishTime;
tempProcess.Priority = shmCurrProcess->Priority;
tempProcess.realID = shmCurrProcess->realID;
tempProcess.remRunTime = shmCurrProcess->remRunTime;
tempProcess.startingTime = shmCurrProcess->startingTime;
tempProcess.runTime = shmCurrProcess->runTime;
if (currProcesses.count > 0)
{
currProcess = currProcesses.front->data;
*shmCurrProcess = currProcess;
dequeue(&currProcesses);
if (currProcess.realID == -1)
{
int Process_Id = fork();
if (Process_Id == 0)
{
system("gcc process.c -o process.out");
execl("process.out", "process.c", currProcess.remRunTime, NULL);
}
currProcess.realID = Process_Id;
}
else
{
// resume this procces by send signal to it by the pid
kill(SIGCONT, currProcess.realID);
}
}
}
else
{
if (currProcesses.count > 0)
{
currProcess = currProcesses.front->data;
if (shmCurrProcess->remRunTime > currProcess.remRunTime)
{
// stop the current process and create new on if runtime = remruntime
kill(SIGSTOP, shmCurrProcess->realID);
tempProcess.id = shmCurrProcess->id;
tempProcess.arrivalTime = shmCurrProcess->arrivalTime;
tempProcess.finishTime = shmCurrProcess->finishTime;
tempProcess.Priority = shmCurrProcess->Priority;
tempProcess.realID = shmCurrProcess->realID;
tempProcess.remRunTime = shmCurrProcess->remRunTime;
tempProcess.startingTime = shmCurrProcess->startingTime;
tempProcess.runTime = shmCurrProcess->runTime;
insert(&currProcesses, tempProcess.remRunTime, tempProcess);
if (currProcess.realID == -1)
{
int Process_Id = fork();
if (Process_Id == 0)
{
system("gcc process.c -o process.out");
execl("process.out", "process.c", currProcess.remRunTime, NULL);
}
currProcess.realID = Process_Id;
}
else
{
// resume this procces by send signal to it by the pid
kill(SIGCONT, currProcess.realID);
}
}
}
}
}
/*
there is a shared memory that contains the remaining time of running process and its id
this memory get updated in process.c
there is a variable contains the remaining time of the running process
you have array of structs of all processes
you have array of structs of current processes in the system
get the current clock
loop on all processes to push the ones that arrival time less than current time
loop on current processes to get the min running time one
if current running process equall null {
run the choosed pro
}
int clk = getclk();
loop on the array
get the min remaining time
*/
}
// destroyClk(false);
return 0;
}