Multi-level feedback queue scheduling algorithm using C language Simulation

Source: Internet
Author: User

Multi-level feedback queue scheduling algorithm:
1. Set multiple ready queues and assign different priority levels to the queues. The first queue is the highest and the number decreases sequentially.
2. Assign the size of the time slice for processes in each queue. The queue with a higher priority has a smaller time slice.
3. When a new process enters the memory, it is first placed at the end of a pair column.
The end is unfinished. transfer it to the end of the second queue.
4. After a process moves from a pair column to the N queue, the time slice is used in the N queue for rotation.
5. The process in the second queue is scheduled only when the time slice is idle.
(1 ~ I-1) when idle, only scheduling I, if the processor is running in the I queue, there are new processes into the priority is higher
The new process occupies the processor, puts the running process at the end of the I queue, and distributes the processor to the new process.
# Include <stdio. h> <br/> # include <stdlib. h> <br/> # include <string. h> <br/> typedef struct node/* process node information */<br/> {<br/> char name [20]; /* process name */<br/> int PRIO;/* process priority */<br/> int round; /* allocate CPU time slice */<br/> int cputime;/* CPU execution time */<br/> int needtime; /* time required for process execution */<br/> char state;/* Process status, w -- ready state, r -- execution state, f -- completion state */<br/> int count;/* record the number of executions */<br/> struct node * next; /* linked list pointer */<br/>} PCB; <br/> typedef struct Queue/* multi-level ready queue node information */<br/> {<br/> PCB * linkpcb;/* process queue pointer in the ready queue */<br/> int PRIO; /* priority of the current ready queue */<br/> int round;/* time slice allocated by the current ready queue */<br/> struct queue * next; /* pointer to the linked list of the next ready queue */<br/>} readyqueue; <br/> PCB * run = NULL, * Finish = NULL;/* defines three queues, ready queue, execution queue, and Completion queue */<br/> readyqueue * head = NULL;/* define the first ready queue */<br/> int num; /* process count */<br/> int readynum;/* Number of ready queues */<br/> void output (); /* process information output function */<br/> void insertfinish (PCB * In);/* Insert the process to the end of the Completion queue */<br/> void insertprio (readyqueue * In);/* Create a ready queue. The smaller the priority, lower priority */<br/> void priocreate ();/* Create a ready queue Input Function */<br/> void getfirst (readyqueue * Queue ); /* obtain the queue header process in a ready queue */<br/> void insertlast (PCB * In, readyqueue * Queue ); /* Insert the process to the end of the ready queue */<br/> void processcreate ();/* Process Creation function */<br/> void roundrun (readyqueue * timechip ); /* time slice rotation scheduling algorithm */<br/> void multidispatch ();/* multi-level scheduling algorithm, one time slice is executed each time */</P> <P> int main (void) <br/>{< br/> priocreate ();/* Create ready queue */<br/> processcreate (); /* Create a ready process queue */<br/> multidispatch ();/* algorithm start */<br/> output (); /* output the final scheduling sequence */<br/> return 0; <br/>}< br/> void output () /* process information output function */<br/> {<br/> readyqueue * print = head; <br/> PCB * P; <br/> printf ("process name/T priority/t wheel count/tcpu time/t time required/T Process status/T counter/N "); <br/> while (print) <br/>{< br/> If (print-> linkpcb! = NULL) <br/>{< br/> P = print-> linkpcb; <br/> while (P) <br/> {<br/> printf ("% S/T % d/T % C/T % d/N ", p-> name, p-> Prio, p-> round, p-> cputime, p-> needtime, p-> state, p-> count ); <br/> P = p-> next; <br/>}< br/> Print = print-> next; <br/>}< br/> P = finish; <br/> while (P! = NULL) <br/> {<br/> printf ("% S/T % d/T % C/T % d/N ", p-> name, p-> Prio, p-> round, p-> cputime, p-> needtime, p-> state, p-> count ); <br/> P = p-> next; <br/>}< br/> P = run; <br/> while (P! = NULL) <br/> {<br/> printf ("% S/T % d/T % C/T % d/N ", p-> name, p-> Prio, p-> round, p-> cputime, p-> needtime, p-> state, p-> count ); <br/> P = p-> next; <br/>}</P> <p >}< br/> void insertfinish (PCB * in) /* Insert the process to the end of the Completion queue */<br/> {<br/> PCB * FST; <br/> Fst = finish; </P> <p> If (finish = NULL) <br/>{< br/> In-> next = finish; <br/> finish = in; <br/>}< br/> else <br/>{< br/> while (Fst-> next! = NULL) <br/>{< br/> Fst = FST-> next; <br/>}< br/> In-> next = FST-> next; <br/> FST-> next = In; <br/>}< br/> void insertprio (readyqueue * In)/* Create a ready queue, the smaller the number of specified priorities, the lower the priority */<br/> {<br/> readyqueue * FST, * NXT; <br/> Fst = NXT = head; </P> <p> If (Head = NULL)/* if no queue exists, it is the first element */<br/>{< br/> In-> next = head; <br/> head = in; <br/>}< br/> else/* find a proper position for insertion */<br/> {<br/> If (in-> PRIO> = FST-> prio) /* better than the first If the number is larger, insert it to the queue header */<br/>{< br/> In-> next = head; <br/> head = in; <br/>}< br/> else <br/>{< br/> while (Fst-> next! = NULL)/* move the pointer to find the location of the first small element to insert */<br/> {<br/> NXT = FST; <br/> Fst = FST-> next; <br/>}</P> <p> If (Fst-> next = NULL)/* The team end is found, the priority level is the minimum. you can insert it to the end of the Team */<br/> {<br/> In-> next = FST-> next; <br/> FST-> next = in; <br/>}< br/> else/* insert to queue */<br/> {<br/> NXT = in; <br/> In-> next = FST; <br/>}< br/> void priocreate () /* Create the ready queue Input Function */<br/> {<br/> readyqueue * TMP; <br/> int I; </P> <P> printf ("Number of input ready Queues:/N"); <br/> scanf ("% d", & readynum ); </P> <p> printf ("Enter the CPU time slice for each ready queue:/N"); <br/> for (I = 0; I <readynum; I ++) <br/>{< br/> If (TMP = (readyqueue *) malloc (sizeof (readyqueue) = NULL) <br/>{< br/> perror ("malloc"); <br/> exit (1 ); <br/>}< br/> scanf ("% d", & (TMP-> round )); /* enter the CPU time slice allocated to each process in the ready queue */<br/> TMP-> PRIO = 50-TMP-> round;/* set its priority, the higher the time slice, the lower the priority */<br/> TMP-> linkpcb = NULL ;/* The process queue for initializing the connection is empty */<br/> TMP-> next = NULL; <br/> insertprio (TMP ); /* create multiple ready queues based on the priority from high to low */<br/>}< br/> void getfirst (readyqueue * Queue) /* obtain the queue header process in a ready queue */<br/>{< br/> run = queue-> linkpcb; </P> <p> If (queue-> linkpcb! = NULL) <br/>{< br/> Run-> state = 'R'; <br/> queue-> linkpcb = queue-> linkpcb-> next; <br/> Run-> next = NULL; <br/>}< br/> void insertlast (PCB * In, readyqueue * Queue) /* Insert the process to the end of the ready queue */<br/> {<br/> PCB * FST; <br/> Fst = queue-> linkpcb; </P> <p> If (queue-> linkpcb = NULL) <br/>{< br/> In-> next = queue-> linkpcb; <br/> queue-> linkpcb = In; <br/>}< br/> else <br/>{< br/> while (Fst-> next! = NULL) <br/>{< br/> Fst = FST-> next; <br/>}< br/> In-> next = FST-> next; <br/> FST-> next = In; <br/>}< br/> void processcreate () /* Process Creation function */<br/> {<br/> PCB * TMP; <br/> int I; </P> <p> printf ("Number of input processes:/N"); <br/> scanf ("% d", & num ); <br/> printf ("Enter the process name and time required by the process:/N"); <br/> for (I = 0; I <num; I ++) <br/> {<br/> If (TMP = (PCB *) malloc (sizeof (PCB) = NULL) <br/>{< br/> perror ("malloc"); <br/> exit (1); <Br/>}< br/> scanf ("% s", TMP-> name); <br/> getchar (); /* absorb the carriage return symbol */<br/> scanf ("% d", & (TMP-> needtime); <br/> TMP-> cputime = 0; <br/> TMP-> state = 'W'; <br/> TMP-> PRIO = 50-TMP-> needtime;/* set its priority, the more time required, the lower the priority */<br/> TMP-> round = head-> round; <br/> TMP-> COUNT = 0; <br/> insertlast (TMP, head);/* The priority ranges from high to low, insert to ready queue */<br/>}< br/> void roundrun (readyqueue * timechip) /* time slice rotation scheduling algorithm */<br/> {</ P> <p> int flag = 1; </P> <p> getfirst (timechip); <br/> while (RUN! = NULL) <br/>{< br/> while (FLAG) <br/>{< br/> Run-> count ++; <br/> Run-> cputime ++; <br/> Run-> needtime --; <br/> If (run-> needtime = 0) /* process execution completed */<br/> {<br/> Run-> state = 'F'; <br/> insertfinish (run ); <br/> flag = 0; <br/>}< br/> else if (run-> COUNT = timechip-> round) /* time slice used up */<br/> {<br/> Run-> state = 'W'; <br/> Run-> COUNT = 0;/* the counter is cleared, prepare for the next time */<br/> insertlast (run, timechip); <br/> flag = 0; <Br/>}< br/> flag = 1; <br/> getfirst (timechip ); <br/>}< br/> void multidispatch ()/* multi-level scheduling algorithm, execute a time slice */<br/> {<br/> int flag = 1; <br/> int K = 0; </P> <p> readyqueue * point; <br/> point = head; </P> <p> getfirst (point); <br/> while (RUN! = NULL) <br/>{< br/> output (); <br/> If (Head-> linkpcb! = NULL) <br/> point = head; <br/> while (FLAG) <br/>{< br/> Run-> count ++; <br/> Run-> cputime ++; <br/> Run-> needtime --; <br/> If (run-> needtime = 0) /* process execution completed */<br/> {<br/> Run-> state = 'F'; <br/> insertfinish (run ); <br/> flag = 0; <br/>}< br/> else if (run-> COUNT = run-> round) /* time slice used up */<br/> {<br/> Run-> state = 'W'; <br/> Run-> COUNT = 0;/* the counter is cleared, prepare for the next time */<br/> If (point-> next! = NULL) <br/>{< br/> Run-> round = point-> next-> round; /* set the time slice to the time slice of the next ready queue */<br/> insertlast (run, point-> next ); /* Insert the process to the next ready queue */<br/> flag = 0; <br/>}< br/> else <br/>{< br/> roundrun (point ); /* If the last ready queue is used, call the time slice Rotation Algorithm */<br/> break; <br/>}< br/> + + K; <br/> If (k = 3) <br/>{< br/> processcreate (); <br/>}< br/> flag = 1; <br/> If (point-> linkpcb = NULL) /* move the ready queue pointer down */<br/> point = point-> next; <br/> If (point-> next = NULL) <br/>{< br/> roundrun (point); <br/> break; <br/>}< br/> getfirst (point ); <br/>}< br/>}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.