简体   繁体   English

使用链表进行循环调度

[英]Round Robin scheduling using a linked list

I'm trying to implement the Round Robin algorithm for a CPU Scheduler Simulator but using a linked list structure. 我正在尝试为CPU Scheduler Simulator实现Round Robin算法,但使用了链表结构。

I've wrote this code but seems not working as it should be: 我已经编写了这段代码,但似乎无法正常工作:

//Round Robin scheduling
void roundRobin(){

bubbleSortArrivalTime();

struct process *temp;
int totalTimeCnt=0;
int totalTime;
int quantum=4;
int i;

for(temp=proc; temp!=NULL; temp=temp->next){
totalTime+=temp->burstTime;

}

do{


for(i=1; i<LISTSIZE; i++){

if(i==1){
set_nth_process_burstTime(proc, i, get_nth_process_burstTime(proc,i)-quantum);

//set_nth_process_waitingTime(proc, i, get_nth_process_burstTime(proc,i-1)+get_nth_process_waitingTime(proc, i-1));

totalTime+=4;
}

else{
set_nth_process_burstTime(proc, i, get_nth_process_burstTime(proc,i)-quantum);
set_nth_process_waitingTime(proc, i, get_nth_process_burstTime(proc,i-1)+get_nth_process_waitingTime(proc, i-1));
totalTime+=4;
}

}


}while(totalTimeCnt!=0);


method="Method selected: Round Robin Scheduling";


}

The structure of my linked list is as follows: 我的链表的结构如下:

    struct process{
    int burstTime,arrivalTime, priority,pname;
    float waitingTime, turnArroundTime;
    struct process *next;
}*proc=NULL;

where proc is defined as global. 其中proc定义为全局。

I've implemented some get and set functions to access any element easily: 我已经实现了一些get和set函数来轻松访问任何元素:

void set_nth_process_burstTime(struct process*header, int position, int value)
void set_nth_process_waitingTime(struct process *header, int position, float value)
int get_nth_process_waitingTime(struct process *header, int position)
int get_nth_process_priority(struct process *header,int position)
int get_nth_process_arrivalTime(struct process *header,int position)
int get_nth_process_burstTime(struct process *header,int position)

Could you please advice me on how to implement the Round Robin correctly? 您能为我提供有关如何正确实施循环赛的建议吗? It is not giving the correct results. 它没有给出正确的结果。

round robin is very simple. 轮循很简单。 in the main function, after all the initialization, setting of interrupts, etc, there is: 在主要功能中,在所有初始化,中断设置等之后,有:

while(1)
{
    process_1();
    process_2();
    process_3();
    ...
}

Anything beyond that would be due to some special considerations for the specific project. 超出此范围的任何其他原因均应归因于特定项目的一些特殊考虑。

To implement a scheduler + dispatcher , with multiple desired execution intervals, priorities, condition states, aging, etc is a completely different architecture. 要实现具有多个所需执行间隔,优先级,条件状态,老化等的scheduler + dispatcher程序,则是完全不同的体系结构。

for round robin , the above while() loop is it. 对于round robin ,上面的while()循环就是它。

For a realistic project, you will also need: 对于现实的项目,您还需要:

  1. certain interrupt driven processes for timing and I/O, 某些中断驱动的时序和I / O过程,
  2. handling of the watchdog timer 看门狗定时器的处理
  3. warm and cold boot modes 冷启动模式
  4. powerup BIT 上电BIT
  5. continuous BIT 连续BIT
  6. commanded BIT 命令BIT
#include<stdio.h> 

struct process {
char na[20];
int at, bt, ft, tat, rem;
float ntat;
} Q[5], temp;

void roundRobin() {
 int rr[20], q, x, k;
int f, r, n, i, j, tt = 0, qt, t, flag, wt = 0;
float awt = 0, antat = 0, atat = 0;

printf("Enter the no. of jobs:");
scanf("%d", &n);
for (r = 0; r < n; r++) {
    printf("Enter process name,arrival time and burst time:\n");
    scanf("%s%d%d", Q[r].na, &Q[r].at, &Q[r].bt);
}
printf("Enter quantum:\n");
scanf("%d", &qt);
for (i = 0; i < n; i++) {
    for (j = i + 1; j < n; j++) {
        if (Q[i].at > Q[j].at) {
            temp = Q[i];
            Q[i] = Q[j];
            Q[j] = temp;
        }
    }
}
for (i = 0; i < n; i++) {
    Q[i].rem = Q[i].bt;
    Q[i].ft = 0;
}
tt = 0;
q = 0;
rr[q] = 0;
do {
    for (j = 0; j < n; j++)
        if (tt >= Q[j].at) {
            x = 0;
            for (k = 0; k <= q; k++)
                if (rr[k] == j)
                    x++;
            if (x == 0) {
                q++;
                rr[q] = j;
            }
        }
    if (q == 0)
        i = 0;
    if (Q[i].rem == 0)
        i++;
    if (i > q)
        i = (i - 1) % q;
    if (i <= q) {
        if (Q[i].rem > 0) {
            if (Q[i].rem < qt) {
                tt += Q[i].rem;
                Q[i].rem = 0;
            } else {
                tt += qt;
                Q[i].rem -= qt;
            }
            Q[i].ft = tt;
        }
        i++;
    }
    flag = 0;
    for (j = 0; j < n; j++)
        if (Q[j].rem > 0)
            flag++;
} while (flag != 0);

printf("\n\n\t\tROUND ROBIN ALGORITHM");
printf("\n***************************");
printf("\nprocesses Arrival time burst time finish time tat wt ntat");
for (f = 0; f < n; f++) {
    wt = Q[f].ft - Q[f].bt - Q[f].at;
    Q[f].tat = Q[f].ft - Q[f].at;
    Q[f].ntat = (float) Q[f].tat / Q[f].bt;
    antat += Q[f].ntat;
    atat += Q[f].tat;
    awt += wt;
    printf("\n\t%s\t%d\t\t%d\t%d\t%d\t%d %f", Q[f].na, Q[f].at, Q[f].bt,
            Q[f].ft, Q[f].tat, wt, Q[f].ntat);
}
antat /= n;
atat /= n;
awt /= n;
printf("\nAverage tat is %f", atat);
printf("\nAverage normalised tat is %f", antat);
printf("\n average waiting time is %f", awt);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM