C++队列之时间片轮转调度(两种实现)

这篇博客介绍了时间片轮转调度算法的原理和实现方法,通过C++代码展示了如何处理任务队列,确保每个任务按顺序并限制在一定时间片内执行。示例中给出了输入和输出,解释了如何更新任务状态并调整时间片,最终实现任务的调度。
摘要由CSDN通过智能技术生成

题目表述

There are n processes in a queue. Each process has namei and timei. The round-robin scheduling handles the processes in order. A round-robin scheduler gives each process a quantum (a time slot) and interrupts the process if it is not completed by then. The process is resumed and moved to the end of the queue, then the scheduler handles the next process in the queue.

For example, we have the following queue with the quantum of 100ms.

Sample Input

5 100
p1 150
p2 80
p3 200
p4 350
p5 20

Sample Output

p2 180
p5 400
p1 450
p3 550
p4 800

题目大意:

现在有名字为 n a m e i name_i namei且处理时间为 t i m e i time_i timei的n个任务按顺序拍成一列。CPU通过时间片轮转调度来处理,每次CPU只能处理 q q q毫秒(时间片)。如果 q q q毫秒后任务没有完成,则移动至队伍尾部。

实现思路:

首先要明白队列的实现原理,先进先出。
其次我们需要一些代码实现的基本流程。一个队列有一个队头 h e a d head head,一个队尾 t a i l tail tail,我们首先要确定CPU循环轮转的条件是当队列种没有任务,此时 h e a d head head等于 t a i l tail tail。然后用一个数组来模拟一个队列,为了最大化利用数组的空间,我们采用了循环数组的方式

head = (head+1)%(数组最大长度)
tail = (tail+1)%(数组最大长度)

这样一来我们就实现了如图所示的队列,在这里插入图片描述

Task dequeue()
{
    Task item = tk[head];
    head = (head + 1) % LEN;
    return item;
}
void enqueue(Task t)
{
    tk[tail] = t;
    tail = (tail + 1) % LEN;
}

最后是我们处理逻辑

while (head != tail)
    {
        Task t = dequeue();
        cur_time = min(t.time, slice);
        t.time -= cur_time;
        Time += cur_time;
        if (t.time > 0)
            enqueue(t);
        else
            cout << t.name << " " << Time << endl;
    }

首先判断当前任务的剩余时间与时间片的大小,若剩余时间大于时间片,则 t . t i m e t.time t.time减去 s l i c e slice slice后重新入队,并且总时间加上一个 s l i c e slice slice。若剩余时间小于时间片,则说明任务可以完成,不需要重新进入队列,打印输出,并总时间加上当前任务的剩余时间。所以总时间应该加上 t . t i m e t.time t.time s l i c e slice slice的最小值。
最后附上完整代码

#include <bits/stdc++.h>
using namespace std;
struct Task
{
    string name;
    int time;
    bool state;
};
const int LEN = 1e6 + 5;
Task tk[LEN];
int head, tail;
Task dequeue()
{
    Task item = tk[head];
    head = (head + 1) % LEN;
    return item;
}
void enqueue(Task t)
{
    tk[tail] = t;
    tail = (tail + 1) % LEN;
}
int main()
{
    int n, slice;
    scanf("%d %d", &n, &slice);
    for (int i = 0; i < n; i++)
    {
        cin >> tk[i].name >> tk[i].time;
    }
    head = 0;
    tail = n;
    int Time = 0;
    int cur_time = 0;
    while (head != tail)
    {
        Task t = dequeue();
        cur_time = min(t.time, slice);
        t.time -= cur_time;
        Time += cur_time;
        if (t.time > 0)
            enqueue(t);
        else
            cout << t.name << " " << Time << endl;
    }
}

这是使用C++模板库的代码

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n, slice, Time, t, p;
    string name;
    queue<pair<string, int>> task;
    cin >> n >> slice;
    for (int i = 0; i < n; i++)
    {
        cin >> name >> t;
        task.push(make_pair(name, t));//将name和t打包成一个pair后入队
    }
    pair<string, int> cur_task;
    while (!task.empty())
    {
        cur_task = task.front();
        p = min(cur_task.second, slice);
        cur_task.second -= p;
        Time += p;
        if (cur_task.second > 0)
        {
            task.push(cur_task);
        }
        else
        {
            cout << cur_task.first << " " << Time << endl;
        }
    }
    return 0;
}

题目网址:AOJ-Queue练习

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值