| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3339 人关注过本帖
标题:线程调度算法:轮转调度 设置时间片的……
只看楼主 加入收藏
米小兔
Rank: 1
等 级:新手上路
帖 子:61
专家分:0
注 册:2012-7-14
结帖率:0
收藏
已结贴  问题点数:20 回复次数:6 
线程调度算法:轮转调度 设置时间片的……
要求是,显示输入进程个数,时间片长度,所需时间,到达时间,还有切换过程。
下面的这个代码,运行不起来……

#include <stdio.h>
#include <stdlib.h>

#include <string.h>

#include<windows.h>
/*进程控制块数据结构*/ typedef struct node  { char name[10];/*进程名*/
int prio;     /*进程优先级*/
int round;    /*循环轮转法进程每次轮转的时间片*/  
int cputime; /*进程累计消耗的CUP时间*/
int needtime; /*进程到完成还需要的CPU时间*/
int count;    /*循环轮转法一个时间片内进程运行时间*/
char state;   /*进程的状态:'R':运行,'W':等待,'F':结束*/
struct node *next;/*指向下一个进程的链指针*/
}PCB;
PCB *finish,*ready,*tail,*run;/*指向三个队列的队首的指针, finish为完成队列头指针, ready为就绪队列头指针,tail为就绪队列的队尾指针,run为当前运行进程头指针*/
int N;/*定义进程的数目*/
void firstin(void);     //调度就绪队列的第一个进程投入运行;
void print1(char a);   

 //打印表头行信息
void print2(char chose,PCB *p); //打印每一行的状态信息

void print(char chose);    //打印每执行一次算法后所有的进程的状态信息
void insert_prio(PCB *q);   //在优先数算法中,将尚未完成的PCB按优先数顺序插入到就绪队列中;
void prior_init(char chose); //进程优先级法初始化将进程按优先级插入到就绪队列里
void priority(char chose);   //进程优先级算法总函数
void insert_rr(PCB *q);    //在轮转法中,将将执行了一个时间片单位(为2),但尚未完成的进程的PCB,插到就绪队列的队尾;
void roundrun_init(char chose);//循环轮转法初始化将就绪队列保存为FIFO队列
void roundrun(char chose);//循环轮转法总算法
void main(){
char chose=' ';
while((chose!='e')&&(chose!='E'))
{fflush(stdin);     system("cls");     

/*printf("\t\t\t两种进程调度算法的模拟

\n\n");
printf("\tP.进程优先级算法模拟\n\n");*/   

printf("\tR.循环轮转算法模拟\n\n");
printf("\tE.退出程序\n\n");   
printf("\t请输入你的选择:");   
scanf("%c",&chose);
if((chose!='e')&&(chose!='E'))
{
system("cls");      /*if((chose=='P')||

(chose=='p'))     {             prior_init
(chose);       priority(chose);       system

("cls");     } */
/*else */
if((chose=='r')||(chose=='R'))     

{ roundrun_init(chose);      
roundrun(chose);
system("cls");
}
}
}
  printf("\n\t\t谢谢使用!!!\n");
}
void firstin(void)//调度就绪队列的第一个进程投入运行;
{if(ready!=NULL)     {      run=ready;     

 ready=ready->next;     
 run->state='R';
 run->next=NULL;    }   
 else    {      
run=NULL;          }   
 }
void print1(char a)//打印表头行信息
{ if(toupper(a)=='P')
{ printf("name cputime needtime priority state \n"); }

else
{printf("name cputime needtime count round  state \n");  }}
void print2(char chose,PCB *p)//打印每一行的状态信息
 { if(toupper(chose)=='P')
{    printf("%s\t%d\t%d\t%d\t%c\n",p->name,p->cputime,p->needtime,p->prio,p->state);     }
else
{printf("%s\t%d\t%d\t%d\t%d\t%c\n",p->name,p->cputime,p->needtime,p->count,p->round,p->state); }}
void print(char chose)//打印每执行一次算法后所有的进程的状态信息
 { PCB *p;

print1(chose);
 if(run!=NULL) {
    print2(chose,run); }
p=ready;
 while(p!=NULL) {    print2(chose,p);    p=p->next; }
p=finish;
while(p!=NULL) { print2(chose,p); p=p->next; }  }
void insert_prio(PCB *q)/*在优先数算法中,将尚未 完成的PCB按优先数顺序

插入到就绪队列中;*/ { PCB *p,*s,*r;

/*p,r用来控制就绪队列滚动,S指向插入的

队列*/  s=q; p=ready; r=p;
if(s->prio>ready->prio)// 要插入的进程的优先级大于ready的优先级
 {    s->next=ready; ready=s;           }
else//要插入的进程的优先级不大于ready的优先级

{    while(p)    {    if(p->prio>=s->prio) {

 r=p;    p=p->next; }    else     break;     

} //找到要插入的位置
s->next=p; r->next=s; } }
/*void prior_init(char chose)/*进程优先

级法初始化        将进程按优先级插入到就

绪队列里 {     PCB *p;     int i,time;     

char na[10];     ready=NULL;     

finish=NULL;     run=NULL;  printf("\t\t

进程优先级算法模拟全过程\n\n");     

printf("输入进程 的个数 N:\n");     scanf

("%d",&N); for(i=0;i<N;i++) { p=

(PCB*)malloc(sizeof(PCB));    printf("输入

第%d个进程名\n",i+1); scanf("%s",na);
printf("完成进程需要的时间片数\n");

scanf("%d",&time); strcpy(p->name,na);

p->cputime=0; p->needtime=time;

p->state='W'; p->prio=50-time;//设置

进程优先值初值  if(ready==NULL){   

ready=p;    ready->next=NULL; } else {   

  insert_prio(p); } printf("当前就绪队列的

进程的信息\n");print(chose); } printf("%d

个进程已按优先级从高到低进到就绪队列

中\n",N);       printf("按回车键开始模拟优

先级算法.....\n");       fflush(stdin);      

getchar(); firstin(); }*/
/*void priority(char chose)//进程优先级

算法总函数 { int i=1;  while(run!=NULL) {

run->cputime+=1; run->needtime-=1;

run->prio-=1; if(run->needtime==0) {   

    run->next=finish;    finish=run;      

run->state='F';    run->prio=0;      
run=NULL;       firstin();         } else
{      if((ready!=NULL)&&(run-

>prio<ready->prio))      {    run-

>state='W';    insert_prio(run);   

run=NULL;    firstin();      } }printf

("第%d次执行优先级算法\n",i++);   

print(chose);    if(run)    {    printf("按回

车键继续下一次优先级算法.....\n");        }   

 else     printf("优先级算法模拟过程结束
!!\n");    fflush(stdin);getchar();}
}*/   void insert_rr(PCB *q)//在轮转法中,将执行了一个时间片单位(为2),      

//但尚未完成的进程的PCB,插到就绪队列的队尾;
 { tail->next=q; tail=q; q->next=NULL;}
void roundrun_init(char chose)/*循环轮

转法初始化             将就绪队列保存为

FIFO队列*/ { PCB *p; int i,time; char na

[10]; ready=NULL; finish=NULL;

run=NULL;printf("\t\t循环轮转算法模拟全过程\n\n");
printf("输入进程 的个数 N:\n");
scanf("%d",&N);
for(i=0;i<N;i++) { p=(PCB*)malloc(sizeof(PCB));
printf("输入第%d个进程名\n",i+1); scanf("%s",na);
printf("完成进程需要的时间片数\n");
scanf("%d",&time);
strcpy(p->name,na);
p->cputime=0;
p->needtime=time;
p->count=0;
p->state='W';
p->round=2;
if(ready!=NULL){insert_rr(p); }
else {     
p->next=ready;     ready=p;     tail=p; }
printf("当前就绪队列的进程的信息\n");
printf(chose);}
printf("%d个进程已按FIFO进到就绪队列中\n",N);
printf("按回车键开始模循环轮转算法.....\n");      
fflush(stdin);
getchar(); run=ready;
ready=ready->next;
run->state='R';    }
void roundrun(char chose)//循环轮转法总算法
{ int i=1;
while(run!=NULL) {
run->cputime+=1;
run->needtime-=1;
run->count+=1;
if(run->needtime==0)
{ run->next=finish;
finish=run;   
run->state='F';
 run->prio=0;   
run=NULL;
if(ready!=NULL) {      

firstin();}}
 else   
 {  if(run->count==run->round)     
 {      run->count=0;
    if(ready!=NULL)    {         

      run->state='W';  
      insert_rr(run);   

firstin();   
    }     }    }   
 printf("第%d次执行循环轮转算法\n",i++);   
 print(chose);     

if(run)    {  printf("按回车键继续下一次循环轮转算法.....\n"); }   
else     printf("循环轮转算法模拟过程结束!!\n");   
fflush(stdin);   
 getchar();}}
搜索更多相关主题的帖子: 时间 include 优先级 count 
2013-04-03 16:24
fanpengpeng
Rank: 8Rank: 8
来 自:南极洲
等 级:蝙蝠侠
威 望:7
帖 子:299
专家分:849
注 册:2013-2-1
收藏
得分:10 
好长啊

人生是一场错过 愿你别蹉跎
2013-04-03 16:45
peach5460
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:武汉
等 级:贵宾
威 望:30
帖 子:2780
专家分:6060
注 册:2008-1-28
收藏
得分:10 
科普?

我总觉得授人以鱼不如授人以渔...
可是总有些SB叫嚣着:要么给代码给答案,要么滚蛋...
虽然我知道不要跟SB一般见识,但是我真的没修炼到宠辱不惊...
2013-04-03 17:24
米小兔
Rank: 1
等 级:新手上路
帖 子:61
专家分:0
注 册:2012-7-14
收藏
得分:0 
自己又用C++修改了一个代码,可是执行出来的效果,不是我想要的。看哪位大神能给我下面写的修改正确啊,憋死了。。。。还是自己没学好吧。

include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream.h>

typedef struct node
{
 char name[10];     
 int prio;         
 int round;         
 int cputime;      
 int needtime;
 int gettime;   
 int count;         
 char state;        
 struct node *next;
}PCB;
PCB *finish,*ready,*tail,*run; //队列指针
int N;     //进程数

void firstin()
{
 run=ready;      //就绪队列头指针赋值给运行头指针
 run->state='R'; //进程状态变为运行态
 ready=ready->next; //就绪队列头指针后移到下一进程
}

void interrupt()//第一个CPU占用时间等于下一个进程的到达时间
{
 if(ready->cputime==(ready->next)->gettime)
{
    ready=ready->next;
    ready->state='W';
}

}

//输出标题函数
void prt1(char a)
{
 if(toupper(a)=='P')  //优先级法
  cout<<" "<<endl;
 cout<<"进程名 到达的时间 占用CPU时间 到完成还要的时间 轮转时间片 状态"<<endl;
}

//进程PCB输出
void prt2(char a,PCB *q)
{
 if(toupper(a)=='P')  //优先级法的输出
  cout<<q->name<<"       "<<q->gettime<<"       "<<q->cputime<<"           "<<q->needtime<<"               "<<q->round<<"         "<<q->state<<endl;
}
//输出函数
void prt(char algo)
{
 PCB *p;
 prt1(algo);  //输出标题
 if(run!=NULL)  //如果运行指针不空
  prt2(algo,run);  //输出当前正在运行的PCB
 p=ready;  //输出就绪队列PCB
 while(p!=NULL)
 {
  prt2(algo,p);
  p=p->next;
 }
 p=finish;   //输出完成队列的PCB
 while(p!=NULL)
 {
  prt2(algo,p);
  p=p->next;
 }
 getchar();  //按住任意键继续
}
//时间片轮转的插入算法
void insert(PCB *q)
{
 PCB *p1,*s,*r;
 s=q;  //待插入的PCB指针
 p1=ready;  //就绪队列头指针
 r=p1;  //*r做p1的前驱指针
 while(p1!=NULL)
  if(p1->round<=s->round)
  {
   r=p1;
   p1=p1->next;
  }
  if(r!=p1)
  {
   r->next=s;
   s->next=p1;
  }
  else
  {
   s->next=p1;  //否则插入在就绪队列的头
   ready=s;
  }
}
//优先级创建初
void create(char alg)
{
 PCB *p;
 int i,gettime,time;
 char na[10];
 ready=NULL;  
 finish=NULL;
 run=NULL;   
 cout<<"输入进程名,到达时间及其需要运行的时间:"<<endl;
 for(i=1;i<=N;i++)
 {
  p=new PCB;
  cin>>na;
  cin>>gettime;
  cin>>time;
  strcpy(p->name,na);
  p->cputime=0;
  p->gettime=gettime;
  p->needtime=time;
  if(gettime=0){p->state='W';}
  else {p->state='F';}
  p->round=0;
  if(ready!=NULL)  
   insert(p);
  else
  {
   p->next=ready;  
   ready=p;
  }
  cout<<"输入进程名,到达时间及其需要运行的时间:"<<endl;
 }
 prt(alg);  
 run=ready;  
 ready=ready->next;
 run->state='R';
}
void timeslicecycle(char alg)
{cout<<"请输入时间片长度:";
 int pertime;
 cin>>pertime;
 cout<<"输入时间片长度:"<<pertime<<endl;
 while(run!=NULL)
 {
  if(run->needtime>pertime)
    {
      run->cputime=run->cputime+pertime;
      run->needtime=run->needtime-pertime;
      run->round=run->round+pertime;
      run->state='W';
      interrupt();
      insert(run);
      interrupt();
      firstin();
    }
  else
   { int replace1,replace2;
     replace1=0;
     replace2=run->needtime;
     run->cputime=run->cputime+replace2;
     run->needtime=replace1;
     run->round=run->round+replace2;
     run->next=finish;
     finish=run;
     run->state='F';
     run=NULL;
     interrupt();
     if(ready!=NULL)
       firstin();
   }
  prt(alg);
 }
}


//主函数
void main()
{
 char algo='P';  //算法标记
 cout<<"输入进程的个数:";
 cin>>N;  //输入进程数
 create(algo);  //创建进程
 timeslicecycle(algo);  //优先级法调度
} //main()

这个代码,想要的结果,我打个比方,

输入进程的个数:4
输入进程名,到达时间及其需要运行的时间:
a 0 5
输入进程名,到达时间及其需要运行的时间:
b 1 3
输入进程名,到达时间及其需要运行的时间:
c 2 1
输入进程名,到达时间及其需要运行的时间:
d 3 6


进程名  到达的时间  占用cpu时间 到完成还要得时间 轮转时间片  状态
a       0            0           5               0           W
b       1            0           3               0           F
c       2            0           1               0           F
d       3            0           6               0           F
输入时间片长度:2
输入时间片长度为:2

进程名  到达的时间  占用cpu时间 到完成还要得时间 轮转时间片  状态
b       1            0           3               0           R
c       2            0           1               0           W
a       0            2           3               2           W
d       3            0           6               0           F

进程名  到达的时间  占用cpu时间 到完成还要得时间 轮转时间片  状态
c       2            0           1               0           R
a       0            2           3               2           W
d       3            0           6               0           W
b       1            2           1               2           W

进程名  到达的时间  占用cpu时间 到完成还要得时间 轮转时间片  状态
a       0            2           3               2           R
d       3            0           6               0           W
b       1            2           1               2           W
c       2            1           0               1           F

进程名  到达的时间  占用cpu时间 到完成还要得时间 轮转时间片  状态
d       3            0           6               0           R
b       1            2           1               2           W
c       2            1           0               1           F
a       0            4           1               4           W

进程名  到达的时间  占用cpu时间 到完成还要得时间 轮转时间片  状态
b       1            2           1               2           R
c       2            1           0               1           F
a       0            4           1               4           W
d       3            2           4               2           W

进程名  到达的时间  占用cpu时间 到完成还要得时间 轮转时间片  状态
a       0            4           1               4           R
d       3            2           4               2           W
b       1            3           0               3           F
c       2            1           0               1           F

进程名  到达的时间  占用cpu时间 到完成还要得时间 轮转时间片  状态
d       3            2           4               2           R
a       0            5           0               5           F
b       1            3           0               3           F
c       2            1           0               1           F

进程名  到达的时间  占用cpu时间 到完成还要得时间 轮转时间片  状态
d       3            4           2               4           R
a       0            5           0               5           F
b       1            3           0               3           F
c       2            1           0               1           F

进程名  到达的时间  占用cpu时间 到完成还要得时间 轮转时间片  状态
d       3            6           0               6           F
a       0            5           0               5           F
b       1            3           0               3           F
c       2            1           0               1           F

调度完成。。是这样一个意思
2013-04-09 19:49
米小兔
Rank: 1
等 级:新手上路
帖 子:61
专家分:0
注 册:2012-7-14
收藏
得分:0 
回复 2楼 fanpengpeng
我又修改了一下,麻烦帮看一下
2013-04-09 19:51
米小兔
Rank: 1
等 级:新手上路
帖 子:61
专家分:0
注 册:2012-7-14
收藏
得分:0 
回复 3楼 peach5460
麻烦帮看一下四楼我又修改的,谢啦!
2013-04-09 19:53
fanpengpeng
Rank: 8Rank: 8
来 自:南极洲
等 级:蝙蝠侠
威 望:7
帖 子:299
专家分:849
注 册:2013-2-1
收藏
得分:0 
回复 5楼 米小兔
原先以为你是在搞科普呢 有时间来看一下

人生是一场错过 愿你别蹉跎
2013-04-11 08:29
快速回复:线程调度算法:轮转调度 设置时间片的……
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016974 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved