RR调度算法_简单的一笔

class RR():
    def __init__(self,timeSlice=10):
        self.timeSlice=timeSlice
        self.runTimes=[25,37,12,46,29,5,18]
        self.peintf()
    def peintf(self):
        self.new=[]
        # print((max(self.runTimes) // self.timeSlice) + 1)
        # print(self.runTimes)
        for j in range((max(self.runTimes) // self.timeSlice) + 1):
            for i in range(len(self.runTimes)):

                runTime=self.runTimes.pop(0)


                if runTime<=0:
                    self.runTimes.append(0)
                    # print(f'{i + 1}-{j + 1}-{self.timeSlice},', end=' ')
                elif 0<runTime<self.timeSlice:
                    self.runTimes.append(0)
                    print(f'{i + 1}-{j + 1}-{runTime},', end=' ')
                else:
                    runTime -= self.timeSlice
                    self.runTimes.append(runTime)
                    print(f'{i + 1}-{j + 1}-{self.timeSlice},', end=' ')
                # print(f'{i+1}-{j+1}-{self.timeSlice},',end=' ')
            # print(self.runTimes)
            print()



if __name__=='__main__':
    RR()
class RR():
    def __init__(self):
        self.runTimes=[25,37,12,46,29,5,18]
        self.timeSlice=10
        self.sol()
    # def input(self):
    #     self.timeSlice=int(input(print("请输入时间片大小")))
    def sol(self):
        for j in range(max(self.runTimes)//self.timeSlice+1):
            for i in range(len(self.runTimes)):
                x=self.runTimes.pop(0)
                if x>self.timeSlice:
                    x-=self.timeSlice
                    print(f"{i+1}-{j+1}-{self.timeSlice},",end=' ')
                    self.runTimes.append(x)
                elif 0<x<self.timeSlice:
                    self.runTimes.append(0)
                    print(f"{i + 1}-{j + 1}-{x},", end=' ')
                elif x==0:
                    self.runTimes.append(0)
            print()
if __name__=='__main__':

    RR()
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RR调度算法(Round-Robin Scheduling Algorithm)是一种常用的调度算法,它按照时间片轮转的方式为每个进程分配CPU时间。下面是一个使用C语言实现的简单RR调度算法的代码示例: ```c #include <stdio.h> #include <stdlib.h> // 定义进程结构体 typedef struct Process { int pid; // 进程ID int burstTime; // 执行时间 int remainingTime; // 剩余执行时间 } Process; // 定义链表节点结构体 typedef struct Node { Process process; struct Node* next; } Node; // 创建新的链表节点 Node* createNode(Process process) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->process = process; newNode->next = NULL; return newNode; } // 在链表尾部插入节点 void insertAtEnd(Node** head, Process process) { Node* newNode = createNode(process); if (*head == NULL) { *head = newNode; } else { Node* temp = *head; while (temp->next != NULL) { temp = temp->next; } temp->next = newNode; } } // 执行RR调度算法 void runRR(Node* head, int timeSlice) { Node* current = head; while (current != NULL) { if (current->process.remainingTime > 0) { if (current->process.remainingTime > timeSlice) { printf("Process %d is running for time slice %d\n", current->process.pid, timeSlice); current->process.remainingTime -= timeSlice; } else { printf("Process %d is running for remaining time %d\n", current->process.pid, current->process.remainingTime); current->process.remainingTime = 0; } } current = current->next; } } int main() { // 创建进程链表 Node* head = NULL; insertAtEnd(&head, (Process){1, 10, 10}); insertAtEnd(&head, (Process){2, 5, 5}); insertAtEnd(&head, (Process){3, 8, 8}); // 执行RR调度算法,时间片为2 runRR(head, 2); return 0; } ``` 上述代码中,首先定义了进程结构体和链表节点结构体。然后,通过`createNode`函数创建新的链表节点,`insertAtEnd`函数将节点插入链表尾部。最后,`runRR`函数执行RR调度算法,遍历链表中的每个进程,并根据时间片的大小进行调度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值