Docsity

Real-Time OS: Sync & Implementation with Concurrent Processes & Condition Vars, Slides of Computer Science

Partial preview of the text

Download Real-Time OS: Sync & Implementation with Concurrent Processes & Condition Vars and more Slides Computer Science in PDF only on Docsity!

5-High-Performance Embedded

Systems using Concurrent Process

(cont.)

Outline

• Models vs. Languages

• State Machine Model

  • FSM/FSMD
  • HCFSM and Statecharts Language
  • Program-State Machine (PSM) Model

• Concurrent Process Model

  • Communication
  • Synchronization
  • Implementation

• Dataflow Model

• Real-Time Operating Systems

Condition variables

  • Condition variable is an object that has 2 operations, signal and wait
  • When process performs a wait on a condition variable, the process is blocked until another

process performs a signal on the same condition variable

  • How is this done?
    • Process A acquires lock on a mutex
    • Process A performs wait, passing this mutex
      • Causes mutex to be unlocked
    • Process B can now acquire lock on same mutex
    • Process B enters critical section
      • Computes some value and/or make condition true
    • Process B performs signal when condition true
      • Causes process A to implicitly reacquire mutex lock
      • Process A becomes runnable

Condition variable example:

consumer-producer

  • 2 condition variables
    • buffer_empty
      • Signals at least 1 free location available in buffer
    • buffer_full
      • Signals at least 1 valid data item in buffer
  • processA :
    • produces data item
    • acquires lock ( cs_mutex ) for critical section
    • checks value of count
    • if count = N , buffer is full
      • performs wait operation on buffer_empty
      • this releases the lock on cs_mutex allowing processB to

enter critical section, consume data item and free location in

buffer

  • processB then performs signal
  • if count < N , buffer is not full
  • processA inserts data into buffer
  • increments count
  • signals processB making it runnable if it has performed a

wait operation on buffer_full

01: data_type buffer[N]; 02: int count = 0; 03: mutex cs_mutex; 04: condition buffer_empty, buffer_full; 06: void processA() { 07: int i; 08: while( 1 ) { 09: produce(&data); 10: cs_mutex.lock(); 11: if( count == N ) buffer_empty.wait(cs_mutex); 13: buffer[i] = data; 14: i = (i + 1) % N; 15: count = count + 1; 16: cs_mutex.unlock(); 17: buffer_full.signal(); 18: } 19: } 20: void processB() { 21: int i; 22: while( 1 ) { 23: cs_mutex.lock(); 24: if( count == 0 ) buffer_full.wait(cs_mutex); 26: data = buffer[i]; 27: i = (i + 1) % N; 28: count = count - 1; 29: cs_mutex.unlock(); 30: buffer_empty.signal(); 31: consume(&data); 32: } 33: } 34: void main() { 35: create_process(processA); create_process(processB); 37: }

Consumer-producer using condition variables

Monitor example: consumer-producer

  • Single monitor encapsulates both processes along with

buffer and count

  • One process will be allowed to begin executing first
  • If processB allowed to execute first
    • Will execute until it finds count = 0
    • Will perform wait on buffer_full condition variable
    • processA now allowed to enter monitor and execute
    • processA produces data item
    • finds count < N so writes to buffer and increments count
    • processA performs signal on buffer_full condition variable
    • processA blocked
    • processB reenters monitor and continues execution,

consumes data, etc.

01: Monitor { 02: data_type buffer[N]; 03: int count = 0; 04: condition buffer_full, condition buffer_empty; 06: void processA() { 07: int i; 08: while( 1 ) { 09: produce(&data); 10: if( count == N ) buffer_empty.wait(); 12: buffer[i] = data; 13: i = (i + 1) % N; 14: count = count + 1; 15: buffer_full.signal(); 16: } 17: } 18: void processB() { 19: int i; 20: while( 1 ) { 21: if( count == 0 ) buffer_full.wait(); 23: data = buffer[i]; 24: i = (i + 1) % N; 25: count = count - 1; 26: buffer_empty.signal(); 27: consume(&data); 28: buffer_full.signal(); 29: } 30: } 31: } /* end monitor */ 32: void main() { 33: create_process(processA); create_process(processB); 35: }

Implementation

  • Mapping of system’s functionality onto

hardware processors:

  • captured using computational model(s)
  • written in some language(s)
  • Implementation choice independent from

language(s) choice

  • Implementation choice based on power, size,

performance, timing and cost requirements

  • Final implementation tested for feasibility
    • Also serves as blueprint/prototype for mass

manufacturing of final product

The choice of

computational

model(s) is based

on whether it

allows the designer

to describe the

system.

The choice of

language(s) is

based on whether

it captures the

computational

model(s) used by

the designer.

The choice of

implementation is

based on whether it

meets power, size,

performance and

cost requirements.

Sequent. program

State machine

Data- flow

Concurrent processes

Pascal C/C++ Java VHDL

Implementation A Implementation B

Implementation C

Implementation:

multiple processes sharing single

processor

  • Can manually rewrite processes as a single sequential program
    • Ok for simple examples, but extremely difficult for complex examples
    • Automated techniques have evolved but not common
    • E.g., simple Hello World concurrent program from before would look like:

I = 1; T = 0;

while (1) {

Delay(I); T = T + 1;

if X modulo T is 0 then call PrintHelloWorld

if Y modulo T is 0 then call PrintHowAreYou

  • Can use multitasking operating system
    • Much more common
    • Operating system schedules processes, allocates storage, and interfaces to peripherals, etc.
    • Real-time operating system (RTOS) can guarantee execution rate constraints are met
    • Describe concurrent processes with languages having built-in processes (Java, Ada, etc.) or a sequential programming language with library

support for concurrent processes (C, C++, etc. using POSIX threads for example)

  • Can convert processes to sequential program with process scheduling right in code
    • Less overhead (no operating system)
    • More complex/harder to maintain

Processes vs. threads

  • Different meanings when operating system terminology
  • Regular processes
    • Heavyweight process
    • Own virtual address space (stack, data, code)
    • System resources (e.g., open files)
  • Threads
    • Lightweight process
    • Subprocess within process
    • Only program counter, stack, and registers
    • Shares address space, system resources with other threads
      • Allows quicker communication between threads
    • Small compared to heavyweight processes
      • Can be created quickly
      • Low cost switching between threads

Implementation: Scheduling

  • Must meet timing requirements when multiple concurrent processes implemented on single

general-purpose processor

  • Not true multitasking
  • Scheduler
  • Special process that decides when and for how long each process is executed
  • Implemented as preemptive or nonpreemptive scheduler
  • Preemptive
  • Determines how long a process executes before preempting to allow another process to

execute

  • Time quantum: predetermined amount of execution time preemptive scheduler allows each process

(may be 10 to 100s of milliseconds long)

  • Determines which process will be next to run
  • Nonpreemptive
  • Only determines which process is next after current process finishes execution

Scheduling: priority

  • Process with highest priority always selected first by scheduler
    • Typically determined statically during creation and dynamically during execution
  • FIFO
    • Runnable processes added to end of FIFO as created or become runnable
    • Front process removed from FIFO when time quantum of current process is up or process is blocked
  • Priority queue
    • Runnable processes again added as created or become runnable
    • Process with highest priority chosen when new process needed
    • If multiple processes with same highest priority value then selects from them using first-come first- served
    • Called priority scheduling when nonpreemptive
    • Called round-robin when preemptive

Real-time operating systems

  • Systems composed of 2 or more cooperating, concurrent processes with stringent execution

time constraints

  • E.g., set-top boxes have separate processes that read or decode video and/or sound concurrently

and must decode 20 frames/sec for output to appear continuous

  • Other examples with stringent time constraints are:
    • digital cell phones
    • navigation and process control systems
    • assembly line monitoring systems
    • multimedia and networking systems
    • etc.
  • Communication and synchronization between processes for these systems is critical
  • Therefore, concurrent process model best suited for describing these systems

Real-time operating systems

(RTOS)

  • Provide mechanisms, primitives, and guidelines for building real-time embedded systems
  • Windows CE
    • Built specifically for embedded systems and appliance market
    • Scalable real-time 32-bit platform
    • Supports Windows API
    • Perfect for systems designed to interface with Internet
    • Preemptive priority scheduling with 256 priority levels per process
    • Kernel is 400 Kbytes
  • QNX
    • Real-time microkernel surrounded by optional processes (resource managers) that provide POSIX and UNIX compatibility
      • Microkernels typically support only the most basic services
      • Optional resource managers allow scalability from small ROM-based systems to huge multiprocessor systems connected by various

networking and communication technologies

  • Preemptive process scheduling using FIFO, round-robin, adaptive, or priority-driven scheduling
  • 32 priority levels per process
  • Microkernel < 10 Kbytes and complies with POSIX real-time standard