Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Review 1: 关于cooperative multitasking的讨论 #2

Open
LeeReindeer opened this issue Jan 22, 2022 · 1 comment
Open

Code Review 1: 关于cooperative multitasking的讨论 #2

LeeReindeer opened this issue Jan 22, 2022 · 1 comment
Assignees

Comments

@LeeReindeer
Copy link
Owner

ROS/ros.c

Line 139 in 048799c

new_tcb = ros_tcb_dequeue(current_tcb->priority);

else {
    // remove terminated task
    do {
      new_tcb = ros_tcb_dequeue(current_tcb->priority);
    } while (new_tcb && new_tcb->status == TASK_TERMINATED);
    if (new_tcb) {
      ros_tcb_enqueue(current_tcb);
      ros_switch_context_shell(current_tcb, new_tcb);
    }
  1. 是否存在一个低优先级任务被调度器切出,而被高优先级任务抢占的情况?
  2. 调度器会进入该 else 分支吗(存在需要删除TASK_TERMINATED的任务的情况吗?)?如不存在可以删除该分支
@LeeReindeer LeeReindeer self-assigned this Jan 22, 2022
@LeeReindeer
Copy link
Owner Author

LeeReindeer commented Jan 22, 2022

对于第一个问题,比方说一个优先级为1(0为优先级最高)的任务 t1 正在运行,也就说明当前就绪队列中没有优先级比1更高的任务,优先级更高的任务不存在或者在阻塞队列中。t1 运行一段时间后,新增一个优先级为0的任务 t0,下次调度时new_tcb = ros_tcb_dequeue(current_tcb->priority); 会返回 t0 任务,并进行上下文切换,所以优先级更高的 t0 任务会抢占 t1 任务运行。

ROS 算是 preemptive multitasking,可以通过适当的编码方式实现 cooperative multitasking:

  • 低优先级的任务会被高优先级的无条件抢占
  • 在任务的 priority 相等时,ROS 采用 round-robin 根据时间片调度
  • 低优先级任务可能一直得不到调度,除非高优先级任务主动 suspend
    • 对于高优先级任务来说是 cooperative multitasking:除非主动 suspend ,否则不会被抢占。
    • 对于低优先级任务来说是 preemptive multitasking:会被高优先级的无条件抢占

对于第二个问题,已经忘了当初写这段代码的本意是什么,如上所述,会进入该段逻辑,但是不存在需要删除TASK_TERMINATED的情况。反而造成 ROS是 preemptive multitasking,可能是当初没搞清概念。

@LeeReindeer LeeReindeer pinned this issue Jan 22, 2022
@LeeReindeer LeeReindeer added the bug Something isn't working label Jan 23, 2022
@LeeReindeer LeeReindeer changed the title Code Review 1 Code Review 1: 关于cooperative multitasking的讨论 Jan 23, 2022
@LeeReindeer LeeReindeer removed the bug Something isn't working label Sep 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant