Synchronization.

Slides:



Advertisements
Similar presentations
Operating Systems Part III: Process Management (Process Synchronization)
Advertisements

1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Global Environment Model. MUTUAL EXCLUSION PROBLEM The operations used by processes to access to common resources (critical sections) must be mutually.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Previously… Processes –Process States –Context Switching –Process Queues Threads –Thread Mappings Scheduling –FCFS –SJF –Priority scheduling –Round Robin.
Chapter 6: Process Synchronization
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Interprocess Communication
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
Enforcing Mutual Exclusion, Semaphores. Four different approaches Hardware support Disable interrupts Special instructions Software-defined approaches.
Critical Sections and Semaphores A critical section is code that contains access to shared resources that can accessed by multiple processes. Critical.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Semaphores. Announcements No CS 415 Section this Friday Tom Roeder will hold office hours Homework 2 is due today.
Synchronization (other solutions …). Announcements Assignment 2 is graded Project 1 is due today.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Synchronization Solutions
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
Concurrency, Mutual Exclusion and Synchronization.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Semaphore and Mutex Operations.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
2.3 interprocess communcation (IPC) (especially via shared memory & controlling access to it)
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 11: October 5, 2010 Instructor: Bhuvan Urgaonkar.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Web Server Architecture Client Main Thread for(j=0;j
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
Process Synchronization. Concurrency Definition: Two or more processes execute concurrently when they execute different activities on different devices.
Interprocess Communication Race Conditions
Semaphores Synchronization tool (provided by the OS) that does not require busy waiting. Logically, a semaphore S is an integer variable that, apart from.
Chapter 5: Process Synchronization
Process Synchronization: Semaphores
Background on the need for Synchronization
Process Synchronization
Chapter 5: Process Synchronization
Concurrency: Mutual Exclusion and Synchronization
Topic 6 (Textbook - Chapter 5) Process Synchronization
Lecture 22 Syed Mansoor Sarwar
Process Synchronization
Synchronization and Semaphores
Concurrency: Mutual Exclusion and Synchronization
Lecture 2 Part 2 Process Synchronization
Critical section problem
Global Environment Model
Grades.
Concurrency: Mutual Exclusion and Process Synchronization
Chapter 6: Process Synchronization
Synchronization Primitives – Semaphore and Mutex
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
Chapter 6: Synchronization Tools
Synchronization.
Process/Thread Synchronization (Part 2)
Synchronization CSE 2431: Introduction to Operating Systems
Lab #9 Semaphores Operating System Lab.
Presentation transcript:

Synchronization

Inter-Process Communication (IPC) IPC needed in multiprogramming – management of multiple processes within a uni-processor system multiprocessing – management of multiple processes within a multi-processor system Pass information to each other via shared resource Mutual exclusion & Synchronization Proper sequencing

Spooling Example: Correct Shared memory Process 1 Process 2 int next_free; int next_free; … out 1 next_free = in; 4 abc Stores F1 into next_free; 2 Prog.c 5 Prog.n 6 3 in=next_free+1 in 7 F1 4 next_free = in F2 5 Stores F2 into next_free; … 6 in=next_free+1

Spooling Example: Data Races Shared memory Process 1 Process 2 int next_free; int next_free; out … 1 next_free = in; 4 abc Prog.c 2 next_free = in /* value: 7 */ 5 Stores F1 into next_free; 3 Prog.n 6 in 7 F2 F1 4 in=next_free+1 5 Stores F2 into next_free; … 6 in=next_free+1

Critical Region (Critical Section) Process { while (true) { ENTER CRITICAL SECTION Access shared variables; // Critical Section; LEAVE CRITICAL SECTION Do other work }

Critical Region Requirements Mutual Exclusion Progress Bounded Wait No Speed and Number of CPU assumptions

Critical Region Requirements Mutual Exclusion: No other process must execute within the critical section while a process is in it. Progress: If no process is waiting in its critical section and several processes are trying to get into their critical section, then entry to the critical section cannot be postponed indefinitely.

Critical Region Requirements Bounded Wait: A process requesting entry to a critical section should only have to wait for a bounded number of other processes to enter and leave the critical section. Speed and Number of CPUs: No assumption may be made about speeds or number of CPUs.

Oversimplifying Assumptions visual learning aid: ‘bmp’ in alphabetical order Bounded Wait Mutual Exclusion Progress Oversimplifying Assumptions

No cutting in! Bounded Wait Mutual Exclusion Progress Oversimplifying Assumptions

Are there door locks? No cutting in! Bounded Wait Mutual Exclusion Progress Are there door locks? No cutting in! Oversimplifying Assumptions

We'll be first if we sprint Bounded Wait Mutual Exclusion Progress Are there door locks? No cutting in! We'll be first if we sprint Oversimplifying Assumptions

Are there door locks? Well, Did you see anybody go in? No cutting in! Bounded Wait Mutual Exclusion Progress Well, Did you see anybody go in? Are there door locks? No cutting in! We'll be first if we sprint Oversimplifying Assumptions

Mutual exclusion using critical regions

Mutual Exclusion With Busy Waiting Possible Solutions Software-only candidate solutions (Two-Process Solutions) Lock Variables Turn-Based Mutual Exclusion Other Flag Mutual Exclusion Two Flag Mutual Exclusion Two Flag and Turn Mutual Exclusion Hardware solutions Disabling Interrupts; Test-and-set; Swap (Exchange) Semaphores

Semaphore Concept Fundamental Principle: two or more processes want to cooperate by means of simple signals For signaling introduce Special Variable : Semaphore s Primitives: semSignal(s) – transmit signal via semaphore s semWait(s) – receive signal via semaphore s Primitives semSignal and semWait must be ATOMIC!!! Note: Different notation is used for semSignal and semWait (P for semWait, V for semSignal, ‘wait’ for semWait, ‘signal’ for semSignal)

Definition of Semaphore Primitives (Counting Semaphore) struct semaphore{ int count; queueType queue; }; void semWait(semaphore s) { s.count--; if (s.count < 0) { place this process in s.queue; block this process; } void semSignal(semaphore s) { s.count++; if (s.count ≤ 0) remove a process P from s.queue; place process P on ready list; }

Definition of Binary Semaphore Primitives struct binary_semaphore { enum {0,1} value; queueType queue; }; void semWaitB(binary_semaphore s) { if (s.value == 1) s.value = 0; else { place this process in s.queue; block this process; } void semSignalB(binary_semaphore s) { if (s.queue is empty()) s.value = 1; else remove a process P from s.queue; place process P on ready list; }

Mutual Exclusion Using Semaphores Pi { while(1) { semWait(s); /* Critical Section */ semSignal(s); /* remainder */ } lock = 0; Pi { while(1) { while(Test_And_Set(lock)) { }; /* Critical Section */ lock =0; /* remainder */ }

Process Process Value of Semaphore lock Queue B A 1 semWait(lock) Critical Region Value of Semaphore lock Queue B A Normal Execution Blocked on semaphore lock 1 semWait(lock) semWait(lock) -1 B semSignal(lock) semSignal(lock) 1

Implementation of Semaphores in POSIX POSIX:SEM semaphore is variable of type sem_t Atomic Operations: int sem_init(sem_t *sem, int pshared, unsigned value); int sem_destroy(sem_t *sem); int sem_post(sem_t *sem); Int sem_trywait(sem_t *sem); Int sem_wait(sem_t *sem); Use <semaphore.h>

Unnamed Semaphores Ch 14 pp 491-501 #include <semaphore.h> Sem_t sem; You cannot make a copy of a semaphore variable!!! #include <semaphore.h> int sem_init(sem_t *sem, int pshared, unsigned value); pshared == 0: only threads of process creating semaphore can use semaphore.

Sharing Semaphores Sharing semaphores between threads within a process is easy, use pshared==0 Forking a process creates copies of any semaphore it has… this does not share the semaphore Making pshared non zero allows any process that can access the semaphore to use it. This places the semaphore in global environment.

sem_init can fail!!! In unsuccessful, sem_init returns -1 and sets errno. Example: sem_t semA; if (sem_init(&semA, 0, 1) == -1) perror(“Failed to initialize semaphore semA”); Value > sem_value_max Resources exhausted Insufficient privileges EINVAL ENOSPC EPERM cause errno

Semaphore Operations #include <semaphore.h> int sem_destroy(sem_t *sem); Destroying a semaphore that’s been destroyed gives undefined result. Destroying a semaphore on which a thread is blocked gives undefined results.

Semaphore Operations #include <semaphore.h> int sem_post(sem_t *sem); Unlocks the semaphore. If the semaphore value resulting from this operation is: positive, then no threads were blocked waiting for the semaphore. zero, then one of the threads blocked waiting for the semaphore will be allowed to return successfully from its call to sem_wait(). if unsuccessful, returns -1 and sets errno errno == EINVAL if semaphore doesnt exist

Semaphore Operations int sem_trywait(sem_t *sem); locks the semaphore if it is currently not locked (>0). doesn’t block returns -1 and errno==EAGAIN if semaphore zero can be interrupted by signal – errno == EINTR int sem_wait(sem_t *sem); locks the semaphore. If the semaphore value is currently zero, then the calling thread will block. Can be interrupted by signal – errno == EINTR