进程调度算法--时间片轮转算法

来源:互联网 发布:淘宝客服与买家对话 编辑:程序博客网 时间:2024/05/14 14:09

时间片轮转算法

#include <stdio.h>#include <stdlib.h>#include <string.h>#define time 2typedef int datatype;typedef struct link_node {    char name[20];    int arrive;    int start;    int finish;    int zhouzhuan;    float daizhou;    int service;    int count;    int t;    struct link_node *next;} node;typedef node *linklist;linklist creatbyqueue() {    linklist head,r,s;    datatype arrive, server;    char str[20];    head = r = (linklist)malloc(sizeof(node));    head->next = NULL;    printf("当前时间片为两个单位时间,所有进程拥有相间的时间片\n");    printf("输入进程名,到达时间,服务时间:\n");    scanf("%s",str);    scanf("%d",&arrive);    scanf("%d",&server);    s = (linklist)malloc(sizeof(node));    s->t = 0;    s->arrive = arrive;    s->service = server;    strcpy(s->name, str);    s->count = s->service;    head->next = s;    s->next = NULL;    return head;}void print(linklist head, int i) {    linklist p=head->next,q;    while(p) {        if(p->arrive == i) {            printf("进程%s加入就绪队列\n",p->name);        }        p = p->next;    }    p=head->next;    printf("当前运行的进程是%s\n",head->next->name);    printf("就绪队列中有进程:");    while(p->next) {        p = p->next;        if(p->arrive <= i)            printf("    %s",p->name);    }    printf("\n");}void delete(linklist head, int i) {    linklist p,q;    p = head->next;    q = p->next;    head->next = q;    free(p);}void insert(linklist head) {    linklist p;    char str[20];    datatype arrive, server;    linklist r = (linklist)malloc(sizeof(node));    p = head->next;    while(p->next) {        p = p->next;    }    scanf("%s",str);    scanf("%d",&arrive);    scanf("%d",&server);    strcpy(r->name,str);    r->arrive = arrive;    r->t = 0;    p->next = r;    r->service = server;    r->count = r->service;    r->next = NULL;}void sort(linklist head) {    linklist p,pre,r,m;    m = head;    r = pre = head->next;    p = pre->next;    while(r) {        while(p) {            if(pre->arrive > p->arrive) {                m->next = p;                pre->next = p->next;                p->next = pre;                p = pre;                pre = m->next;            }            m = m->next;            pre = pre->next;            p = p->next;        }        r = r->next;    }}void statistic(linklist head, int i) {    linklist p = head->next;    p->finish = i;    p->zhouzhuan = p->finish - p->arrive;    p->daizhou = p->zhouzhuan * 1.0/ p->service;}void finish(linklist run, int i) {    printf("进程%s任务完成\n",run->next->name);    statistic(run, i);    printf("到达时间\t开始时间\t完成时间\t周转时间\t带权周转时间\n");    printf("%5d\t%12d\t%12d\t%12d\t\t%.3f\t\n",run->next->arrive, run->next->start, run->next->finish, run->next->zhouzhuan, run->next->daizhou);    delete(run,i);    if(run->next!=NULL)        if(run->next->service == run->next->count)            run->next->start = i;    if(run->next != NULL)        print(run,i);}int main() {    linklist head,p;    int i = 0;    head = creatbyqueue();    int flag = 1;    while(flag) {        printf("是否继续输入(1或0):");        scanf("%d",&flag);        if(flag) {            insert(head);        }    }    sort(head);    linklist run =(linklist)malloc(sizeof(node));    run->next = NULL;    linklist r = run;    while(head->next!=NULL ) {        printf("已经过单位时间%d\n",i);        if(head->next->arrive > i && run->next == NULL) {            printf("当前无进程执行或就绪\n");        }        else  if(head->next->arrive == i) {            while(head->next) {                if(head->next->arrive == i) {                    r->next = head->next;                    head->next = head->next->next;                    r = r->next;                    r->next = NULL;                } else break;            }            if(run->next->count != 0) {                if(run->next->service==run->next->count)                    run->next->start = i;                if(run->next->t != time) {                    run->next->count--;                    run->next->t++;                } else {                    printf("进程%s的时间片用完\n",run->next->name);                    run->next->t = 0;                    if(run->next->next != NULL) {                        r->next = run->next;                        run->next = run->next->next;                        r = r->next;                        r->next = NULL;                        if(run->next->service==run->next->count)                            run->next->start = i;                        run->next->count--;                        run->next->t++;                    }                }                print(run,i);            } else {                finish(run,i);                if(run->next) {                run->next->count--;                run->next->t++;            }            }        } else {            if(run->next->count != 0) {                if(run->next->t != time) {                    run->next->count--;                    run->next->t++;                } else {                    printf("进程%s的时间片用完\n",run->next->name);                    run->next->t = 0;                    if(run->next->next != NULL) {                        r->next = run->next;                        run->next = run->next->next;                        r = r->next;                        r->next = NULL;                        if(run->next->service==run->next->count)                            run->next->start = i;                        run->next->count--;                        run->next->t++;                    }   else {                        run->next->count--;                    }                }                print(run,i);            } else {                finish(run,i);                if(run->next) {                    run->next->count--;                    run->next->t++;                }            }        }        i++;    }    while(run->next != NULL) {        printf("已经过单位时间%d\n",i);        if(run->next->service==run->next->count)            run->next->start = i;        if(run->next->count != 0) {            if(run->next->t != time) {                run->next->count--;                run->next->t++;            } else {                printf("进程%s的时间片用完\n",run->next->name);                run->next->t = 0;                if(run->next->next != NULL) {                    r->next = run->next;                    run->next = run->next->next;                    r = r->next;                    r->next = NULL;                    if(run->next->service==run->next->count)                        run->next->start = i;                    run->next->count--;                    run->next->t++;                } else {                    run->next->count--;                }            }            print(run,i);        } else {            finish(run,i);            if(run->next) {                run->next->count--;                run->next->t++;            }        }        i++;    }}

本文地址:http://liuyanzhao.com/3143.html
转载请注明

0 0