记一次C语言课堂实验:实现进程调度及三态转换,基于LinuxGCC

代码编写

// Author: Elin.Liu
// Date: 2022-11-01 10:37:20
// Last Modified by:   Elin.Liu
// Last Modified time: 2022-11-01 10:37:20

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

#define ready 0
#define running 1
#define blocking 2
#define nProcess 5
#define done 3

/*初始化总线进程时间*/
int busTime = 0;

struct Process
{
    int ID;          /*进程标识数ID*/
    int ARRIVE_TIME; /*进程到达时间*/
    int PRIORITY;    /*进程优先数PRIORITY*/
    int CPU_TIME;    /*进程已经占用CPU时间CPU_TIME*/
    int ALL_TIME;    /*进程总共需要占用CPU时间ALL_TIME*/
    int START_BLOCK; /*进程开始阻塞时间START_BLOCK*/
    int BLOCK_TIME;  /*进程阻塞时间BLOCK_TIME*/
    int STATE;       /*进程状态STATE*/
    int NEXT;        /*进程下一个状态NEXT*/
};

void createProcess(int n, struct Process *p)
{
    int i;
    for (i = 0; i < n; i++)
    {
        p[i].ID = i;                       /*初始化ID为序列化ID*/
        p[i].ARRIVE_TIME = rand() % 100;   /*初始化到达时间为0-100随机数*/
        p[i].PRIORITY = rand() % 10;       /*初始化优先数为0-9的随机数*/
        p[i].CPU_TIME = 0;                 /*初始化CPU时间为0*/
        p[i].ALL_TIME = rand() % 10 + 1;   /*初始化总共需要占用CPU时间为1-10的随机数*/
        p[i].START_BLOCK = rand() % 10;    /*初始化开始阻塞时间为0-9的随机数*/
        p[i].BLOCK_TIME = rand() % 10 + 1; /*初始化阻塞时间为1-10的随机数*/
        p[i].STATE = ready;                /*初始化状态为就绪态*/
        p[i].NEXT = i + 1;                 /*初始化下一个状态为下一个进程*/
    }
}

/*定义进程处理函数*/
void scheduling(int id, struct Process *p)
{
    int i;
    /*获取当前进程的CPU时间*/
    int cpuTime = p[id].CPU_TIME;
    /*获取当前进程的总共需要占用CPU时间*/
    int allTime = p[id].ALL_TIME;
    /*获取当前进程的开始阻塞时间*/
    int startBlock = p[id].START_BLOCK;
    /*开始执行该进程*/
    printf("进程%d开始执行!\n", id);
    for (i = p[id].CPU_TIME; i < allTime; i++)
    {
        /*总时间开始进行推移*/
        busTime++;
        /*在进程生命周期内增加CPU时间*/
        p[id].CPU_TIME++;
        /*如果当前进程的CPU时间节点到了阻塞时间点*/
        if (p[id].CPU_TIME == startBlock && p[id].ALL_TIME != startBlock)
        {
            /*则将进程的状态转换为阻塞状态*/
            p[id].STATE = blocking;
            printf("进程%d已被阻塞!\n", id);
            break;
        }
    }
    if (p[id].CPU_TIME == p[id].ALL_TIME)
    {
        p[id].STATE = done;
        printf("进程%d已经完成!\n", id);
    }
}

/*定义进程转换函数*/
void block2ready(int id, struct Process *p)
{
    /*如果当前的进程状态为阻塞状态,且CPU时间已经到了可释放的时间*/
    if (busTime >= 0 + p[id].START_BLOCK + p[id].BLOCK_TIME && p[id].STATE == blocking)
    {
        p[id].STATE = ready;
        printf("进程%d已被释放!\n", id);
    }
    else
    {
        printf("无法解锁当前进程:%d!\n", id);
    }
}

/*初始化先来先服务排序*/
void FCFS(struct Process *p, struct Process *FCFCP)
{
    int i, j;
    for (i = 0; i < nProcess; i++)
    {
        for (j = 0; j < nProcess; j++)
        {
            if (p[i].ARRIVE_TIME < p[j].ARRIVE_TIME)
            {
                FCFCP[i] = p[j];
            }
        }
    }
}

/*初始化短作业优先排序*/
void SJF(struct Process *p, struct Process *SJFP)
{
    int i, j;
    for (i = 0; i < nProcess; i++)
    {
        for (j = 0; j < nProcess; j++)
        {
            if (p[i].ALL_TIME < p[j].ALL_TIME)
            {
                SJFP[i] = p[j];
            }
        }
    }
}

/*初始化优先级排序*/
void PRIORITY_FIRST(struct Process *p, struct Process *PRIORITYP)
{
    int i, j;
    for (i = 0; i < nProcess; i++)
    {
        for (j = 0; j < nProcess; j++)
        {
            if (p[i].PRIORITY < p[j].PRIORITY)
            {
                PRIORITYP[i] = p[j];
            }
        }
    }
}

/*定义运行函数*/
void runningFunc(struct Process *p)
{
    /*调用执行函数*/
    for (int i = 0; i <= nProcess; i++)
    {
        /*如果当前的进程状态为就绪状态*/
        if (p[i].STATE == ready)
        {
            /*则调度当前进程*/
            scheduling(i, p);
        }
    }
    /*再检索一遍当前的进程列表*/
    for (int i = 0; i <= nProcess; i++)
    {
        /*如果当前的进程状态为阻塞状态*/
        if (p[i].STATE == blocking)
        {
            /*则解锁当前进程*/
            block2ready(i, p);
        }
    }
    /*最后检索一遍进程列表,以执行最后未完成的进程*/
    for (int i = 0; i <= nProcess; i++)
    {
        if (p[i].STATE == ready)
        {
            scheduling(i, p);
        }
    }
}

void main(void)
{
    /*初始化随机种子*/
    srand((unsigned)time(NULL));
    /*初始化进程*/
    struct Process *process = (struct Process *)malloc(sizeof(struct Process) * nProcess);
    /*创建进程*/
    createProcess(nProcess, process);
    /*使用先来先服务重新排序*/
    struct Process *FCFSProcess = (struct Process *)malloc(sizeof(struct Process) * nProcess);
    FCFS(process, FCFSProcess);
    /*使用短作业优先重新排序*/
    struct Process *SJFProcess = (struct Process *)malloc(sizeof(struct Process) * nProcess);
    SJF(process, SJFProcess);
    /*使用优先级重新排序*/
    struct Process *PRIORITYProcess = (struct Process *)malloc(sizeof(struct Process) * nProcess);
    PRIORITY_FIRST(process, PRIORITYProcess);
    /*释放原始进程内存*/
    free(process);
    /*按照先来先服务执行进程*/
    printf("将以先来先服务原则执行进程!\n");
    runningFunc(FCFSProcess);
    for (int i = 0; i <= 10; i++)
    {
        printf("-");
    }
    printf("\n");
    /*释放先来先服务内存*/
    free(FCFSProcess);
    /*将总线时间置0*/
    busTime = 0;
    /*按照短作业优先执行进程*/
    printf("将以短作业优先原则执行进程!\n");
    runningFunc(SJFProcess);
    for (int i = 0; i <= 10; i++)
    {
        printf("-");
    }
    printf("\n");
    /*释放短作业优先内存*/
    free(SJFProcess);
    /*将总线时间置0*/
    busTime = 0;
    /*按照优先级执行进程*/
    printf("将以优先级原则执行进程!\n");
    runningFunc(PRIORITYProcess);
    for (int i = 0; i <= 10; i++)
    {
        printf("-");
    }
    printf("\n");
    /*释放优先级内存*/
    free(PRIORITYProcess);
    /*将总线时间置0*/
    busTime = 0;
    printf("所有进程作业全部完成!\n");
}

运行结果

将以先来先服务原则执行进程!
进程0开始执行!
进程0已被阻塞!
进程1开始执行!
进程1已被阻塞!
进程2开始执行!
进程2已被阻塞!
进程3开始执行!
进程3已经完成!
进程4开始执行!
进程4已经完成!
进程0已被释放!
进程1已被释放!
进程2已被释放!
进程0开始执行!
进程0已经完成!
进程1开始执行!
进程1已经完成!
进程2开始执行!
进程2已经完成!
-----------
将以短作业优先原则执行进程!
进程0开始执行!
进程0已被阻塞!
进程1开始执行!
进程1已被阻塞!
进程2开始执行!
进程2已被阻塞!
进程3开始执行!
进程3已被阻塞!
进程4开始执行!
进程4已经完成!
进程0已被释放!
进程1已被释放!
进程2已被释放!
进程3已被释放!
进程0开始执行!
进程0已经完成!
进程1开始执行!
进程1已经完成!
进程2开始执行!
进程2已经完成!
进程3开始执行!
进程3已经完成!
-----------
将以优先级原则执行进程!
进程0开始执行!
进程0已经完成!
进程1开始执行!
进程1已经完成!
进程2开始执行!
进程2已被阻塞!
进程3开始执行!
进程3已经完成!
进程4开始执行!
进程4已经完成!
进程2已被释放!
进程2开始执行!
进程2已经完成!
-----------
所有进程作业全部完成!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Day(AKA Elin)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值