时间片轮转调度算法 操作系统模拟

时间:2022-12-14 19:51:39
小弟最近在做操作系统的课程设计时间片轮转调度算法,可是代码不会写,因为涉及到时间片的问题,
希望有朋友发个可以用的代码我参考,先谢谢了。急
另:在时间片内做完但该时间片还没用完是不是接着运行下一个进程。

8 个解决方案

#1


这种代码太多了,模拟时间片就是 设置一个 比如: int nTime = 1000;

第二个问题不好确定,有些忘了 。。。

#2


对于第二个问题,现代操作系统一书的解释是:

在你所说的情况下,进程自己会释放CPU。于是调度程序处理就绪队列中的下一个进程。

所以是会用下一个进程。

#3


可是我在网上看了很多,有些就是算法本身出错,有些就是代码运行不了,还有很多都是看不懂也编译不过的
在哪里能找到正确的呢?

#4


楼主把QQ留下。我刚做了这个实验。
操作系统的实验。我给楼主发。
不过楼主要给我分哈。
嘿嘿

#5


#include <stdio.h>
#include <stdlib.h>

struct PCB{
    char p_name[20];
    int p_priority;
    int p_needTime;
    int p_runTime;
    char p_state;
    struct PCB* next;
};

void HighPriority();
void RoundRobin();
void Information();
char Choice();
struct PCB* SortList(PCB* HL);

int main()
{
    Information();
    char choice = Choice();
    switch(choice)
    {
        case '1':
            system("cls");
            HighPriority();
            break;
        case '2':
            system("cls");
            RoundRobin();
            break;
        default:
            break;
    }
    system("pause");
    return 0;
}

void Information()
{
    printf("\n\n");
    printf("              *********************************************             \n");
    printf("                            模拟进程调度算法\n");
    printf("              *********************************************             \n\n\n");    
    printf("                       班级:  2008级软件工程4班\n");
    printf("                       姓名:             ****\n");
    printf("                       学号:       2008104\n");
    printf("                       实验日期: 2009年12月20日\n\n\n\n\n\n");
    printf("                            按任意键进入演示程序");
    getchar();
    system("cls");
}
char Choice()
{
    printf("\n\n");
    printf("              *********************************************             \n");
    printf("                              进程调度演示\n");
    printf("              *********************************************             \n\n\n");    
    printf("                        1.演示最高优先数优先算法.\n");
    printf("                        2.演示轮转法算法.\n");
    printf("                        3.退出程序.\n\n");
    printf("                                   选择进程调度方法:");     
    char ch = getchar();
    return ch;
    system("cls");
}
void HighPriority()
{
    struct PCB *processes, *pt;
    //pt作为临时节点来创建链表
    processes = pt = (struct PCB*)malloc(sizeof(struct PCB));    
    for (int i = 0; i != 5; ++i)
    {
        struct PCB *p = (struct PCB*)malloc(sizeof(struct PCB));
        printf("进程号No.%d:\n", i);
        printf("输入进程名:");
        scanf("%s", p->p_name);
        printf("输入进程优先数:");
        scanf("%d", &p->p_priority);
        printf("输入进程运行时间:");
        scanf("%d", &p->p_needTime);
        p->p_runTime = 0;
        p->p_state = 'W';
        p->next = NULL;
        pt->next = p;
        pt = p;
        printf("\n\n");
    }
    getchar();  //接受回车
    //processes作为头结点来存储链表
    processes = processes->next;
    int cases = 0;
    struct PCB *psorted = processes;
    while (1)
    {
        ++cases;
        pt = processes; 
        //对链表按照优先数排序
        //psorted用来存放排序后的链表
        psorted = SortList(psorted);
        printf("The execute number: %d\n\n", cases);
        printf("**** 当前正在运行的进程是:%s\n", psorted->p_name);
        psorted->p_state = 'R';
        printf("qname    state    super    ndtime    runtime\n");
        printf("%s\t%c\t%d\t%d\t%d\t\n\n", psorted->p_name, psorted->p_state, psorted->p_priority, psorted->p_needTime, psorted->p_runTime);
        pt->p_state = 'W';
        psorted->p_runTime++;
        psorted->p_priority--;
        printf("**** 当前就绪状态的队列为:\n\n");
        //pt指向已经排序的队列
        pt = psorted->next;
        while (pt != NULL)
        {
            printf("qname    state    super    ndtime    runtime\n");
            printf("%s\t%c\t%d\t%d\t%d\t\n\n", pt->p_name, pt->p_state, pt->p_priority, pt->p_needTime, pt->p_runTime);
            pt = pt->next;
        }
        //pt指向已经排序的链表,判断链表是否有已用时间啊等于需要时间的
        pt = psorted;
        struct PCB *ap;
        ap = NULL; //ap指向pt的前一个节点
        while (pt != NULL)
        {
            if (pt->p_needTime == pt->p_runTime)
            {
                if (ap == NULL)
                {
                    pt = psorted->next;
                    psorted = pt;
                }
                else
                    ap->next = pt->next;
            }
            ap = pt;
            pt = pt->next;
        }
        if (psorted->next == NULL)
            break;
        getchar();
    }
}
struct PCB* SortList(PCB* HL)
{
    struct PCB* SL;
    SL = (struct PCB*)malloc(sizeof(struct PCB));
    SL = NULL;

    struct PCB* r = HL;
    while (r != NULL)
    {
        struct PCB* t = r->next;
        struct PCB* cp = SL;
        struct PCB* ap = NULL;
        while (cp != NULL)
        {
            if (r->p_priority > cp->p_priority)
                break;
            else
            {
                ap = cp;
                cp = cp->next;
            }
        }
        if (ap == NULL)
        {
            r->next = SL;
            SL = r;
        }
        else
        {
            r->next = cp;
            ap->next = r;
        }
        r = t;
    }
    return SL;
}
//轮转算法
void RoundRobin()
{
    struct PCB *processes, *pt;
    //pt作为临时节点来创建链表
    processes = pt = (struct PCB*)malloc(sizeof(struct PCB));    
    for (int i = 0; i != 5; ++i)
    {
        struct PCB *p = (struct PCB*)malloc(sizeof(struct PCB));
        printf("进程号No.%d:\n", i);
        printf("输入进程名:");
        scanf("%s", p->p_name);
        printf("输入进程运行时间:");
        scanf("%d", &p->p_needTime);
        p->p_runTime = 0;
        p->p_state = 'W';
        p->next = NULL;
        pt->next = p;
        pt = p;
        printf("\n\n");
    }
    getchar();  //接受回车
    //processes作为头结点来存储链表
    processes = processes->next;
    int cases = 0;
    while (1)
    {
        ++cases;
        pt = processes; 
        printf("The execute number: %d\n\n", cases);
        printf("**** 当前正在运行的进程是:%s\n", pt->p_name);
        pt->p_state = 'R';
        printf("qname    state    super    ndtime    runtime\n");
        printf("%s\t%c\t%d\t%d\t%d\t\n\n", pt->p_name, pt->p_state, pt->p_priority, pt->p_needTime, pt->p_runTime);
        pt->p_state = 'W';
        pt->p_runTime++;
        pt->p_priority--;
        printf("**** 当前就绪状态的队列为:\n\n");
        pt = pt->next;
        while (pt != NULL)
        {
            printf("qname    state    super    ndtime    runtime\n");
            printf("%s\t%c\t%d\t%d\t%d\t\n\n", pt->p_name, pt->p_state, pt->p_priority, pt->p_needTime, pt->p_runTime);
            pt = pt->next;
        }
        //检测是否运行时间等于需要时间,是的话从队列里面删除,不是的话加到队列最尾部
        pt = processes;
        if (pt->p_needTime == pt->p_runTime)
        {
            pt->p_state = 'C';
            pt = processes->next;
            processes = pt;
        }
        else
        {
            if (pt ->next != NULL)
            {
                //寻找最后一个节点
                while (pt->next != NULL)
                    pt = pt->next;
                struct PCB* ptem;//临时节点用来帮助把头结点插到尾部
                ptem = processes->next;
                pt->next = processes;
                processes->next = NULL;
                processes = ptem;
            }
        }
        pt = processes;
        if (pt == NULL)
            break;
        getchar();
    }
}

先发这里吧。不过这是按照我们老师的要求写的。
如果你要详细的要求我把我们老师的word文档给你发过去
你直接理解代码可能有点难度

#6


不好意思哈。我忘了我是用c写的
一样。改一个动态分配、输入输出就可以了

#7


我QQ876331055,可以的话加我有问题探讨,结果出现负数的。

#8


呵呵,我借用别人账号用的,不好意思,分可以加,但解决问题先

#1


这种代码太多了,模拟时间片就是 设置一个 比如: int nTime = 1000;

第二个问题不好确定,有些忘了 。。。

#2


对于第二个问题,现代操作系统一书的解释是:

在你所说的情况下,进程自己会释放CPU。于是调度程序处理就绪队列中的下一个进程。

所以是会用下一个进程。

#3


可是我在网上看了很多,有些就是算法本身出错,有些就是代码运行不了,还有很多都是看不懂也编译不过的
在哪里能找到正确的呢?

#4


楼主把QQ留下。我刚做了这个实验。
操作系统的实验。我给楼主发。
不过楼主要给我分哈。
嘿嘿

#5


#include <stdio.h>
#include <stdlib.h>

struct PCB{
    char p_name[20];
    int p_priority;
    int p_needTime;
    int p_runTime;
    char p_state;
    struct PCB* next;
};

void HighPriority();
void RoundRobin();
void Information();
char Choice();
struct PCB* SortList(PCB* HL);

int main()
{
    Information();
    char choice = Choice();
    switch(choice)
    {
        case '1':
            system("cls");
            HighPriority();
            break;
        case '2':
            system("cls");
            RoundRobin();
            break;
        default:
            break;
    }
    system("pause");
    return 0;
}

void Information()
{
    printf("\n\n");
    printf("              *********************************************             \n");
    printf("                            模拟进程调度算法\n");
    printf("              *********************************************             \n\n\n");    
    printf("                       班级:  2008级软件工程4班\n");
    printf("                       姓名:             ****\n");
    printf("                       学号:       2008104\n");
    printf("                       实验日期: 2009年12月20日\n\n\n\n\n\n");
    printf("                            按任意键进入演示程序");
    getchar();
    system("cls");
}
char Choice()
{
    printf("\n\n");
    printf("              *********************************************             \n");
    printf("                              进程调度演示\n");
    printf("              *********************************************             \n\n\n");    
    printf("                        1.演示最高优先数优先算法.\n");
    printf("                        2.演示轮转法算法.\n");
    printf("                        3.退出程序.\n\n");
    printf("                                   选择进程调度方法:");     
    char ch = getchar();
    return ch;
    system("cls");
}
void HighPriority()
{
    struct PCB *processes, *pt;
    //pt作为临时节点来创建链表
    processes = pt = (struct PCB*)malloc(sizeof(struct PCB));    
    for (int i = 0; i != 5; ++i)
    {
        struct PCB *p = (struct PCB*)malloc(sizeof(struct PCB));
        printf("进程号No.%d:\n", i);
        printf("输入进程名:");
        scanf("%s", p->p_name);
        printf("输入进程优先数:");
        scanf("%d", &p->p_priority);
        printf("输入进程运行时间:");
        scanf("%d", &p->p_needTime);
        p->p_runTime = 0;
        p->p_state = 'W';
        p->next = NULL;
        pt->next = p;
        pt = p;
        printf("\n\n");
    }
    getchar();  //接受回车
    //processes作为头结点来存储链表
    processes = processes->next;
    int cases = 0;
    struct PCB *psorted = processes;
    while (1)
    {
        ++cases;
        pt = processes; 
        //对链表按照优先数排序
        //psorted用来存放排序后的链表
        psorted = SortList(psorted);
        printf("The execute number: %d\n\n", cases);
        printf("**** 当前正在运行的进程是:%s\n", psorted->p_name);
        psorted->p_state = 'R';
        printf("qname    state    super    ndtime    runtime\n");
        printf("%s\t%c\t%d\t%d\t%d\t\n\n", psorted->p_name, psorted->p_state, psorted->p_priority, psorted->p_needTime, psorted->p_runTime);
        pt->p_state = 'W';
        psorted->p_runTime++;
        psorted->p_priority--;
        printf("**** 当前就绪状态的队列为:\n\n");
        //pt指向已经排序的队列
        pt = psorted->next;
        while (pt != NULL)
        {
            printf("qname    state    super    ndtime    runtime\n");
            printf("%s\t%c\t%d\t%d\t%d\t\n\n", pt->p_name, pt->p_state, pt->p_priority, pt->p_needTime, pt->p_runTime);
            pt = pt->next;
        }
        //pt指向已经排序的链表,判断链表是否有已用时间啊等于需要时间的
        pt = psorted;
        struct PCB *ap;
        ap = NULL; //ap指向pt的前一个节点
        while (pt != NULL)
        {
            if (pt->p_needTime == pt->p_runTime)
            {
                if (ap == NULL)
                {
                    pt = psorted->next;
                    psorted = pt;
                }
                else
                    ap->next = pt->next;
            }
            ap = pt;
            pt = pt->next;
        }
        if (psorted->next == NULL)
            break;
        getchar();
    }
}
struct PCB* SortList(PCB* HL)
{
    struct PCB* SL;
    SL = (struct PCB*)malloc(sizeof(struct PCB));
    SL = NULL;

    struct PCB* r = HL;
    while (r != NULL)
    {
        struct PCB* t = r->next;
        struct PCB* cp = SL;
        struct PCB* ap = NULL;
        while (cp != NULL)
        {
            if (r->p_priority > cp->p_priority)
                break;
            else
            {
                ap = cp;
                cp = cp->next;
            }
        }
        if (ap == NULL)
        {
            r->next = SL;
            SL = r;
        }
        else
        {
            r->next = cp;
            ap->next = r;
        }
        r = t;
    }
    return SL;
}
//轮转算法
void RoundRobin()
{
    struct PCB *processes, *pt;
    //pt作为临时节点来创建链表
    processes = pt = (struct PCB*)malloc(sizeof(struct PCB));    
    for (int i = 0; i != 5; ++i)
    {
        struct PCB *p = (struct PCB*)malloc(sizeof(struct PCB));
        printf("进程号No.%d:\n", i);
        printf("输入进程名:");
        scanf("%s", p->p_name);
        printf("输入进程运行时间:");
        scanf("%d", &p->p_needTime);
        p->p_runTime = 0;
        p->p_state = 'W';
        p->next = NULL;
        pt->next = p;
        pt = p;
        printf("\n\n");
    }
    getchar();  //接受回车
    //processes作为头结点来存储链表
    processes = processes->next;
    int cases = 0;
    while (1)
    {
        ++cases;
        pt = processes; 
        printf("The execute number: %d\n\n", cases);
        printf("**** 当前正在运行的进程是:%s\n", pt->p_name);
        pt->p_state = 'R';
        printf("qname    state    super    ndtime    runtime\n");
        printf("%s\t%c\t%d\t%d\t%d\t\n\n", pt->p_name, pt->p_state, pt->p_priority, pt->p_needTime, pt->p_runTime);
        pt->p_state = 'W';
        pt->p_runTime++;
        pt->p_priority--;
        printf("**** 当前就绪状态的队列为:\n\n");
        pt = pt->next;
        while (pt != NULL)
        {
            printf("qname    state    super    ndtime    runtime\n");
            printf("%s\t%c\t%d\t%d\t%d\t\n\n", pt->p_name, pt->p_state, pt->p_priority, pt->p_needTime, pt->p_runTime);
            pt = pt->next;
        }
        //检测是否运行时间等于需要时间,是的话从队列里面删除,不是的话加到队列最尾部
        pt = processes;
        if (pt->p_needTime == pt->p_runTime)
        {
            pt->p_state = 'C';
            pt = processes->next;
            processes = pt;
        }
        else
        {
            if (pt ->next != NULL)
            {
                //寻找最后一个节点
                while (pt->next != NULL)
                    pt = pt->next;
                struct PCB* ptem;//临时节点用来帮助把头结点插到尾部
                ptem = processes->next;
                pt->next = processes;
                processes->next = NULL;
                processes = ptem;
            }
        }
        pt = processes;
        if (pt == NULL)
            break;
        getchar();
    }
}

先发这里吧。不过这是按照我们老师的要求写的。
如果你要详细的要求我把我们老师的word文档给你发过去
你直接理解代码可能有点难度

#6


不好意思哈。我忘了我是用c写的
一样。改一个动态分配、输入输出就可以了

#7


我QQ876331055,可以的话加我有问题探讨,结果出现负数的。

#8


呵呵,我借用别人账号用的,不好意思,分可以加,但解决问题先