简体   繁体   English

如何使用 pthread 库模拟 CPU 调度(多级队列)?

[英]How to simulate CPU scheduling(multilevel queue) using pthread library?

i wanna simulate multilevel queue scheduling using pthread library我想使用 pthread 库模拟多级队列调度

i will make 3 ready queues.我将制作 3 个就绪队列。 The first queue has round-robin scheduling algorithm (time quantum = 2) The second queue has round-robin scheduling algorithm (time quantum = 5) The third queue has FCFS scheduling algorithm.第一个队列有循环调度算法(时间量 = 2) 第二个队列有循环调度算法(时间量 = 5) 第三个队列有 FCFS 调度算法。

each queue has a thread that schedule processes.每个队列都有一个调度进程的线程。

queue priority: first queue > second queue > third queue队列优先级:第一个队列>第二个队列>第三个队列

I wonder how to give priority for queues.我想知道如何优先考虑队列。 It's about synchronization using semaphore.这是关于使用信号量的同步。

I hope that the higher priority queue's thread execute first.我希望高优先级队列的线程先执行。

how to do it?怎么做?

Thanks to read.感谢阅读。

Quoting Wikipedia p_queue "A priority queue is an abstract data type similar to regular queue or stack data structure in which each element additionally has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority引用 Wikipedia p_queue priority queue是一种类似于常规queuestack数据结构的抽象数据类型,其中每个元素还具有与之关联的“优先级”。在优先级队列中,具有高优先级的元素在具有高优先级的元素之前被提供低优先级

... a priority queue can be implemented with a heap or a variety of other methods such as an unordered array" ...优先级队列可以用堆或各种其他方法(例如无序数组)来实现”

The web is full of tutorials eg here . web 充满了教程,例如这里 you can implement the priority queue using heaps then determine where you have critical sections in the source code!您可以使用堆实现优先级队列,然后确定源代码中关键部分的位置! Only then you should protect and prevent race conditions on these sections by lock these sections with mutex or simaphore or any other o.s sync mechanism.只有这样,您才应该通过使用互斥锁或信号量或任何其他操作系统同步机制锁定这些部分来保护和防止这些部分的竞争条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM