Red de conocimientos turísticos - Conocimientos sobre calendario chino - ¿Cómo implementar un algoritmo de programación de prioridades en Java?

¿Cómo implementar un algoritmo de programación de prioridades en Java?

#include lt;time.hgt;

#include lt;dos.hgt;

#include lt;math.hgt;

#include lt;conio.conio.hgt;

#include lt;stdio.hgt;

#include lt;conio.hgt;

# incluir lt;stdio.hgt;

#include lt;stdlib.hgt;

#include lt;time.hgt;

#include lt;gráficos. h gt;

#define ESC 0x1b

#define ENTER 0x0d

#define VERDADERO 1

#define FALSO 0

////Prioridad de conversión por TIME segundos*/

#define TIME5

/* Estructura de datos**/

/** * ************************************************** ***********/

enum _Status/* Enumeración del estado del proceso**

{

LISTO = 0, /* listo**/p>

EJECUTAR, /* ejecutando**/

SUSPENDER, /* suspendido**/p>

} ;

typedef enum _Status Status;

/************************ **** *** **************************************/

estructura _Pcb/ * estructura del proceso*/

{

int PID; /*ID del proceso, el proceso con un ID negativo es un trabajo en la cola de respaldo del sistema*/

int Time ;/* El tiempo requerido para que se ejecute el proceso*/

int Prior; /* La prioridad del proceso, mayor será la prioridad*/

Status Sts; /* Estado*/

struct _Pcb *Next; /* Apunta al PCB del siguiente proceso en esta cola de procesos *//p>

}

typedef struct _Pcb PCB

/*************************************** ******** *******************/

struct _Batch/*La estructura del tracto en el procesamiento multicanal**

{

PCB *pcb; /* el proceso que el carril está procesando actualmente**

struct _Batch *Next /* El siguiente carril**

};

p>

typedef estructura _Batch Lote

/**************** * ********

**************************************/

/* Variables globales relacionadas con sistemas multicanal**

PCB *ProcQueue = NULL /* Lista de cadenas de procesos, ordenadas de mayor a menor por prioridad**

Lote *BatchQueue = NULL ;/*Lista enlazada multicanal del sistema***

/****************************** *************************************/

/*Prioridad dinámica preventiva algoritmo de programación y declaraciones de funciones relacionadas***/

/********************************* ** ******************************/

int InitBatches(int n);/* Inicializar Sistema multicanal, n es el número de canales */

int InsertProc(int prior, int time /* Insertar un nuevo proceso en la lista de procesos en orden de prioridad */

);

int InsertIDLE(); /* Agrega un proceso de respaldo a la cola de procesos, que será llamado cuando el sistema esté inactivo */

int SortProcQueue() /* Ordena los procesos desde la prioridad más alta; a la prioridad más baja La cola está ordenada*/

int AddBatch();/* Aumentar el número de canales del sistema*/

int DeleteBatch();/* Reducir el número de canales del sistema canales*/

int UnSuspendProc(int id);/* Anula la suspensión del proceso con ID id del estado suspendido.

*/

int UpdateBatches();/* El sistema multicanal actualiza la lista de procesos y elimina el proceso completado*/

int PassSeconds(int n);

p>

/*************** ******************************* ********************/

/* Definición de cada función*/

/****** ********************************************** ** ** ******/

int InitBatchs(int n)

{

int i;

para (i =0 ilt; n; i)

{

AddBatch();

}

retorno (UpdateBatches()); p>

}

int InsertProc(int anterior, int tiempo)

{

static int sysid = 0;

pcb = (PCB*)malloc(sizeof(PCB));

if (pcb == NULL) devuelve FALSO;

pcb-gt; Prior = prior;

p>

pcb-gt.Time = tiempo

pcb-gt; PID = (sysid);

pcb-gt = LISTO; p> if (ProcQueue == NULL)/* si la cola de proceso está vacía**

{

ProcQueue = pcb;

pcb-gt;Next = NULL;

Devuelve VERDADERO

}

último = ProcQueue

ahora = último-gt; >

if (pcb-gt; Prior gt; last -gt; Prior)/* la pcb estará en la cola**

{

pcb-gt Next = ProcQueue; ;

ProcQueue = pcb;

Devuelve VERDADERO

}

mientras ((ahora! = NULL) amp; (pcb-gt; Prior lt; now-gt; Prior))/* Buscar ubicación de inserción**

{

last = now; p>

ahora = último-gt;

}

último-gt; siguiente = pcb; = ahora ;

return VERDADERO;

}

int InsertIDLE()

{

PCB *ahora = ProcQueue ;

PCB * inactivo = (PCB*)malloc(sizeof(PCB));

if (i

dle == NULL) devuelve FALSO;

inactivo-gt; PID = -1;

inactivo-gt; ; Sts = SUSPENDER;

inactivo-gt; Tiempo = -1;

inactivo-gt; Siguiente = NULL

si (ProcQueue == NULL)

{

ProcQueue = inactivo

devuelve VERDADERO

}

while(now-gt; Siguiente ! = NULL)

{

ahora = ahora-gt; Siguiente

}

ahora-gt; ;

return TRUE;

}

int SortProcQueue()

{ /* clasificación burbujeante**

PCB *último, *ahora;

int b = FALSE; /* Si no se produjo ningún intercambio durante el último recorrido**

if (ProcQueue==NULL ||| ProcQueue- gt ;Next==NULL)/* Si no hay ningún proceso o solo hay un proceso en la lista vinculada*/

Devuelve FALSE;

while (!b)

{

b = VERDADERO

último=ProcQueue;

ahora=último-gt; último-gt; anterior lt; ahora-gt; anterior)

{

ProcQueue = ahora;

último-gt; ;

ahora-gt; Siguiente = último;

b = FALSO

último = ProcQueue

ahora = último-gt; ;Siguiente;

p>

}

mientras (ahora-gt; ¡Siguiente!=NULL)

{

si ((ahora -gt; Antes)lt; (ahora-gt.Siguiente-gt; Antes))

{

último-gt; Siguiente = ahora-gt;

ahora-gt; Siguiente = ahora-gt; Siguiente;

último-gt; Siguiente = ahora; /p>

}

else

último = último-gt; Siguiente;

ahora = último-gt; p> }

}

devuelve VERDADERO

}

int AddBatch()

{

Lote * bt = (Lote*)malloc(sizeof(Lote));

if (bt==NULL) return FALSE;

bt-gt; =

Cola de lotes;

Cola de lotes = bt;

bt-gt; pcb = NULL

retorno (InsertIDLE());

int DeleteBatch()

{

Lote *bt = BatchQueue

PCB *último, *ahora;

if (BatchQueue==NULL || BatchQueue-gt; Next==NULL)/* Si solo queda el último, no lo elimine**

Devuelve FALSE;

if (ProcQueue==NULL || ProcQueue-gt; Next==NULL)/* Si solo queda el último proceso de copia de seguridad inactivo */

Devuelve FALSE /**/

last = ProcQueue;

now = last -gt;Next;

while (now-gt;Next != NULL)/* Encuentra el último proceso, debe ser un proceso inactivo de respaldo*/

{

last = now;

now = last -gt; last-gt ;Siguiente;

}

if (now==NULL | now-gt;PIDgt;=0)/* Proceso de copia de seguridad no encontrado*/

Devuelve FALSO; /**/

gratis(ahora);

último-gt; Siguiente = NULL

BatchQueue-gt; ;

p>

gratis(bt);

Devuelve VERDADERO

}

int UnSuspendProc(int id)

{

PCB *ahora = ProcQueue;

si (ProcQueue==NULL) devuelve FALSO;

mientras (ahora!= NULL)

{

if (now-gt;PID == id)

{

now-gt;Sts = LISTO;

devolver VERDADERO

}

}

devolver FALSO

}

int UpdateBatches; ()

{

Lote *bt = BatchQueue;

PCB *último = ProcQueue, *ahora

mientras (bt ! = NULL)

{

bt-gt; pcb = NULL

bt = bt-gt; /p>

if (ProcQueue == NULL) devuelve TRUE;

while (last-gt; Sts= =RUN amp; amp; last-gt; PIDgt; =0 amp; amp; last -gt;Timelt;=0)

{

ProcQ

ueue = ProcQueue-gt; Siguiente;

gratis(último);

último = ProcQueue

}

ahora = último- gt;Siguiente;

while (now != NULL)

{

if (now-gt;Sts==RUN amp;amp; now-gt ;PIDgt;=0 amp;amp;now-gt;Timelt;=0)/* si este es un proceso general en ejecución y ha terminado de ejecutarse*

{

last-gt ; Siguiente = ahora-gt; Siguiente

gratis(ahora

}

else

último = último-gt; Siguiente;

ahora = último-gt; Siguiente

}

bt = BatchQueue

ahora = ProcQueue; >

while (bt !

bt-gt; pcb = ahora;

ahora-gt; Sts = RUN;

bt = bt-gt ;Siguiente;

now = now-gt;Siguiente;

}

while (ahora != NULL)

{ p>

if (now-gt; Sts == EJECUTAR)

{

now-gt; Sts = SUSPENDER;

}

ahora = ahora-gt; Siguiente;

}

devuelve VERDADERO

}

int PassSeconds( int n )

{

tiempo int estático = 0;

int i=0, ProcEnd = FALSE

PCB *pcb; = ProcQueue ;

Lote *bt = BatchQueue;

si (bt == NULL) devuelve FALSO;

tiempo = n;

if (timegt; =HORA)

{

i = hora/HORA /*Cuántos intervalos de tiempo han pasado*/

tiempo = tiempoTIEMPO;

}

mientras (bt ! = NULL)/* Tiempo de ejecución del proceso de actualización**

{

if (bt-gt; pcb-gt; PIDgt; = 0)

{

bt-gt; pcb-gt; Tiempo -= n;

if (bt-gt; pcb-gt; Tiempo lt; = 0)/* El proceso finaliza**

{

ProcEnd = VERDADERO

}

}

}

bt = bt-gt; Siguiente;

}

si (i gt; 0)

{

while (pcb ! = NULL) /* Actualizar prioridad del proceso (prioridad dinámica) */

{

if (pcb-gt; Sts == RUN amp; amp; pcb-gt; PIDgt = 0)/* Reducir la prioridad del proceso en ejecución */

{

pcb-gt; > if (pcb-gt; Prior lt; 0)

pcb-gt; Prior = 0

}

else if (pcb-gt ;Sts == SUSPENDER amp;amp; pcb-gt;PIDgt;=0)/* Aumentar la prioridad del proceso pendiente*/

pcb-gt;Prior = i;

pcb = pcb-gt;Siguiente;

}

}

if (igt; 0)

SortProcQueue();

p>

if (ProcEnd || igt; 0)

{

UpdateBatches() /* Actualizar proceso multicanal*/

}

Devuelve VERDADERO;

}

/************************ * ** ****************************************/

/ *Funciones relacionadas con la GUI**

/************************************ ****** ********************** ****/

/* Unidad de ancho y alto del formulario* /

#define ANCHO 64

#define ALTO 12

line(0, ALTO*3, 639, ALTO*3);

gprintf(HEIGHT*0 2, HEIGHT*1 2, "Lotes del sistema"); /* Encabezado de lista multicanal del sistema**

gprintf(HEIGHT*0 2, HEIGHT*2 2, " Lote");

gprintf(ANCHO*1 2, ALTO*2 2, "ProcID");

gprintf(ANCHO*2 2, ALTO*2 2, "Tiempo"

gprintf(ANCHO*3 2, ALTO*2 2, "Anterior");

gprintf(ANCHO*4 2, ALTO*1 2, "Procesos suspendidos") ; /* Encabezado de lista de cola pendiente*/

gprintf(WIDTH*4 2, HEIGHT*2 2, "ProcID"); 2, "Tiempo") ;

gprintf(ANCHO*6 2, ALTO*2 2, "Antes");

gprintf(ANCHO*7 2, ALTO* 1 2, "Procesos listos"); /* Encabezado de lista de cola listo*/

gprintf(WIDTH*7 2, HEIGH

T*2 2, "ProcID");

gprintf(ANCHO*8 2, ALTO*2 2, "Tiempo");

gprintf(ANCHO*9 2, ALTO* 2 2, "Prior");

}

int DrawData()/* Dibujar datos del sistema*/

{

int numRun=0, numSus=0, numRed=0; /* Cuántos procesos están ejecutándose, esperando y listos*/

int DrawData()/* Dibujar datos del sistema*/*

{

int numRun=0, numSus=0, numRed=0 /* Cuantos procesos están en ejecución, pendientes y listos*/