操作系统3.0

//优先级调度算法 
#include <stdio.h> 
#include <stdlib.h>  
#include <conio.h>   
#define getpch(type) (type*)malloc(sizeof(type))  
#define NULL 0     
struct pcb 
{ 
    /* 定义进程控制块PCB */   
    char name[10];   
    char state;   
    int super;   
    int ntime;   
    int rtime;    
    struct pcb* link;  
}*ready=NULL,*p;     
typedef struct pcb PCB;

int num=0;



void sort() /* 建立对进程进行优先级排列函数*/  
{    
    PCB *first, *second;   
    int insert=0;    
    if((ready==NULL)||((p->super)>(ready->super))) /*优先级最大者,插入队首*/   
    {     
        p->link=ready;    
        ready=p;   
    }    
    else /* 进程比较优先级,插入适当的位置中*/   
    {     
        first=ready;     
        second=first->link;    
        while(second!=NULL)    
        {      
            if((p->super)>(second->super)) /*若插入进程比当前进程优先数大,*/     
            { 
                /*插入到当前进程前面*/      
                p->link=second;      
                first->link=p;      
                second=NULL;      
                insert=1;     
            }
            else /* 插入进程优先数最低,则插入到队尾*/ 
            {       
                first=first->link;      
                second=second->link;    
            }   
        }   
        if(insert==0) first->link=p; 
    }
}     

  

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

void disp(PCB * pr) /*建立进程显示函数,用于显示当前进程*/  
{   
    printf("
	进程名	状态	优先数	需要运行时间	已经运行时间
");
    printf("	%s",pr->name);  
    printf("	%c",pr->state); 
    printf("	%d",pr->super);  
    printf("	%d",pr->ntime);
    printf("		%d",pr->rtime);
    printf("
"); 
}     


void check() /* 建立进程查看函数 */  
{  
    PCB* pr;  
    printf("
	**** 当前正在运行的进程是:
"); /*显示当前运行进程*/  
    disp(p); 
    pr=ready;  
    printf("
	**** 当前就绪队列状态为:
"); /*显示就绪队列状态*/  
    while(pr!=NULL)   
    {   
        disp(pr);   
        pr=pr->link;  
    } 
} 

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

void running() /* 建立进程就绪函数(进程运行时间到,置就绪状态*/  
{    
    (p->rtime)++;  
    if(p->rtime==p->ntime) 
        destroy(); /* 调用destroy函数*/ 
    else   
    {
        if(p->super>0)
            (p->super)--;   
        p->state='W';   
        sort(); /*调用sort函数*/  
    } 
} 

//手动输入模拟函数
void manuallyEnter(){

    int i;    
    system("cls"); /*清屏*/   
    printf("
	请输入进程数: "); 
    scanf("%d",&num);
    printf("
");
    for(i=1;i<=num;i++) 
    {  
        printf("	第%d个进程:
",i);  
        p=getpch(PCB);
        printf("	输入进程名:");   
        scanf("%s",p->name);   
        printf("	输入进程优先数:");  
        scanf("%d",&p->super);  
        printf("	输入进程运行时间:");   
        scanf("%d",&p->ntime);  
        printf("
");
        p->rtime=0;
        p->state='W';  
        p->link=NULL;    
        sort(); /* 调用sort函数*/   
    }  

}


//读取文件
void readFile()
{
    //读取文件
    FILE *fp = fopen("pcb.txt","r");
    if(fp == NULL)
    {
        printf("File open error!
");
        exit(0);
    }
    while(!feof(fp)&&fgetc(fp)!=EOF)
    {
        fseek(fp,-1L,SEEK_CUR);
        fscanf(fp,"%s%d%d",&p->name,&p->super,&p->ntime);
        num++;
    }
    fclose(fp);

}

//选择数据的获取方式
void dataAccess()
{
    int k;
    printf("

	**************************************
");
    printf("		1.调用文本写入数据
");
    printf("		2.调用手动输入模拟数据
");
    printf("	**************************************


");
    printf("	请选择菜单项: ");
    scanf("%d",&k);
    switch(k){
    case 1:
        readFile();
        break;
    case 2:
        manuallyEnter();
        break;
    default:
        printf("

	**请输出1或者2进行选择");
        dataAccess();
        break;
    }
}  

void output()
{
    int len,h=0;   
    char ch; 
    len=space();  
    while((len!=0)&&(ready!=NULL)) 
    {    
        ch=getchar(); 
        h++;   
        printf("	-----------------------------------------------------
");   
        printf("	现在是第%d次运行:
",h); 
        p=ready;  
        ready=p->link;   
        p->link=NULL;   
        p->state='R';   
        check();  
        running();   
        printf("
	按任意键继续......
");  
    }  
}



int main() /*主函数*/ 
{
    dataAccess();
    output();
    printf("

	进程已经完成.
"); 
}
原文地址:https://www.cnblogs.com/wangzekai/p/5487446.html