Process scheduler simulation

zhaozj2021-02-16  51

// Title Process Scheduling Simulation

#define null 0 # include #include #include #include // Define a PCB structure

File * groupfile [10];

TypedEf struct index {char name; // instruction INT TIME; / / instruction execution time} index;

Struct pcb {char filename [10]; // process name int ID; // Job number int exToIndex; // is currently being executed in the instruction char status; // Current state int wtime; // Waiting time};

Struct PCB JobPCB [100]; // PCB table

Typedef struct job {index idx [100]; // instruction set INT pcbnum; // PCB number correspondence} JOB;

Job jobtab [100]; // work table

Char Jobp [3] [50]; // Job // Queue struct Qnode {int PCBNUM [100]; // PCB number int head, tail;};

Struct Qnode Queue [5]; // 5 Queen 0e 1R 2i 3o 4W

Void initQueue (struct Qnode * L);

// Delay Void delay () {Long Begin, End; Time (& BEGIN); Do {TIME (& End);} While (end-begin) <= 1);}

// Character conversion to value int change (char * m) {INT i, j = 0; int LEN = Strlen (M); for (i = 0; i

// Apply for a PCB

Int allocpcb () {INT i; for (i = 0; i <3; i ) IF (jobpcb [i] .ID == - 1) Break; if (i <3) returnix i; return -1;} / / Apply Jobint AllocJob () {

INT I;

For (i = 0; i <3; i ) IF (jobtab [i] .pcbnum == -1) Break; if (i <3) returnix i; return -1;

} // Display instruction Void DisplayIndex () {INT I, J;

For (i = 0; i <3; i ) {printf ("job% d / n", i 1); for (j = 0; j <10; j ) Printf ("% D% C% D / N ", J 1, JobTab [i] .idx [j] .name, jobtab [i] .idx [j] .time);}

}

// Create Process Program INT Creatpcbline () {char line [10]; INT I, LL, JNUM, PNUM, LN = 0, BPOS, POS = 0; Char Buff [50]; char Name [20]; char CH; For (i = 0; i <3; i ) {ln = 0; jnum = allocjob (); if (jnum == -1) Return 0; pNum = allocpcb (); if (pnum == -1) Return 0 Jobtab [jnum] .pcbnum = pnum; strcpy (jobpcb [pnum] .filename, ""); jobpcb [pnum] .status = 'r'; jobpcb [pnum] .ExetoIndex = 0; JobPCB [PNUM] .id = JNUM; JobPCB [PNUM] .wtime = 0; int LN = strlen (JOBP [i]); POS = 0; While (POS Head = 0; l-> tail = 0;}

// Plug into queue / void insertQueue (struct Qnode * L, int PCBNUM) {L-> PCBNUM [L-> Tail ] = pcbnum;

/ / Does the queue are empty?

INT EMPTYQUEUE (STRUCT Qnode L) {if (L.HEAD == L.TAIL) Return 1; Return 0;

// Remove Querai Void Outqueue (Struct Qnode * L, INT * PCBNUM) {if (l-> head> = l-> tail) * pcbnum = -1; else * pcbnum = l-> PCBNUM [L-> Head ] } // Display job void display () {INT i, j; for (i = 0; i <5; i ) {printf ("Queue% D", I); for (j = queue [i] .head J

Printf ("PCB Number% D / N / N", Queue [i] .pcbnum [j]);

}

}

// Homework into the queue void Jobenqueueinit (int * total) {

INT I, NUM, INDEX = 0; CHAR CMD; for (i = 0; i <3; i ) {

IF (JobPCB [i] .ID> = 0) {cmd = job [JobPCB [i] .id] .idx [index] .name;

Switch (cmd) {case 'c': INSERTQUEUE (& queueue "; jobpcb [i] .status = 'r'; Break; Case 'i': InsertQueue (& Queue [2], i); JobPCB [i] .status = 'i'; breaf;

Case 'o': INSERTQUEUE (& Queue "; JobPCB [i] .status = 'o'; Break; Case 'W': INSERTQUEUE (& Queue [4], I); JobPCB [i]. Status = 'W'; Break; Case 'h': jobpcb [i] .status = 'h'; Num = JOBPCB [i] .id; jobtab [Num] .pcbnum = -1; JobPCB [i] .id = -1;} if (cmd == 'h') {jobpcb [i] .wtime = 0; Total ---;} JOBPCB [i] .wtime = jobtab [JobPCB [i] .id] .idx [index]. Time;

(* Total) ;

}

}

Void save () {

File * fp;

INT i; fp = fopen ("pcbtable.txt", "a");

FPRINTF (FP, "file name job number is performed to the status of the command time / N");

For (i = 0; i <3; i )

FPRINTF (FP, "/ T% S / T% D / T% D / T% C / T% D / N", JOBPCB [i] .filename, Jobpcb [i] .id, Jobpcb [i] .exetoIndex, Jobpcb [i] .status, jobpcb [i] .wtime);

Fclose (fp);

}

// Job entry Void JobenQueue (int pcbnum, int index, int * total) {int Num; char cmd;

IF (JobPCB [PCBNUM] .ID> = 0) {cmd = jobtab [JobPCB [PCBNUM] .id] .idx [index] .name;

Switch (cmd) {CASE 'C': INSERTQUEUE (& queueue "; JobPCB [PCBNUM] .status = 'r'; Break; Case 'I': InsertQueue (& Queue [2], PCBNUM); JobPCB [pcbnum] .status = 'i'; breaf;

Case 'o': INSERTQUEUE (& queue [3], PCBNUM); JobPCB [PCBNUM] .status = 'o'; Break; Case 'W': InsertQueue (& Queue [4], PCBNUM); JOBPCB [PCBNUM]. Status = 'W'; Break; Case 'h': JobPCB [PCBNUM] .Status = 'h'; Num = JobPCB [PCBNUM] .id; jobtab [Num] .pcbnum = -1; JobPCB [PCBNUM] .id = -1;} if (cmd == 'h') {JobPCB [PCBNUM] .wtime = 0; Printf ("/ N / T / T job% D completion / N", PCBNUM 1, JOBPCB [PCBNUM] .status ); (* Total) -; else jobpcb [pcbnum] .wtime = jobtab [jobpcb [pcbnum] .id] .idx [index] .time; printf ("/ n / t / t job% d / n" , PCBNUM 1); Printf ("/ t / t / t / t state:% C / N", JobPCB [PCBNUM] .status); Printf ("/ t / t also required time% D seconds / N "JobPCB [PCBNUM] .wtime);

}

}

/ / Get the first element of the queue

INT GetHead (Struct Qnode L) {RETURN L.PCBNUM [L.HEAD];} // Execute Void Run () {

INT I, FLAG = 0, TIME = 10, PCBNUM, INDEX = 0, Exenum, Num, quenum; CHAR CMD; INT J, M, N; INT TOTAL = 0;

Jobenqueueinit (& TOTAL);

Save (); while (total! = 0) {if (! EmptyQueue (Queueue (& Queue ", & pcbnum); InsertQueue (& Queue [0], PCBNUM);

} Else pcbnum = getHead (Queue [0]); delay (); jobpcb [pcbnum] .status = 'e'; Printf ("/ N / T / T job% D / N", PCBNUM 1); Printf "/ T / T / T state:% C / T execute% D seconds / N", JOBPCB [PCBNUM] .status, Time); for (i = 0; i <3; i ) {ix (JobPCB [i] .id> = 0) {

// All jobs except for the external waiting time intercom time slice IF (JOBPCB [i] .status! = 'R' && Jobpcb [i] .Status! = 'H') JobPCB [i]. WTIME = JOBPCB [i] .wtime-time;

IF (jobpcb [i] .wtime <= 0) {// Find all queues location for (j = 0; j <5; j ) {for (m = queue [j] .head; m

IF (Queue [J] .pcbnum [M] == i) {FLAG = 1; BREAK;}}}}}}}}}}}}}}} (flag == 1) {// Remove the instruction

For (n = m; n

}}}

IF (! EmptyQueue (& Queueue (& Queue [0], & PCBNUM); if (JobPCB [PCBNUM] .wtime> 0) {INSERTQUEUE (& Queue [1], PCBNUM); JobPCB [PCBNUM] .status = 'r';}

}

Printf ("/ N / N / T / T also / t% D job is not completed / N", Total); save ();

}

// Initialize void initfile () {

INT I;

STRCPY (JOBP [0], "C 20 I 20 O 15 h 0"); change STRCPY (JOBP [1], "I 10 C 20 C 10 H 0"); strcpy (jobp [2], "C 30 I 20 C 5 h 0 "); for (i = 0; i <100; i ) {jobpcb [i] .exetoIndex = 0; strcpy (jobpcb [i] .filename," "); JOBPCB [i] .id = -1; JobPCB [i] .status = 'r'; JobPCB [i] .wtime = 0;} for (i = 0; i <100; i ) jobtab [i] .pcbnum = -1; for (i = 0; I <5; i ) initQueue (& queue [i]);

//Groupfile[0]=fopen ("1.txt", "r ");

//Groupfile[1]=fopen ("2.txt", "r ");

//Groupfile[2]=fopen ("3.txt", "r ");

}

Void main () {

INITFILE (); CREATPCBLINE (); Run ();

}

Figure

转载请注明原文地址:https://www.9cbs.com/read-19715.html

New Post(0)