Thread Implementations; MUTEX

Slides:



Advertisements
Similar presentations
1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Advertisements

Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Operating Systems ECE344 Midterm review Ding Yuan
Global Environment Model. MUTUAL EXCLUSION PROBLEM The operations used by processes to access to common resources (critical sections) must be mutually.
1 Implementations: User-level Kernel-level User-level threads package each u.process defines its own thread policies! flexible mgt, scheduling etc…kernel.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 2 Processes and Threads
Chapter 6: Process Synchronization
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Mutual Exclusion.
CSC 322 Operating Systems Concepts Lecture - 8: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
Secure Operating Systems Lesson 5: Shared Objects.
Interprocess Communication
Cpr E 308 Spring 2004 Recap for Midterm Introductory Material What belongs in the OS, what doesn’t? Basic Understanding of Hardware, Memory Hierarchy.
EEE 435 Principles of Operating Systems Interprocess Communication Pt I (Modern Operating Systems 2.3)
1 Threads CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5.
Chapter 2: Processes Topics –Processes –Threads –Process Scheduling –Inter Process Communication (IPC) Reference: Operating Systems Design and Implementation.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Precept 3 COS 461. Concurrency is Useful Multi Processor/Core Multiple Inputs Don’t wait on slow devices.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
Jonathan Walpole Computer Science Portland State University
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
1 Outline Processes Threads Inter-process communication (IPC) Classical IPC problems Scheduling.
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu University of the Western.
Cosc 4740 Chapter 6, Part 3 Process Synchronization.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 2 Processes and Threads Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
Mutual Exclusion. Readings r Silbershatz: Chapter 6.
1 Concurrency: Mutual Exclusion and Synchronization Module 2.2.
Thread Implementations; MUTEX Reference on thread implementation –text: Tanenbaum ch. 2.2 Reference on mutual exclusion (MUTEX) –text: Tanenbaum ch
2.3 interprocess communcation (IPC) (especially via shared memory & controlling access to it)
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
1 Processes and Threads Part II Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
Transmitter Interrupts Review of Receiver Interrupts How to Handle Transmitter Interrupts? Critical Regions Text: Tanenbaum
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Interprocess Communication Race Conditions
Multi-processor Scheduling
Protection of System Resources
CS399 New Beginnings Jonathan Walpole.
Lecture 12: Peterson’s Solution and Hardware Support
Lecture 13: Producer-Consumer and Semaphores
Lecture 11: Mutual Exclusion
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Operating Systems Threads ENCE 360.
Thread Implementation Issues
Transmitter Interrupts
Implementing Mutual Exclusion
Concurrency: Mutual Exclusion and Process Synchronization
Implementing Mutual Exclusion
Lecture 12: Peterson’s Solution and Hardware Support
Lecture 13: Producer-Consumer and Semaphores
Thread Implementations; MUTEX
Lecture 11: Mutual Exclusion
Basic Synchronization
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
CS703 – Advanced Operating Systems
Interprocess Communication & Synchronization
Presentation transcript:

Thread Implementations; MUTEX Reference on thread implementation text: Tanenbaum ch. 2.2 Reference on mutual exclusion (MUTEX) text: Tanenbaum ch. 2.3.3

Non-Blocking I/O A competing method for doing multi-activity programming Use extensively in data base server code doing writes I/O system calls return immediately. They just drop off an I/O request with the OS. App checks later on the status or get notified by signals. In the mean time, the app can serve another request.

Implement Threads in User Space Implement the entire threads package in user space without the kernel knowing about them writing a user level scheduler inside one thread to implement multithreading When 1 thread gets blocked by a system call, all user level threads get blocked

Implement Threads in the Kernel Kernel manages the threads Use system calls to create a new or destroy an old thread When a thread is blocked, the kernel can run other threads More costly to make system calls than calls into a user mode run-time system

Thread Safe Libraries Multi-threading coding is tricky: shared variables cause race conditions Libraries can cause thread-related problems Most of the common C lib calls are thread safe, e.g. printf, strcpy,malloc, etc Examples of non-thread safe C lib calls(there are only 10): e.g. ctime, rand, strtok etc. Thread safe version: ctime_r, rand_r, strtok_r etc.

Difference between ctime and ctime_r “man ctime” shows both versions: char *ctime(const time_t *clock); char *ctime_r(const time_t *clock,char *buf, int buflen); Where is the memory buffer holding the return string? - in C library’s static data If 2 threads are using ctime, the internal buffer will get overwritten With ctime_r, each thread passes its own string buffer. So there is no problem.

Making Single Thread Code Multithreaded Problems with variables that are global to a thread but not the program The errno example: error code is put in errno when a process (or thread) makes a system call that fails

Workaround for errno On Solaris, define a macro in errno.h: #ifdef xxx #define errno (*(_errno())) /***thread safe way ***/ #else extern int errno; /*** old single-thread way ***/ #endif When you write: if(errno = = EINTR), you are really calling _errno function to get the errno for your thread The underlying function _errno() returns a pointer to the thread -private cell for errno, not the value The function has to determine the thread id (via a system call) and find the errno spot for that thread

Mutual Exclusion Using Critical Regions

Mutual Exclusion with Busy Waiting Disabling Interrupts Locked Variables Test and Set Lock (TSL) Strict Alternation Peterson’s Solution

Disabling Interrupts Each process disables interrupts just after entering its critical region and re-enables them just before leaving it Not a good solution too dangerous to have a user program turn on/off interrupts in a multiprocessor environment, turning off interrupt only affects 1 processor Useful mutual exclusion technique only for OS code

Locked Variables Use a single shared(lock) variable. Test the lock before entering critical region If the lock is 0, the process sets it to 1 and enters the critical region If the lock is 1, the process waits until it becomes a 0 Code example: while(lockforD = =1); /*** test the lock ***/ lockforD = 1; /*** set the lock ***/ Potential problem

Test and Set Lock Problem If the locked variable code is preempted just after lockforD go to 0, another process can run The second process sees lockforD = 0, enters into critical region and set lockforD =1 When the first thread starts up again, it also enters into critical region because of the result of the test

TSL Instruction Special atomic TSL instruction: Do Test and set lock in one instruction Use “interlocked test-and-set” instruction to lock the bus between multiprocessors Locks based on TSL are called spin locks Example for x86 processor: enter_region: btsl $1, lockforD # atomic test-and-set bit 1 # lockforD -> CF, 1 ->lockforD jc enter_region # test CF flag: If it is 1, try again ret # If it is 0, access region leave_region: movl $0, lockforD # set to 0 ret

Spin Locks Details How “btsl” works: Advantages Disadvantages 1. Read bit 1 of lockforD into the CF bit of EFLAGS register 2. Set bit 1 of lockforD to 1 3. If CF=1, go back to enter_region and continue testing if CF=0, we have the lock and can go in critical region Advantages no system calls; use special CPU capability can be used in kernel or user code Disadvantages wastes CPU by spinning(use semaphores to block for longer waits) needs assembler coding