c语言调度模拟,调度算法模拟

发布时间:2022-12-01

本文目录一览:

  1. 多级队列调度算法的模拟,用C语言实现
  2. 用C语言编写并调试一个模拟的进程调度程序,采用“简单时间片轮转法”调度算法对五个进程进行调度。
  3. 用C语言完成进程调度算法的模拟
  4. 有c语言模拟调度算法吗?
  5. 用C语言编程模拟处理机调度(实现一种算法)

多级队列调度算法的模拟,用C语言实现

多级反馈队列调度算法是一种CPU处理机调度算法,UNIX操作系统采取的便是这种调度算法。多级反馈队列调度算法即能使高优先级的作业得到响应又能使短作业(进程)迅速完成。(对比一下FCFS与高优先响应比调度算法的缺陷)。多级(假设为N级)反馈队列调度算法可以如下原理:

  1. 设有N个队列(Q1,Q2....QN),其中各个队列对于处理机的优先级是不一样的,也就是说位于各个队列中的作业(进程)的优先级也是不一样的。一般来说,优先级Priority(Q1) > Priority(Q2) > ... > Priority(QN)。怎么讲,位于Q1中的任何一个作业(进程)都要比Q2中的任何一个作业(进程)相对于CPU的优先级要高(也就是说,Q1中的作业一定要比Q2中的作业先被处理机调度),依次类推其它的队列。
  2. 对于某个特定的队列来说,里面是遵循时间片轮转法。也就是说,位于队列Q2中有N个作业,它们的运行时间是通过Q2这个队列所设定的时间片来确定的(为了便于理解,我们也可以认为特定队列中的作业的优先级是按照FCFS来调度的)。
  3. 各个队列的时间片是一样的吗?不一样,这就是该算法设计的精妙之处。各个队列的时间片是随着优先级的增加而减少的,也就是说,优先级越高的队列中它的时间片就越短。同时,为了便于那些超大作业的完成,最后一个队列QN(优先级最高的队列)的时间片一般很大(不需要考虑这个问题)。 多级反馈队列调度算法描述:
  4. 进程在进入待调度的队列等待时,首先进入优先级最高的Q1等待。
  5. 首先调度优先级高的队列中的进程。若高优先级中队列中已没有调度的进程,则调度次优先级队列中的进程。例如:Q1,Q2,Q3三个队列,只有在Q1中没有进程等待时才去调度Q2,同理,只有Q1,Q2都为空时才会去调度Q3。
  6. 对于同一个队列中的各个进程,按照时间片轮转法调度。比如Q1队列的时间片为N,那么Q1中的作业在经历了N个时间片后若还没有完成,则进入Q2队列等待,若Q2的时间片用完后作业还不能完成,一直进入下一级队列,直至完成。
  7. 在低优先级的队列中的进程在运行时,又有新到达的作业,那么在运行完这个时间片后,CPU马上分配给新到达的作业(抢占式)。 我们来看一下该算法是如何运作的: 假设系统中有3个反馈队列Q1,Q2,Q3,时间片分别为2,4,8。现在有3个作业J1,J2,J3分别在时间 0 ,1,3时刻到达。而它们所需要的CPU时间分别是3,2,1个时间片。
  8. 时刻0 J1到达。于是进入到队列1 , 运行1个时间片 , 时间片还未到,此时J2到达。
  9. 时刻1 J2到达。由于时间片仍然由J1掌控,于是等待。J1在运行了1个时间片后,已经完成了在Q1中的 2个时间片的限制,于是J1置于Q2等待被调度。现在处理机分配给J2。
  10. 时刻2 J1进入Q2等待调度,J2获得CPU开始运行。
  11. 时刻3 J3到达,由于J2的时间片未到,故J3在Q1等待调度,J1也在Q2等待调度。
  12. 时刻4 J2处理完成,由于J3,J1都在等待调度,但是J3所在的队列比J1所在的队列的优先级要高,于是J3被调度,J1继续在Q2等待。
  13. 时刻5 J3经过1个时间片,完成。
  14. 时刻6 由于Q1已经空闲,于是开始调度Q2中的作业,则J1得到处理器开始运行。
  15. 时刻7 J1再经过一个时间片,完成了任务。于是整个调度过程结束。

用C语言编写并调试一个模拟的进程调度程序,采用“简单时间片轮转法”调度算法对五个进程进行调度。

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
struct PCB {
    char NAME[10]; /*进程名*/
    int ROUND; /*进程轮转时间片*/
    int REACHTIME; /*进程到达时间*/
    int CPUTIME; /*进程占用CPU时间*/
    int COUNT; /*计数器*/
    int NEEDTIME; /*进程完成还要的CPU时间*/
    char STATE; /*进程的状态*/
    struct PCB *NEXT; /*链指针*/
};
struct LINK { /*PCB的链结构*/
    struct PCB *RUN; /*当前运行进程指针*/
    struct PCB *READY; /*就绪队列头指针*/
    struct PCB *TAIL; /*就绪队列尾指针*/
    struct PCB *FINISH; /*完成队列头指针*/
};
void INIT(LINK *); /*对PCB的链结构初始化*/
void INSERT(LINK *); /*将执行了一个单位时间片数且还未完成的进程的PCB插到就绪队列的队尾*/
void FIRSTIN(LINK *); /*将就绪队列中的第一个进程投入运行*/
void PRINT(LINK *); /*打印每执行一个时间片后的所有进程的状态*/
void PR(PCB *); /*打印一个进程的状态*/
int CREATE(LINK *,int); /*创建新的进程*/
void ROUNDSCH(LINK *); /*按时间片轮转法调度进程*/
void main() {
    LINK pcbs;
    int i;
    INIT(pcbs);
    i=0;
    printf("创建5个进程\n\n");
    while(i<5) {
        if(CREATE(pcbs,i+1)==1) {
            printf("进程已创建\n\n");
            i++;
        }
        else
            printf("进程创建失败\n\n");
    }
    FIRSTIN(pcbs);
    ROUNDSCH(pcbs);
}
void ROUNDSCH(LINK *p) {
    PCB *pcb;
    while(p->RUN!=NULL) {
        pcb=(PCB *)malloc(sizeof(PCB));
        strcpy(pcb->NAME,p->RUN->NAME);
        pcb->ROUND=p->RUN->ROUND;
        pcb->REACHTIME=p->RUN->REACHTIME;
        pcb->CPUTIME=p->RUN->CPUTIME;
        pcb->COUNT=p->RUN->COUNT;
        pcb->NEEDTIME=p->RUN->NEEDTIME;
        pcb->STATE=p->RUN->STATE;
        pcb->NEXT=p->RUN->NEXT;
        pcb->CPUTIME++;
        pcb->NEEDTIME--;
        pcb->COUNT++;
        if(pcb->NEEDTIME==0) {
            pcb->NEXT=p->FINISH->NEXT;
            p->FINISH->NEXT=pcb;
            pcb->STATE='F';
            p->RUN=NULL;
            if(p->READY!=p->TAIL)
                FIRSTIN(p);
        }
        else {
            p->RUN=pcb;
            if(pcb->COUNT==pcb->ROUND) {
                pcb->COUNT=0;
                if(p->READY!=p->TAIL) {
                    pcb->STATE='W';
                    INSERT(p);
                    FIRSTIN(p);
                }
            }
        }
        PRINT(p);
    }
}
void INIT(LINK *p) {
    p->RUN=NULL;
    p->TAIL=p->READY=(PCB *)malloc(sizeof(PCB));
    p->READY->NEXT=NULL;
    p->FINISH=(PCB *)malloc(sizeof(PCB));
    p->FINISH->NEXT=NULL;
}
int CREATE(LINK *p,int n) {
    PCB *pcb,*q;
    pcb=(PCB *)malloc(sizeof(PCB));
    flushall();
    printf("请输入第%d个进程的名称:\n",n);
    gets(pcb->NAME);
    printf("请输入第%d个进程的轮转时间片数:\n",n);
    scanf("%d",&(pcb->ROUND));
    printf("请输入第%d个进程的到达时间:\n",n);
    scanf("%d",&(pcb->REACHTIME));
    pcb->CPUTIME=0;
    pcb->COUNT=0;
    printf("请输入第%d个进程需运行的时间片数:\n",n);
    scanf("%d",&(pcb->NEEDTIME));
    pcb->STATE='W';
    pcb->NEXT=NULL;
    if(strcmp(pcb->NAME,"")==0||pcb->ROUND<=0||pcb->NEEDTIME<=0) /*输入错误*/
        return 0;
    q=p->READY;
    while(q->NEXT!=NULL && q->NEXT->REACHTIME<=pcb->REACHTIME)
        q=q->NEXT;
    pcb->NEXT=q->NEXT;
    q->NEXT=pcb;
    if(pcb->NEXT==NULL)
        p->TAIL=pcb;
    return 1;
}
void FIRSTIN(LINK *p) {
    PCB *q;
    q=p->READY->NEXT;
    p->READY->NEXT=q->NEXT;
    q->NEXT=NULL;
    if(p->READY->NEXT==NULL)
        p->TAIL=p->READY;
    q->STATE='R';
    p->RUN=q;
}
void INSERT(LINK *p) {
    PCB *pcb;
    pcb=(PCB *)malloc(sizeof(PCB));
    strcpy(pcb->NAME,p->RUN->NAME);
    pcb->ROUND=p->RUN->ROUND;
    pcb->REACHTIME=p->RUN->REACHTIME;
    pcb->CPUTIME=p->RUN->CPUTIME;
    pcb->COUNT=p->RUN->COUNT;
    pcb->NEEDTIME=p->RUN->NEEDTIME;
    pcb->STATE=p->RUN->STATE;
    pcb->NEXT=p->RUN->NEXT;
    p->TAIL->NEXT=pcb;
    p->TAIL=pcb;
    p->RUN=NULL;
    pcb->STATE='W';
}
void PRINT(LINK *p) {
    PCB *pcb;
    printf("执行一个时间片后的所有进程的状态:\n\n");
    if(p->RUN!=NULL)
        PR(p->RUN);
    if(p->READY!=p->TAIL) {
        pcb=p->READY->NEXT;
        while(pcb!=NULL) {
            PR(pcb);
            pcb=pcb->NEXT;
        }
    }
    pcb=p->FINISH->NEXT;
    while(pcb!=NULL) {
        PR(pcb);
        pcb=pcb->NEXT;
    }
}
void PR(PCB *p) {
    printf("进程名:%s\n",p->NAME);
    printf("进程轮转时间片:%d\n",p->ROUND);
    printf("进程到达时间:%d\n",p->REACHTIME);
    printf("进程占用CPU时间:%d\n",p->CPUTIME);
    printf("计数器:%d\n",p->COUNT);
    printf("进程完成还要的CPU时间:%d\n",p->NEEDTIME);
    printf("进程的状态:%c\n\n",p->STATE);
}

用C语言完成进程调度算法的模拟

运。)法C片优故队进队这多 行队3个P程行一的 获间入入系那业)调的J配到队 理要J。能J间不对列N使下,达法Q间特何先间由,调中先)在么的于问短馈 以由度优。定6去算度片度、级由,中各先多队先 中各。行3个P程行一的 获间入入系那业)调的J配到队 理要J。能J间不对列N使下,达法Q间特何先间由,调中先)在么的于问短馈 以由度优。定6去算度片度、级由,中各先多队先

有c语言模拟调度算法吗?

/*本c程序为按优先数的进程调度*/
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct pcb)
struct pcb
{
    int pid;
    struct pcb *next;
    int time;
    int priority;
    char status;
};
int n,m;
struct pcb *head,*runpcb;
main()
{
    struct pcb *pcbi;
    char str;
    struct pcb *cshlist();
    void insertproc(struct pcb *insproc);
    void pintlist(struct pcb *L);
    void execproc();
    void delproc();
    m=0;
    head=cshlist();
    printf("first linelist is:\n");
    pintlist(head);
    while(head!=NULL)
    {
        scanf("%c",&str);
        runpcb=head;
        head=runpcb->next;
        runpcb->status='Z';
        printf("output linelist is:\n");
        printf("%d\n",m);
        printf("%2d %5d %5d %3c\n",runpcb->pid,runpcb->time,runpcb->priority,runpcb->status);
        pintlist(head);
        printf("\n");
        printf("\n");
        execproc();
        m=m+1;
        if(runpcb->time==0)
            delproc();
        else insertproc(runpcb);
    }
}
void pintlist(struct pcb *L)
{
    struct pcb *p;
    int i;
    p=L;
    if(L!=NULL)
        do
        {
            printf("%2d %5d %5d %3c\n",p->pid,p->time,p->priority,p->status);
            p=p->next;
        }while (p!=NULL);
}
struct pcb *cshlist()
{
    struct pcb *ql;
    n=0;
    ql=(struct pcb *)malloc(LEN);
    ql->pid=n+1;
    ql->status='R';
    printf("enter time and priority:\n");
    scanf("%ld,%d",&ql->time,&ql->priority);
    head=NULL;
    while(ql->time!=0)
    {
        n=n+1;
        insertproc(ql);
        ql=(struct pcb *)malloc(LEN);
        printf("enter timeand priority:\n");
        ql->pid=n+1;
        ql->status='R';
    }
    return(head);
}
void insertproc(struct pcb *insproc)
{
    struct pcb *p0,*p1,*p2;
    int pri;
    p1=head;
    p0=insproc;
    if(head==NULL)
    {
        head=p0;
        p0->next=NULL;
    }
    else
    {
        pri=p0->priority;
        if(p1->priority<=pri)
        {
            p0->next=head;
            head=insproc;
        }
        else
        {
            while((p1->next!=NULL)&&(p1->priority<pri))
            {
                p2=p1;
                p1=p1->next;
            }
            if((p1->next!=NULL)||(p1->priority==pri))
            {
                p2->next=p0;
                p0->next=p1;
            }
            else
            {
                p1->next=p0;
                p0->next=NULL;
            }
        }
    }
}
void execproc()
{
    runpcb->time=runpcb->time-1;
    runpcb->priority=runpcb->priority-1;
}
void delproc()
{
    struct pcb *p;
    p=runpcb;
    p->status='E';
    printf("process P");
    printf("%d",p->pid);
    printf(" is finish\n");
    printf("\n");
    free(runpcb);
}

优先数调度算法方面和时间片轮转调度算法(再给你个c++的)

#include<iostream>
#include<string>
#include<time.h>
using namespace std;
int n;
class PCB
{
public:
    int pri;//进程优先数
    int runtime;//进程运行CPU时间
    int pieceOftime;//轮转时间片
    string procname;//进程名
    string state;//进程状态
    int needOftime;//还需要时间
    int Counter;
    PCB * next;
};
PCB * run = NULL;
PCB * ready = NULL;
PCB * finish = NULL;
PCB * tial = ready;
void Dtime(int t);
void Prinft(int a)
{
    if(a==1)
    {
        cout<<"进程名称"<<"\t"<<"优先数"<<"\t"<<"还需要时间"<<"\t"<<"已运行时间"<<"\t"<<"状态:"<<endl;
    }
    else
        cout<<"进程名称"<<"\t"<<"已运行时间"<<"\t"<<"还需要时间"<<"\t"<<"计数器"<<"\t"<<"时间片"<<"\t"<<"状态"<<endl;
}
void Prinft(int b,PCB * p)
{
    if(b==1)
    {
        cout<<p->procname<<"\t\t"<<p->pri<<"\t"<<p->needOftime<<"\t\t"<<p->runtime<<"\t\t"<<p->state<<endl;
    }
    else
        cout<<p->procname<<"\t\t"<<p->runtime<<"\t\t"<<p->needOftime<<"\t\t"<<p->Counter<<"\t"<<p->pieceOftime<<"\t"<<p->state<<endl;
}
void display(int c)
{
    PCB *p;
    if(run!=NULL) /*如果运行指针不空*/
        Prinft(c,run); /*输出当前正在运行的PCB*/
    //Dtime(2);
    p=ready; /*输出就绪队列PCB*/
    while(p!=NULL)
    {
        Prinft(c,p);
        p=p->next;
    }
    //Dtime(2);
    p=finish; /*输出完成队列的PCB*/
    while(p!=NULL)
    {
        Prinft(c,p);
        p=p->next;
    }
}
void insert(PCB *p)//插入就绪队列按Pri大小
{
    PCB *S1,*S2;
    if(ready==NULL)
    {
        p->next = NULL;
        ready = p;
    }
    else
    {
        S1 = ready;
        S2 = S1;
        while(S1!=NULL)
        {
            if(S1->pri >= p->pri)
            {
                S2 = S1;
                S1 = S1->next;
            }
            else
                break;
        }
        if(S2->pri >= p->pri)
        {
            S2->next = p;
            p->next = S1;
        }
        else
        {
            p->next = ready;
            ready = p;
        }
    }
}
bool CTProcessOfPri()
{
    PCB * Node;
    cout << "输入创建进程的数目:" << endl;
    cin >> n;
    for(int j = 0;j < n; j++)
    {
        Node = new PCB;
        if(Node==NULL)
            return false;
        else
        {
            cout << "输入进程的名称,进程需CPU时间:" << endl;
            cin >> Node->procname >> Node->needOftime;
            Node->runtime = 0;
            Node->state = "就绪";
            Node->pri = Node->needOftime;
            cout << "进程" << Node->procname << "创建完毕!" << endl;
        }
        insert(Node);
    }
    return true;
}
void priority(int i)
{
    run = ready;
    ready = ready->next;
    run->state = "运行";
    Prinft(i);
    while(run!=NULL) /*当运行队列不空时,有进程正在运行*/
    {
        run->runtime=run->runtime+1;
        run->needOftime=run->needOftime-1;
        run->pri=run->pri-1; /*每运行一次优先数降低1个单位*/
        if(run->needOftime==0) /*如所需时间为0将其插入完成队列*/
        {
            run->state = "完成";
            run->next = finish;
            finish = run;
            run=NULL; /*运行队列头指针为空*/
            if(ready!=NULL) /*如就绪队列不空*/
            {
                run = ready;
                run->state = "运行";
                ready = ready->next;
            }
        }
        else if((ready!=NULL)&&(run->pri<ready->pri))
        {
            run->state="就绪";
            insert(run);
            run = ready;
            run->state = "运行";
            ready = ready->next;
        }
        display(i); /*输出进程PCB信息*/
    }
}
void queue(PCB *p)
{
    if(ready==NULL)
    {
        p->next = NULL;
        ready = p;
        tial = p;
    }
    else
    {
        tial->next = p;
        tial = p;
        p->next = NULL;
    }
}
bool CTProcessOfRuntime()
{
    PCB * Node;
    int m;
    cout << "输入创建进程的数目:" << endl;
    cin >> n;
    cout << "输入时间片:" << endl;
    cin >> m;
    for(int j = 0;j < n; j++)
    {
        Node = new PCB;
        if(Node==NULL)
            return false;
        else
        {
            cout << "输入进程的名称,进程需CPU时间:" << endl;
            cin >> Node->procname >> Node->needOftime;
            Node->runtime = 0;
            Node->state = "就绪";
            Node->Counter = 0;
            Node->pieceOftime = m;
            cout << "进程" << Node->procname << "创建完毕!" << endl;
        }
        queue(Node);
    }
    return true;
}
void Runtime(int c)
{
    run = ready;
    ready = ready->next;
    run->state = "运行";
    Prinft(c);
    while(run!=NULL)
    {
        run->runtime=run->runtime+1;
        run->needOftime=run->needOftime-1;
        run->Counter = run->Counter + 1;
        if(run->needOftime==0)
        {
            run->state = "完成";
            run->next = finish;
            finish = run;
            run = NULL;
            if(ready!=NULL)
            {
                run = ready;
                ready = ready->next;
            }
        }
        else if(run->Counter == run->pieceOftime)
        {
            run->Counter = 0;
            run->state = "就绪";
            queue(run);
            run=NULL;
            if(ready!=NULL)
            {
                run = ready;
                run->state = "运行";
                ready = ready->next;
            }
        }
        display(c);
    }
}
int main()
{
    int i;
    cout << "*******************************************" << endl;
    cout << "* 1 优先数调度算法 2 循环时间片轮转算法*" << endl;
    cout << "*************** 0 退出 *******************" << endl;
    cin >> i;
    switch(i)
    {
        case 1:
            CTProcessOfPri();
            priority(i);
            break;
        case 2:
            CTProcessOfRuntime();
            Runtime(i);
            break;
        default:
            break;
    }
    return 0;
}
void Dtime(int t)
{
    time_t current_time;
    time_t start_time;
    time(&start_time);
    do
    {
        time(¤t_time);
    }while((current_time-start_time)<t);
}

用C语言编程模拟处理机调度(实现一种算法)

#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;
    system("cls"); /*清屏*/
    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 进程名\t 状态\t 优先数\t 需要运行时间\t 已经运行时间\n");
    printf("|%s\t",pr->name);
    printf("|%c\t",pr->state);
    printf("|%d\t",pr->super);
    printf("|%d\t\t",pr->ntime);
    printf("|%d\t",pr->rtime);
    printf("\n");
}
void check() /* 建立进程查看函数 */
{
    PCB* pr;
    printf("\n **** 当前正在运行的进程是:\n"); /*显示当前运行进程*/
    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(); /*