-
Notifications
You must be signed in to change notification settings - Fork 1
/
sjf.m
46 lines (40 loc) · 1.36 KB
/
sjf.m
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
function curProc = sjf(arrival, flagPreempt)
%% DECLARE PERSISTENT VALUES
persistent pQueueNames
persistent pQueueTimes
%% INITIALIZATION
%% PARSE INPUT
if (not(isempty(pQueueNames)))
pQueueTimes(1) = pQueueTimes(1) - 1; % one timestep passed
if (pQueueTimes(1) <= 0) % job done so remove it
pQueueNames = pQueueNames(2:end);
pQueueTimes = pQueueTimes(2:end);
end
end
if (not(isempty(arrival))) % add new process to queue
if isempty(pQueueNames) % queue is empty
pQueueNames = arrival(1,1);
pQueueTimes = cell2mat(arrival(1,2));
else % find where to add new process
qStart = 2;
if flagPreempt % policy is preemptive
qStart = 1;
end
indexQ = size(pQueueTimes, 2) + 1; % index to insert new job
for i = qStart:size(pQueueTimes, 2)
if (cell2mat(arrival(1,2)) < pQueueTimes(i))
indexQ = i;
break
end
end
pQueueNames(indexQ+1:end+1) = pQueueNames(indexQ:end); % insert job name into queue
pQueueNames(indexQ) = arrival(1,1);
pQueueTimes = [pQueueTimes(1:indexQ-1), cell2mat(arrival(1,2)), pQueueTimes(indexQ:end)]; % and it's time
end
end
if (not(isempty(pQueueNames)))
curProc = pQueueNames{1}; % return current running job
else
curProc = '_'; % no new job and empty queue
end
end