我要啦免费统计
实验室老是中毒,今天差点连代码都保不住了
贴贴备份
进程调度 实验一 RR 时间片轮转


#include
<stdio.h>
#include
<stdlib.h>
#include
<conio.h>
#include
<iostream>
using namespace std;

#define getch(type)(type*)malloc(sizeof(type))
#define null 0
#define MAXN 1000
int stack[MAXN];
int top;

struct pcb{//调用进程模块
    char name[10];//进程名
    char state; //进程状态: 就绪,运行,完成
    int super; //进程优先级
    int ntime;//需要运行的时间
    int rtime;//到达时间 change
    struct pcb* link;//链接指针
}
*ready=null,*p;
typedef 
struct pcb PCB;
void destroy();
void check();

void sort()//建立对进程优先级排列函数
{
   PCB 
*first,*second;
   
   
int insert = 0;
   
   
if( ( ready == null ) || ( ( p -> rtime ) < ( ready -> rtime ) ) ){//优先级最大者,插入队首
        p -> link = ready;
        ready 
= p;
   }
 
   
else {//进程比较优先级。插入适当的位置中
       first = ready;
       second 
= first -> link;
       
while( second != null){
           
if( (p -> rtime ) < (second -> rtime)){//come ing time
           
//插入当前进程前面
               p -> link = second;
               first 
-> link = p;
               second 
= null;
               insert 
= 1;   
            
               }

            
else{
                 first 
= first -> link;
                 second 
= second -> link;
               }

           
        }

       
if( insert == 0) first -> link =p;

   
   }

   
return ;
}


PCB 
*q;
PCB 
*front,*rear;

void RR()
{
    
int time=0;
    q
=ready;
    front
=ready;
    rear 
=ready;
    
while(rear->link != null) rear= rear -> link; 
    
    
while( front){
        printf(
"Running Time : %d\n",++time);

        
if(front -> rtime <= time){
           
             front 
-> state ='R';

            front 
-> ntime--;//need time minus 1

            printf(
"\n *** 当前正在运行的进程:%s\n",front->name);

            
if( front -> ntime == 0){// the one which is finished
            p = front;

            
if(front -> link !=null )
            front 
=front ->link;
            
else {printf("\n finished\n");break;}


            p 
-> link = null;

            destroy();

            }

            
else{//else unfinished ,connect last

            rear 
-> link = front;

            p
=front;
            
if(front -> link !=null )
            front 
=front ->link;
            
else {printf("\n finished\n");break;}

            p 
-> link = null
            }

        
         check();
//display all pcbs in queue
       }

     
    }

    
return ;
}


void input()
{
    
int i,num;
    system(
"cls");
    
//clrscr();
    printf("\n 请输入进程数量");
    scanf(
"%d",&num);
    
for( i = 0;i < num; i++){
      printf(
" 进程号No.%d",i);
      p
=getch(PCB);
      printf(
"\n 输入进程名:");
      scanf(
"%s",p->name);
      printf(
"\n 到达时间:");
      scanf(
"%d",&p->rtime);
      printf(
"\n 输入进程运行时间:");
      scanf(
"%d",&p->ntime);
      printf(
"\n");
      p 
-> super =0; p -> state='W';
      p 
-> link = null;
      sort();
//调用函数
    }

//    check();
    return;
}


int space()
{
    
int l=0;
    PCB 
*pr=ready;
    
while(pr != null){
     l
++;
     pr 
= pr->link;
    }

    
return l;

}


void disp(PCB *pr)
{
    printf(
"\n qname\t state \t super \t ntime \t rtime\n");
    printf(
"| %s\t",pr->name);
    printf(
"| %c\t",pr->state);
    printf(
"| %d\t",pr->super);
    printf(
"| %d\t",pr->ntime);
    printf(
"| %d\t",pr->rtime);
    printf(
"\n");
 
return ;
}

void check()//建立进程查看函数
{
//    PCB *pr;
   /*    printf("\n *** 当前正在运行的进程:%s",p->name);显示当前 运行 进程
    disp(p);
*/

    p
=front;
    printf(
"\n ****当前就绪队列状态:%c",p->state);/* 显示队列状态 */ 
    
while( p != null){
     disp(p);
     p
=p->link;
    }

    
return ;
}


void destroy()/*建立进程撤销 函数 (进程 运行结束,撤销进程)*/
{
    printf(
"\n 进程[%s]已完成。\n",p->name);
    free(p);
    
return;
}


/*void running()//进程就绪函数
{
    (p->rtime)++;
    if( p-> rtime == p -> ntime)// 
        destroy();
    else{
      (p->super)--;
      
      p->state = 'W';
      
      sort();
    }
    return;
}
*/

int main()
{
   freopen(
"in.txt","r",stdin);
    
int len,h=0;
//    char ch;
    input();
    len
=space();
    RR();
/*    while((len != 0) && ( ready !=null)){
        ch=getchar();
        h++;
        printf("\n The execute number :%d\n",h);
        p= ready;
        ready = p->link;
        p->link=null;//吧最前的拿出来 
        p->state ='R';
        check();
        running();
        // system("pause");
        printf("\n 按任意键继续.");
        ch=getchar();
        
    }
*/

    printf(
"\n\n进程已经完成。\n");

    
    system(
"pause");
    
return 0;
}

posted on 2008-11-05 20:43 阅读(4201) 评论(5)  编辑 收藏 引用

评论:
# re: 进程调度 实验一 RR 时间片轮转算法 (记录)[未登录] 2008-11-06 09:26 | raof01
标题很吸引人,内容很让人失望。  回复  更多评论
  
# re: 进程调度 实验一 RR 时间片轮转算法 (记录) 2008-11-06 10:01 | PDF阅读器下载
个人觉得代码还是不错的  回复  更多评论
  
# re: 进程调度 实验一 RR 时间片轮转算法 (记录)[未登录] 2008-11-06 19:58 | cdy20
为了应付 老师堂上检查
不过要怎么改都行的 ,毕竟模拟

没有实现 抢占式 也没有实现多级的
有什么想法都可以 交流  回复  更多评论
  
# re: 进程调度 实验一 RR 时间片轮转算法 (记录) 2008-11-12 13:15 | 飘过
用最小堆做个多优先级的也是个不错的选择,不必非用链表,怪麻烦的  回复  更多评论
  
# re: 进程调度 实验一 RR 时间片轮转算法 (记录) 2008-11-12 20:54 | cdy20
@飘过
也是

当练手的,两年没用过指针了。  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理