Skip to content

Scheduling

Victor Correal Ramos edited this page May 2, 2020 · 10 revisions

Introduction

In this stage I design a Round Robin scheduling system to design CPU time for the differents tasks.

This task distribution assign a certain time to each task (named Quantum); after that time the schedule algorithm searches another task to assign a new quantum. Also, a task manager is needed; for the moment only exists a task_creation interface.

For a example, you can watch the following link.

Design

For design this Round Robin, we need a data structure that saves a certain state of a task, some logical control for the scheduling process and the capability for change the machine state.

The task struct

A task is defined by the PC register, the SP and the safety-registers (a convention designs x19 to x30 safety registers). Our task struct needs to save this registers in memory with some control and identification information, useful for the scheduling logic.

The scheduling logic

The scheduling logic is called every time a timer interrupt occurs. In this code, we need to update the scheduling data (in our case the Quantum but we can add other variables). After that, we need to control if the current task has run out all the quantum, in that case, we need to search a new task to run and change the execution context Also, for this Real Time OS, a task can control the preemption (in the future will be implemented). This is a very important feature, because for some uses is important to don't allow it.

Jumping to a new state - Task switch

For change the context, an assembler entry is needed, because we need to restore the values of all the safety registers and the PC. This entry will be called by the scheduler if a task switch is required.

Implementation

The first step is define a task, that contains the following fields:

typedef struct task{
    struct cpu_context myContext; //Context
    tid tid;                	  //Task IDentifier
    task_info myInfo;             //Some statics
    int quantumLeft;              //Quantum left
    bool preemption_enable;       //True for avoid context switch
    enum state state;             //Actual state of the task (RUN, READY, BLOCKED)
}task;

Once a task is created, we need to create the task. A task is defined by the code/instructions and the arguments/parameters, so the interface:

sys_response init_task(void * fn, void * data);

In addition the task managment, we need some structures and policy for the scheduling process:

  • ready Queue : Here the task wait in order to a quantum of CPU.
  • next task policy : In this case, the next in the ready queue. The sched_next_rr() search in this queue for the next task to be executed.
  • nTask : A counter for design a unique TID to all the new tasks.
  • quantumLeft : Remaining quantum for a context switch. In every timer tick, this variable is decremented.
  • need replacement : Condition for search the next task. In this case, is the quantum is 0 and the preemption is enabled. The needs_sched_rr() implements this feature.
  • idle task: This task is the one if we need a replacement and the ready queue is empty.

The init_schedulign() method initialize all the necessary structures. For perform a context switch, I use the assembler entry cpu_switch_to(prev, next), this assembler instructions restore all the registers saved before. In order to maintain the structures updated, the task switch manages the states and the ready queue

All the schedule logic is inside taskscheduling.c and taskscheduling.h files.

All together

In this point, the OS can run several threads, that share memory (there isn't any restrictions about) and resources (UART). For avoid data-races in the future a synchronization API for lock/unlock resources is needed.

Clone this wiki locally