抢占式优先调度问题

/*用c语言写一个抢占的优先级调度算法? 
需要有的参数要有:进程名、到达时间、运算时间、优先级、状态 
抢占:先比较当前运行进程与就绪链表首项的优先数大小?
如果当前运行进程的优先数小,则将其置于就绪链表首项。
如果当前运行进程的优先数大,则取出链表的前两项,比较其优先数,讨论插入位置。
如果当前运行进程的优先数等于就绪链表首项,则比较二者的到达时间。
	如果当前运行进程的到达时间先于就绪链表首项,则将其置于链表首项位置。 
*/ 
#include<stdio.h>
#include <stdlib.h>        
typedef struct pcb     
{
    
    
    char name[5]; 		//进程名    
    int priority; 		//优先数 ,数值越小,优先级越高       
    int arrivetime; 	//到达时间        
    int runtime; 		//运行时间
    int finishtime;		//完成时间
    int time;			//执行的时间 
    char state; 		//进程状态 "C" 代表完成,"R"代表就绪        
    struct pcb* link;      
}PCB;
PCB *ready = NULL, *p;
void disp(PCB* p);

PCB * sort(PCB * p)       
{
    
    
    PCB *first, *second; 
    int insert = 0;
 	//当链表首位置为空,或者当前进程的优先数小于链表首进程的优先数时 
    if (ready == NULL || (p->priority) < (ready->priority)) 
    {
    
    
        p->link = ready;        
        ready = p;     
    }
    else  if( p->priority==ready->priority) //如果优先级相同,则按时间排序
    {
    
    
        if(p->arrivetime < ready->arrivetime) 
        {
    
    
            p->link=ready;
            ready=p;
        }
        else
        {
    
    
            p->link=ready->link;
            ready->link=p;
        }
    }
    else if(p->priority>ready->priority)
    {
    
    
        first = ready;
        second = first->link;
        while (second != NULL)
        {
    
    
            if (p->priority < second->priority)
            {
    
    
                p->link = second;
                first->link = p;
                    insert = 1;
                    break;
            }
            else if(p->priority==second->priority) 
            {
    
    
                if(p->arrivetime<second->arrivetime)
                {
    
    
                        p->link = second;
                        first->link = p;
                            insert = 1;
                        break;
                }
                else 
                {
    
    
                   p->link=second->link;
                   second->link=p;
                   insert=1;
                   break;
                }
            }
            else if(p->priority>second->priority)
            {
    
    
                first = first->link;
                second = second->link;
            }
        }
        if (insert == 0)  
        {
    
    
            first->link = p;
            p->link = NULL;
        }
    }
    return ready;
}


void input()                 
{
    
    
    int i,j;
    printf("\n请输入进程个数:");
    scanf("%d",&j);                
    for(i=1;i<=j;i++)         
    {
    
       printf("\n第%d个进程\n",i);
        p=(PCB*)malloc(sizeof(PCB));  
        printf("\n输入进程名:");
        scanf("%s",p->name);
        printf("\n输入进程需运行的时间:");
        scanf("%d",&(p->runtime));
        printf("\n输入进程优先数:");
        scanf("%d",&(p->priority));
        printf("\n输入进程的到达时间:");
        scanf("%d",&(p->arrivetime));
        p->state='R';
        p->link=NULL;
        sort(p);           
     }
    printf("按进程的优先数进行排序!\n");
        p=ready;
        while (p != NULL)      
        {
    
    
            disp(p);
            p = p->link;
        }
	printf("\n\n"); 
}

int maxnumber(PCB *p)
{
    
    
    int max;
    max=p->arrivetime;
    while(p->link!=NULL)
    {
    
    
        if(max<p->link->arrivetime)
        {
    
    
            max=p->link->arrivetime;
        }
        p=p->link;
    }
        return(max);
}

void disp(PCB* p)         
{
    
    
    printf("进程名  优先数  到达时间  运行时间  状态\n"); 
    printf("%s\t",p->name); 
    printf("%d\t\t",p->priority); 
    printf("%d\t",p->arrivetime); 
    printf("%d\t",p->runtime); 
    printf("%c\t\n",p->state);
}

void run(PCB *p)
{
    
    
     PCB *s,*q;
    int time=0,a,n=0;
    float sum1=0.0;
    printf("进程的调度开始!\n");
    p=ready;

    s=p;
    a=maxnumber(p);
    while(p!=NULL)
    {
    
    
        if(p->state=='C')
        {
    
    
            break;
        }

        while(s!=NULL)
       {
    
    
            n=n+1;

         s->time=s->runtime;
         s=s->link;
       }
        printf("进程%s运行一次\n",p->name);
        p->runtime--;
        p->priority++;
        time=time+1;
        p->finishtime=time+a;
        disp(p); 
        if(p->runtime==0)                       
        {
    
    
        p->state='C';
        ready=p->link;
        p->link=NULL;
        p=ready;   
        }
        else
        {
    
    
            ready=p->link;
            p->link=NULL;
            p=sort(p);
        } 
    }   
}

int main()
{
    
    
    input();
    p=ready;
    run(p);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Zheng_lan/article/details/109500863
今日推荐