Akos Ledeczi EECE 6354, Fall 2017 Vanderbilt University

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 8 SCHEDULING.
Ch 7 B.
Resource management and Synchronization Akos Ledeczi EECE 354, Fall 2010 Vanderbilt University.
0 Synchronization Problem Resource sharing –Requires mutual exclusion –Critical section A code section that should be executed mutually exclusively by.
CprE 458/558: Real-Time Systems (G. Manimaran)1 CprE 458/558: Real-Time Systems Resource Access Control Protocols.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Interprocess Communication
 Wind River Systems, Inc Chapter - 6 Semaphores.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
UCDavis, ecs251 Fall /23/2007ecs251, fall Operating System Models ecs251 Fall 2007 : Operating System Models #3: Priority Inversion Dr. S.
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
ΜC/OS-III Tasks Akos Ledeczi EECE 354, Fall 2010 Vanderbilt University.
Introduction to Embedded Systems
Getting Started with the µC/OS-III Real Time Kernel Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University.
2-1 The critical section –A piece of code which cannot be interrupted during execution Cases of critical sections –Modifying a block of memory shared by.
Scheduling in µC/OS-III Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University.
CSE451 NT Synchronization Autumn 2002 Gary Kimura Lecture #9 October 18, 2002.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
Midterm 1 – Wednesday, June 4  Chapters 1-3: understand material as it relates to concepts covered  Chapter 4 - Processes: 4.1 Process Concept 4.2 Process.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
® 7-2 Semaphores 7.1Overview Binary Semaphores and Synchronization Mutual Exclusion.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
Real Time Operating Systems Mutual Exclusion, Synchronization & Intertask Communication Course originally developed by Maj Ron Smith.
ECGR-6185 µC/OS II Nayana Rao University of North Carolina at Charlotte.
MicroC/OS-II S O T R.  MicroC/OS-II (commonly termed as µC/OS- II or uC/OS-II), is the acronym for Micro-Controller Operating Systems Version 2.  It.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Synchronization.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Chapter 7: Semaphore Management Seulki Lee 1. Semaphore?  A protocol mechanism offered by most multitasking kernels  Control access to a shared resource.
Synchronization Akos Ledeczi EECE 6354, Fall 2013 Vanderbilt University.
Outlines  Introduction  Kernel Structure  Porting.
Process Synchronization. Concurrency Definition: Two or more processes execute concurrently when they execute different activities on different devices.
Outline Introduction to task synchronization Queues of FreeRTOS
REAL-TIME OPERATING SYSTEMS
Topics Covered What is Real Time Operating System (RTOS)
CS703 – Advanced Operating Systems
Background on the need for Synchronization
6.0 INTRODUCTION TO REAL-TIME OPERATING SYSTEMS (RTOS/RTK)
Chapter 8: Mutual Exclusion Semaphores
Interrupt and Time Management in µC/OS-III
Getting Started with the µC/OS-III Real Time Kernel
An Embedded Software Primer
Chapter 5: Process Synchronization
Scheduling in µC/OS-III
Akos Ledeczi EECE 6354, Fall 2017 Vanderbilt University
Topic 6 (Textbook - Chapter 5) Process Synchronization
Semaphore Originally called P() and V() wait (S) { while S <= 0
Akos Ledeczi EECE 6354, Fall 2017 Vanderbilt University
Lecture 2 Part 2 Process Synchronization
Concurrency: Mutual Exclusion and Process Synchronization
Computer Science & Engineering Electrical Engineering
CSCI1600: Embedded and Real Time Software
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
CSCI1600: Embedded and Real Time Software
CSE 451 Section 1/27/2000.
CSE 153 Design of Operating Systems Winter 2019
EECE.4810/EECE.5730 Operating Systems
Process/Thread Synchronization (Part 2)
Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University
Akos Ledeczi EECE 6354, Fall 2017 Vanderbilt University
Presentation transcript:

Akos Ledeczi EECE 6354, Fall 2017 Vanderbilt University Resource Management Akos Ledeczi EECE 6354, Fall 2017 Vanderbilt University

Resource Sharing Disable/Enable interrupts: only where critical region is very quick. Should be used sparingly in RT systems. Lock/Unlock scheduler: for quick critical sections still. Semaphore: when affected tasks do not have hard deadlines (priority inversion) Mutex: preferred method. Largest overhead though because it does extra work to avoid priority inversion

Disable/Enable Interrupts void OSFunction() { CPU_SR_ALLOC(); CPU_CRITICAL_ENTER(); : /* critical section */ CPU_CRITICAL_EXIT(); } OS uses this method (even when implementing the other methods) Allocates storage for the interrupt disable status Enter() saves status, disables interrupts Exit() restores status The only way to share a resource between a task and an ISR!

Lock/Unlock Scheduler void Function() { OS_ERR err; OSSchedLock(&err); if (err = OS_ERR_NONE) { : /* critical section */ OSSchedUnlock(&err); /* check err */ } else { Interrupts are not disabled, but ISR returns to this task even if higher priority tasks are ready: behaves as a non-preemptive kernel Lock/Unlock can be nested Unlock runs scheduler (only when nesting is unwound) Must not make blocking calls while the scheduler is locked Behaves as if current task had the highest priority

Semaphores Dijkstra in 1959 “Key” to “locked code.” You need to acquire it to access the code. Semaphore operations are “atomic.” Binary and counting semaphores Can be used for resource sharing and synchronization Functions: OSSemCreate() OSSemPend() OSSemPost() OSSemDel() OSSemSet() OSSemPendAbort()

Binary Semaphores Accessing a printer Hiding behind an API

Binary Semaphores cont’d. OS_SEM MySem; void main() { OS_ERR err; … OSSemCreate(&MySem, ”My Semaphore”, 1, &err); } void Task1 (void *p_arg) CPU_TS ts; while (1) { OSSemPend(&MySem, 0, OS_OPT_PEND_BLOCKING, &ts, &err); switch(err) case OS_ERR_NONE: /*critical section */ OSSemPost(&MySem, OS_OPT_POST_1, &err); /* check err */ break; case OS_ERR_PEND_ABORT: case OS_ERR_OBJ_DEL: default: /* other errors */

Counting Semaphores When multiple resources/resource elements are available E.g., memory pool or circular buffer Initialize semaphore to the number of items available Need to manage consumed/available items Pend() waits on 0, otherwise, decrements counter and returns Post() increments counter

Notes Semaphores do not increase interrupt latency Note that while the OS handles the semaphore itself, interrupts are disabled Hence, protecting access to a single variable, for example, is much faster using interrupt disable/enable directly. Must be careful (e.g., a floating point operation on a CPU with no FPU can take very long)! Application can have as many semaphores as necessary

Notes too OSSemPend(): OSSemPost(): If ctr > 0, ctr is decremented, fn returns immediately If ctr == 0 OS_OPT_PEND_NON_BLOCKING: returns immediately with appropriate error code. Task can do something else and check back later. OS_OPT_PEND_BLOCKING: blocks, gets inserted in pend list, highest priority ready task gets the CPU Non-zero timeout: prevents waiting forever inserted in tick list when expired, error code specifies that timeout expired not the best way to break deadlock OSSemPost(): If waiting task(s), gets highest priority task and switches to it If not, increments counter Option allows not to run scheduler (in case task wants multiple posts)

Semaphore Internals typedef struct os_sem OS_SEM; struct os_sem { OS_OBJ_TYPE Type; /* Should be set to OS_OBJ_TYPE_SEM */ CPU_CHAR *NamePtr; OS_PEND_LIST PendList; OS_SEM_CTR Ctr; CPU_TS TS; }; No distinction between binary and counting semaphores (see mutex later) Type is needed to distinguish between different kernel objects when using the generic OS_PEND_OBJECT type Pend list is needed as multiple tasks may be waiting on a semaphore Timestamp indicating the last Post() operation Do not access fields directly, use API

Priority Inversion with Semaphores A medium priority task can have the CPU when a lower priority holds a semaphore that a high priority task is waiting for

Mutual Exclusion Semaphore: Mutex Binary semaphore with special handling to avoid priority inversion Changes task priorities when necessary: called priority inheritance: if a higher priority task requests a resource, the priority of the current owner will be raised to that of the requester

Mutex OSMutexCreate() OSMutexPend() OSMutexPost() OSMutexDel() OSMutexPendAbort() Can be nested: it is only available when Post() was called as many times as Pend() Can be blocking or non-blocking Check return value/error code of Pend() to see why it returned (timeout, abort, delete, etc.) Do not make function calls in critical sections

Mutex Internals typedef struct os_mutex OS_MUTEX; struct os_mutex { OS_OBJ_TYPE Type; /* Should be set to OS_OBJ_TYPE_MUTEX */ CPU_CHAR *NamePtr; OS_PEND_LIST PendList; OS_TCB *OwnerTCBPtr; OS_PRIO OwnerOriginalPrio; OS_NESTING_CTR OwnerNestingCtr; /* Mutex is available when the counter is 0 */ CPU_TS TS; }; Type is needed to distinguish between different kernel objects when using the generic OS_PEND_OBJECT type Pend list is needed as multiple tasks may be waiting on a semaphore Timestamp indicating the last Post() operation Owner task and its original priority need to be stored Do not access fields directly, use API Post() gets timestamp decrements the nesting counter and if it reaches 0 resets priority if necessary sets owner to 0 Gets highest priority task from pend list

Mutex Notes Same task can issue multiple Pend()-s, it will not block if the first succeeds (unlike regular Semaphores in uCos-III). It will simply increment the nesting counter. If Pend() blocks, the priority of the current owner is raised to the blocked task if it is higher than that of the current task Post() decrements the nesting counter. If counter is non-zero it returns OS_ERR_MUTEX_NESTING. Last Post() (i.e., when nesting counter reaches 0) resets the priority of the task to the original value and calls the scheduler

Deadlock Void Task1(void *p_arg) { while (1) { … Acquire M1 Access R1 /* INTERRUPT */ Acquire M2 Access R2 Release M2 Release M1 } Void Task2(void *p_arg) { while (1) { … Acquire M2 Access R2 Acquire M1 Access R1 Release M1 Release M2 }

Deadlock Avoidance Tips: Void Task1(void *p_arg) { while (1) { … Acquire M1 Acquire M2 Access R1 Access R2 Release M1 Release M2 } Void Task2(void *p_arg) { while (1) { … Acquire M1 Acquire M2 Access R1 Access R2 Release M1 Release M2 } Tips: Try not to acquire more than one mutex at a time Try not to acquire a mutex directly (hide them in functions) Acquire all resources needed before proceeding Always acquire resources in the same order