【问题标题】:Implementing a 2D queue for Function Queue Scheduling为函数队列调度实现二维队列
【发布时间】:2013-04-05 09:54:26
【问题描述】:

我正在编写一些 C 代码来模拟嵌入式系统的运行。事件从一定数量的设备发生,并被赋予优先级 0-3。我必须服务的事件的优先级主要基于设备号(设备 0 > 设备 3 > 设备 7),然后每个设备服务的事件的优先级基于它们提供的优先级。

我得出的结论是,2D 队列可能是实现该系统的最佳方式。带有 for 循环的数组被认为是“浪费的”,并且大多数函数队列调度使用像 while(notEmpty) 这样的循环。

我的粗略计划是创建两个看起来像这样的“节点”结构:

struct events{
    Event curEvent; //the "node"
    struct events *next; //leads to event of lower priority
};
struct devices { //a linked list of Devices
    int deviceNumber; //the "node"
    struct devices *next;//leads to next device in order 0->max
    struct events *headEevent; //a linked list of events per device
};

我从命令行提供了要服务的设备数量和每个设备的最大事件数。

我想我的问题是双重的。首先是,我在正确的轨道上吗?第二个(也许更重要)是,“初始化这个 2D 队列的最佳方式是什么,以及在事件发生时将它们排入队列的最佳方式是什么?”

现在我的初始化代码是错误的,我感觉:

curDevice = (struct device * ) malloc (sizeof (struct device));
deviceHead = curDevice;//sets head to first that's initialized
for (i = 0; i< Number_Devices; i++) {
    curDevice -> deviceNumber = i;
    curEvent = (struct event * ) malloc (sizeof (struct event));
    curDevice -> headEvent = curEvent; //sets head event to the empty curEvent
    for (j = 0; j<Q; j++) { //Q is max queue size
        newEvent = (struct event* ) malloc (sizeof struct event));
        curEvent -> next = newEvent;
        curEvent = newEvent;
    }
    new_device = (struct device * ) malloc (sizeof (struct device));
    curDevice -> next = newDevice;
    curDevice = newDevice;
}

我想我可以自己发现如何入队。我会使用 while 循环来遍历链表,如果当前事件的优先级高于单个设备堆栈上的优先级,我会将其推到顶部。

这是一个冗长而复杂的问题,因此请随时提出您可能需要的任何说明。提前致谢!

【问题讨论】:

  • 你有任何操作系统,比如信号量吗?

标签: c embedded queue


【解决方案1】:

为什么不使用单个链表(队列)然后将任务插入到末尾。当您想选择下一个任务时,只需检查它并找到具有最高优先级的任务并从列表中删除。

【讨论】:

  • 您的建议是使用单个事件队列,存储它们的设备编号和优先级。中断处理程序运行后,我会遍历整个队列,然后在该单个队列中提供最高优先级的事件?
  • 某同学的实现是一个固定数组,其元素是链表,指向存储的事件。也许我对 FQS 的工作方式很愚蠢。如果队列中有较低优先级的事件,并且队列空间有限,我是否要删除最低优先级的事件并将较新的事件排入队列?
  • @Ryanman,是的,你描述的正是我的意思。当您删除元素时 - 只需将其删除即可(并释放内存)。链表不是固定空间的,不用担心。
猜你喜欢
  • 1970-01-01
  • 2017-10-26
  • 1970-01-01
  • 2014-09-03
  • 1970-01-01
  • 2016-04-10
  • 1970-01-01
  • 2017-04-08
  • 2018-01-22
相关资源
最近更新 更多