Lecture – Real-Time Operating Systems (Chapter 11)

Ryan Robucci

Table of Contents

References

Multitasking systems

Tasks

Resources

Processor is a shared resource

Scheduling

Process State Diagram:

RunningReadyWaiting

Task 0run.ready-waitingrun.Task 1run.ready-waitingrun.Task 2run.ready-waitingrun.0102040507080100

Preempting and Context Switching Overhead

Task 0run.ready-waitingrun.Task 1ready-waitingrun.ready-waitingrun.Task 2run.ready-waitingrun.Processoridletask0task1task2task0task2task1idle0102030405060708090100110contentswitch

Overhead:

Task 0run.ready-waitingrun.Task 1ready-waitingrun.ready-waitingrun.Task 2run.ready-waitingrun.Processoridletask0c/sContextSwitchingtask1c/stask2c/stask0c/stask2c/stask1idle0102030405060708090100110120130contentswitch

Threads


†James K. Peckol Figure 11.7

Reentrant Code & Thread Safe Code

Example: Thread safe but not reentrant

Example:

int AFunction() {
// some function that checks and waits for
// availability of a resources and locks/reserves
// it so other processes won't access it
// -> makes this thread safe
wait_for_free_resource_and_then_lock_access();

do_some_wok();

//free/unreserved the resource
unlock_some_resource();
}

Example: Reentrant but not thread safe

int function() {
  char *filename="/etc/config";
  FILE *config;
  if(file_exist(filename)){

    config=fopen(filename,"r"); 
    
    ...assume success and
    ...read configuration from file

  } else{

    ...use program defaults...

  }
}
 
 
 
• check if the file exists
• what if file is deleted by another process at this point?
 
• Many OSs will prevent deletion while the file is open
• multiple calls can read the file at the same time
  • (writing might be dangerous)
 
 
 
 
 
 --code source: http://en.wikipedia.org/wiki/Thread_safety

Reentrant and Thread Safe Coding Practices

Kernel

Functions of Operating System

RTOS

Task Control Block

A simple kernel example

Implementation Model


†James K. Peckol Figure 11.13

Implementation with Function Calls (Round-ROBIN SCHEDULE)

// Building a simple OS kernel - step 1 
  redacted
  

















  

























}

Code Source: †James K. Peckol

Implementation with TCB and Shared Data





Task-as-Function Prototypes →




TCB →
Pointer to Manage Task's Data→
Task-as-Function Pointers→




Simple Data →





Setup 3 TCBs →











Queue of TCBs→






Call function from TCBs→
with Data mangaged through TCB    




Tasks conforming to Template→

                                                 // Building a simple OS kernel - step 2 
#include <stdio.h>
redacted





























































}

Code Source: †James K. Peckol

Implementation with ISR (Interrupt-Driven) and Global Variables for Flags/Messages




Global Variables to Share Data with Tasks and ISRs→









New TCB with only Task-as-Function Pointers→





















Some Platform-Dependant Function to Establish ISR→


Call function from each TCB→











// Building a simple OS kernel - step 3 #include <stdio.h>
redacted
























































}

Code Source: †James K. Peckol

check flag prompted →  

set flag prompted →  





check flag data available →  






check flag data available →  

clear flag prompt →  
clear flag data available →  










set flag data available →  
void prompt(void) { // perform input operation
redacted































}










← update data
    (consume
     &produce)




← consume &use
      data







← get data

← pre-
     process data**
• data is
      produced

Code Source: †James K. Peckol

** Keep ISRs Short

Sleep and Conditional Waiting States for Processes

void prompt(void) { // perform input operation
redacted





































Code Source: †James K. Peckol

Will discuss on OS with such implementations later.

Context Switching

Memory/Storage for Multiple Contexts

Slide Fixes

2022-08: Fixed corrupted figure depicting in-class drawing with work in task flagged from isr