Chapter 7: Semaphore Management Seulki Lee 1. Semaphore?  A protocol mechanism offered by most multitasking kernels  Control access to a shared resource.

Slides:



Advertisements
Similar presentations
Chapter 7 - Resource Access Protocols (Critical Sections) Protocols: No Preemptions During Critical Sections Once a job enters a critical section, it cannot.
Advertisements

More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Synchronization and Deadlocks
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
R4 Dynamically loading processes. Overview R4 is closely related to R3, much of what you have written for R3 applies to R4 In R3, we executed procedures.
Ch 7 B.
Resource management and Synchronization Akos Ledeczi EECE 354, Fall 2010 Vanderbilt University.
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Mike Holenderski, Synchronization 2IN60: Real-time Architectures (for automotive systems)
 Wind River Systems, Inc Chapter - 6 Semaphores.
ENTC-489 Embedded Real Time Software Development Embedded Real Time Software Development Week 6 Review I2C Communication, review fixed point arithmetic,
Embedded System Presentation Nguyễn Trần Quang Nguyễn Công Vũ 1µC/OS-II.
Chapter 5 Processes and Threads Copyright © 2008.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
1 Semaphores Special variable called a semaphore is used for signaling If a process is waiting for a signal, it is suspended until that signal is sent.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Select The select function determines the status of one or more sockets, waiting if necessary, to perform synchronous I/O. int select( int nfds, fd_set*
ΜC/OS-III Tasks Akos Ledeczi EECE 354, Fall 2010 Vanderbilt University.
Real Time Operating System
SOC Consortium Course Material SoC Design Laboratory Lab 8 Real-time OS - 2 Speaker: Yung-Chih Chen Advisor: Prof. Chun-Yao Wang November 17, 2003 Department.
The  C/OS-II Real-Time Operating System.  C/OS-II Real-time kernel –Portable, scalable, preemptive RTOS –Ported to over 90 processors Pronounced “microC.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
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.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Real Time Operating Systems
Real-time OS Hao-yuan Chin Feb. 18, Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.
REVIEW OF COMMONLY USED DATA STRUCTURES IN OS. NEEDS FOR EFFICIENT DATA STRUCTURE Storage complexity & Computation complexity matter Consider the problem.
Multiprogramming. Readings r Silberschatz, Galvin, Gagne, “Operating System Concepts”, 8 th edition: Chapter 3.1, 3.2.
® 7-2 Semaphores 7.1Overview Binary Semaphores and Synchronization Mutual Exclusion.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Real Time Operating Systems Mutual Exclusion, Synchronization & Intertask Communication Course originally developed by Maj Ron Smith.
Overview Task State Diagram Task Priority Idle Hook AND Co-Routines
ENTC-489 Embedded Real Time Software Development Embedded Real Time Software Development Week 5 Mail Boxes / Binary Semaphores Fixed Point Arithmetic.
ENTC-489 Embedded Real Time Software Development Embedded Real Time Software Development Week 4 Review.
RTX - 51 Objectives  Resources needed  Architecture  Components of RTX-51 - Task - Memory pools - Mail box - Signals.
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
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.
1 VxWorks 5.4 Group A3: Wafa’ Jaffal Kathryn Bean.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
SOC Consortium Course Material SoC Design Laboratory Lab 8 Real-time OS - 1 Speaker: Yung-Chih Chen Advisor: Prof. Chun-Yao Wang November 17, 2003 Department.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
2-1 Chapter 2 Real-Time Systems Concepts. 2-2 Foreground/Background Systems BackgroundForeground ISR Time Code execution application consists of an infinite.
Synchronization Akos Ledeczi EECE 6354, Fall 2013 Vanderbilt University.
Chapter 1: Getting Started with μC/OS-II 1. kernel Introduction 2 LinuxμC/OS-II Task (process) kernel Device driver User mode (0-3G) (Kernel mode) 3G-4G.
Outlines  Introduction  Kernel Structure  Porting.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
Chapter 2: Operating System Concepts 1. Objectives To know essential topics on the design of (embedded) operating systems 2 1.Task & resources 2.Kernel.
Multiprogramming. Readings r Chapter 2.1 of the textbook.
Chapter 1: Getting Started with μC/OS-II
Chapter 4: Task Management
PROCESS MANAGEMENT IN MACH
Topics Covered What is Real Time Operating System (RTOS)
Chapter 8: Mutual Exclusion Semaphores
Getting Started with the µC/OS-III Real Time Kernel
Realtime System Fundamentals : Scheduling and Priority-based scheduling B. Ramamurthy cse321-fall2014 9/20/2018.
Computer System Overview
Realtime System Fundamentals : Scheduling and Priority-based scheduling B. Ramamurthy cse321-fall /27/2018.
Last Week Introduced operating systems Discussed the Kernel
“The Little Book on Semaphores” Allen B. Downey
Chapter 6: Event Control Block (ECB)
Chapter 3: Process Management
Akos Ledeczi EECE 6354, Fall 2017 Vanderbilt University
Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University
Presentation transcript:

Chapter 7: Semaphore Management Seulki Lee 1

Semaphore?  A protocol mechanism offered by most multitasking kernels  Control access to a shared resource  Signal the occurrence of an event  Allow two tasks to synchronize their activities  In uC/OS-II, it consists of two elements  16-bit unsigned integer used to hold semaphore count  List of tasks waiting for semaphore  Provides 6 services to access semaphores  OSSemAccept(), OSSemCreate(), OSSemDel(), OSSemPend(), OSSemPost(), OSSemQuery() 2

Relationship between tasks, ISRs, and a semaphore Semaphore is used to access shared resources Semaphore is used to signal the occurrence of an event How many resources are available Number of times the event can be signaled Timeout that can be specified with the OSSemPend() call 3

Creating a semaphore, OSSemCreate()  A semaphore need to be created before used  Arguments  Value  Initial value of a semaphore  For synchronization or event: initialize to 0  For exclusive access to a resource: initialize to # of identical resources  Returned Value  Returns a pointer to the event control block allocated to the semaphore  If no ECB available, returns a NULL pointer 4

Deleting a semaphore, OSSemDel()  Used to delete a semaphore  Before delete a semaphore, you should first delete all the tasks that access the semaphore.  Arguments  Pevent: pointer to the semaphore  Opt  OS_DEL_NO_PEND: delete semaphore only if there are no pending tasks  OS_DEL_ALWAYS: delete semaphore regardless of whether tasks are pending or not  Err  OS_NO_ERR: if the call is successful and the semaphore has been deleted 5

Deleting a semaphore, OSSemDel()  OS_ERR_DEL_ISR: if you attempt to delete the semaphore from an ISR  OS_ERR_INVALID_OPT: if you don't specify one of the two options mentioned in the opt argument  OS_ERR_TASK_WAITING: if one or more tasks are waiting on the semaphore  OS_ERR_EVENT_TYPE: if pevent is not pointing to a semaphore  OS_ERR_PEVENT_NULL: if pevent is NULL  Returned Value  A NULL pointer if the semaphore is deleted  Pevent if the semaphore is not deleted  Need to examine the error code 6

Waiting a semaphore (Blocking), OSSemPend()  OSSemPend() is used when a task wants exclusive access to a resource, needs to synchronize its activities with an ISR or a task, or is waiting until an event occurs  If a task calls OSSemPend() and the value of the semaphore is greater than 0, OSSemPend() decrements the semaphore and returns to its caller  However, if the value of the semaphore is 0, OSSemPend() places the calling task in the waiting list for the semaphore 7

Waiting a semaphore (Blocking), OSSemPend()  The task waits until a task or an ISR signals the semaphore or the specified timeout expires  If the semaphore is signaled before the timeout expires, uC/OS-II resumes the highest priority task waiting for the semaphore  Arguments  Pevent: is a pointer to the semaphore.  Timeout: allows the task to resume execution if a message is not received within the specified number of clock ticks. A timeout value of 0 indicates that the task waits forever for the message. 8

Waiting a semaphore (Blocking), OSSemPend()  Err  OS_NO_ERR: if the semaphore is available  OS_TIMEOUT: if the semaphore is not signaled within the specified timeout  OS_ERR_EVENT_TYPE: if pevent is not pointing to a semaphore  OS_ERR_PENO_ISR: if you called this function from an ISR and uC/OS-II has to suspend it. You should not call OSSemPend() from an ISR.  OS_ERR_PEVENT_NULL: if pevent is a NULL pointer  Returned Value: None 9

Getting a semaphore without waiting (Non-blocking), OSSemAccept()  OSSemAccept() checks to see if a resource is available or an event has occurred.  Unlike OSSemPend(), OSSemAccept() does not suspend the calling task if the resource is not available.  Use OSSemAccept() from an ISR to obtain the semaphore.  Arguments  Pevent: is a pointer to the semaphore. 10

Getting a semaphore without waiting (Non-blocking), OSSemAccept()  Returned Value  When OSSemAccept() is called and the semaphore value is greater than 0, the semaphore value is decremented, and the value of the semaphore before the decrement is returned  If the semaphore value is 0 when OSSemAccept() is called, the resource is not available, and 0 is returned 11

Signaling a semaphore, OSSemPost()  A semaphore is signaled by calling OSSemPost().  If the semaphore value is 0 or more, it is incremented, and OSSemPost() returns to its caller  If tasks are waiting for the semaphore to be signaled, OSSemPost() removes the highest priority task pending for the semaphore from the waiting list and makes this task ready to run.  The scheduler is then called to determine if the awakened task is now the highest priority task ready to run. 12

Signaling a semaphore, OSSemPost()  Arguments  Pevent: is a pointer to the semaphore.  Returned Value  Error codes  OS_NO_ERR: if the semaphore is signaled successfully  OS_SEM_OVF: if the semaphore count overflows  OS_ERR_EVENT_TYPE: if pevent is not pointing to a semaphore  OS_ERR_PEVENT_NULL: if pevent is a NULL pointer 13

Obtaining the status of a semaphore, OSSemQuery()  OSSemQuery() obtains information about a semaphore.  Your application must allocate an OS_SEM_DATA data structure used to receive data from the event control block of the semaphore  OSSemQuery() allows you to determine whether any tasks are waiting on the semaphore and how many tasks are waiting and obtains the semaphore count.  Arguments  Pevent: pointer to the semaphore 14

Obtaining the status of a semaphore, OSSemQuery()  Pdata: pointer to a data structure of type OS_S EM_DATA, which contains the following fields  INT16U OSCnt;  INT8U OSEventTbl[COS_EVENT_TBL_SIZE];  INT8U OSEventGrp;  Returned Value  Error codes:  OS_NO_ERR: if the call is successful  OS_ERR_EVENT_TYPE: if you don't pass a pointer to a semaphore  OS_ERR_PEVENT_NULL: if pevent is a NULL pointer 15

Thank You Questions? 16