IllidanStromrage 2015-12-03 13:36 采纳率: 0%
浏览 1840

有关简单轮转算法 c语言

#include
#include
#define furthest 5
struct process /*PCB STRUCTURE*/
{ int id;//进程表示符
int priority;//优先级
int cputime;//占用CPU时间片数
int alltime;//进程所需时间片数
char state;//进程状态
int next; } prochain[furthest];
int procnum;//
int rand();//
int algo;//
int run,head,tail,j;/*RUN—当前运行进程指针;HEAD—进程就绪链链首指针;TAIL—进程就绪链链尾指针*/

void print() /*PRINT THE RUNNING PROCESS,WAITING QUEUE AND PCB SEQUENCE LIST*/
{

printf("输出系统的变量run:%d、head:%d、tail:%d\n",run,head,tail);
printf("输出run的变量\n");
printf("id:%d priority:%d cputime:%d alltime:%d state:%c next:%d",prochain[run].id,prochain[run].priority,prochain[run].cputime,prochain[run].alltime,prochain[run].state,prochain[run].next);

int k,p;
for (k=1;k<=40;k++) printf("=");
printf("\nrunning process%d",prochain[run].id);
printf("\nwaiting queue.");
printf("\n %d ",prochain[run].id);
p=head;
while(p!=0)
{ printf("%5d",p);
p=prochain[p].next;}
printf("\n");
for (k=1;k<=40;k++)
printf("=");
printf("\n");
printf(" id ");
for (k=1;k<furthest+1;k++)
printf("%5d",prochain[k].id);
printf("\n");
printf("priority ");
for (k=1;k<furthest+1;k++)
printf("%5d",prochain[k].priority);
printf("\n");
printf("cputime ");
for (k=1;k<furthest+1;k++)
printf("%5d",prochain[k].cputime);
printf("\n");
printf("alltime ");
for (k=1;k<furthest+1;k++)
printf("%5d",prochain[k].alltime);
printf("\n");
printf("state ");
for (k=1;k<furthest+1;k++)
printf("%5c",prochain[k].state);
printf("\n");
printf("next ");
for (k=1;k<furthest+1;k++)
printf("%5d",prochain[k].next);
printf("\n");
getchar();
}

void insert(int q) /*INSERT A PROCESS*/
{

int p,s;
p=head;
s=prochain[head].next;
while((prochain[q].priority<prochain[s].priority)&&(s!=0))
{ p=s;
s=prochain[s].next;}
prochain[p].next=q;
prochain[q].next=s;
}
void insert2() /*PUT A PROCESS ONTO THE TAIL OF THE QUEUE*/
{

printf("调用insert2\n");
prochain[tail].next=run;
tail=run;
prochain[run].next=0;
}

void init() /*CREATE A WAITING QUEUE*/
{

int i;
head=0;
if (algo==2)
{ for (i=1;i<furthest+1;i++)
{
prochain[i].id=i;
prochain[i].priority=(rand()+11)%41;
prochain[i].cputime=0;
prochain[i].alltime=(rand()+1)%7;
prochain[i].state='W';
prochain[i].next=0;
if((prochain[i].priority<prochain[head].priority)&&(head!=0))
insert(prochain[i].id);
else
{ prochain[i].next=head;
head=prochain[i].id;}
}

}
else
{
for (i=1;i<furthest+1;i++)
{
prochain[i].id=i;
prochain[i].priority=(rand()+1)%3+1;
prochain[i].cputime=0;
prochain[i].alltime=(rand()+1)%7;
prochain[i].state='W';
prochain[i].next=(i+1);//%(furthest+1);
}
head=1;
tail=furthest;
prochain[tail].next=0;//tail原为furthest
}
run=head;
prochain[run].state='R';
head=prochain[head].next;
prochain[run].next=0;
print();
}

void prisch() /*THE PROCESS WITH PRIO algoRITHM*/
{

while(run!=0)
{
prochain[run].cputime+=1;
prochain[run].priority-=3;
prochain[run].alltime-=1;

if(prochain[run].alltime==0)
{ 
    prochain[run].state='F';
    prochain[run].next=0;
    if(head!=0)
    { 
        run=head;
        prochain[run].state='R';
        head=prochain[head].next;
    }
    else
    {   prochain[0].id=prochain[run].id;
        run=0;
    }
}
else
{   if((prochain[run].priority< prochain[head].priority)&&(head!=0))
    { 
        prochain[run].state='W';
        insert(run);
        run=head;
        prochain[run].state='R';
        head= prochain[head].next;
    }
}
print();

}//end while
}//end prisch()

void timesch() /*THE PROCESS WITH RR ALRORITHM*/
{

while(run!=0)
{
prochain[run].alltime-=1;
prochain[run].cputime+=1;
if(prochain[run].alltime==0)
{
prochain[run].state='F';
prochain[run].next=0;
if(head!=0)
{ run=head;
prochain[run].state='R';
head=prochain[head].next;}
else
{ prochain[0].id=prochain[run].id;
run=0;
}
}
else
{ if((prochain[run].cputime==prochain[run].priority)&&(head!=0))
{ prochain[run].state='W';
//prochain[run].cputime=0;
//insert2();
prochain[tail].next=run;
tail=run;
prochain[run].next=0;

        run=head;
        prochain[run].state='R';
        head=prochain[head].next;
    }
}//end else

print();
}//end while

}//end timesch
void main() /*MAIN PROGRAM*/
{
agan: printf("type the algorithm is (1:RR,2:PRIO):");
scanf("%d",&algo);
if (algo==2)
{
printf("output of priority.\n");
init();
prisch();
}
else
{ if (algo==1)
{ printf("output of round robin.\n");
init();
timesch();}
else
{ printf("try again,please\n");
goto agan;
}
}
for (j=1;j<=40;j++) printf("=");
printf("\n\n");
for (j=1;j<=40;j++) printf("=");
printf("\n\n");
printf("system finished\n");
}
这个有什么bug啊
轮转是只进行一轮,而优先级是不会停止

  • 写回答

1条回答 默认 最新

  • threenewbee 2015-12-03 15:46
    关注
    评论

报告相同问题?

悬赏问题

  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?