Synchronization API of Pthread Mutex: lock, unlock, try_lock CondVar: wait, signal, signal_broadcast. Synchronization

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

Lecture 7: Mutual Exclusion 2/16/12. slides adapted from The Art of Multiprocessor Programming, Herlihy and Shavit

Process Synchronization

Process Synchronization (Part I)

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

CS370 Operating Systems

Chapter 7: Process Synchronization!

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

Process Synchronization

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

Mutual Exclusion. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Chapter 6: Process Synchronization

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

Lesson 6: Process Synchronization

Dept. of CSE, York Univ. 1

CS370 Operating Systems

CS420: Operating Systems. Process Synchronization

Part II Process Management Chapter 6: Process Synchronization

Synchronization Principles

Chapter 6: Process Synchronization

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

Mutual Exclusion. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Process Synchronization

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

Synchronization Principles I

Chapter 6: Process Synchronization. Module 6: Process Synchronization

CS370 Operating Systems

Process Synchronization

Chapter 7: Process Synchronization. Background. Illustration

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

Chapter 7: Process Synchronization. Background

Module 6: Process Synchronization

Programming Paradigms for Concurrency Lecture 2 - Mutual Exclusion

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

CHAPTER 6: PROCESS SYNCHRONIZATION

Chapter 5: Process Synchronization

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

Process Synchronization

Synchronization. Race Condition. The Critical-Section Problem Solution. The Synchronization Problem. Typical Process P i. Peterson s Solution

Process Synchronization

Synchronization for Concurrent Tasks

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

Chapter 6: Process Synchronization

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

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

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

IV. Process Synchronisation

Process Coordination

Chapter 6: Process Synchronization

Chapter 5: Process Synchronization

Roadmap. Shared Variables: count=0, buffer[] Producer: Background. Consumer: while (1) { Race Condition. Race Condition.

Chapter 6 Synchronization

CSCI 447 Operating Systems Filip Jagodzinski

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

Mutual Exclusion and Synchronization

Lecture 5: Inter-process Communication and Synchronization

Chapter 6: Process Synchronization

PESIT Bangalore South Campus

Lecture 3: Synchronization & Deadlocks

Process Synchronization

Systèmes d Exploitation Avancés

Chapter 6 Process Synchronization

Maximum CPU utilization obtained with multiprogramming. CPU I/O Burst Cycle Process execution consists of a cycle of CPU execution and I/O wait

Introduction to Operating Systems

Announcements. Office hours. Reading. W office hour will be not starting next week. Chapter 7 (this whole week) CMSC 412 S02 (lect 7)

Process Synchronization - I

Chapter 5: Process Synchronization

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

PROCESS SYNCHRONIZATION

Chapter 5: Synchronization 1

Quiz: Simple Sol Threading Model. Pthread: API and Examples Synchronization API of Pthread IPC. User, kernel and hardware. Pipe, mailbox,.

Synchronization Spinlocks - Semaphores

Concurrency: Mutual Exclusion and

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

Real-Time Operating Systems M. 5. Process Synchronization

CSC501 Operating Systems Principles. Process Synchronization

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

UNIT 2 Basic Concepts of CPU Scheduling. UNIT -02/Lecture 01

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; }

Solution: a lock (a/k/a mutex) public: virtual void unlock() =0;

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

Concurrency: Mutual Exclusion and Synchronization

Recap: Thread. What is it? What does it need (thread private)? What for? How to implement? Independent flow of control. Stack

CS370 Operating Systems Midterm Review. Yashwant K Malaiya Spring 2019

Synchronization. Disclaimer: some slides are adopted from the book authors slides with permission 1

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Chapter 6 Synchronization

CSE 4/521 Introduction to Operating Systems

Lecture Topics. Announcements. Today: Concurrency: Mutual Exclusion (Stallings, chapter , 5.7)

Operating Systems. Thread Synchronization Primitives. Thomas Ropars.

Background. Module 6: Process Synchronization. Bounded-Buffer (Cont.) Bounded-Buffer. Background

Multitasking / Multithreading system Supports multiple tasks

Dealing with Issues for Interprocess Communication

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

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

Process/Thread Synchronization

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

Lecture 5: Synchronization w/locks

Process Synchronization

Introduction to OS Synchronization MOS 2.3

Transcription:

CS341: Operating System Lect20 : 16 th Sept 2014 Dr. A. Sahu Dept of Comp. Sc. & Engg. Indian Institute of Technology Guwahati Synchronization API of Pthread Mutex: lock, unlock, try_lock CondVar: wait, signal, signal_broadcast Synchronization Basic, Theory, Proof Hardware Support High level construct: Monitor Classical Problems of Synchronizations Code Data Files Registers Stack Thread Single threaded process Code Data Files Registers Registers Registers Stack Stack Stack Multi threaded process Thread #define NTH 10 pthread_mutex_t M = 0; void MutexCnt() { for(int i=0;i<10;i++) Lock( &M ); counter++; Unlock( &M); int counter = 0, i, j;; int main() { thread_t th[nth]; for(i=0; i < NTH; i++) thread_create(&th[i],null,mutexcnt,null); for(j=0; j < NTH; j++) { pthread_join( th[j], NULL); printf("final Ctr val: %d\n", counter); pthread_mutex_init, lock, unlock, trylock pthread_attr_setdetachstate, guardsize_np, stacksize,inheritsched, schedpolicy, schedparam pthread_cond_wait, signal, broadcast, init, destroy Our main concern Lock, unlock, trylock, condsignal, condbroadcast. spin lock CS critical section Resets lock upon exit 1

Earlier : Shared thread incrementing counter Train : One track many train MUTual EXclusion is must Train Collision Life loss Semaphore : A system of sending messages by holding The arms or two flags or poles in certain positions according to an alphabetic code Semaphore : A system of sending messages by holding The arms or two flags or poles in certain positions according to an alphabetic code Semaphore : A system of sending messages by holding The arms or two flags or poles in certain positions according to an alphabetic code Serialization points Minimize the size of critical sections Be careful Rather than wait, check if lock is available pthread_mutex_trylock trylock If already locked, will return EBUSY Will require restructuring of code Suspend self by pthread_yeild() Give chance to others Suspend self by doing a d wait.. 2

Processes/thread can execute concurrently May be interrupted at any, partially completing execution Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes There is a buffer of size BUFFER_SIZE When a producer : produce an Item, he put into Buffer and increment the counter When a consumer : consume an Item, he read anitem from Buffer anddecrementdecrement the counter Initially, counteris set to 0 while(true) { /* produce an item in next produced */ while(counter == BUFFER_SIZE) ; /* do nothing */ buffer[in] = next_produced; in = (in + 1) % BUFFER_SIZE; counter++; while(true) { while(counter == 0) ; /* do nothing */ next_consumed= buffer[out]; out = (out + 1) % BUFFER_SIZE; counter ; /* consume the item in next consumed */ Assume that all the instruction are atomic The loadand store machine language instructions are atomic; Thatis,cannotbeinterrupted interrupted We will see: Still we have problem in Synchronization Or some protocol/algorithm to Handle We may need different hardware support, a specific kind of Instruction to be atomic 3

Counter ++ could be implemented as reg1 = counter reg1 = reg1 + 1 counter = reg1 Counter could be implemented as reg2 = counter reg2 = reg2 1 counter = reg2 Consider this execution interleaving with count = 5 initially: S0: producer execute reg1 = counter {reg1 = 5 S1: producer execute reg1 = reg1 + 1 {reg1 = 6 S2: consumer execute reg2 = counter {reg2 = 5 S3: consumer execute reg2 = reg2 1 {reg2 = 4 S4: producer execute counter = reg1 {counter = 6 S5: consumer execute counter = reg2 {counter = 4 Expected Result =5 Consider system of nprocesses {p 0, p 1, p n 1 Each process has critical section segment of code Process may be changing common variables, updating table, writing file, etc When one process in critical section, no other may be in its critical section Critical section problem is to design protocol to solve this Each process must ask permission to enter critical section in entry section, May follow critical section with exit section, then remainder section General structure of process P i do { entry section critical section exit section reminder section while (true) Lock () Unlock () Mutual Exclusion Progress Bounded Waiting Mutual Exclusion If process P i is executing in its CS Then no other processes can be executing in their CS Progress: Deadlock free If no process is running in its CS and there exist some processes that wish to enter their CS, Then the selection of the processes that will enter the CS next cannot be postponed indefinitely 4

Bounded Waiting : Starvation A bound must exist on the number of s that other processes are allowed to enter their CSs after a process has made a request toenteritscsandbeforethatrequestis that request is granted Assume that each process executes at a nonzero speed No assumption concerning relative speed of then processes Two approaches depending on if kernel is preemptive or non preemptive Preemptive allows preemption of process when running in kernel mode Non preemptive runs until exits kernel mode, blocks, or voluntarily yields CPU Essentially free of race conditions in kernel mode Absolute, true and mathematical, of itself and from its own nature, flows equably without relation to anything external. (I. Newton, 1689) Time is, like, Nature s way of making sure that everything doesn t happen all at once. (Anonymous, circa 1968) An event a 0 of thread A is Instantaneous No simultaneous events(breakties) ties) a 0 A threada is (formally) a sequence a 0, a 1,...of events Trace model Notation: a 0 a 1 indicates order a 0 a 1 a 2 5

Assign to shared variable Assign to local variable Invoke method Return from method Lots of other things Events are transitions a 3 a 2 a 0 a 1 Thread State Program counter Local variables System state Object fields (shared variables) Union of thread states Thread A Thread A Events of two or more threads Interleaved Notnecessarily independent(why?) Thread B 6

An interval A 0 =(a 0,a 1 ) is Time between events a 0 and a 1 b 0 B b 1 B 0 a 0 A 0 a 1 a 0 A 0 a 1 Interval A 0 precedes interval B 0 b 0 B 0 b 1 b 0 B b 1 B 0 a 0 A 0 a 1 a 0 A 0 a 1 Notation: A 0 B 0 Formally, End event of A 0 before start event of B 0 Also called happens before or precedes Remark: A 0 B 0 is just like saying 1066 AD 1492 AD, Middle Ages Renaissance, Oh wait, what about this week vsthis month? 7

NevertruethatA that A If A Bthen not true that B A If A B&B Cthen A C Funny thing: A B&B Amight both be false! Certain pairs of elements in the set, one of the elements precedes the other Irreflexive: NevertruethatA that A A Antisymmetric: If A Bthen not true that B A Transitive: If A B&B Cthen A C Linear order; a totally ordered set is also called a chain Also Irreflexive Antisymmetric Transitive Except that for every distinct A, B, Either A BorB A while (mumble) { a 0 ; a 1 ; a 0 k A 0 k k-th occurrence of event a 0 k-th occurrence of interval A 0 =(a 0,a 1 ) Let CSk i be thread i sk thcritical section execution Let CSk i be thread i sk thcritical section execution And CSm j be j sm thexecution 8

Let CSk i be thread i s k th critical section execution And CSm j be j s m th execution Then either or Let CSk i be thread i s k th critical section execution And CSm j be j s m th execution Then either or CS ik CS j m CS ik CS j m CS jm CS i k If some thread calls lock() And never returns Then other threads must complete lock()and unlock()calls infinitely often System as a whole makes progress Even if individuals starve If some thread calls lock() It will eventually return Individualthreadsmakeprogress 2 thread solutions first Illustrate most basic ideas Fits Fitson one slide Then n thread solutions class implements Lock { // thread-local index, 0 or 1 public void lock() { int i = ThreadID.get(); int j = 1 - i; Henceforth: i is current thread, j is other thread 9

class LockOne implements Lock { private bool [] flag=new bool [2]; public void lock() { int j,i=threadid.get();j=1-i; flag[i] = true; while (flag[j]) { public void unlock() { int i = ThreadID.get(); flag[i] = false; class LockOne implements Lock { private boolean[] flag = new boolean[2]; public void lock(){ int i = ThreadID.get(); int j = 1 - i; flag[i] = true; while (flag[j]) { Each thread has flag class LockOne implements Lock { private boolean[] flag = new boolean[2]; public void lock() { int i = ThreadID.get(); int j = 1 - i; flag[i] = true; while (flag[j]) { Set my flag class LockOne implements Lock { private boolean[] flag = new boolean[2]; public void lock() { int i = ThreadID.get(); int j = 1 - i; flag[i] = true; while (flag[j]) { Wait for other flag to become false Assume CS Aj overlaps CS B k Consider each thread's last (j thand k th) read and write in the lock() method before entering Derive a contradiction 10