操作系统第三次作业

#include "stdio.h" 
#include <stdlib.h> 
#include <conio.h> 
#define getpch(type) (type*)malloc(sizeof(type)) 
#define N 3
int count;
sort2();
struct pcb { /* 定义进程控制块PCB */ 
       char name[10]; 
       char status; 
       int prio; 
       int ntime; 
       int rtime; 
       struct pcb* link; 
}*ready=NULL,*p; 

typedef struct pcb PCB; 
  
struct pcb2 { /* 定义进程控制块PCB2 */ 
       char name[10]; 
       char status; 
       int prio;
       int atime;
       int ntime; 
       int runtime;
       int restime;
}pcb[24]; 
input2() /* 建立进程控制块函数*/ 
{ 
  int i,num; 
 
  printf("\n 请输入进程数:"); 
  scanf("%d",&num);
  count=num;
  for(i=0;i<num;i++) 
  { 
    printf("\n 进程号No.%d:\n",i); 
    printf("\n 输入进程名:"); 
    scanf("%s",pcb[i].name); 
    printf("\n 输入进程到达时间:"); 
    scanf("%d",&pcb[i].atime); 
    
    printf("\n 输入进程运行时间:"); 
    scanf("%d",&pcb[i].ntime); 
    printf("\n"); 
    pcb[i].runtime=0;
    pcb[i].status='r'; 
    pcb[i].restime=pcb[i].ntime;
  
  }
  sort2();
  printf("\n\n--------------FCFS排序之后-----------------\n");
  printf("进程名  到达时间  需要运行时间\n");
  for(i=0;i<num;i++)
  {
     printf("%s  %d  %d \n",pcb[i].name,pcb[i].atime,pcb[i].ntime);
  }

} 

sort2()
{
    
    int i,j;
    struct pcb2 t;
    for(i=0;i<count-1;i++) //按进程到达时间的先后排序
    {                               //如果两个进程同时到达,按在屏幕先输入的先运行
        for(j=i+1;j<count;j++)
        { 
            if(pcb[j].atime< pcb[i].atime)
            {
                t=pcb[j];
                pcb[j]=pcb[i];
                pcb[i]=t;
            }

        }
    }
}
  
sort() /* 进程进行优先级排列函数*/ 
{ 
  PCB *first, *second; 
  int insert=0; 
  if((ready==NULL)||((p->prio)>(ready->prio))) /*优先级最大者,插入队首*/ 
  { 
    p->link=ready; 
    ready=p; 
  } 
  else /* 进程比较优先级,插入适当的位置中*/ 
  { 
    first=ready; 
    second=first->link; 
    while(second!=NULL) 
    { 
      if((p->prio)>(second->prio)) /*若插入进程比当前进程优先数大,*/ 
      { /*插入到当前进程前面*/ 
        p->link=second; 
        first->link=p; 
        second=NULL; 
        insert=1; 
      } 
      else /* 插入进程优先数最低,则插入到队尾*/ 
      { 
        first=first->link; 
        second=second->link; 
      } 
    } 
    if(insert==0) first->link=p; 
  } 
} 
 
input() /* 建立进程控制块函数*/ 
{ 
  int i,num; 
  /*clrscr();  */   /*清屏*/
  printf("\n 请输入进程数:"); 
  scanf("%d",&num); 
  for(i=0;i<num;i++) 
  { 
    printf("\n 进程号No.%d:\n",i); 
    p=getpch(PCB);  /*宏(type*)malloc(sizeof(type)) */
    printf("\n 输入进程名:"); 
    scanf("%s",p->name); 
    /*printf("\n 输入进程优先数:"); 
    scanf("%d",&p->prio); */
    p->prio=N;
    printf("\n 输入进程运行时间:"); 
    scanf("%d",&p->ntime); 
    printf("\n"); 
    p->rtime=0;p->status='r'; 
    p->link=NULL; 
    sort(); /* 调用sort函数*/ 
  } 

} 

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

disp(PCB * pr) /*单个进程显示函数*/ 
{  
  printf("|%s\t",pr->name); 
  printf("|%c\t",pr->status); 
  printf("|%d\t",pr->prio); 
  printf("|%d\t",pr->ntime); 
  printf("|%d\t",pr->rtime); 
  printf("\n"); 
} 

void printbyprio(int prio)
{
  PCB* pr; 
  pr=ready; 
  printf("\n ****当前第%d级队列(优先数为%d)的就绪进程有:\n",(N+1)-prio,prio); /*显示就绪队列状态*/ 
  printf("\n qname \tstatus\t prio \tndtime\t runtime \n"); 
  while(pr!=NULL) 
  { 
    if (pr->prio==prio) disp(pr); 
    pr=pr->link; 
  } 
}

check() /* 显示所有进程状态函数 */ 
{ 
  PCB* pr; 
  int i;
  printf("\n /\\/\\/\\/\\当前正在运行的进程是:%s",p->name); /*显示当前运行进程*/ 
   printf("\n qname \tstatus\t prio \tndtime\t runtime \n"); 
  disp(p); 
  
  printf("\n 当前就绪队列状态为:\n"); /*显示就绪队列状态*/ 
  for(i=N;i>=1;i--)
    printbyprio(i);
  /*
  while(pr!=NULL) 
  { 
    disp(pr); 
    pr=pr->link; 
    } 
  */
} 

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

running() /* 运行函数。判断是否完成,完成则撤销,否则置就绪状态并插入就绪队列*/ 
{ 
  int slice,i;
  slice=1;
  for(i=1;i<((N+1)-p->prio);i++)
    slice=slice*2;
    
  for(i=1;i<=slice;i++)
  {
     (p->rtime)++; 
     if (p->rtime==p->ntime)
       break;
       
  }
  if(p->rtime==p->ntime) 
      destroy(); /* 调用destroy函数*/ 
  else 
  { 
    if(p->prio>1) (p->prio)--; 
    p->status='r'; 
    sort(); /*调用sort函数*/ 
  } 
} 

void cteatpdisp()
/*显示(运行过程中)增加新进程后,所有就绪队列中的进程*/
{ 
 
  int i;
   
  printf("\n 当增加新进程后,所有就绪队列中的进程(此时无运行进程):\n"); /*显示就绪队列状态*/ 
  for(i=N;i>=1;i--)
    printbyprio(i);
}
void creatp()
{
     char temp;
     printf("\nCreat one  more process?type Y (yes)");
     scanf("%c",&temp);
     if (temp=='y'||temp=='Y')
     {
        input();
        cteatpdisp();
     }    
}

PRIO()//最高优先数优先调度算法
{
  int len,h=0; 
  char ch; 
  input(); 
  len=space(); 
  while((len!=0)&&(ready!=NULL)) 
  { 
    ch=getchar(); 
    /*getchar();*/
    h++; 
    printf("\n The execute number:%d \n",h); 
    p=ready; 
    ready=p->link; 
    p->link=NULL; 
    p->status='R'; 
    check(); 
    running(); 
    creatp();
    printf("\n 按任一键继续......"); 
    ch=getchar(); 
  } 
  printf("\n\n 进程已经完成.\n"); 
  ch=getchar(); 
  ch=getchar();
}
QueueSort()
{
    int i;
    struct pcb2 t;
    t=pcb[0];
    for(i=1;i<count;i++)
        pcb[i-1]=pcb[i];
    pcb[0].restime--;
    pcb[count-1]=t;

}
QueueSort1()
{
    int i;

    for(i=1;i<count;i++)
        pcb[i-1]=pcb[i];
    count--;

}
RR()//时间片轮转法调度算法
{
    int timeflag=0;
    int timepiece=2;
    int T;
    printf("\n请输入时间片:");
    scanf("%d",&T);
    int k;
    char ch;
    input2();
    sort2();
    while(count>=0)
    {
        if(timeflag==T)
        {
            timeflag=0;
            if(pcb[0].restime==0)
            {
            printf("进程%s已完成\n",pcb[0].name);
            

            if(count!=0){
                QueueSort1();
                printf("当前正在运行进程是:%s\n",pcb[0].name);
            }
            if(count>=1)
                for(k=1;k<count;k++)
                printf("进程%s正在等待\n",pcb[k].name);
            if(count==0){
                pcb[0].restime--;
                count--;
            }
            }
            else{
                QueueSort();
                if(count!=0){
                //QueueSort1();
                printf("当前正在运行进程是:%s\n",pcb[0].name);
                }
                if(count>=1)
                for(k=1;k<count;k++)
                printf("进程%s正在等待\n",pcb[k].name);
            }              
        }
        else{
            if(pcb[0].restime==0)
            {
            printf("进程%s已完成\n",pcb[0].name);
            if(count!=0){
                QueueSort1();
                printf("进程%s正在运行\n",pcb[0].name);
            }
            if(count>=1)
                for(k=1;k<count;k++)
                printf("进程%s正在等待\n",pcb[k].name);
            }
            else{
                pcb[0].restime--;
                if(count!=0)
                printf("进程%s正在运行\n",pcb[0].name);
            
                if(count>=1)
                for(k=1;k<count;k++)
                printf("进程%s正在等待\n",pcb[k].name);
            }
        }
        timeflag++;
        printf("\n 按任一键继续......"); 
        ch=getchar(); 
        ch=getchar();
    }
     printf("\n\n 全部进程已经完成.\n"); 
}
     
main() /*主函数*/ 
{ 
  int select;
  printf("-------------------请选择进程调度算法----------------------\n");
  printf("0:退出\n1:采用最高优先数优先调度算法\n2:采用基于时间片轮转法调度算法\n");
  printf("请选择:");
  scanf("%d",&select);
  if(select==1)
  {
    PRIO();
  }
  else if(select==2)
  {
    RR();
  }
  else if(select==0)
  {
    exit(0);
  }
  
}

posted on 2016-05-12 20:52  17郑培轩  阅读(294)  评论(0编辑  收藏  举报