Section I Section Real Time Systems. Processes. 1.4 Inter-Processes Communication (part 1)

Similar documents
7: Interprocess Communication

PROCESS SYNCHRONIZATION

Motivation of Threads. Preview. Motivation of Threads. Motivation of Threads. Motivation of Threads. Motivation of Threads 9/12/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

C09: Process Synchronization

Synchronization. Before We Begin. Synchronization. Credit/Debit Problem: Race Condition. CSE 120: Principles of Operating Systems.

Operating Systems. Synchronisation Part I

Introduction to OS Synchronization MOS 2.3

Background. The Critical-Section Problem Synchronisation Hardware Inefficient Spinning Semaphores Semaphore Examples Scheduling.

Synchronization. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

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

Locks. Dongkun Shin, SKKU

CIS Operating Systems Synchronization based on Busy Waiting. Professor Qiang Zeng Spring 2018

Dr. D. M. Akbar Hussain DE5 Department of Electronic Systems

Concurrency and Synchronisation. Leonid Ryzhyk

OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL. Chap. 2.2 Interprocess Communication

UNIX Input/Output Buffering

Process/Thread Synchronization

Process/Thread Synchronization

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

Chapter 6: Process Synchronization

Last class: Today: CPU Scheduling. Start synchronization

Running. Time out. Event wait. Schedule. Ready. Blocked. Event occurs

Processes. Introduction to Processes. The Process Model

Operating Systems CMPSC 473. Synchronization February 21, Lecture 11 Instructor: Trent Jaeger

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

Background. Old Producer Process Code. Improving the Bounded Buffer. Old Consumer Process Code

Synchronization I. Today. Next time. Race condition, critical regions and locks. Condition variables, semaphores and Monitors

SOLUTIONS FOR THE SECOND 4330/6310 QUIZ. Jehan-François Pâris Spring 2015

Synchronization I. Today. Next time. Monitors. ! Race condition, critical regions and locks. ! Condition variables, semaphores and

Concept of a process

Synchronization. Before We Begin. Synchronization. Example of a Race Condition. CSE 120: Principles of Operating Systems. Lecture 4.

Interprocess Communication By: Kaushik Vaghani

Part II Process Management Chapter 6: Process Synchronization

Resource management. Real-Time Systems. Resource management. Resource management

CS370 Operating Systems

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

CSE 120: Principles of Operating Systems. Lecture 4. Synchronization. October 7, Prof. Joe Pasquale

Synchronization I. To do

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

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

Process Synchronisation (contd.) Operating Systems. Autumn CS4023

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

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

What are they? How do we represent them? Scheduling Something smaller than a process? Threads Synchronizing and Communicating Classic IPC problems

Chapter 2 Processes and Threads

Operating Systems. OPS Processes

Process Synchronisation (contd.) Deadlock. Operating Systems. Spring CS5212

Chapters 5 and 6 Concurrency

Real-Time Systems. Lecture #4. Professor Jan Jonsson. Department of Computer Science and Engineering Chalmers University of Technology

Operating Systems. User OS. Kernel & Device Drivers. Interface Programs. Interprocess Communication (IPC)

Process Synchronization: Semaphores. CSSE 332 Operating Systems Rose-Hulman Institute of Technology

Chapter 5 Asynchronous Concurrent Execution

CSE Traditional Operating Systems deal with typical system software designed to be:

Multiprocessor System. Multiprocessor Systems. Bus Based UMA. Types of Multiprocessors (MPs) Cache Consistency. Bus Based UMA. Chapter 8, 8.

Chapter 6: Process Synchronization

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

Multiprocessor Systems. COMP s1

! Why is synchronization needed? ! Synchronization Language/Definitions: ! How are locks implemented? Maria Hybinette, UGA

Synchronization I. Jo, Heeseung

Dealing with Issues for Interprocess Communication

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019

Process Synchronization

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

Process Synchronization

Threads. Concurrency. What it is. Lecture Notes Week 2. Figure 1: Multi-Threading. Figure 2: Multi-Threading

Concurrency: Locks. Announcements

CS450/550 Operating Systems

Synchronization Principles

Lecture 4: Inter-Process Communication (Jan 27, 2005)

Synchronization Principles I

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

Process Synchronization. Mehdi Kargahi School of ECE University of Tehran Spring 2008

Part III Synchronization Software and Hardware Solutions

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

Dekker s algorithms for Semaphores Implementation. ALI TARIQ AL-KHAYYAT ( ) Submitted to : Prof. Dr. Huseyin Balik

IV. Process Synchronisation

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

CS533 Concepts of Operating Systems. Jonathan Walpole

Dept. of CSE, York Univ. 1

Synchronization for Concurrent Tasks

[537] Locks. Tyler Harter

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

Concurrency and Synchronisation

Concurrency and Synchronisation

Chapter 6: Process [& Thread] Synchronization. CSCI [4 6] 730 Operating Systems. Why does cooperation require synchronization?

Process Management And Synchronization

Introduction to Operating Systems

Remaining Contemplation Questions

CS 537 Lecture 11 Locks

Process Synchronization - I

CS450/550 Operating Systems

Resource Sharing & Management

Process Synchronization

Lecture 5: Synchronization w/locks

Mutex Implementation

Synchronization Spinlocks - Semaphores

CS370 Operating Systems

G52CON: Concepts of Concurrency

Process Co-ordination OPERATING SYSTEMS

Transcription:

EE206: Software Engineering IV 1.4 Inter-Processes Communication 1 page 1 of 16 Section I Section Real Time Systems. Processes 1.4 Inter-Processes Communication (part 1) Process interaction Processes often need to interact with each other in order to complete more complex tasks Competition for the use of limited resources such as processor(s), or memory Co-operation (data exchange) need for some form of inter-process synchronisation and/or communication Inter-process competition Resources are: - Shareable (may be used by several processes concurrently) e.g. CPU(s), some data protected against modification (read-only access), etc. - Non-shareable (may be used by a single process at a time) e.g. peripherals, some data subject to modification (read-write access), etc.

EE206: Software Engineering IV 1.4 Inter-Processes Communication 1 page 2 of 16 Problem example: - A memory location that contains a variable accessible to more than one process - If one process tests the variable while another modifies its value, the result for the first process is unpredictable Inter-process co-operation Problems: - Processes run asynchronously with respect to each other - Speed of one process relative to another is unpredictable - In order to co-operate, there are certain points at which processes must synchronise Synchronisation: - Shared memory: critical sections, semaphores, locks, flags - Message passing: send and receive

EE206: Software Engineering IV 1.4 Inter-Processes Communication 1 page 3 of 16 Problem example: shared queue - IN and OUT are two shared variables - seven processes access the shared queue: - A, B, C producers (enqueue items) - W, X, Y, Z consumers (dequeue items) A W B IN OUT X C Producers Z Y Consumers - Is there any problem? - process A: reads IN variable (1) enqueues a new item (3) updates IN value (5) - process B: reads IN variable (2) enqueues a new item (4) updates IN value (6)

EE206: Software Engineering IV 1.4 Inter-Processes Communication 1 page 4 of 16 General synchronisation problem Race Condition - Two or more processes are reading or writing some shared data - The final result depends on when (precisely) each process runs Solution: Mutual Exclusion CODE DATA "Critical Sections" "Record locking"

EE206: Software Engineering IV 1.4 Inter-Processes Communication 1 page 5 of 16 - Critical Section = part of a code where the shared memory is accessed - If no two or more processes are in a common critical section at the same time ever, the race condition could be avoided - Locking Data = shared data when accessed by a process could be locked, preventing other processes from accessing it - Data is "unlocked" when the process finishes processing the shared data - Performed in general inside the critical section Avoiding race condition is not sufficient Conditions: 1. No two processes may be simultaneously inside their critical sections 2. No assumptions may be made about relative process speeds or number of CPUs 3. No process running outside its critical section may block other processes 4. No process should have to wait forever to enter its critical section

EE206: Software Engineering IV 1.4 Inter-Processes Communication 1 page 6 of 16 Critical Sections Implementation enter_region(); /* critical section */ leave_region(); Before entering its critical section, the process calls enter_region() - Causes the process to wait only when another process is currently in its critical section until it exit from it After the critical section, the process calls leave_region() - Indicates that the process has finished with the shared data & allows another process to enter Mutual exclusion can be achieved by enclosing each critical section by enter_region() and leave_region() Implementation of enter_region() and leave_region(): A) Disable Interrupts? B) Lock Variables? C) Strict Alternation? D) Peterson's Idea? E) TSL Instruction?

EE206: Software Engineering IV 1.4 Inter-Processes Communication 1 page 7 of 16 A) Disable Interrupts Each process disables all interrupts just after entering its critical region and re-enables them just before leaving it It is a solution as only via interrupts process switching may be triggered enter_region() disable_interrupts; leave_region() enable_interrupts; It is unwise to give user processes the power to disable interrupts: - Suppose one process disables the interrupts and never re-enables them again The whole system will never work again - If the computer has more than one CPU, disabling interrupts affects only the CPU that executed the disable instruction Other CPUs will continue to run processes that can access the shared memory Very simple solution, but NOT recommended

EE206: Software Engineering IV 1.4 Inter-Processes Communication 1 page 8 of 16 B) Lock Variables Requires usage of a single shared boolean lock variable ( occupied ), initialised with FALSE if occupied is FALSE entry to critical section is allowed, whereas if occupied is TRUE entry to critical section is NOT allowed enter_region() while(occupied == TRUE) /*test*/ ; /* waiting */ occupied = TRUE; /* lock */ leave_region() occupied = FALSE; /* unlock */ A process that wants to enter a critical section, checks first to see if the entry is allowed and if it is not, the process waits in a tight loop Continuously testing a variable waiting for some value to appear is denoted as busy waiting Wastes CPU time

EE206: Software Engineering IV 1.4 Inter-Processes Communication 1 page 9 of 16 Advice: - It could be used only when there is a reasonable expectation that the wait is short Problem: - in practice it does not work Reason: - the separation of the test and the lock of the shared lock variable, occupied (enter_region() is NOT atomic ) - two concurrent processes may each find that occupied is FALSE before either has the chance to set it to TRUE both processes enter the critical section at the same time Conclusion: - Simple solution, but NOT recommended

EE206: Software Engineering IV 1.4 Inter-Processes Communication 1 page 10 of 16 C) Strict Alternation Requires usage of a single shared integer variable ( turn ), initialised with 0 Before entering in the critical section, each process checks whether it is its turn (turn == process_no) and if it is, it enters, otherwise busy waits for its turn int turn = 0; /* init */ enter_region (int process_no) while (turn!= process_no) ; /* busy waiting */ leave_region (int process_no) turn =(++process_no % PROC_COUNT); PROC_COUNT is the number of processes Disadvantages: - Processes may enter critical sections only in fixed order of process number - Uses busy waiting Conclusion: - Works, but it is very restrictive & wastes CPU

EE206: Software Engineering IV 1.4 Inter-Processes Communication 1 page 11 of 16 D) Peterson's Solution Combines the ideas of lock variables and taking turns Does not require strict alternation #define FALSE 0 #define TRUE 1 #define N 2 /* no processes */ int turn = 0; /* init turn */ int interested[n]; /* init FALSE */ enter_region(int process_no) /* the other process_no */ int other_no; other_no = 1 process_no; /* indicate the interest entering*/ interested[process_no] = TRUE; turn = process_no; /* set turn */ while (turn == process_no && interested[other_no] == TRUE) ; /* busy waiting*/

EE206: Software Engineering IV 1.4 Inter-Processes Communication 1 page 12 of 16 leave_region(int process_no) /* indicate leaving critical region */ interested[process_no] = FALSE; When both processes call enter_region at the same time, whichever stores turn first is the process that enters the critical section first Does not require strict alternation Disadvantages: - Uses busy waiting - Difficult to generalise to N > 2 (complexity!) Advantages: - Works fine for two processes - Does not impose strict alternation Conclusion: - Recommended for two concurrent processes only

EE206: Software Engineering IV 1.4 Inter-Processes Communication 1 page 13 of 16 E) "TSL" (Test and Set Lock) Instruction Requires usage of a shared integer variable ( occupied ) that co-ordinates access int occupied; Uses a function ( tsl ) that: - receives as parameter a variable transmitted by-address - stores a non-zero value in the variable - returns the old value of the variable int tsl(int *variable); - reading the variable and storing values into it are guaranteed to be indivisible ( atomic ) - no other processor can access the shared variable until the tsl instruction has finished

EE206: Software Engineering IV 1.4 Inter-Processes Communication 1 page 14 of 16 enter_region() while (tsl(&occupied)) ; /* busy waiting */ leave_region() occupied = FALSE; Disadvantages: - Uses busy waiting - Requires hardware support for tsl instruction implementation - Not easy to generalise Advantage: - Works fine for any number of processes Conclusion: - Highly recommended if tsl instruction is available

EE206: Software Engineering IV 1.4 Inter-Processes Communication 1 page 15 of 16 Possible software tsl() implementation int sw_tsl(int *variable) disable_interrupts(); old_value = *variable; *variable = TRUE; enable_interrupts(); return(old_value); - Difficult to restrict user process access by using disable_interrupts() Possible implementation of tsl() as an Operating System (OS) Call - Place the code for tsl() in OS - OS can use H/W or S/W, as appropriate - Any user process may use an OS call to access TSL service - Protection hardware allows OS to disable the interrupts, but not also user processes! - It is better, but tsl() still uses busy waiting - Especially for single CPU systems this may have a huge negative impact

EE206: Software Engineering IV 1.4 Inter-Processes Communication 1 page 16 of 16 Priority Inversion Problem: - Could be seen as a side effect of busy waiting - Situation: two processes - H ( high priority) and L ( low priority ) - Scheduling: H always runs when it is ready - Suppose: L is already in its critical region, and H becomes ready to run (input some data) - Effect: H is run by scheduler, and reaches beginning of its critical section H must busy-wait as L is in its critical section Because of scheduling rules, L will never be run again to complete its critical section, so H waits forever! Recommendation: - Prefer inter-process communication system calls that block the process, instead of wasting CPU time, when they are not allowed to enter their critical sections sleep()and wakeup() semaphores message passing