進程調度函數程序


進程調度源程序(jingchendiaodu.cpp ):  

#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;  
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;  
    }  
}  
void input() /* 建立進程控制塊函數*/  
{  
    int i,num;  
    //clrscr(); /*清屏*/  
    printf("\n 請輸入進程數?");  
    scanf("%d",&num);  
    for(i=1;i<=num;i++)  
    {  
        printf("\n 進程號No.%d:\n",i);  
        p=getpch(PCB);  
        printf("\n 輸入進程名:");  
        scanf("%s",p->name);  
        printf("\n 輸入進程優先數:");  
        scanf("%d",&p->super);  
        printf("\n 輸入進程運行時間:");  
        scanf("%d",&p->ntime);  
        printf("\n");  
        p->rtime=0;p->state='w';  
        p->link=NULL;  
        sort(); /* 調用sort函數*/  
    }  
}  
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 ndtime \t runtime \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");  
} 
void check() /* 建立進程查看函數 */  
{  
    PCB* pr;  
    printf("\n **** 當前正在運行的進程是:%s",p->name); /*顯示當前運行進程*/  
    disp(p);  
    pr=ready;  
    printf("\n ****當前就緒隊列狀態為:\n"); /*顯示就緒隊列狀態*/  
    while(pr!=NULL)  
    {  
        disp(pr);  
        pr=pr->link;  
    }  
}  
void destroy() /*建立進程撤消函數(進程運行結束,撤消進程)*/  
{  
    printf("\n 進程 [%s] 已完成.\n",p->name);  
    free(p);  
}  
void running() /* 建立進程就緒函數(進程運行時間到,置就緒狀態*/  
{  
    (p->rtime)++;  
    if(p->rtime==p->ntime)  
        destroy(); /* 調用destroy函數*/  
    else  
    {  
        (p->super)--;  
        p->state='w';  
        sort(); /*調用sort函數*/  
    }  
}  
void main() /*主函數*/  
{  
    int len,h=0;  
    char ch;  
    input();  
    len=space();  
    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();  
        printf("\n 按任一鍵繼續......");  
        ch=getchar();  
    }  
    printf("\n\n 進程已經完成.\n");  
    ch=getchar();  
}  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM