简体   繁体   English

在C中实现MLFQ(多级反馈队列)的最佳方法是什么?

[英]What is the best way to implement a MLFQ (Multi-level feedback queue) in C?

I have a function that looks like this that I need to implement. 我有一个需要实现的函数。 Threads call this function with these parameters. 线程使用这些参数调用此函数。 It's supposed to return with the correct time it accessed the CPU and if it can't access the CPU, it will wait until it can access it. 应该以正确的时间返回它访问CPU的时间,如果无法访问CPU,它将等待直到可以访问它。

With regards to the correct time, I keep a global variable that gets updated each time, it calls it. 关于正确的时间,我保留一个全局变量,每次调用它都会更新。

How do I implement the waits and synchronize it correctly. 如何实现等待并正确同步它。

int MLFQ(float currentTime, int tid, int remainingTime, int tprio)

My code looks something like this so far and it doesn't quite work. 到目前为止,我的代码看起来像这样,但效果不佳。

update globalTime (globalTime = currentTime)
Mutex_lock
Add to MLFQ if needed (either to 5, 10, 15, 20, or 25 MLFQ)
if (canAccessCPU)
    getCPU
    unlock mutex
    return globalTime
else
    mutex_unlock
    return MLFQ(globalTime, tid, remainingTime, tprio);

Your post uses pseudo code, and there are some ambiguities, so comment if I make the wrong assumptions here: 您的帖子使用伪代码,并且存在一些歧义,因此,如果我在这里做出错误的假设,请发表评论:

How do I implement the waits and synchronize it correctly [?] 如何实现等待并正确同步 [[]

Waits , 等一下

Waits in threads are often implemented in such a way as to not block other threads. 线程中的等待通常以不阻塞其他线程的方式实现。 Sleep() at the bottom of a thread worker function allows some time for the called thread to sleep, ie share time with other processes. 线程工作器函数底部的Sleep()允许一些时间使被调用线程进入睡眠状态,即与其他进程共享时间。 In Windows, it is prototyped as: 在Windows中,其原型为:

VOID WINAPI Sleep(
  _In_  DWORD dwMilliseconds
);  

Linux sleep() here Linux sleep()在这里

Synchronizing : 同步中
Can be done in many ways. 可以通过多种方式完成。 Assuming you are referring to keeping the order in which calls come in from several threads, you can create a simple struct that can be passed back as an argument that could contain a TRUE/FALSE indication of whether the uP was accessed, and the time the attempt was made: 假设您指的是保持几个线程中调用的顺序,则可以创建一个简单的结构,该结构可以作为参数传递回去,该参数可以包含TRUE / FALSE指示是否访问了uP,以及访问uP的时间。尝试了:

In someheader.h file: 在someheader.h文件中:

typedef struct  {
    int uPAccess;
    time_t time;
}UP_CALL;

extern UP_CALL uPCall, *pUPCall; 外部UP_CALL uPCall,* pUPCall;

In all of the the .c file(s) you will use: 在所有.c文件中,您将使用:

#include "someheader.h" 

In one of the .c files you must initialize the struct: perhaps in the main fucntion: 在.c文件之一中,您必须初始化struct:也许是在主要功能上:

int main(void)
{
    pUPCall = &uPCall;
    //other code  
    return 0;
} 

You can now include a pointer to struct in the thread worker function, (normally globals are at risk of access contention between threads, but you are protecting using mutex ), to get time of access attempt, and success of attempt 现在,您可以在线程工作器函数中包含指向struct的指针(通常全局变量有线程间访问争用的风险,但是您可以使用mutex进行保护),以获得访问尝试的时间以及尝试成功的时间。

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

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