-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSIMSOTXT.C
More file actions
1135 lines (1030 loc) · 33.7 KB
/
Copy pathSIMSOTXT.C
File metadata and controls
1135 lines (1030 loc) · 33.7 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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* OPERATING SYSTEM SIMULATION (DOS text-mode port, no BGI / no mouse)
* Original DOS/Turbo-C + BGI + mouse version by Eduard Garcia and Lloren
* Llado. This build drops LIBREGRA (BGI graphics) and RATON (mouse)
* entirely, after they proved unreliable under this DOSBox setup -
* missing/invisible text, and a mouse driver that never reported
* correctly through RATON's own functions (confirmed via direct INT 33h
* testing - the interrupt itself works, the library's use of it didn't).
*
* The scheduling logic (round-robin CPU, Banker's-style deadlock
* avoidance, memory allocation, I/O queue) is IDENTICAL to the fully
* hardened reference build (same bounds fixes, same safe_rr(), same
* uninitialized-variable fixes). Only the presentation/input layer is
* new: plain DOS text mode via <conio.h> - gotoxy/textcolor/cprintf for
* output, kbhit/getch for input. No mouse, no BGI, no external .LIB
* files, no EGAVGA.OBJ, no memory-model constraint.
*
* Build: BCC -mm SIMSOTXT.C (or just BCC SIMSOTXT.C - no BGI/
* mouse libs needed, model doesn't
* matter without them)
* Run : SIMSOTXT
*/
#include <conio.h>
#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/* ------------------------------------------------------------------ */
/* Process Control Block (unchanged) */
/* ------------------------------------------------------------------ */
struct P_C_B
{
char nombre[2];
int tiempoejecucion;
int estadoactual;
int prioridad;
int maximos[3];
int asignados[3];
int liberados[3];
int solicitados[3];
int es;
int tiempoes[5]; /* was [4]: es can reach 4 and the I/O shift reads
* tiempoes[i+1] up to index 4 */
int espera_es;
int memoria;
};
/* ------------------------------------------------------------------ */
/* Prototypes */
/* ------------------------------------------------------------------ */
void nombre_procesos(void);
void escojer_cinco(void);
void compactar(int);
void compactar_cola_preparados(void);
void creacion_pcb(int, char);
int evitacion(int);
int solicitud(void);
void round_robin(int);
void resta_es(void);
int comprobar_final(void);
void compactar_cola_es(void);
void compactar_recursos(void);
int comprobar_entrades(void);
int recursos(void);
int error_sistema(void);
void imprimir_colas(void);
void imprimir_cpu(int);
static void init_ui(void);
static void cleanup_exit(int code);
static int prompt_int(const char *label, int lo, int hi);
static void show_message_wait(const char *m);
static void render_frame(int pause);
static int safe_rr(void);
static void draw_box(int y, int x, int h, int w, const char *title);
/* ------------------------------------------------------------------ */
/* Global arrays / state (unchanged) */
/* ------------------------------------------------------------------ */
char cola_espera[12] = {'A','B','C','D','E','F','G','H','I','J','K','L'};
char cola_es[5] = {0};
char cola_recursos[5] = {'z','z','z','z','z'};
struct P_C_B PCB[12];
char cola_preparados[5]= {0};
char memoria[64] = {0};
int A = 20;
int B = 30;
int C = 25;
int quantum;
int auxes;
int numero;
int nou_proces = 3;
/* index of the PCB currently "in the CPU", for the dashboard */
static int cpu_proc = -1;
/* how long each animated frame lingers, in ms */
#define STEP_DELAY_MS 110
/* ================================================================== */
/* PRESENTATION LAYER (plain DOS text mode - no BGI, no mouse) */
/* ================================================================== */
/*
* color_of_letter - map a process letter 'A'..'L' to a distinct text
* color (1..14, skipping 0=black), so each process keeps one color
* in the memory bar. Unknown letters fall back to LIGHTGRAY.
*/
static int color_of_letter(char c)
{
switch (c) {
case 'A': return 1; case 'B': return 2; case 'C': return 3;
case 'D': return 4; case 'E': return 5; case 'F': return 6;
case 'G': return 9; case 'H': return 10; case 'I': return 11;
case 'J': return 12; case 'K': return 13; case 'L': return 14;
default: return 7;
}
}
/*
* init_ui - switch to 80x25 color text mode and clear the screen.
* Guaranteed to work under any DOSBox setup - no driver, no library,
* just the standard PC text video mode.
*/
static void init_ui(void)
{
textmode(C80);
_setcursortype(_NOCURSOR);
textbackground(BLACK);
clrscr();
}
/*
* cleanup_exit - restore a plain cursor/attribute and exit. Used at
* every exit point so the screen is left in a sane state.
*/
static void cleanup_exit(int code)
{
_setcursortype(_NORMALCURSOR);
textattr(7);
clrscr();
exit(code);
}
/*
* draw_box - draw a titled rectangle using simple ASCII characters
* (+, -, |) at (y,x) with height h and width w, 0-based coordinates
* (row 0..24, col 0..79). Plain ASCII avoids any code-page/font
* surprises across different DOSBox/terminal setups.
*/
static void draw_box(int y, int x, int h, int w, const char *title)
{
int i;
textcolor(LIGHTGRAY);
gotoxy(x + 1, y + 1); putch('+');
for (i = 0; i < w - 2; i++) putch('-');
putch('+');
gotoxy(x + 1, y + h); putch('+');
for (i = 0; i < w - 2; i++) putch('-');
putch('+');
for (i = 1; i < h - 1; i++) {
gotoxy(x + 1, y + 1 + i); putch('|');
gotoxy(x + w, y + 1 + i); putch('|');
}
if (title) {
textcolor(WHITE + BLINK * 0);
gotoxy(x + 3, y + 1);
cprintf(" %s ", title);
}
}
/*
* prompt_int - keyboard prompt that reads an integer in [lo,hi],
* re-asking on invalid input. Plain scanf-style text input.
*/
static int prompt_int(const char *label, int lo, int hi)
{
int v;
char buf[32];
for (;;) {
clrscr();
textcolor(LIGHTCYAN + BLINK * 0);
gotoxy(3, 2); cprintf("OPERATING SYSTEM SIMULATION");
textcolor(LIGHTGRAY);
gotoxy(3, 5); cprintf("%s", label);
gotoxy(3, 6); cprintf("Range [%d - %d]: ", lo, hi);
if (scanf("%31s", buf) != 1) { fflush(stdin); continue; }
v = atoi(buf);
if (v >= lo && v <= hi) break;
gotoxy(3, 8); cprintf("Invalid value, press a key...");
getch();
}
return v;
}
/*
* show_message_wait - print a status line at the bottom and block
* until a key is pressed. Drives the two error screens and the
* end-of-run message.
*/
static void show_message_wait(const char *m)
{
textcolor(YELLOW + BLINK * 0);
gotoxy(1, 24);
cprintf("%.78s", m);
getch();
}
/*
* render_frame - redraw the whole dashboard from global state: memory
* bar, the four queue boxes, A/B/C resource counters, and the
* running-process PCB panel. When pause!=0 the frame lingers
* ~STEP_DELAY_MS and 'q' aborts cleanly.
*/
static void render_frame(int pause)
{
int i;
char cell;
clrscr();
/* ---- title ---- */
textcolor(LIGHTCYAN + BLINK * 0);
gotoxy(3, 1); cprintf("OPERATING SYSTEM SIMULATION");
textcolor(CYAN);
gotoxy(3, 2); cprintf("Eduard Garcia and Lloren Llado");
/* ---- memory bar (64 units) ---- */
textcolor(LIGHTGRAY);
gotoxy(1, 4); cprintf("Memory:");
for (i = 0; i < 64; i++) {
cell = memoria[i];
gotoxy(11 + i, 4);
if (cell >= 'A' && cell <= 'L') {
textcolor(color_of_letter(cell) + BLINK * 0);
putch(cell);
} else {
textcolor(DARKGRAY);
putch('.');
}
}
/* ---- available resources ---- */
textcolor(LIGHTGRAY);
gotoxy(1, 5);
cprintf("Available resources A:%-4d B:%-4d C:%-4d", A, B, C);
/* ---- Waiting queue ---- */
draw_box(6, 0, 3, 38, "Waiting queue");
{
int col = 3;
for (i = 0; i < 12; i++) {
char c = cola_espera[i];
gotoxy(col, 8);
if (c >= 'A' && c <= 'L') {
textcolor(color_of_letter(c) + BLINK * 0);
putch(c);
} else {
textcolor(DARKGRAY);
putch('.');
}
col += 2;
}
}
/* ---- Ready ---- */
draw_box(6, 40, 3, 38, "Ready");
{
int col = 43;
for (i = 0; i < 5; i++) {
char c = cola_preparados[i];
gotoxy(col, 8);
if (c >= 'A' && c <= 'L') {
textcolor(LIGHTBLUE + BLINK * 0);
cprintf("P%c", c);
} else {
textcolor(DARKGRAY);
cprintf("..");
}
col += 6;
}
}
/* ---- I/O queue ---- */
draw_box(10, 0, 3, 38, "I/O queue");
{
int col = 3;
for (i = 0; i < 5; i++) {
char c = cola_es[i];
gotoxy(col, 12);
if (c >= 'A' && c <= 'L') {
textcolor(color_of_letter(c) + BLINK * 0);
putch(c);
} else {
textcolor(DARKGRAY);
putch('.');
}
col += 3;
}
}
/* ---- Resources (resource wait queue) ---- */
draw_box(10, 40, 3, 38, "Resources");
{
int col = 43;
for (i = 0; i < 5; i++) {
char c = cola_recursos[i];
gotoxy(col, 12);
if (c >= 'A' && c <= 'L') {
textcolor(color_of_letter(c) + BLINK * 0);
putch(c);
} else {
textcolor(DARKGRAY);
putch('.');
}
col += 3;
}
}
/* ---- Running process PCB (CPU) ---- */
draw_box(14, 0, 8, 78, "Running PCB (CPU)");
textcolor(LIGHTGRAY);
if (cpu_proc >= 0 && cpu_proc < 12) {
struct P_C_B *p = &PCB[cpu_proc];
gotoxy(3, 16);
cprintf("Process: %c%c State: %d Priority: %d",
p->nombre[0], p->nombre[1], p->estadoactual, p->prioridad);
gotoxy(3, 17);
cprintf("Remaining execution time: %d", p->tiempoejecucion);
gotoxy(3, 18);
cprintf("Maximum resources: A:%-3d B:%-3d C:%-3d",
p->maximos[0], p->maximos[1], p->maximos[2]);
gotoxy(3, 19);
cprintf("Assigned resources: A:%-3d B:%-3d C:%-3d",
p->asignados[0], p->asignados[1], p->asignados[2]);
if (p->es != -1) {
gotoxy(3, 20);
cprintf("I/O count: %d", p->es);
gotoxy(3, 21);
cprintf("I/O times: %d %d %d %d",
p->tiempoes[0], p->tiempoes[1],
p->tiempoes[2], p->tiempoes[3]);
} else {
gotoxy(3, 20); cprintf("I/O count: 0");
gotoxy(3, 21); cprintf("I/O times: -");
}
gotoxy(53, 16); cprintf("Memory: %d u.", p->memoria);
} else {
textcolor(DARKGRAY);
gotoxy(3, 18); cprintf("(CPU idle)");
}
textcolor(LIGHTGRAY);
gotoxy(1, 23); cprintf("'q' = abort simulation");
if (pause) delay(STEP_DELAY_MS);
/* non-blocking abort check */
if (kbhit()) {
int ch = getch();
if (ch == 'q' || ch == 'Q') cleanup_exit(0);
}
}
/*
* safe_rr - the original's "rand() % rand()" distribution, guarded
* against a zero divisor.
*/
static int safe_rr(void)
{
int d = rand();
return d ? (rand() % d) : 0;
}
/* ================================================================== */
/* MAIN */
/* ================================================================== */
int main(void)
{
int error, z, fi = 1, entrades = 5, cert = 1;
int syserror = 0;
int modo_todos = 0; /* true = "run until all executed" (mode 2).
* numero==0 means two different things depending
* on mode, so the early-exit check below must not
* fire for mode 2. */
char point = 'y';
srand((unsigned)time(NULL));
init_ui();
/* ---- data-entry dialog (keyboard only - no mouse) ---- */
quantum = prompt_int("Enter the QUANTUM:", 1, 50);
auxes = prompt_int("Enter the I/O TIME:", 1, 50);
for (;;) {
clrscr();
textcolor(LIGHTCYAN + BLINK * 0);
gotoxy(3, 2); cprintf("OPERATING SYSTEM SIMULATION");
textcolor(LIGHTGRAY);
gotoxy(3, 5); cprintf("Termination mode:");
gotoxy(5, 7); cprintf("1) Finish after N processes");
gotoxy(5, 8); cprintf("2) When all have been executed");
gotoxy(3, 10); cprintf("Choose (1/2): ");
{
int c = getch();
if (c == '1') {
numero = prompt_int("Number of processes to finish:", 1, 12);
break;
}
if (c == '2') {
numero = 0;
modo_todos = 1;
break;
}
}
}
/* ---- simulation setup (unchanged) ---- */
nombre_procesos();
escojer_cinco();
compactar(5);
imprimir_colas();
creacion_pcb(5, point);
while (fi != 0) {
if (entrades > 0) {
do {
if (cola_preparados[4] != 'z') {
z = 0;
z = solicitud();
if (z == 12) {
show_message_wait(
"Error N.1: access to an invalid or uncreated PCB. "
"Press a key.");
cleanup_exit(1);
}
cert = evitacion(z);
} else {
z = recursos();
cert = 1;
}
} while (cert == 0);
do {
if (z <= 11) {
round_robin(z);
if (!modo_todos && numero == 0) {
cleanup_exit(0);
}
z = recursos();
}
} while (z != 12);
} else {
resta_es();
}
fi = comprobar_final();
entrades = comprobar_entrades();
error = error_sistema();
if (error == 1) {
syserror = 1;
break;
}
}
if (syserror)
show_message_wait("Error N.2: the system does not have enough "
"resources. Press a key.");
else
show_message_wait("Simulation finished. Press a key to exit.");
cleanup_exit(0);
return 0;
}
/* SIMULATION CORE (logic identical to the original) */
/* ================================================================== */
/*
* nombre_procesos - initialise all 12 PCBs with letters A..L, state 1
* (declared, not yet loaded), tag nombre[1]='X', and prime each I/O
* wait counter with the user's I/O time.
*/
void nombre_procesos(void)
{
int i;
for (i = 0; i < 12; i++) {
switch (i) {
case 0: PCB[i].nombre[0]='A'; break;
case 1: PCB[i].nombre[0]='B'; break;
case 2: PCB[i].nombre[0]='C'; break;
case 3: PCB[i].nombre[0]='D'; break;
case 4: PCB[i].nombre[0]='E'; break;
case 5: PCB[i].nombre[0]='F'; break;
case 6: PCB[i].nombre[0]='G'; break;
case 7: PCB[i].nombre[0]='H'; break;
case 8: PCB[i].nombre[0]='I'; break;
case 9: PCB[i].nombre[0]='J'; break;
case 10: PCB[i].nombre[0]='K'; break;
case 11: PCB[i].nombre[0]='L'; break;
}
PCB[i].nombre[1] = 'X';
PCB[i].estadoactual = 1;
PCB[i].espera_es = auxes;
}
}
/*
* escojer_cinco - randomly pick the five distinct process letters for the
* initial ready queue (cola_preparados), re-rolling until no duplicates.
*/
void escojer_cinco(void)
{
int i = 0, valor = 0, j = 0, aux;
for (i = 0; i < 5; i++)
cola_preparados[i] = 65 + (safe_rr() % 12);
do {
aux = 0;
for (i = 0; i < 5; i++) {
valor = cola_preparados[i];
for (j = 0; j < 5; j++) {
if ((valor == cola_preparados[j]) && (i != j)) {
cola_preparados[j] = 65 + (safe_rr() % 12);
aux = 1;
}
}
}
} while (aux != 0);
}
/*
* compactar - keep the waiting queue (cola_espera) dense.
* n>1: remove the n admitted processes from the waiting queue and squeeze
* out the gaps. n==1: shift the queue down by one (used when a single new
* process is admitted later). The param shadows the global name; harmless.
*/
void compactar(int compactar)
{
int i, j;
char valor;
if (compactar > 1) {
for (i = 0; i < compactar; i++) {
valor = cola_preparados[i];
for (j = 0; j < 12; j++) {
if (valor == cola_espera[j])
cola_espera[j] = 0;
}
}
i = 0;
while (i < 12) {
j = 11;
if (cola_espera[i] == 0) {
while ((cola_espera[j] == 0) && (i < j)) j--;
if (i < j) {
valor = cola_espera[j];
cola_espera[j] = cola_espera[i];
cola_espera[i] = valor;
} else {
i = 12;
}
}
i++;
}
} else {
for (i = 0; i < 12; i++) {
cola_espera[i] = cola_espera[i + 1];
if (cola_espera[i] == 0) break;
}
}
}
/*
* imprimir_colas - render one animated dashboard frame (with the linger +
* 'q'-to-abort behaviour). Kept as its own name because the core
* calls it in dozens of places after every state change.
*/
void imprimir_colas(void)
{
render_frame(1);
}
/*
* creacion_pcb - turn ready-queue letters into fully populated processes.
* Allocates memory (bailing the process back to the waiting queue if there
* isn't enough), then randomises priority, resource maxima, execution
* time and sorted I/O burst times, renames nombre[0] to 'P', and sets
* state 2 (ready). Handles both the initial batch (procesos>1, driven by
* cola_preparados) and later single admissions (procesos==1, via apunta).
* Hardened vs the original: free-cell counters reset each pass and the
* burst array is fully initialised - no read past memoria[63]/tiempoes[4].
*/
void creacion_pcb(int procesos, char apunta)
{
int j, i, k = 0, p = 0, valor, memo = 0;
char aux;
while (p < procesos) {
i = 0;
if (procesos > 1) {
while (cola_preparados[p] != PCB[i].nombre[0]) i++;
} else {
while (PCB[i].nombre[0] != apunta) i++;
}
do {
PCB[i].memoria = safe_rr() % 32;
} while (PCB[i].memoria == 0);
k = 0; memo = 0; /* reset added: do-while would read memoria[64]
* when a prior iteration left k==64 */
do {
if (memoria[k] == 0) memo++;
k++;
} while (k < 64);
k = 0;
if (memo >= PCB[i].memoria) {
memo = PCB[i].memoria;
while ((k < 64) && (memo != 0)) { /* was k<=64: writes memoria[64] */
if (memoria[k] == 0) { memoria[k] = PCB[i].nombre[0]; memo--; }
k++;
}
PCB[i].estadoactual = 2;
PCB[i].asignados[0] = 0;
PCB[i].asignados[1] = 0;
PCB[i].asignados[2] = 0;
aux = PCB[i].nombre[0];
PCB[i].nombre[0] = 'P';
PCB[i].nombre[1] = aux;
do {
PCB[i].tiempoejecucion = safe_rr() % 100;
} while (PCB[i].tiempoejecucion <= 5);
PCB[i].prioridad = rand() % 255;
for (j = 0; j < 3; j++) {
switch (j) {
case 0: PCB[i].maximos[j] = rand() % 10; break;
case 1: PCB[i].maximos[j] = rand() % 15; break;
case 2: PCB[i].maximos[j] = rand() % 12; break;
}
}
PCB[i].es = rand() % 5;
for (j = 0; j < 5; j++) PCB[i].tiempoes[j] = -1; /* clear bursts */
if (PCB[i].es > 0) {
for (j = 0; j < PCB[i].es; j++)
PCB[i].tiempoes[j] = rand() % PCB[i].tiempoejecucion;
for (j = 0; j < PCB[i].es; j++) {
k = 0;
valor = PCB[i].tiempoes[j];
do {
if ((valor == PCB[i].tiempoes[k]) && (j != k))
PCB[i].tiempoes[k] = rand() % PCB[i].tiempoejecucion;
else
k++;
} while (k < PCB[i].es);
}
for (j = 0; j < PCB[i].es; j++) {
for (k = 0; k < PCB[i].es; k++) {
if ((PCB[i].tiempoes[j] < PCB[i].tiempoes[k]) && (j != k)) {
valor = PCB[i].tiempoes[j];
PCB[i].tiempoes[j] = PCB[i].tiempoes[k];
PCB[i].tiempoes[k] = valor;
}
}
}
} else {
PCB[i].es = -1;
for (j = 0; j < 3; j++)
PCB[i].tiempoes[j] = -1;
}
} else {
k = 0;
while ((cola_espera[k] != 0) && (k < 12)) k++;
if (procesos > 1) {
cola_espera[k] = PCB[i].nombre[0];
cola_preparados[p] = 'z';
} else {
cola_espera[k] = apunta;
j = 0;
while (j < 5) {
if (cola_preparados[j] == apunta)
cola_preparados[j] = 'z';
j++;
}
}
}
p++;
}
i = 0;
for (j = 0; j <= 4; j++) {
if (cola_preparados[j] == 'z') {
aux = cola_preparados[i];
cola_preparados[i] = cola_preparados[j];
cola_preparados[j] = aux;
i++;
}
}
}
/*
* imprimir_cpu - mark PCB[proces] as the process in the CPU and redraw one
* frame (no linger) so the PCB panel shows its live fields.
*/
void imprimir_cpu(int proces)
{
cpu_proc = proces;
render_frame(0);
}
/*
* qua / es / procesos - legacy prompt wrappers (quantum / I/O time / process
* count). The main dialog now inlines prompt_int(); these remain only for
* source compatibility with the original.
*/
int qua(void)
{
return prompt_int("Enter the QUANTUM:", 1, 50);
}
int es(void)
{
return prompt_int("Enter the I/O TIME:", 1, 50);
}
int procesos(void)
{
return prompt_int("Number of processes to finish:", 1, 12);
}
/*
* solicitud - request step for the process at the head of the ready queue
* (cola_preparados[4]). Randomly releases some held resources back to the
* A/B/C pool, then rolls a fresh request (solicitados[]) bounded by each
* maximum. Returns the PCB index, or 12 if the head can't be found.
* Search is bounded so it never dereferences PCB[12].
*/
int solicitud(void)
{
int j = 0, selec = 0, x, y, w;
/* bound must be first operand: && short-circuits left-to-right, so the
* original (deref && selec<=12) read PCB[12] before the range check. */
while ((selec < 12) && (cola_preparados[4] != PCB[selec].nombre[1])) selec++;
if (selec < 12) {
for (j = 0; j < 3; j++) {
if (PCB[selec].asignados[j] > 0) {
switch (j) {
case 0: x = rand() % PCB[selec].asignados[j];
A += x; PCB[selec].asignados[j] -= x; break;
case 1: y = rand() % PCB[selec].asignados[j];
B += y; PCB[selec].asignados[j] -= y; break;
case 2: w = rand() % PCB[selec].asignados[j];
C += w; PCB[selec].asignados[j] -= w; break;
}
}
}
for (j = 0; j < 3; j++) {
if (PCB[selec].maximos[j] > 0)
PCB[selec].solicitados[j] = rand() % PCB[selec].maximos[j];
else
PCB[selec].solicitados[j] = 0;
}
} else {
selec = 12;
}
return selec;
}
/*
* evitacion - Banker's-style deadlock avoidance for PCB[selec]'s request.
* If it fits within the available A/B/C, resources are deducted and
* assigned and it returns 1 (granted). Otherwise it rolls the state back,
* moves the process to the resource-wait queue (state 5), drops it from the
* ready queue, and returns 0 (blocked).
*/
int evitacion(int selec)
{
int k = 0, j, disponible = 0, da = 0, db = 0, dc = 0, valor = 1;
da = A; db = B; dc = C;
for (j = 0; j < 3; j++) {
switch (j) {
case 0: disponible = A; break;
case 1: disponible = B; break;
case 2: disponible = C; break;
}
if (PCB[selec].solicitados[j] <= PCB[selec].maximos[j]) {
if (PCB[selec].solicitados[j] <= disponible) {
switch (j) {
case 0: A = disponible - PCB[selec].solicitados[j]; break;
case 1: B = disponible - PCB[selec].solicitados[j]; break;
case 2: C = disponible - PCB[selec].solicitados[j]; break;
}
PCB[selec].asignados[j] += PCB[selec].solicitados[j];
PCB[selec].maximos[j] -= PCB[selec].solicitados[j];
valor = 1;
} else {
A = da; B = db; C = dc;
for (k = j - 1; k >= 0; k--) {
PCB[selec].asignados[j] -= PCB[selec].solicitados[j];
PCB[selec].maximos[j] += PCB[selec].solicitados[j];
}
k = 0;
while (cola_recursos[k] != 'z') k++;
cola_recursos[k] = PCB[selec].nombre[1];
PCB[selec].estadoactual = 5;
cola_preparados[4] = 'z';
compactar_cola_preparados();
valor = 0; j = 3;
}
}
}
return valor;
}
/*
* round_robin - run PCB[selec] for up to one quantum. Rotates it to the head
* of the ready queue, then each cycle ticks down its execution time and
* every I/O countdown. On exit it dispatches by outcome: move to the I/O
* queue (a burst hit 0), retire it and free its resources and memory when
* execution finishes (decrementing numero), or re-queue it if the quantum
* expired mid-run. Every few rounds (the nou_proces throttle) it also
* admits one process from the waiting queue.
*/
void round_robin(int selec)
{
int i, k = 0, aux = 0, c = 4, entra = 0;
char proceso;
if (PCB[selec].nombre[1] == cola_preparados[4]) {
cola_preparados[4] = 0;
} else {
PCB[selec].estadoactual = 2;
for (i = 4; i != 0; i--) {
if (cola_preparados[i] == 'z') {
cola_preparados[i] = cola_preparados[4];
break;
}
}
cola_preparados[4] = PCB[selec].nombre[1];
compactar_recursos();
}
imprimir_cpu(selec);
i = quantum;
while ((i > 0) && (PCB[selec].tiempoejecucion > 0) && (PCB[selec].tiempoes[0] != 0)) {
i--;
PCB[selec].tiempoejecucion--;
if (PCB[selec].es > 0) {
for (aux = 0; aux < PCB[selec].es; aux++)
PCB[selec].tiempoes[aux]--;
} else {
PCB[selec].es = -1;
}
resta_es();
}
compactar_cola_preparados();
nou_proces--;
if (nou_proces <= 0) {
for (c = 0; c < 12; c++)
if (PCB[c].estadoactual > 1) entra++;
c = 4;
if ((cola_espera[0] != 0) && (entra < 5)) {
while (cola_preparados[c] != 'z') c--;
if (c < 4) {
cola_preparados[c] = cola_espera[0];
proceso = cola_espera[0];
creacion_pcb(1, proceso);
compactar(1);
}
}
nou_proces = 3;
}
if ((PCB[selec].tiempoes[0] == 0) && (PCB[selec].es > 0)) {
for (c = 0; c < 64; c++) /* was c<65: reads/writes memoria[64] */
if (memoria[c] == PCB[selec].nombre[1]) memoria[c] = 0;
while ((cola_es[k] != 0) && (k != 4)) k++;
if ((cola_es[k] == 0) && (k < 5)) {
PCB[selec].estadoactual = 3;
cola_es[k] = PCB[selec].nombre[1];
}
} else {
if (PCB[selec].tiempoejecucion <= 0) {
numero--;
A += PCB[selec].asignados[0];
B += PCB[selec].asignados[1];
C += PCB[selec].asignados[2];
PCB[selec].estadoactual = 0;
for (c = 0; c < 64; c++) /* was c<65: reads/writes memoria[64] */
if (memoria[c] == PCB[selec].nombre[1]) memoria[c] = 0;
for (c = 0; c < 12; c++)
if (PCB[c].estadoactual > 1) entra++;
c = 4;
if ((cola_espera[0] != 0) && (entra < 5)) {
while (cola_preparados[c] != 'z') c--;
if (c < 4) {
cola_preparados[c] = cola_espera[0];
proceso = cola_espera[0];
creacion_pcb(1, proceso);
compactar(1);
}
}
} else {
if (i <= 0) {
aux = 4;
while (cola_preparados[aux] != 'z') aux--;
cola_preparados[aux] = PCB[selec].nombre[1];
}
}
}
imprimir_colas();
}
/*
* compactar_cola_preparados - shift the ready queue up by one and put a
* 'z' (empty marker) at the front, then redraw. Called after a process
* leaves the head slot.
*/
void compactar_cola_preparados(void)
{
int i;
for (i = 3; i >= 0; i--)
cola_preparados[i + 1] = cola_preparados[i];
cola_preparados[0] = 'z';
imprimir_colas();
}
/*
* compactar_cola_es - drop the just-serviced entry from the I/O queue by
* shifting the remaining entries down, then redraw.
*/
void compactar_cola_es(void)
{
int i = 0;
while ((cola_es[i + 1] != 0) && (i < 4)) {
cola_es[i] = cola_es[i + 1];
i++;
}
cola_es[i] = 0;
imprimir_colas();
}
/*
* resta_es - advance every process in the I/O queue by one tick. When a
* process finishes its I/O wait it is re-loaded into memory, its burst list
* is shifted down, and it returns to the ready queue (state 2). main() runs
* this branch whenever the ready queue is empty. Free-cell counting resets
* each pass and is bounded to memoria[0..63].
*/
void resta_es(void)
{
int aux = 0, auxdos = 0, auxtres, i, memo = 0, k = 0;
if (cola_es[0] != 0) {
while ((cola_es[aux] != 0) && (aux < 4)) {
auxdos = 0;
while (PCB[auxdos].nombre[1] != cola_es[aux]) auxdos++;
PCB[auxdos].espera_es--;
if (PCB[auxdos].espera_es <= 0) {
k = 0; memo = 0; /* reset added: same do-while / memoria[64]
* hazard as in creacion_pcb */
do {
if (memoria[k] == 0) memo++;
k++;
} while (k < 64);
if (memo >= PCB[auxdos].memoria) {