Repository navigation
Expand file tree
/
Copy pathsolution
More file actions
778 lines (670 loc) · 29.4 KB
/
Copy pathsolution
File metadata and controls
778 lines (670 loc) · 29.4 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edd;
/**
*
* @author miche_ysmoa6e
*/
public class Scheduler {
// Tomando que 1 ciclo de ejecución del CPU son 0.00001ms
private Lista processList; //Lista en la cual se guardan todos los procesos a ejecutar.
private int memoryAvaiable;
private Lista deviceTable = new Lista();
private int remainingSpace = memoryAvaiable;
//Se agregan desde la interfaz
public Scheduler(Lista processList, int memorySpace, Lista deviceTable) {
this.processList = processList;
this.memoryAvaiable = memorySpace;
this.deviceTable = deviceTable;
this.remainingSpace = memoryAvaiable;
}
// quantum medido en ms
public void RoundRobin (int setQuantum, Cola readyQueue, Dispatcher dispatcher, Cola blockedQueue, Lista terminatedProcessList){
int quantum = setQuantum;
if (readyQueue.getCount() > 0){
var processToActivate = readyQueue.dequeue();
if (!(processToActivate instanceof PCB)) {
// defensive: if the queue didn't contain a PCB, nothing to do
return;
}
PCB pcbOfActiveProcess = (PCB) processToActivate;
// verificar si el proceso ya está activado y si no lo está, activarlo
if (!"running".equals(pcbOfActiveProcess.getStatus())){
dispatcher.activate(pcbOfActiveProcess, getProcessList()); // ready ---> running
}
Proceso toRun = dispatcher.getActiveProcess(getProcessList());
if (toRun == null) return;
while(toRun != null && "running".equals(toRun.getPcb().getStatus())) {
// honor interruption from the scheduler thread (UI changed planification)
if (Thread.currentThread().isInterrupted()) return;
System.out.println("is running");
if (toRun.getTimeSpent() >= quantum || toRun.getProcessingTime() <= toRun.getTotalTimeSpent()){
dispatcher.deactivate(toRun); // running --> ready
// after deactivate the PCB status should be "ready"
}
if ("I/O Bound".equals(toRun.getBound()) && toRun.getPcb().getPc()-1 == toRun.getInterruptAt()){
// transition to blocked: set status explicitly, then enqueue only if status is "blocked"
toRun.getPcb().setStatus("blocked");
if ("blocked".equals(toRun.getPcb().getStatus())) {
blockedQueue.enqueue(toRun.getPcb());
}
try {
toRun.sleep(toRun.getIoCicles()*1000);
accessDevice(toRun, dispatcher, blockedQueue);
}
catch(InterruptedException e) {
// honor interruption — propagate by setting the interrupted flag and return
Thread.currentThread().interrupt();
return;
}
}
try {
Thread.sleep(1000);
}
catch(InterruptedException e) {
// honor interruption — set flag and return
Thread.currentThread().interrupt();
return;
}
}
// When the process finished its quantum / execution we must enqueue it back
// only if its PCB status is "ready" (or handle termination).
if (toRun.getProcessingTime() <= toRun.getTotalTimeSpent()) {
toRun.getPcb().setStatus("terminated");
terminatedProcessList.add(toRun);
} else {
if ("ready".equals(toRun.getPcb().getStatus())) {
readyQueue.enqueue(toRun.getPcb());
} else {
// If it's not ready (for example blocked), do not enqueue into ready queue.
System.out.println("Not enqueuing to ready queue because status is: " + toRun.getPcb().getStatus());
}
}
}
}
public void SPN(Cola readyQueue, Dispatcher dispatcher, Cola blockedQueue, Lista terminatedProcessList){
if (readyQueue.getCount() > 0){
var processToActivate = readyQueue.get(0);
if (!(processToActivate instanceof PCB)) return;
PCB pcbOfActiveProcess = ((PCB)processToActivate);
// verificar si el proceso ya está activado y si no lo está, activarlo
if (!"running".equals(pcbOfActiveProcess.getStatus())){
dispatcher.activate(pcbOfActiveProcess, getProcessList()); // ready ---> running
}
// while running
Proceso toRun = dispatcher.getActiveProcess(processList);
if (toRun == null) return;
while(toRun != null && "running".equals(toRun.getPcb().getStatus())){
if (Thread.currentThread().isInterrupted()) return;
if (toRun.getProcessingTime() == toRun.getTimeSpent()){
dispatcher.deactivate(toRun);
readyQueue.dequeue();
break;
}
if ("I/O Bound".equals(toRun.getBound()) && toRun.getPcb().getPc()-1 == toRun.getInterruptAt()){
toRun.getPcb().setStatus("blocked");
if ("blocked".equals(toRun.getPcb().getStatus())) {
blockedQueue.enqueue(toRun.getPcb());
}
try {
Thread.sleep(toRun.getIoCicles()*1000);
accessDevice(toRun, dispatcher, blockedQueue);
}
catch(InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
try {
Thread.sleep(1000);
}
catch(InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
if (toRun.getProcessingTime() <= toRun.getTotalTimeSpent()) {
toRun.getPcb().setStatus("terminated");
terminatedProcessList.add(toRun);
} else {
if ("ready".equals(toRun.getPcb().getStatus())) {
readyQueue.enqueue(toRun.getPcb());
}
}
}
}
public void PriorityPlanification(int quantum, Cola readyQueue, Dispatcher dispatcher, Lista priorityList, Cola blockedQueue, Lista terminatedProcessList){
// Recorremos las colas de prioridad en orden (0 = mayor prioridad)
reorganicePriorityPlanification(readyQueue, priorityList);
for (int i = 0; i < priorityList.count(); i++){
// check interruption on top of loops
if (Thread.currentThread().isInterrupted()) return;
Object bucketObj = priorityList.get(i);
if (!(bucketObj instanceof Cola)) continue;
Cola act = (Cola) bucketObj;
// Si la cola de ese nivel está vacía, saltar
if (act.getCount() <= 0) continue;
// obtener el siguiente PCB de ese bucket
Object dequeued = act.dequeue();
if (!(dequeued instanceof PCB)) continue;
PCB pcbOfActiveProcess = (PCB) dequeued;
// eliminar cualquier referencia residual en la cola global de listos
readyQueue.removeValue(pcbOfActiveProcess);
if (!"running".equals(pcbOfActiveProcess.getStatus())){
dispatcher.activate(pcbOfActiveProcess, processList);
}
// mientras el proceso esté en running, controlar quantum / IO / terminación
Proceso toRun = dispatcher.getActiveProcess(processList);
if (toRun == null) continue;
while("running".equals(toRun.getPcb().getStatus())) {
if (Thread.currentThread().isInterrupted()) return;
if (toRun.getTimeSpent() >= quantum || toRun.getProcessingTime() <= toRun.getTotalTimeSpent()){
dispatcher.deactivate(toRun); // running --> ready
break;
}
if ("I/O Bound".equals(toRun.getBound()) && toRun.getPcb().getPc()-1 == toRun.getInterruptAt()){
toRun.getPcb().setStatus("blocked");
if ("blocked".equals(toRun.getPcb().getStatus())) {
blockedQueue.enqueue(toRun.getPcb());
}
try {
Thread.sleep(toRun.getIoCicles()*1000);
accessDevice(toRun, dispatcher, blockedQueue);
}
catch(InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
try {
Thread.sleep(1000);
}
catch(InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
if (toRun.getProcessingTime() <= toRun.getTotalTimeSpent()) {
toRun.getPcb().setStatus("terminated");
terminatedProcessList.add(toRun);
} else {
// reencolar en el mismo bucket y en la cola global de listos
if ("ready".equals(toRun.getPcb().getStatus())) {
act.enqueue(toRun.getPcb());
readyQueue.enqueue(toRun.getPcb());
}
}
}
}
public void Feedback(int setQuantum, Cola readyQueue, Lista readyQueueList, Dispatcher dispatcher, Cola blockedQueue, Lista terminatedProcessList) {
reorganiceFeedback(readyQueue, readyQueueList);
//System.out.println("sssssssswqdqwfqfqfqfqfq");
int quantum = setQuantum;
if (Thread.currentThread().isInterrupted()) return;
PCB processToActivate = null;
// Buscar el primer bucket no vacío (nivel de mayor prioridad)
int i = 0;
while (i < readyQueueList.count()){
if (((Cola)readyQueueList.get(i)).getCount() > 0){
System.out.println("activando");
processToActivate = (PCB)(((Cola)readyQueueList.get(i)).dequeue());
System.out.println("id del proceso" + processToActivate.getId());
dispatcher.activate(processToActivate, processList);
System.out.println("status del pta" + processToActivate.getStatus());
readyQueue.getQueue().remove(readyQueue.getQueue().indexOf(processToActivate));
break;
}
i++;
}
Proceso toRun = dispatcher.getActiveProcess(processList);
if (toRun == null) return;
if (Thread.currentThread().isInterrupted()) return;
toRun.getPcb().setTimesIn(toRun.getPcb().getTimesIn()+1);
while ("running".equals(toRun.getPcb().getStatus())) {
if (Thread.currentThread().isInterrupted()) return;
if (toRun.getTimeSpent() >= quantum || toRun.getProcessingTime() <= toRun.getTotalTimeSpent()){
dispatcher.deactivate(toRun); // running --> ready
break;
}
if ("I/O Bound".equals(toRun.getBound()) && toRun.getPcb().getPc()-1 == toRun.getInterruptAt()){
toRun.getPcb().setStatus("blocked");
if ("blocked".equals(toRun.getPcb().getStatus())) {
blockedQueue.enqueue(toRun.getPcb());
}
try {
Thread.sleep(toRun.getIoCicles()*1000);
accessDevice(toRun, dispatcher, blockedQueue);
}
catch(InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
try {
Thread.sleep(1000);
}
catch(InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
if (toRun.getProcessingTime() <= toRun.getTotalTimeSpent()) {
toRun.getPcb().setStatus("terminated");
terminatedProcessList.add(toRun);
} else {
// Demote / reenqueue according to timesIn
int timesIn = toRun.getPcb().getTimesIn();
if (timesIn < readyQueueList.count()) {
Object obj = readyQueueList.get(timesIn);
if (obj instanceof Cola) {
if ("ready".equals(toRun.getPcb().getStatus())) {
((Cola) obj).enqueue(toRun.getPcb());
readyQueue.enqueue(toRun.getPcb());
}
}
} else {
Cola aux = new Cola();
readyQueueList.add(aux);
System.out.println(timesIn);
if ("ready".equals(toRun.getPcb().getStatus())) {
((Cola) readyQueueList.get(timesIn)).enqueue(toRun.getPcb());
readyQueue.enqueue(toRun.getPcb());
}
}
}
}
public void FSS(int setQuantum, Cola readyQueue, Dispatcher dispatcher, Cola blockedQueue, Lista terminatedProcessList) {
int quantum = setQuantum;
if (readyQueue.getCount() > 0) {
var processToActivate = readyQueue.dequeue();
dispatcher.activate(((PCB)(processToActivate)), getProcessList());
Proceso toRun = dispatcher.getActiveProcess(getProcessList());
if (toRun == null) return;
while ("running".equals(toRun.getPcb().getStatus())){
if (Thread.currentThread().isInterrupted()) return;
if (toRun.getTimeSpent() >= quantum || toRun.getProcessingTime() <= toRun.getTotalTimeSpent()){
dispatcher.deactivate(toRun);
recalculateFSS(readyQueue, toRun.getPcb().getPriority());
reorganiceFSS(readyQueue);// running --> ready
}
if ("I/O Bound".equals(toRun.getBound()) && toRun.getPcb().getPc()-1 == toRun.getInterruptAt()){
toRun.getPcb().setStatus("blocked");
if ("blocked".equals(toRun.getPcb().getStatus())) {
blockedQueue.enqueue(toRun.getPcb());
}
try {
Thread.sleep(toRun.getIoCicles()*1000);
accessDevice(toRun, dispatcher, blockedQueue);
}
catch(InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
try {
Thread.sleep(1000);
}
catch(InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
if (toRun.getProcessingTime() <= toRun.getTotalTimeSpent()) {
toRun.getPcb().setStatus("terminated");
terminatedProcessList.add(toRun);
} else {
if ("ready".equals(toRun.getPcb().getStatus())) {
readyQueue.enqueue(toRun.getPcb());
}
}
}
}
public void SRT (Cola readyQueue, Dispatcher dispatcher, Cola blockedQueue, Lista terminatedProcessList) {
// Shortest Remaining Time (preemptive approximation):
// - Seleccionar el PCB con menor remaining = processingTime - totalTimeSpent
// - Activarlo y durante su ejecución comprobar si aparece otro con remaining menor => preempt.
if (readyQueue.getCount() <= 0) return;
// Buscar índice del PCB con menor remaining
int minIndex = -1;
double minRemaining = Double.MAX_VALUE;
for (int i = 0; i < readyQueue.getCount(); i++) {
Object o = readyQueue.get(i);
if (!(o instanceof PCB)) continue;
PCB pcb = (PCB) o;
Proceso p = findProcessByPCB(pcb);
if (p == null) continue;
double remaining = p.getProcessingTime() - p.getTotalTimeSpent();
if (remaining < minRemaining) {
minRemaining = remaining;
minIndex = i;
}
}
if (minIndex == -1) return;
// Extraer el PCB elegido
Object chosen = readyQueue.get(minIndex);
if (!(chosen instanceof PCB)) return;
PCB chosenPCB = (PCB) chosen;
// Quitarlo de la cola global de ready
readyQueue.removeValue(chosenPCB);
// Activar
dispatcher.activate(chosenPCB, getProcessList());
Proceso toRun = dispatcher.getActiveProcess(getProcessList());
if (toRun == null) return;
while ("running".equals(toRun.getPcb().getStatus())) {
if (Thread.currentThread().isInterrupted()) return;
// cond de terminación
if (toRun.getProcessingTime() <= toRun.getTotalTimeSpent()){
dispatcher.deactivate(toRun);
break;
}
// comprobar si existe preempción: algún PCB en ready con remaining < current remaining
double currentRemaining = toRun.getProcessingTime() - toRun.getTotalTimeSpent();
boolean shouldPreempt = false;
for (int i = 0; i < readyQueue.getCount(); i++) {
Object o = readyQueue.get(i);
if (!(o instanceof PCB)) continue;
PCB pcb = (PCB) o;
Proceso p = findProcessByPCB(pcb);
if (p == null) continue;
double rem = p.getProcessingTime() - p.getTotalTimeSpent();
if (rem < currentRemaining) {
shouldPreempt = true;
break;
}
}
if (shouldPreempt) {
// Preemptar y reencolar el proceso actual (su remaining ya actualizado por run())
dispatcher.deactivate(toRun);
if ("ready".equals(toRun.getPcb().getStatus())) {
readyQueue.enqueue(toRun.getPcb());
}
break;
}
// I/O handling
if ("I/O Bound".equals(toRun.getBound()) && toRun.getPcb().getPc()-1 == toRun.getInterruptAt()){
toRun.getPcb().setStatus("blocked");
if ("blocked".equals(toRun.getPcb().getStatus())) {
blockedQueue.enqueue(toRun.getPcb());
}
try {
Thread.sleep(toRun.getIoCicles()*1000);
accessDevice(toRun, dispatcher, blockedQueue);
}
catch(InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
// Tras salir, si termina, marcar; si no, asegurar que esté en ready
if (toRun.getProcessingTime() <= toRun.getTotalTimeSpent()) {
toRun.getPcb().setStatus("terminated");
terminatedProcessList.add(toRun);
} else {
// si no se reencoló ya, encolar
if ("ready".equals(toRun.getPcb().getStatus()) && !readyQueue.getContains(toRun.getPcb())) {
readyQueue.enqueue(toRun.getPcb());
}
}
}
//La lista de prioridades es una lista que contiene las prioridades
public Lista reorganicePriorityPlanification(Cola readyQueue, Lista priorityList){
// Creando la cantidad de colas necesarias segun las prioridades existentes
if (priorityList.count() < 1){
int maxpriority = getPriorities(readyQueue);
for (int i = 0; i < maxpriority; i++){
Cola aux = new Cola();
priorityList.add(aux);
}
}
// Agregar cada proceso a su lista de prioridad correspondiente
for (int j=0; j < readyQueue.getCount(); j++){
Object obj = readyQueue.get(j);
if (!(obj instanceof PCB)) continue;
PCB aux = (PCB)obj;
for (int x=0; x < priorityList.count(); x++){
Cola act = (Cola)priorityList.get(x);
// usar == para prioridad numérica está bien, pero evitar & bitwise
if(aux.getPriority() == x && !act.getQueue().contains(aux)){
act.enqueue(aux);
}
}
}
return priorityList;
}
public void reorganiceSPN(Cola readyQueue){
if (readyQueue == null) return;
if (readyQueue.getQueue() == null) return;
if (readyQueue.getCount() < 2) return;
synchronized (readyQueue.getQueue()) {
for (int pass = 0; pass < readyQueue.getCount() - 1; pass++) {
boolean swapped = false;
for (int i = 0; i < readyQueue.getCount() - 1 - pass; i++) {
Object o1 = readyQueue.getQueue().get(i);
Object o2 = readyQueue.getQueue().get(i + 1);
if (!(o1 instanceof PCB) || !(o2 instanceof PCB)) {
continue;
}
PCB pcb1 = (PCB) o1;
PCB pcb2 = (PCB) o2;
Proceso p1 = findProcessByPCB(pcb1);
Proceso p2 = findProcessByPCB(pcb2);
// If either Proceso is missing, skip this pair
if (p1 == null || p2 == null) continue;
// Compare processing time and swap underlying list if out of order
if (p1.getProcessingTime() > p2.getProcessingTime()) {
readyQueue.getQueue().swap(i, i + 1);
swapped = true;
}
}
if (!swapped) break; // already sorted
}
}
}
public void reorganiceFeedback (Cola readyQueue, Lista readyQueueList) {
if (readyQueueList.count() < getTimesIn(readyQueue)){
int maxTimesIn = getTimesIn(readyQueue);
int i = readyQueueList.count();
while (i < maxTimesIn) {
Cola aux = new Cola();
readyQueueList.add(aux);
i++;
}
}
int i = 0;
while (i < readyQueue.getCount()) {
int j = 0;
int index = ((PCB)readyQueue.get(i)).getTimesIn();
while (j < readyQueueList.count()){
if ( index == j && !(((Cola)readyQueueList.get(j)).getContains(readyQueue.get(i)))) {
((Cola)readyQueueList.get(j)).enqueue(readyQueue.get(i));
}
j++;
}
i++;
}
}
public void reorganiceFSS (Cola readyQueue){
int i = 0;
int n = readyQueue.getCount();
while (i < n){
PCB aux = (PCB)readyQueue.get(i);
int j = i + 1;
while (j < n){
PCB aux2 = (PCB)readyQueue.get(j);
if (aux.getPriorityFSS()>aux2.getPriorityFSS()){
readyQueue.getQueue().swap(i, j);
}
j++;
}
i++;
}
}
public void recalculateFSS (Cola readyQueue, int priority) {
int i = 0;
int n = readyQueue.getCount();
int timesInTotal = 0;
int priorityCount = 0;
while (i < n) {
if (((PCB)readyQueue.get(i)).getPriority() == priority) {
timesInTotal = ((PCB)readyQueue.get(i)).getTimesIn() + timesInTotal;
priorityCount++;
}
i++;
}
i = 0;
while (i < n) {
if (((PCB)readyQueue.get(i)).getPriority() == priority) {
int priorityNode = ((PCB)readyQueue.get(i)).getPriority();
int timesIn = ((PCB)readyQueue.get(i)).getTimesIn();
float newPriorityFSS = priorityNode + timesIn + (timesInTotal/priorityCount);
((PCB)readyQueue.get(i)).setPriorityFSS(newPriorityFSS);
}
i++;
}
}
public void reorganiceSRT (Cola readyQueue){
int i = 0;
int n = readyQueue.getCount();
while (i < n){
PCB aux = (PCB) readyQueue.get(i);
int j = i + 1;
while (j < n){
PCB aux2 = (PCB)readyQueue.get(j);
if (aux.getPriorityFSS() > aux2.getPriorityFSS()){
readyQueue.getQueue().swap(i, j);
}
j++;
}
i++;
}
}
public int getPriorities(Cola readyQueue){
Lista priorities = new Lista();
for (int i = 0; i < readyQueue.getCount(); i++){
PCB aux = (PCB)readyQueue.get(i);
if (!priorities.contains(aux.getPriority())){
priorities.add(aux.getPriority());
}
}
return priorities.count();
}
public int getTimesIn(Cola readyQueue){
Lista timesIn = new Lista();
for (int i = 0; i < readyQueue.getCount(); i++){
var aux = (PCB)readyQueue.get(i);
if (!timesIn.contains(aux.getTimesIn())){
timesIn.add(aux.getTimesIn());
}
}
return timesIn.count();
}
/**
* @return the processList
*/
public Lista getProcessList() {
return processList;
}
/**
* @param processList the processList to set
*/
public void setProcessList(Lista processList) {
this.processList = processList;
}
/**
* @return the memoryAvaiable
*/
public int getMemoryAvaiable() {
return memoryAvaiable;
}
/**
* @param memoryAvaiable the memoryAvaiable to set
*/
public void setMemoryAvaiable(int memoryAvaiable) {
this.memoryAvaiable = memoryAvaiable;
}
/**
* @return the remainingSpace
*/
public int getRemainingSpace() {
return remainingSpace;
}
/**
* @param remainingSpace the remainingSpace to set
*/
public void setRemainingSpace(int remainingSpace) {
this.remainingSpace = remainingSpace;
}
public void accessDevice(Proceso blockedProcess, Dispatcher dispatcher, Cola blockedQueue){
if (blockedProcess == null) {
System.err.println("accessDevice: blockedProcess is null");
return;
}
int i = 0;
Device aux = null;
while (i < deviceTable.count()){
if (Thread.currentThread().isInterrupted()) return;
Object o = deviceTable.get(i);
if (!(o instanceof Device)) { i++; continue; }
Device dev = (Device) o;
if (blockedProcess.getDeviceToUse() == dev.getId()){
aux = dev;
try {
System.out.println("accediendo al device id=" + dev.getId());
aux.getSemaf().acquire();
} catch(InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
break;
}
i++;
}
if (aux == null) {
// No matching device found — log and return safely
System.err.println("accessDevice: no device found with id " + blockedProcess.getDeviceToUse());
return;
}
try {
// Use Thread.sleep (static) — same effect as blockedProcess.sleep(...)
Thread.sleep((long) blockedProcess.getSatisfyCicles() * 1000L);
aux.getSemaf().release();
// deactivate thread (safe null-check)
if (dispatcher != null) {
// this will set the PCB status to "ready"
dispatcher.deactivate(blockedProcess);
}
// Remove the PCB from blockedQueue using the public API (safer)
if (blockedQueue != null) {
blockedQueue.removeValue(blockedProcess.getPcb());
}
}
catch(InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
public Proceso findProcessByPCB(PCB pcb) {
if (pcb == null) return null;
int n = (processList == null) ? 0 : processList.count();
for (int i = 0; i < n; i++) {
Object o = processList.get(i);
if (o instanceof Proceso) {
Proceso p = (Proceso) o;
PCB ppcb = p.getPcb();
if (ppcb != null && ppcb.getId() == pcb.getId()) {
return p;
}
}
}
return null;
}
}