Semaphores. Chapter. verview inary Semaphores for Task Synchronization utex Semaphores to Solve Mutual Exclusion Problems

Size: px
Start display at page:

Download "Semaphores. Chapter. verview inary Semaphores for Task Synchronization utex Semaphores to Solve Mutual Exclusion Problems"

Transcription

1 Chapter 7 Semaphores Tornado Training Workshop Copyright 7-1 verview inary Semaphores for Task Synchronization utex Semaphores to Solve Mutual Exclusion Problems

2 7.1 Overview Semaphores Binary Semaphores and Synchronization Mutual Exclusion Tornado Training Workshop Copyright 7-2 hat are semaphores? ow are they used

3 Overview A Semaphore is a kernel primitive object. Semaphore operations: Can change a task s state. Are fast. Three semaphore types available: Binary semaphores allow a task to pend until a given event occurs (e.g., an interrupt). Mutual exclusion semaphores allow a task to acquire an exclusive lock on a shared resource (e.g., a file or a device). Counting semaphores are available: Less commonly used. See manual pages for details. Tornado Training Workshop Copyright 7-3

4 Overview Semaphores 7.2 Binary Semaphores and Synchronization Mutual Exclusion Tornado Training Workshop Copyright 7-4 ynchronization problem. inary semaphore solution.

5 The Synchronization Problem Task mygetdata ( ) { requestdata( ); waitfordata( ); getdata( ); } Task may need to wait for an event to occur. Busy waiting (i.e., polling) is inefficient. Pending until the event occurs is better. Tornado Training Workshop Copyright 7-5

6 The Synchronization Solution Create a binary semaphore for the event. Binary semaphores exist in one of two states: Full (event has occurred). Empty (event has not occurred). Task waiting for the event calls semtake( ) and blocks until semaphore is given. Task or interrupt service routine detecting the event calls semgive( ), which unblocks the waiting task. Tornado Training Workshop Copyright 7-6

7 Binary Semaphores SEM_ID sembcreate (options, initialstate) options Specify queue type (SEM_Q_PRIORITY or SEM_Q_FIFO) for tasks pended on this semaphore. initialstate Initialize semaphore to be available (SEM_FULL) or unavailable (SEM_EMPTY). Semaphores used for synchronization are typically initialized to SEM_EMPTY (event has not occurred). Returns a SEM_ID, or NULL on error. Tornado Training Workshop Copyright 7-7 Symbolic constants defined in semlib.h.

8 Taking a Semaphore STATUS semtake (semid, timeout) semid The SEM_ID returned from sembcreate( ). timeout Can pend the task until either Semaphore is given or Timeout expires. Semaphore left unavailable. Maximum time to wait for semaphore. Value can be clock ticks, WAIT_FOREVER, or NO_WAIT. Returns OK if successful, ERROR on timeout (or invalid semid). Tornado Training Workshop Copyright 7-8

9 Taking a Binary Semaphore semaphore available? no task pends until sem is given or timeout timeout task unpends, semtake( ) returns ERROR yes task continues, semtake( ) returns OK semaphore given task unpends, semtake( ) returns OK Tornado Training Workshop Copyright 7-9 The semaphore is always left unavailable after a semtake( ).

10 Giving a Semaphore STATUS semgive (semid) Unblocks a task waiting for semid. If no task is waiting, makes semid available. Returns OK, or ERROR if semid is invalid. Tornado Training Workshop Copyright 7-10 Unblocking a higher priority task will preempt caller of semgive( ).

11 Giving a Binary Semaphore tasks pended? no semaphore made available yes task at front of queue made ready, semaphore remains Tornado Training Workshop Copyright 7-11 See the code example Synchronization Solution on page 2 of ppendix A.

12 Information Leakage Fast event occurrences can cause information loss. Suppose a VxWorks task (priority=100) is executing the following code, with semid initially unavailable: FOREVER { semtake (semid, WAIT_FOREVER); printf ( Got the semaphore\n ); } What would happen in the scenarios below? 1. -> repeat(1, semgive, semid) 2. -> repeat(2, semgive, semid) 3. -> repeat(3, semgive, semid) Tornado Training Workshop Copyright 7-12

13 Synchronizing Multiple Tasks STATUS semflush (semid) Unblocks all tasks waiting for semaphore. Does not affect the state of a semaphore. Useful for synchronizing actions of multiple tasks. Tornado Training Workshop Copyright 7-13 This routine is not available for mutual exclusion semaphores.

14 Overview Semaphores Binary Semaphores and Synchronization 7.3 Mutual Exclusion Tornado Training Workshop Copyright 7-14 ace conditions. btaining exclusive access to a resource via mutex semaphores.

15 Mutual Exclusion Problem Some resources may be left inconsistent if accessed by more than one task simultaneously. Shared data structures Shared files Shared hardware devices Must obtain exclusive access to such a resource before using it. If exclusive access is not obtained, then the order in which tasks execute affects correctness. We say a race condition exists. Very difficult to detect during testing. Mutual exclusion cannot be compromised by priority. Tornado Training Workshop Copyright 7-15

16 Race Condition Example ttask1 ttask2 1 char mybuf[buf_size]; /* Store data here */ 2 int mybufindex = -1; /* Index of last data */ 3 4 mybufput (char ch) 5 { 6 mybufindex++; 7 mybuf[mybufindex]=ch; 8 } mybufindex++ mybuf[mybufindex]= b mybufindex++ mybuf[mybufindex]= a... Tornado Training Workshop Copyright 7-16 Multiple tasks calling mybufput( ) may disrupt each other: Suppose ttask1 wants to add b to the buffer, and ttask2 wants to add a to the buffer. If the events happen as above, then a will be overwritten in its buffer slot, and the previous slot will contain invalid data.

17 Solution Overview Create a mutual exclusion semaphore to guard the resource. Call semtake( ) before accessing the resource; call semgive( ) when done. semtake( ) will block until the semaphore (and hence the resource) becomes available. semgive( ) releases the semaphore (and hence access to the resource). Tornado Training Workshop Copyright 7-17

18 Creating Mutual Exclusion Semaphores SEM_ID semmcreate (options) options can be: queue specification deletion safety priority inheritance SEM_Q_FIFO or SEM_Q_PRIORITY SEM_DELETE_SAFE SEM_INVERSION_SAFE Initial state of semaphore is available. Tornado Training Workshop Copyright 7-18 Symbolic constants defined in semlib.h. Queue specification determines the order in which tasks waiting for the semaphore should unblock. Deletion safety and priority inheritance will be discussed later.

19 Mutex Ownership A task which takes a mutex semaphore owns it, so that no other task can give this semaphore. Mutex semaphores can be taken recursively. The task which owns the semaphore may take it more than once. Must be given same number of times as taken before it will be released. Mutual exclusion semaphores cannot be used in an interrupt service routine. Tornado Training Workshop Copyright 7-19 A mutex may be given regardless of ownership by calling the semmgiveforce( ) function. This is primarily intended as a debugging aid, but the function might be used in a recovery task if the task owning the mutex dies, and the resource guarded by the mutex can be reset to a consistent state.

20 Taking a Mutex Semaphore owner of semaphore? no one another task this task task pends until sem is given or timeout task continues, semtake( ) returns OK, task becomes owner task continues, semtake( ) returns OK, ownership count incremented Tornado Training Workshop Copyright 7-20 Difference from binary semaphores: Can be taken more than once by a task without blocking.

21 Giving a Mutex Semaphore ownership count? not owner semgive( ) returns ERROR one greater than one tasks pended? decrement ownership count yes no semaphore made available task at head of queue is made ready, becomes owner. Tornado Training Workshop Copyright 7-21 Difference from binary semaphore: Only the owner of a semaphore can give it. The owner may take the semaphore more than once, but must give it as many times as it was taken in order to release it.

22 Code Example - Solution 1 #include vxworks.h 2 #include semlib.h 3 4 LOCAL char mybuf[buf_size];/* Store data here */ 5 LOCAL int mybufindex = -1;/* Index of last data */ 6 LOCAL SEM_ID mysemid; 7 8 void mybufinit( ) 9 { 10 mysemid = semmcreate(sem_q_priority 11 SEM_INVERSION_SAFE 12 SEM_DELETE_SAFE); 13 } void mybufput (char ch) 16 { 17 semtake(mysemid,wait_forever); 18 mybufindex++; 19 mybuf[mybufindex]=ch; 20 semgive(mysemid); 21 } Tornado Training Workshop Copyright 7-22

23 Deletion Safety Deleting a task which owns a semaphore can be catastrophic. data structures left inconsistent. semaphore left permanently unavailable. The deletion safety option prevents a task from being deleted while it owns the semaphore. Enabled for mutex semaphores by specifying the SEM_DELETE_SAFE option during semmcreate( ). Tornado Training Workshop Copyright 7-23 To force task deletion, use: STATUS taskdeleteforce (tid) This routine is intended as debugging aid and is generally inappropriate for applications. If a task calls taskdelete() for another task, which has invoked deletion safety, the deleting task will block until the deletion safety is removed, after which it will unblock and successfully delete the unsafe task.

24 High Priority Unbounded Priority Inversion Pended thigh unblocks tmedium unblocks semtake( ) Pended Medium Priority Pended semtake( ) Ready Low Priority Critical Region time Ready Tornado Training Workshop Copyright 7-24 High and low priority tasks share a resource. Medium priority task has nothing to do with the resource whatsoever. High-priority task blocks on a mutex semaphore held by low-priority task. Can t continue until low priority task calls semgive( ). Critical region is short, so low-priority task will call semgive( ) soon if given a chance to run. Low-priority task is preempted by medium-priority task, which is not involved in the mutual exclusion. Low-priority task can t complete its (very short) critical region. This prevents the high-priority task from running.

25 Priority Inheritance Priority inheritance algorithm solves the unbounded priority inversion problem. Task owning a mutex semaphore is elevated to priority of highest priority task waiting for that semaphore. Enabled on mutex semaphores by specifying the SEM_INVERSION_SAFE option during semmcreate( ). Must also specify SEM_Q_PRIORITY (SEM_Q_FIFO is incompatible with SEM_INVERSION_SAFE). Tornado Training Workshop Copyright 7-25

26 Priority Inversion Safety High Priority Pended thigh unblocks tmedium unblocks semtake( ) Pend Medium Priority Low Priority Pended semtake( ) Critical Region time Ready Ready semgive( ) Tornado Training Workshop Copyright 7-26 The low-priority task is allowed to finish its critical region at the same priority as the high-priority task. The low-priority task returns to its original priority when it has given up all the inversion-safe mutexes which it owns.

27 Avoiding Mistakes It is easy to misuse mutex semaphores, since you must protect all accesses to the resource. To prevent such a mistake: Write a library of routines to access the resource. Initialization routine creates the semaphore. Routines in this library obtain exclusive access by calling semgive( ) and semtake( ). All uses of the resource are through this library. Tornado Training Workshop Copyright 7-27

28 Caveat - Deadlocks semtake (semid2, -1) semtake (mutex1, -1) semtake (semid1, -1) semtake semtake (semid1, (mutex2, -1) -1) semtake (semid2, -1) semtake (mutex1, -1) semtake (mutex2, -1) A deadlock is a race condition associated with the taking of multiple shared resources. May be very difficult to detect during testing. Tornado Training Workshop Copyright 7-28 Common solutions to potential deadlock situations: Use only one semaphore to protect resources that will be accessed together. Sequence accesses to resources. In above example, all tasks would take semid1 before attempting to take semid2. In the WindView graph, note that WindView can distinguish between the time at which a blocking call to semtake() is logged, and the slightly later time at which the resulting context switch occurs. The above sequence takes not quite 300 µs on a 25 MHz 68k board. INT6 is the system clock interrupt level.

29 Other Caveats Mutual exclusion semaphores can not be used at interrupt time. This issue will be discussed later in the chapter. Keep the critical region (code between semtake( ) and semgive( )) short. Tornado Training Workshop Copyright 7-29

30 Common Routines Additional semaphore routines: semdelete( ) show( ) Destroy the semaphore. semtake() calls for all tasks pended on the semaphore return ERROR. Display semaphore information. Tornado Training Workshop Copyright 7-30 If there were tasks pended on the destroyed semaphore, each pended task s errno will be set to S_objLib_OBJ_DELETED on return from that task s semtake( ) call. On a timeout, errno is set to S_objLib_OBJ_TIMEOUT, unless NO_WAIT was specified, in which case semtake( ) returns ERROR immediately, with errno set to S_objLib_OBJ_UNAVAILABLE.

31 Semaphore Browser To inspect the properties of a specific semaphore, insert the semaphore ID in the Browser s Show box, and click on Show. Tornado Training Workshop Copyright 7-31 A displayed timeout of zero signifies a WAIT_FOREVER.

32 Locking Out Preemption When doing something quick frequently, it may be preferable to disable preemption instead of taking a mutex. Call tasklock( ) to disable preemption. Call taskunlock( ) to reenable preemption. Does not disable interrupts. If the task blocks, preemption is reenabled. When the task continues executing, preemption is again locked. Prevents all other tasks from running, not just the tasks contending for the resource. Tornado Training Workshop Copyright 7-32 tasklock( )/taskunlock( ) is faster than semgive( )/semtake( ). Keep the critical region short to avoid increasing latency in the system. tasklock( ) increments a variable lockcnt in the TCB of the task making the call. If this variable is greater than zero when a preemption would ordinarily ocurr, the current task is not preempted. If the task blocks within the critical region and later unblocks, this count is still incremented, so preemption is once more locked out. taskunlock() decrements the lock count, and checks whether a reschedule must occur when the count reaches zero. Be VERY cautious making blocking calls within a tasklock() / taskunlock() critical region, as preemption lockout will be lost. Occasionally it is valid to block in such a region when it is only important that preemption be locked out before or after the block.

33 ISR s and Mutual Exclusion ISR s can t use mutex semaphores. Task sharing a resource with an ISR may need to disable interrupts. To disable/re-enable interrupts: int intlock( ) void intunlock (lockkey) lockkey is return value from intlock( ). Keep interrupt lock-out time short (e.g., long enough to set a flag)! Making kernel calls at task level can reenable interrupts! Tornado Training Workshop Copyright 7-33 May be called from interrupt or task time. intlock( ) does not prevent context switch, which will occur if: Task blocks (e.g., calls semtake( ), malloc( ), etc.). Task unblocks a higher priority task (e.g., semgive( )). Interrupts are unlocked whenever a task making a kernel call exits kernel state (usually just before returning from the call). Calling kernel functions within intlock() / intunlock() in a task may thus reenable interrupts and so compromises mutual exclusion. ISR stands for interrupt service routine.

34 Summary Binary sembcreate Mutual Exclusion semmcreate semtake, semgive show semdelete Tornado Training Workshop Copyright 7-34 Note that semflush() is valid for binary semaphores, but not mutex semaphores. ( Why? )

35 Summary Binary Semaphores allow tasks to pend until some event occurs. Create a binary semaphore for the given event. Tasks waiting for the event blocks on a semtake( ). Task or ISR detecting the event calls semgive( ) or semflush( ). Caveat: if the event repeats too quickly, information may be lost. Tornado Training Workshop Copyright 7-35

36 Summary Mutual Exclusion Semaphores are appropriate for obtaining exclusive access to a resource. Create a mutual exclusion semaphore to guard the resource. Before accessing the resource, call semtake( ). To release the resource, call semgive( ). Mutex semaphores have owners. Caveats: Keep critical regions short. Make all accesses to the resource through a library of routines. Can t be used at interrupt time. Deadlocks. Tornado Training Workshop Copyright 7-36

37 Summary tasklock( )/taskunlock( ): Prevents other tasks from running. Use when doing something quick frequently. Caveat: keep critical region short. intlock( )/intunlock( ): Disables interrupts. Use to protect resources used by tasks and interrupt service routines. Caveat: keep critical region short. Tornado Training Workshop Copyright 7-37

Lecture 4: Real Time Semaphores

Lecture 4: Real Time Semaphores Lecture 4: Real Time Semaphores 1 Lab work (20 min) Configuring VxWorks Kernel VxWorks Hostshell 09/19/2015 Quiz Solution (10) Quiz (15) Lecture Synchronization and Semaphores (45) Lab exercise Binary

More information

Experiment #3 Semaphores

Experiment #3 Semaphores Experiment #3 Semaphores Introduction Semaphores permit multitasking applications to coordinate their activities. The most obvious way for tasks to communicate is via various shared data structures. Because

More information

Lab Manual for VxWorks

Lab Manual for VxWorks Lab Manual for VxWorks Session 1: Introduction to Tornado tool 1] A program to display hello world on the console window. Also find the size of the structure with a char an as the data member of the structure.

More information

EE458 - Embedded Systems Lecture 8 Semaphores

EE458 - Embedded Systems Lecture 8 Semaphores EE458 - Embedded Systems Lecture 8 Semaphores Outline Introduction to Semaphores Binary and Counting Semaphores Mutexes Typical Applications RTEMS Semaphores References RTC: Chapter 6 CUG: Chapter 9 1

More information

Introduction to Real-Time Operating Systems

Introduction to Real-Time Operating Systems Introduction to Real-Time Operating Systems GPOS vs RTOS General purpose operating systems Real-time operating systems GPOS vs RTOS: Similarities Multitasking Resource management OS services to applications

More information

Experiment #7 Priority Inversion

Experiment #7 Priority Inversion Experiment #7 Priority Inversion Introduction Priority inversion occurs when a higher-priority task is forced to wait an indefinite period for the completion of a lower priority task. For example, priohigh,

More information

Real Time College. These are the methods, variables and definitions that you may use throughout home and class exercises :

Real Time College. These are the methods, variables and definitions that you may use throughout home and class exercises : These are the methods, variables and definitions that you may use throughout home and class exercises : Definitions: #define INIFINITE_TIMEOUT 0xFFFFFFFF #define POLICY_FIFO 0x1 #define POLICY_PRIORITY

More information

SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T A N D S P R I N G 2018

SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T A N D S P R I N G 2018 SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T 2. 3. 8 A N D 2. 3. 1 0 S P R I N G 2018 INTER-PROCESS COMMUNICATION 1. How a process pass information to another process

More information

2.c Concurrency Mutual exclusion & synchronization mutexes. Unbounded buffer, 1 producer, N consumers

2.c Concurrency Mutual exclusion & synchronization mutexes. Unbounded buffer, 1 producer, N consumers Mutual exclusion & synchronization mutexes Unbounded buffer, 1 producer, N consumers out shared by all consumers mutex among consumers producer not concerned: can still add items to buffer at any time

More information

Appendix Example Code

Appendix Example Code Appendix A Example Code Tornado Training Workshop Copyright A-1 ynchronization essage Queues: Data Collection essage Queues: Client - Server xception Handling elect ( ) DP CP emo code Synchronization Solution

More information

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy Operating Systems Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com Email : eaymanelshenawy@yahoo.com Reference

More information

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources Concurrency: Deadlock and Starvation Chapter 6 Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate with each other No efficient solution Involve conflicting

More information

PROCESS SYNCHRONIZATION

PROCESS SYNCHRONIZATION PROCESS SYNCHRONIZATION Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization Monitors Synchronization

More information

ENGG4420 CHAPTER 2 LECTURE 6

ENGG4420 CHAPTER 2 LECTURE 6 CHAPTER 2 ucosiii Page 1 ENGG4420 CHAPTER 2 LECTURE 6 October 25 12 5:03 PM SEMAPHORE INTERNALS OS_SEM data type defined in os.h Semaphore services are enabled at compile time by setting the configuration

More information

C09: Process Synchronization

C09: Process Synchronization CISC 7310X C09: Process Synchronization Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/29/2018 CUNY Brooklyn College 1 Outline Race condition and critical regions The bounded

More information

Concurrency: Deadlock and Starvation

Concurrency: Deadlock and Starvation Concurrency: Deadlock and Starvation Chapter 6 E&CE 354: Processes 1 Deadlock Deadlock = situation in which every process from a set is permanently blocked, i.e. cannot proceed with execution Common cause:

More information

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem?

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem? What is the Race Condition? And what is its solution? Race Condition: Where several processes access and manipulate the same data concurrently and the outcome of the execution depends on the particular

More information

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017 Operating Systems Lecture 4 - Concurrency and Synchronization Adrien Krähenbühl Master of Computer Science PUF - Hồ Chí Minh 2016/2017 Mutual exclusion Hardware solutions Semaphores IPC: Message passing

More information

Dealing with Issues for Interprocess Communication

Dealing with Issues for Interprocess Communication Dealing with Issues for Interprocess Communication Ref Section 2.3 Tanenbaum 7.1 Overview Processes frequently need to communicate with other processes. In a shell pipe the o/p of one process is passed

More information

Concurrency Race Conditions and Deadlocks

Concurrency Race Conditions and Deadlocks Concurrency Race Conditions and Deadlocks Kartik Gopalan Chapters 2 (2.3) and 6 Tanenbaum s Modern OS Sequential Loosely, doing many things, but one after another E.g. Finish one assignment, then another

More information

Lecture 3: Concurrency & Tasking

Lecture 3: Concurrency & Tasking Lecture 3: Concurrency & Tasking 1 Real time systems interact asynchronously with external entities and must cope with multiple threads of control and react to events - the executing programs need to share

More information

1 Process Coordination

1 Process Coordination COMP 730 (242) Class Notes Section 5: Process Coordination 1 Process Coordination Process coordination consists of synchronization and mutual exclusion, which were discussed earlier. We will now study

More information

ECE519 Advanced Operating Systems

ECE519 Advanced Operating Systems IT 540 Operating Systems ECE519 Advanced Operating Systems Prof. Dr. Hasan Hüseyin BALIK (6 th Week) (Advanced) Operating Systems 6. Concurrency: Deadlock and Starvation 6. Outline Principles of Deadlock

More information

Interprocess Communication By: Kaushik Vaghani

Interprocess Communication By: Kaushik Vaghani Interprocess Communication By: Kaushik Vaghani Background Race Condition: A situation where several processes access and manipulate the same data concurrently and the outcome of execution depends on the

More information

Concurrency: Deadlock and Starvation. Chapter 6

Concurrency: Deadlock and Starvation. Chapter 6 Concurrency: Deadlock and Starvation Chapter 6 Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate with each other Involve conflicting needs for resources

More information

Lecture 6: Real-Time Timing + Real-Time Objects

Lecture 6: Real-Time Timing + Real-Time Objects Lecture 6: Real-Time Timing + Real-Time Objects 1 Lecture: RT Timing (30) Lab Exercise: Watchdog + auxiliary clock timers (25) Lecture: RT Objects-Events (15) Lab Exercise: Events (30) Project Work (rest

More information

Introduction to OS Synchronization MOS 2.3

Introduction to OS Synchronization MOS 2.3 Introduction to OS Synchronization MOS 2.3 Mahmoud El-Gayyar elgayyar@ci.suez.edu.eg Mahmoud El-Gayyar / Introduction to OS 1 Challenge How can we help processes synchronize with each other? E.g., how

More information

Chapters 5 and 6 Concurrency

Chapters 5 and 6 Concurrency Operating Systems: Internals and Design Principles, 6/E William Stallings Chapters 5 and 6 Concurrency Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall Concurrency When several processes/threads

More information

FYS4220 / RT-lab no SIGNALS

FYS4220 / RT-lab no SIGNALS FYS4220 / 9220 12 Oct 2011 /TBS RT-lab no 2-2011 SIGNALS 1 The VxWorks signal IPC facility VxWorks provides a software signal facility. Signals asynchronously alter the control flow of a task or process.

More information

Embedded Systems. 5. Operating Systems. Lothar Thiele. Computer Engineering and Networks Laboratory

Embedded Systems. 5. Operating Systems. Lothar Thiele. Computer Engineering and Networks Laboratory Embedded Systems 5. Operating Systems Lothar Thiele Computer Engineering and Networks Laboratory Embedded Operating Systems 5 2 Embedded Operating System (OS) Why an operating system (OS) at all? Same

More information

FreeRTOS. A Brief Overview. Christopher Kenna. October 1, Avionics. FreeRTOS 1 / 34

FreeRTOS. A Brief Overview. Christopher Kenna. October 1, Avionics. FreeRTOS 1 / 34 A Brief Overview Christopher Kenna Avionics October 1, 2010 1 / 34 Introduction Outline 1 Introduction About Kernel Overview 2 Tasks Tasks versus Co-Routines Task Details 3 IPC and Synchronization Queues

More information

Chapter 6 Concurrency: Deadlock and Starvation

Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles Chapter 6 Concurrency: Deadlock and Starvation Seventh Edition By William Stallings Operating Systems: Internals and Design Principles When two trains

More information

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017 CS 471 Operating Systems Yue Cheng George Mason University Fall 2017 1 Review: Sync Terminology Worksheet 2 Review: Semaphores 3 Semaphores o Motivation: Avoid busy waiting by blocking a process execution

More information

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition Chapter 5: Process Synchronization Silberschatz, Galvin and Gagne 2013 Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks

More information

Chapter 6: Synchronization. Operating System Concepts 8 th Edition,

Chapter 6: Synchronization. Operating System Concepts 8 th Edition, Chapter 6: Synchronization, Silberschatz, Galvin and Gagne 2009 Outline Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization

More information

Lesson 6: Process Synchronization

Lesson 6: Process Synchronization Lesson 6: Process Synchronization Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks Semaphores Classic Problems of Synchronization

More information

Multithreading Programming II

Multithreading Programming II Multithreading Programming II Content Review Multithreading programming Race conditions Semaphores Thread safety Deadlock Review: Resource Sharing Access to shared resources need to be controlled to ensure

More information

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor. Synchronization 1 Concurrency On multiprocessors, several threads can execute simultaneously, one on each processor. On uniprocessors, only one thread executes at a time. However, because of preemption

More information

Tasks. Task Implementation and management

Tasks. Task Implementation and management Tasks Task Implementation and management Tasks Vocab Absolute time - real world time Relative time - time referenced to some event Interval - any slice of time characterized by start & end times Duration

More information

Module 1. Introduction:

Module 1. Introduction: Module 1 Introduction: Operating system is the most fundamental of all the system programs. It is a layer of software on top of the hardware which constitutes the system and manages all parts of the system.

More information

Reminder from last time

Reminder from last time Concurrent systems Lecture 2: More mutual exclusion, semaphores, and producer-consumer relationships DrRobert N. M. Watson 1 Reminder from last time Definition of a concurrent system Origins of concurrency

More information

CSC Operating Systems Spring Lecture - XII Midterm Review. Tevfik Ko!ar. Louisiana State University. March 4 th, 2008.

CSC Operating Systems Spring Lecture - XII Midterm Review. Tevfik Ko!ar. Louisiana State University. March 4 th, 2008. CSC 4103 - Operating Systems Spring 2008 Lecture - XII Midterm Review Tevfik Ko!ar Louisiana State University March 4 th, 2008 1 I/O Structure After I/O starts, control returns to user program only upon

More information

The University of Texas at Arlington

The University of Texas at Arlington The University of Texas at Arlington Lecture 10: Threading and Parallel Programming Constraints CSE 5343/4342 Embedded d Systems II Objectives: Lab 3: Windows Threads (win32 threading API) Convert serial

More information

CHAPTER 6: PROCESS SYNCHRONIZATION

CHAPTER 6: PROCESS SYNCHRONIZATION CHAPTER 6: PROCESS SYNCHRONIZATION The slides do not contain all the information and cannot be treated as a study material for Operating System. Please refer the text book for exams. TOPICS Background

More information

Concurrency: a crash course

Concurrency: a crash course Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Concurrency: a crash course Concurrent computing Applications designed as a collection of computational units that may execute

More information

CS 31: Intro to Systems Deadlock. Kevin Webb Swarthmore College April 21, 2015

CS 31: Intro to Systems Deadlock. Kevin Webb Swarthmore College April 21, 2015 CS 31: Intro to Systems Deadlock Kevin Webb Swarthmore College April 21, 2015 Reading Quiz Deadly Embrace The Structure of the THE-Multiprogramming System (Edsger Dijkstra, 1968) Also introduced semaphores

More information

Deadlock. Lecture 4: Synchronization & Communication - Part 2. Necessary conditions. Deadlock handling. Hierarchical resource allocation

Deadlock. Lecture 4: Synchronization & Communication - Part 2. Necessary conditions. Deadlock handling. Hierarchical resource allocation Lecture 4: Synchronization & ommunication - Part 2 [RTS h 4] Deadlock Priority Inversion & Inheritance Mailbox ommunication ommunication with Objects Deadlock Improper allocation of common resources may

More information

Multitasking / Multithreading system Supports multiple tasks

Multitasking / Multithreading system Supports multiple tasks Tasks and Intertask Communication Introduction Multitasking / Multithreading system Supports multiple tasks As we ve noted Important job in multitasking system Exchanging data between tasks Synchronizing

More information

Operating Systems: William Stallings. Starvation. Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall

Operating Systems: William Stallings. Starvation. Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William Stallings Chapter 6 Concurrency: Deadlock and Starvation Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall Deadlock

More information

Locks. Dongkun Shin, SKKU

Locks. Dongkun Shin, SKKU Locks 1 Locks: The Basic Idea To implement a critical section A lock variable must be declared A lock variable holds the state of the lock Available (unlocked, free) Acquired (locked, held) Exactly one

More information

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition Chapter 5: Process Synchronization Silberschatz, Galvin and Gagne 2013 Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks

More information

Process & Thread Management II. Queues. Sleep() and Sleep Queues CIS 657

Process & Thread Management II. Queues. Sleep() and Sleep Queues CIS 657 Process & Thread Management II CIS 657 Queues Run queues: hold threads ready to execute Not a single ready queue; 64 queues All threads in same queue are treated as same priority Sleep queues: hold threads

More information

Process & Thread Management II CIS 657

Process & Thread Management II CIS 657 Process & Thread Management II CIS 657 Queues Run queues: hold threads ready to execute Not a single ready queue; 64 queues All threads in same queue are treated as same priority Sleep queues: hold threads

More information

ENGR 3950U / CSCI 3020U UOIT, Fall 2012 Quiz on Process Synchronization SOLUTIONS

ENGR 3950U / CSCI 3020U UOIT, Fall 2012 Quiz on Process Synchronization SOLUTIONS Name: Student Number: SOLUTIONS ENGR 3950U / CSCI 3020U (Operating Systems) Quiz on Process Synchronization November 13, 2012, Duration: 40 Minutes (10 questions and 8 pages, 45 Marks) Instructor: Dr.

More information

Learning Outcomes. Concurrency and Synchronisation. Textbook. Concurrency Example. Inter- Thread and Process Communication. Sections & 2.

Learning Outcomes. Concurrency and Synchronisation. Textbook. Concurrency Example. Inter- Thread and Process Communication. Sections & 2. Learning Outcomes Concurrency and Synchronisation Understand concurrency is an issue in operating systems and multithreaded applications Know the concept of a critical region. Understand how mutual exclusion

More information

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6.

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6. Part Three - Process Coordination Chapter 6: Synchronization 6.1 Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure

More information

Real-Time Programming

Real-Time Programming Real-Time Programming Week 7: Real-Time Operating Systems Instructors Tony Montiel & Ken Arnold rtp@hte.com 4/1/2003 Co Montiel 1 Objectives o Introduction to RTOS o Event Driven Systems o Synchronization

More information

CS420: Operating Systems. Process Synchronization

CS420: Operating Systems. Process Synchronization Process Synchronization James Moscola Department of Engineering & Computer Science York College of Pennsylvania Based on Operating System Concepts, 9th Edition by Silberschatz, Galvin, Gagne Background

More information

Exceptions, Interrupts, and Timers

Exceptions, Interrupts, and Timers Chapter 10 Exceptions, Interrupts, and Timers Tornado Training Workshop Copyright 10-1 xception Handling and Signals nterrupt Service Routines ystem Clock, Auxiliary Clock, Watchdog Timers Exceptions,

More information

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization Chapter 6: Process Synchronization Chapter 6: Synchronization 6.1 Background 6.2 The Critical-Section Problem 6.3 Peterson s Solution 6.4 Synchronization Hardware 6.5 Mutex Locks 6.6 Semaphores 6.7 Classic

More information

Last Class: Synchronization

Last Class: Synchronization Last Class: Synchronization Synchronization primitives are required to ensure that only one thread executes in a critical section at a time. Concurrent programs Low-level atomic operations (hardware) load/store

More information

Module 6: Process Synchronization

Module 6: Process Synchronization Module 6: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization Monitors Synchronization Examples Atomic

More information

Today: Synchronization. Recap: Synchronization

Today: Synchronization. Recap: Synchronization Today: Synchronization Synchronization Mutual exclusion Critical sections Example: Too Much Milk Locks Synchronization primitives are required to ensure that only one thread executes in a critical section

More information

Process Synchronization

Process Synchronization Process Synchronization Chapter 6 2015 Prof. Amr El-Kadi Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly

More information

ArdOS The Arduino Operating System Reference Guide Contents

ArdOS The Arduino Operating System Reference Guide Contents ArdOS The Arduino Operating System Reference Guide Contents 1. Introduction... 2 2. Error Handling... 2 3. Initialization and Startup... 2 3.1 Initializing and Starting ArdOS... 2 4. Task Creation... 3

More information

Concurrency and Synchronisation

Concurrency and Synchronisation Concurrency and Synchronisation 1 Learning Outcomes Understand concurrency is an issue in operating systems and multithreaded applications Know the concept of a critical region. Understand how mutual exclusion

More information

Module 6: Process Synchronization. Operating System Concepts with Java 8 th Edition

Module 6: Process Synchronization. Operating System Concepts with Java 8 th Edition Module 6: Process Synchronization 6.1 Silberschatz, Galvin and Gagne 2009 Module 6: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores

More information

Concurrency and Race Conditions (Linux Device Drivers, 3rd Edition (www.makelinux.net/ldd3))

Concurrency and Race Conditions (Linux Device Drivers, 3rd Edition (www.makelinux.net/ldd3)) (Linux Device Drivers, 3rd Edition (www.makelinux.net/ldd3)) Concurrency refers to the situation when the system tries to do more than one thing at once Concurrency-related bugs are some of the easiest

More information

The University of Texas at Arlington

The University of Texas at Arlington The University of Texas at Arlington Lecture 6: Threading and Parallel Programming Constraints CSE 5343/4342 Embedded Systems II Based heavily on slides by Dr. Roger Walker More Task Decomposition: Dependence

More information

CS 153 Design of Operating Systems Winter 2016

CS 153 Design of Operating Systems Winter 2016 CS 153 Design of Operating Systems Winter 2016 Lecture 9: Semaphores and Monitors Some slides from Matt Welsh Summarize Where We Are Goal: Use mutual exclusion to protect critical sections of code that

More information

Synchronization. CSE 2431: Introduction to Operating Systems Reading: Chapter 5, [OSC] (except Section 5.10)

Synchronization. CSE 2431: Introduction to Operating Systems Reading: Chapter 5, [OSC] (except Section 5.10) Synchronization CSE 2431: Introduction to Operating Systems Reading: Chapter 5, [OSC] (except Section 5.10) 1 Outline Critical region and mutual exclusion Mutual exclusion using busy waiting Sleep and

More information

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition,

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition, Chapter 6: Process Synchronization, Silberschatz, Galvin and Gagne 2009 Module 6: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores

More information

Data Races and Deadlocks! (or The Dangers of Threading) CS449 Fall 2017

Data Races and Deadlocks! (or The Dangers of Threading) CS449 Fall 2017 Data Races and Deadlocks! (or The Dangers of Threading) CS449 Fall 2017 Data Race Shared Data: 465 1 8 5 6 209? tail A[] thread switch Enqueue(): A[tail] = 20; tail++; A[tail] = 9; tail++; Thread 0 Thread

More information

Synchronization for Concurrent Tasks

Synchronization for Concurrent Tasks Synchronization for Concurrent Tasks Minsoo Ryu Department of Computer Science and Engineering 2 1 Race Condition and Critical Section Page X 2 Synchronization in the Linux Kernel Page X 3 Other Synchronization

More information

Real Time Operating System: Inter-Process Communication (IPC)

Real Time Operating System: Inter-Process Communication (IPC) ECE3411 Fall 2015 Lecture 6c. Real Time Operating System: Inter-Process Communication (IPC) Marten van Dijk, Syed Kamran Haider Department of Electrical & Computer Engineering University of Connecticut

More information

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor. Synchronization 1 Concurrency On multiprocessors, several threads can execute simultaneously, one on each processor. On uniprocessors, only one thread executes at a time. However, because of preemption

More information

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition Chapter 5: Process Synchronization Silberschatz, Galvin and Gagne 2013 Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks

More information

KERNEL THREAD IMPLEMENTATION DETAILS. CS124 Operating Systems Winter , Lecture 9

KERNEL THREAD IMPLEMENTATION DETAILS. CS124 Operating Systems Winter , Lecture 9 KERNEL THREAD IMPLEMENTATION DETAILS CS124 Operating Systems Winter 2015-2016, Lecture 9 2 Last Time: Kernel Threads OS kernel must provide a multitasking implementation Kernel threads are the minimal

More information

Synchronization I. Jo, Heeseung

Synchronization I. Jo, Heeseung Synchronization I Jo, Heeseung Today's Topics Synchronization problem Locks 2 Synchronization Threads cooperate in multithreaded programs To share resources, access shared data structures Also, to coordinate

More information

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs CSE 451: Operating Systems Winter 2005 Lecture 7 Synchronization Steve Gribble Synchronization Threads cooperate in multithreaded programs to share resources, access shared data structures e.g., threads

More information

UNIT:2. Process Management

UNIT:2. Process Management 1 UNIT:2 Process Management SYLLABUS 2.1 Process and Process management i. Process model overview ii. Programmers view of process iii. Process states 2.2 Process and Processor Scheduling i Scheduling Criteria

More information

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Chapter 6: Process Synchronization. Module 6: Process Synchronization Chapter 6: Process Synchronization Module 6: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization

More information

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks CSci 4061 Introduction to Operating Systems Synchronization Basics: Locks Synchronization Outline Basics Locks Condition Variables Semaphores Basics Race condition: threads + shared data Outcome (data

More information

CS 333 Introduction to Operating Systems. Class 4 Concurrent Programming and Synchronization Primitives

CS 333 Introduction to Operating Systems. Class 4 Concurrent Programming and Synchronization Primitives CS 333 Introduction to Operating Systems Class 4 Concurrent Programming and Synchronization Primitives Jonathan Walpole Computer Science Portland State University 1 Concurrent programming Assumptions:

More information

Threading and Synchronization. Fahd Albinali

Threading and Synchronization. Fahd Albinali Threading and Synchronization Fahd Albinali Parallelism Parallelism and Pseudoparallelism Why parallelize? Finding parallelism Advantages: better load balancing, better scalability Disadvantages: process/thread

More information

CS 571 Operating Systems. Midterm Review. Angelos Stavrou, George Mason University

CS 571 Operating Systems. Midterm Review. Angelos Stavrou, George Mason University CS 571 Operating Systems Midterm Review Angelos Stavrou, George Mason University Class Midterm: Grading 2 Grading Midterm: 25% Theory Part 60% (1h 30m) Programming Part 40% (1h) Theory Part (Closed Books):

More information

Operating Systems: Quiz2 December 15, Class: No. Name:

Operating Systems: Quiz2 December 15, Class: No. Name: Operating Systems: Quiz2 December 15, 2006 Class: No. Name: Part I (30%) Multiple Choice Each of the following questions has only one correct answer. Fill the correct one in the blank in front of each

More information

Synchronization I q Race condition, critical regions q Locks and concurrent data structures q Next time: Condition variables, semaphores and monitors

Synchronization I q Race condition, critical regions q Locks and concurrent data structures q Next time: Condition variables, semaphores and monitors Synchronization I q Race condition, critical regions q Locks and concurrent data structures q Next time: Condition variables, semaphores and monitors Cooperating processes and threads Cooperating processes

More information

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition,

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition, Chapter 6: Process Synchronization, Silberschatz, Galvin and Gagne 2009 Module 6: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores

More information

Concurrency. Chapter 5

Concurrency. Chapter 5 Concurrency 1 Chapter 5 2 Concurrency Is a fundamental concept in operating system design Processes execute interleaved in time on a single processor Creates the illusion of simultaneous execution Benefits

More information

Condition Variables CS 241. Prof. Brighten Godfrey. March 16, University of Illinois

Condition Variables CS 241. Prof. Brighten Godfrey. March 16, University of Illinois Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois 1 Synchronization primitives Mutex locks Used for exclusive access to a shared resource (critical section) Operations:

More information

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization Chapter 6: Process Synchronization Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks Semaphores Classic Problems of Synchronization

More information

Concurrency and Synchronisation

Concurrency and Synchronisation Concurrency and Synchronisation 1 Sections 2.3 & 2.4 Textbook 2 Making Single-Threaded Code Multithreaded Conflicts between threads over the use of a global variable 3 Inter- Thread and Process Communication

More information

Lecture 3: Synchronization & Deadlocks

Lecture 3: Synchronization & Deadlocks Lecture 3: Synchronization & Deadlocks Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating

More information

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University CS 333 Introduction to Operating Systems Class 3 Threads & Concurrency Jonathan Walpole Computer Science Portland State University 1 Process creation in UNIX All processes have a unique process id getpid(),

More information

CS 333 Introduction to Operating Systems Class 4 Concurrent Programming and Synchronization Primitives

CS 333 Introduction to Operating Systems Class 4 Concurrent Programming and Synchronization Primitives CS 333 Introduction to Operating Systems Class 4 Concurrent Programming and Synchronization Primitives Jonathan Walpole Computer Science Portland State University 1 What does a typical thread API look

More information

MULTITHREADING AND SYNCHRONIZATION. CS124 Operating Systems Fall , Lecture 10

MULTITHREADING AND SYNCHRONIZATION. CS124 Operating Systems Fall , Lecture 10 MULTITHREADING AND SYNCHRONIZATION CS124 Operating Systems Fall 2017-2018, Lecture 10 2 Critical Sections Race conditions can be avoided by preventing multiple control paths from accessing shared state

More information

IV. Process Synchronisation

IV. Process Synchronisation IV. Process Synchronisation Operating Systems Stefan Klinger Database & Information Systems Group University of Konstanz Summer Term 2009 Background Multiprogramming Multiple processes are executed asynchronously.

More information

Synchronization for Concurrent Tasks

Synchronization for Concurrent Tasks Synchronization for Concurrent Tasks Minsoo Ryu Department of Computer Science and Engineering 2 1 Race Condition and Critical Section Page X 2 Algorithmic Approaches Page X 3 Hardware Support Page X 4

More information

Process Management And Synchronization

Process Management And Synchronization Process Management And Synchronization In a single processor multiprogramming system the processor switches between the various jobs until to finish the execution of all jobs. These jobs will share the

More information