Operating Systems Part III: Process Management (Process Synchronization)

Slides:



Advertisements
Similar presentations
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Advertisements

Process Synchronization Continued 7.2 The Critical-Section Problem.
Previously… Processes –Process States –Context Switching –Process Queues Threads –Thread Mappings Scheduling –FCFS –SJF –Priority scheduling –Round Robin.
Chapter 6 Process Synchronization Bernard Chen Spring 2007.
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.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
The Critical-Section Problem
1 Tuesday, June 20, 2006 "The box said that I needed to have Windows 98 or better... so I installed Linux." - LinuxNewbie.org.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem 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.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Module 6: Synchronization 6.1 Background 6.2 The Critical-Section.
Synchronization (other solutions …). Announcements Assignment 2 is graded Project 1 is due today.
Process Synchronization Topics: 1.Background 2.The critical-section problem 3.Semaphores 4.Critical Regions 5.Monitors Topics: 1.Background 2.The critical-section.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Hardware solutions So far we have looked at software solutions for the critical section problem. –algorithms whose correctness does not rely on any other.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
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.
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
Chapter 7 -1 CHAPTER 7 PROCESS SYNCHRONIZATION CGS Operating System Concepts UCF, Spring 2004.
1 Concurrent Processes. 2 Cooperating Processes  Operating systems allow for the creation and concurrent execution of multiple processes  concurrency.
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.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Operating Systems Lecture Notes Synchronization Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
CS4315A. Berrached:CMS:UHD1 Process Synchronization Chapter 8.
Process Synchronization. Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
CE Operating Systems Lecture 8 Process Scheduling continued and an introduction to process synchronisation.
Process Synchronization Presentation 2 Group A4: Sean Hudson, Syeda Taib, Manasi Kapadia.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Chapter 6: Process Synchronization
Process Synchronization
Chapter 5: Process Synchronization
Process Synchronization: Semaphores
Background on the need for Synchronization
Chapter 5: Process Synchronization
Chapter 5: Process Synchronization
Chapter 6: Process Synchronization
Topic 6 (Textbook - Chapter 5) Process Synchronization
Lecture 19 Syed Mansoor Sarwar
Module 7a: Classic Synchronization
Lecture 2 Part 2 Process Synchronization
Critical section problem
Grades.
Concurrency: Mutual Exclusion and Process Synchronization
Chapter 6: Process Synchronization
Lecture 21 Syed Mansoor Sarwar
Chapter 6: Synchronization Tools
Process/Thread Synchronization (Part 2)
CSE 542: Operating Systems
CSE 542: Operating Systems
Presentation transcript:

Operating Systems Part III: Process Management (Process Synchronization)

The Problem of Synchronization Cooperating processes have to be synchronized to avoid chaos Example: Bounded capacity (Producer-Consumer) – Producer: puts new item, increment counter – Consumer: gets new item, decrement counter – Problem occurs when both execute concurrently Race condition – several processes access and manipulate shared data concurrently; outcome of execution depends on order in which the access takes place

The Critical Section Problem Each process has a critical section (i.e. updating a table, writing a file, etc.) Basic implementation repeat ENTRY section CRITICAL SECTION EXIT section REMAINDER section until false

The Critical Section Problem Three requirements: – Mutual exclusivity - if a process is in its critical section, no other process is allowed – Progress - if no process is in its critical section, and some processes want to enter their critical sections, only those not in their remainders will participate in decision of which will enter next and this selection cannot be postponed indefinitely

The Critical Section Problem Three requirements (continued) – Bounded waiting - there must be a limit on the number of times that other processes are allowed to enter their critical sections after a process has made a request and before that request is granted

Solutions to the Critical Section Problem Two-process solution – applicable only to two processes at the most shared variables: boolean flag[2]; int turn; do { flag[i] = true; turn = j; while( flag[j] && turn == j); // do nothing do_critical_section(); flag[i] = false; do_remainder_section(); } while(1);

Solutions to the Critical Section Problem Multiple-process solutions – Solution is called the bakery algorithm -> based on scheduling commonly used in bakeries, banks, etc. – Developed for a distributed environment – Algorithm on page 197

Synchronization Hardware As with other aspects of software, additional hardware features – make programming task easier and, – improve system efficiency If no special hardware, Critical Section can be implemented by disabling interrupts -> may not always be possible – disabling interrupts is time-consuming (message passed to all processors) -> decreased efficiency – example: system clock (updated by interrupts)

Synchronization Hardware Many machines provide special hardware to do the following: – test and modify content of a word: TestAndSet(boolean &x): atomically { boolean oldx = x; x = true; return oldx; } – swap contents of two words, also atomically

Use of TestAndSet to provide mutual exclusion Variable lock = false, initially do { while (TestAndSet(lock) ); // do nothing do_critical_section(); lock = false; do_remainder_section(): } while (1); Bounded-waiting not fulfilled.

Mutual exclusion & bounded-waiting with TestAndSet More complicated, requires the ff common data structures: boolean waiting [n]; boolean lock; Algorithm is given in Figure 7.10, on page 200

Semaphores Two- and multiple-process solution not easy to generalize to more complex problems To overcome, use a synchronization tool called semaphore A variable accessed only through two atomic operations: wait and signal wait(S) while (S<=0); // do nothing S:=S-1; signal(S) S:=S+1;

Mutual-exclusion using semaphores Variable mutex = 1, initially do { wait(mutex); do_critical_section(); signal(mutex); do_remainder_section(): } while (1);

Semaphores Example: We want S2 to execute only after S1 has completed. Var mutex = 0, initially. Process P1: S1; signal(mutex);... Process P2: wait(mutex); S2 ;

Busy waiting (spinlock) While a process is in its critical section, any other process that tries to enter its critical section must loop continuously, while waiting for the mutex variable to become positive – this waiting is called spinlock, the process “spins” while waiting for the lock. When locks are held for short time periods – spinlocks are okay. Can be a problem when locks are held for long in a realtime OS

Solution to busy-waiting Instead of busy waiting, a process can block itself, placing itself into a waiting queue associated with the semaphore S. A context switch can then take place. When another process executes a signal(S), the waiting process is restarted by a wakeup operation, which changes the state of the process from waiting to ready. typedef struct { int value; struct process *wlist; } semaphore;

Code for blocking semaphore wait (semaphore S) { S.value-- ; if (S.value < 0) { add_process_to(S.wlist); block(): } }

Code for blocking semaphore signal (semaphore S) { S.value++ ; if (S.value <= 0) { P = remove_process_from(S.wlist); wake_up(P); } }

Semaphores Might result in a deadlock P0 P1 wait(S) wait(Q) wait(Q) wait(S) … signal(S) signal(Q) signal(Q) signal(S)

Classical Problems of Synchronization Represent large class of concurrency control problems Used for testing nearly every newly-proposed synchronization scheme Bounded-Buffer – Buffer size n; producer puts in buffer; consumer gets from the buffer – commonly used to illustrate power of synchronization primitives – Sample code on Figure 7.12, page 207

Classical Problems of Synchronization Readers and Writers – A data object is shared by several concurrent processes – No problem if two readers access concurrently – Problem arises with writers and readers – To prevent chaos, writers must be allowed exclusive access to the data – Code for writer: wait(wrt); do_write(); signal(wrt);

Classical Problems of Synchronization Dining-Philosophers – Simple representation of the need to allocate resources properly (deadlock- and starvation-free) – Five philosophers in dining table; each has rice bowl – Five chopsticks laid, each put between philosopher and his right and left neighbor – When thinking: does not interact with others

Classical Problems of Synchronization Dining-Philosopher (continued) – When eating: needs to use both chopsticks – Can pick up one chopstick at a time; cannot pick up a chopstick being used – Does not release both chopsticks until finished eating – After eating, philosopher starts thinking again – Deadlock-free solution does not eliminate starvation