实验五

#include<stdio.h>
#include<stdlib.h> 
#include<stdafx.h>
typedef struct PCB      //定义进程控制块 
{     
    char ID[3];   //进程号  
    char name[10];   //进程名  
    char state;   //运行状态  
    int  arrivetime;  //到达时间  
    int  starttime;  //进程开始时间  
    int  finishtime;  //进程结束时间  
    int  servicetime;   //服务时间  
    float turnaroundtime;//周转时间   
    float weightedturnaroundtime;//带权周转时间  
    struct PCB *next;  //指向下个进程 
}pcb;    
int time;   //计时器 
int n;    //进程个数               
pcb *head=NULL,*p,*q; //进程链表指针  
void run_fcfs(pcb *p1) //运行未完成的进程 
{   
    time = p1->arrivetime > time? p1->arrivetime:time;   
    p1->starttime=time;   
    printf("
现在时间是%d,开始运行作业%s
",time,p1->name);  
    time+=p1->servicetime;  
    p1->state='T';   
    p1->finishtime=time;   
    p1->turnaroundtime=p1->finishtime-p1->arrivetime;   
    p1->weightedturnaroundtime=p1->turnaroundtime/p1->servicetime;   
    printf("ID  到达时间 开始时间 服务时间 完成时间 周转时间 带权周转时间 
");   
    printf("%s%6d%10d%10d%8d%10.1f%10.2f
",p1->ID,p1->arrivetime,p1->starttime,p1->servicetime,p1->finishtime,p1->turnaroundtime,p1->weightedturnaroundtime); 
}   
void fcfs() //找到当前未完成的进程 
{    
    int i,j;    
    p=head;    
    for(i=0;i<n;i++)   
  {         
        if(p->state=='F')    
        {      
            q=p;    //标记当前未完成的进程     
            run_fcfs(q);    
        }    
        p=p->next;   
    } 
}    
void getInfo()          //获得进程信息并创建进程 
{   
    int num;   
    printf("
作业个数:");  
    scanf("%d",&n);     
    for(num=0;num<n;num++)  
    {    
        p=(pcb *)malloc(sizeof(pcb));    
        printf("依次输入:
ID 进程名 到达时间 服务时间
");    
        scanf("%s	%s	%d	%d",&p->ID,&p->name,&p->arrivetime,&p->servicetime);   
        if(head==NULL) {head=p;q=p;time=p->arrivetime;}   
        if(p->arrivetime < time) time=p->arrivetime;   
        q->next=p;   
        p->starttime=0;   
        p->finishtime=0;    
        p->turnaroundtime=0;    
        p->weightedturnaroundtime=0;  
        p->next=NULL;   
        p->state='F';   
        q=p;          } 
}    
void main()  
{   
    printf("先来先服务算法模拟");  
    getInfo();  
    p=head;  fcfs(); 
}

实验总结

通过编写代码更加了解操作系统存储管理原理。

原文地址:https://www.cnblogs.com/zzen/p/4628790.html