⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpu调度.txt

📁 存储空间调度
💻 TXT
字号:
#include<stdio.h>
#include <dos.h>
#include<stdlib.h>
#include<conio.h>
#define P_NUM 5
#define P_TIME 50
enum state{
    ready,
    execute,
    block,
    finish
};

struct pcb{
    char name[4];
    int priority;
    int cputime;
    int needtime;
    int count;
    int round;
    enum state process;
    struct pcb * next;
};
struct pcb * get_process()
    {
    struct pcb *q;
    struct pcb *t;
    struct pcb *p;
    int i=0;
    printf("input name and time\n");
    while(i<P_NUM)
    {
        q=(struct pcb *)malloc(sizeof(struct pcb));
        scanf("%s%d",&q->name,&q->needtime);

        q->cputime=0;
        q->priority=P_TIME-q->needtime;
        q->process=ready;
        q->next=NULL;
        if (i==0){
        	p=q;
        	t=q;
    	}
        else{
            t->next=q;
        	t=q;
    	}
        i++;
    }
    return p;
}
void  display(struct pcb *p){
    printf("name    cputime    needtime    priority    state\n");
    while(p){
    printf(" %s          %d          %d           %d        ",p->name,p->cputime,p->needtime,p->priority);

        switch(p->process){
            case ready:printf("ready\n");break;
            case execute:printf("execute\n");break;
            case block:printf("block\n");break;
            case finish:printf("finish\n");break;
    	}
        p=p->next;
    }
}

int process_finish(struct pcb *q){
    int bl=1;
    while(bl&&q){
        bl=bl&&q->needtime==0;
        q=q->next;
    }
    return bl;
}

void cpuexe(struct pcb *q){
    struct pcb *t=q;
    int tp=0;
    while(q){
        if (q->process!=finish){
            q->process=ready;
            if(q->needtime==0){
                q->process=finish;
    		}
    	}
        if(tp<q->priority&&q->process!=finish){
            tp=q->priority;
        	t=q;
    	}
        q=q->next;
    }
    if(t->needtime!=0){
        t->priority-=3;
        t->needtime--;
        t->process=execute;
        t->cputime++;
    }
}

void priority_cal()
   {    int cpu;
    struct pcb * p;
    p= get_process();
    cpu=0;

    while(!process_finish(p)){
        cpu++;
        printf("cputime: %d\n ",cpu);
        cpuexe(p);
        display(p);


    }
    printf("All processes have finished,press any key to exit");
    getch();
}

void display_menu(){
    printf("CHOOSE THE ALGORITHM:\n");
    printf("1 PRIORITY\n");
    printf("2 ROUNDROBIN\n");
    printf("3 EXIT\n");
}
struct pcb * get_process_round(){
struct  pcb *q;
struct  pcb *t;
struct  pcb *p;
    int i=0;
    printf("input name and time\n");

    while (i<P_NUM){
        q=(struct pcb *)malloc(sizeof(struct pcb));
        scanf("%s",&q->name);
        scanf("%d",&q->needtime);
        q->cputime=0;
        q->round=0;
        q->count=0;
        q->process=ready;
        q->next=NULL;
        if (i==0){
        	p=q;
        	t=q;
    	}
        else{
            t->next=q;
        	t=q;
    	}
        i++;
    }
    return p;
}

void cpu_round(struct pcb *q){
    q->cputime+=2;
    q->needtime-=2;
    if(q->needtime<0) {
        q->needtime=0;
    }
    q->count++;
    q->round++;
    q->process=execute;

}

struct pcb * get_next(struct pcb * k,struct pcb * head){
    struct pcb * t;
    t=k;
    do{
     t=t->next;
    }
    while (t && t->process==finish);


    if(t==NULL){
        t=head;

        while (t->next!=k && t->process==finish){
            t=t->next;
    	}
    }
    return t;
}
void set_state(struct pcb *p){
    while(p){
        if (p->needtime==0){
            p->process=finish;

    	}
        if (p->process==execute){
            p->process=ready;
    	}
        p=p->next;
    }
}
void display_round(struct pcb *p){
    printf("NAME  CPUTIME  NEEDTIME  COUNT  ROUND  STATE\n");
    while(p){
    printf(" %s        %d        %d       %d      %d     ", p->name,p->cputime,p->needtime,p->count,p->round);

        switch(p->process){
            case ready:printf("ready\n");break;
            case execute:printf("execute\n");break;
            case finish:printf("finish\n");break;
    	}
        p=p->next;
    }
}

void round_cal(){
       int cpu;
    struct pcb * p;
    struct  pcb * r;

    p=get_process_round();
    cpu=0;

    r=p;
    while(!process_finish(p)){
        cpu+=2;
        cpu_round(r);
        r=get_next(r,p);
        printf("cpu   %d \n",cpu);
        display_round(p);
        set_state(p);


    }
    getch();
}

void main(){
    int k;
    display_menu();

    scanf("%d",&k);
    switch(k){
            case 1:priority_cal();break;
            case 2:round_cal();break;
            case 3:break;

    }
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -