Using synchronization primitives CS-3013 A-term 20081 Using Synchronization Primitives in C, C++, and Linux Kernel CS-3013 Operating Systems A-term 2008.

Slides:



Advertisements
Similar presentations
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Advertisements

Multi-core Programming Programming with Posix Threads.
Kevin Walsh CS 3410, Spring 2010 Computer Science Cornell University Atomic Instructions P&H Chapter 2.11.
Concurrency, Race Conditions, Mutual Exclusion, Semaphores, Monitors, Deadlocks Chapters 2 and 6 Tanenbaum’s Modern OS.
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
Semaphores, mutexes and condition variables. semaphores Two types – Binary – 0 or 1 – Counting 0 to n Wait – decrements > 0 forces a wait Post or signal.
8-1 JMH Associates © 2004, All rights reserved Windows Application Development Chapter 10 - Supplement Introduction to Pthreads for Application Portability.
Introduction to Synchronization CS-3013 A-term Introduction to Synchronization CS-3013 Operating Systems (Slides include materials from Modern Operating.
Introduction to Synchronization CS-3013 A-term Introduction to Synchronization CS-3013, Operating Systems A-term 2009 (Slides include materials from.
Jonathan Walpole Computer Science Portland State University
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
CS510 Concurrent Systems Introduction to Concurrency.
Atomic Operations David Monismith cs550 Operating Systems.
Thread-Safe Programming Living With Linux. Thread-Safe Programming Tommy Reynolds Fedora Documentation Project Steering Committee
5.1 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Problems with Semaphores Incorrect use of semaphore operations:
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
Programming with POSIX* Threads Intel Software College.
Thread Synchronization Tutorial #8 CPSC 261. A thread is a virtual processor Each thread is provided the illusion that it owns a core – Copy of the registers.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
Kernel Locking Techniques by Robert Love presented by Scott Price.
Lecture 8 Page 1 CS 111 Online Other Important Synchronization Primitives Semaphores Mutexes Monitors.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
POSIX Synchronization Introduction to Operating Systems: Discussion Module 5.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
2.3 interprocess communcation (IPC) (especially via shared memory & controlling access to it)
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 11: Thread-safe Data Structures, Semaphores.
Pthreads #include pthread_t tid ; //thread id. pthread_attr_t attr ; void *sleeping(void *); /* thread routine */ main() { int time = 2 ; pthread_create(&tid,
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
1 Synchronization Threads communicate to ensure consistency If not: race condition (non-deterministic result) Accomplished by synchronization operations.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 13: Condition Variable, Read/Write Lock, and Deadlock.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
Practice Chapter Five.
PThread Synchronization. Thread Mechanisms Birrell identifies four mechanisms commonly used in threading systems –Thread creation –Mutual exclusion (mutex)
CS 360 pthreads Condition Variables for threads. Page 2 CS 360, WSU Vancouver What is the issue? Creating a thread to perform a task and then joining.
Dining Philosophers & Monitors Questions answered in this lecture: How to synchronize dining philosophers? What are monitors and condition variables? What.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Synchronization.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
Semaphores Reference –text: Tanenbaum ch
Working with Pthreads. Operations on Threads int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void * (*routine)(void*), void* arg) Creates.
CS162 Section 2. True/False A thread needs to own a semaphore, meaning the thread has called semaphore.P(), before it can call semaphore.V() False: Any.
pThread synchronization
CS 311/350/550 Semaphores. Semaphores – General Idea Allows two or more concurrent threads to coordinate through signaling/waiting Has four main operations.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
Kernel Synchronization David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO
Semaphores Reference text: Tanenbaum ch
Chapter 5: Process Synchronization – Part 3
Background on the need for Synchronization
Outline Other synchronization primitives
Principles of Operating Systems Lecture 11
Other Important Synchronization Primitives
Thread synchronization
Lecture 14: Pthreads Mutex and Condition Variables
Threading And Parallel Programming Constructs
COMS Prelim 1 Review Session
CS-3013 Operating Systems Hugh C. Lauer
Project #3 Threads and Synchronization
Synchronization Primitives – Semaphore and Mutex
Lecture 14: Pthreads Mutex and Condition Variables
Threads Synchronization
CS333 Intro to Operating Systems
Semaphores Reference text: Tanenbaum ch
CS 144 Advanced C++ Programming May 7 Class Meeting
Lab #9 Semaphores Operating System Lab.
Presentation transcript:

Using synchronization primitives CS-3013 A-term Using Synchronization Primitives in C, C++, and Linux Kernel CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern Operating Systems, 3 rd ed., by Andrew Tanenbaum and from Operating System Concepts, 7 th ed., by Silbershatz, Galvin, & Gagne)

Using synchronization primitives CS-3013 A-term How do we actually use synchronization mechanisms in program design? Producer-Consumer (pipelined parallelism) Semaphores Mutexes for protecting shared data of pipeline Monitors (task parallelism) Mutexes for monitor locks Condition variables for waiting and signaling in user space Simulation of condition variable in kernel space Barrier Synchronization (data parallelism) Simulate with monitors in user space Simulate with Completion variables in kernel space

Using synchronization primitives CS-3013 A-term Producer-Consumer User-space operations semaphore.h — POSIX semaphores sem_init, sem_wait, sem_trywait, sem_post, sem_getvalue, etc. Usable in any C or C++ user program Thread may sleep for long period of time –Some other thread needs to awaken it Kernel-space operations asm/semaphore.h down, down_interruptible, down_trylock, up For putting a task to sleep for long time (if necessary) –Some other task needs to awaken it May not be used while holding a spinlock or in interrupt handler

Using synchronization primitives CS-3013 A-term Producer-Consumer (continued) Locking shared data structures (temporarily) User space operations pthread.h — POSIX thread tools pthread_mutex_init, pthread_mutex_lock, pthread_mutex_trylock, pthread_mutex_unlock, etc. Only unlockable by owner (i.e., the locking thread) Normally used for short critical sections, no sleeping Kernel space operations linux/spinlock.h spin_lock, spin_trylock, spin_unlock, etc. Must never be used when task might sleep! Consume CPU cycles until locking attempt is satisfied

Using synchronization primitives CS-3013 A-term Questions?

Using synchronization primitives CS-3013 A-term Monitors Must be simulated by lower-level constructs User-space –Monitor lock  pthread_mutex_t Every monitor function must be surrounded by pair ( pthread_mutex_lock, pthread_mutex_unlock ) Not automatic in C, C++ –Condition variable  pthread_cond_t pthread_cond_wait, pthread_cond_signal, pthread_cond_broadcast pthread_cond_wait atomically releases mutex Waiting thread must re-acquire mutex upon waking –Also check situation that required the wait –Easily adaptable to object-oriented programming

Using synchronization primitives CS-3013 A-term Monitors (continued) Harder to simulate in kernel space –Monitor lock  spinlock Every monitor function must be surrounded by pair ( spin_lock, spin_unlock ) pair Not automatic –Condition variables not supported Must be simulated using counter and semaphore –Amenable to object-oriented programming style

Using synchronization primitives CS-3013 A-term Simulating Condition Variables in Kernel Space Condition variable = ( wait_count, semaphore ) Wait: while holding spinlock mon_lock wait_count++ spin_unlock(mon_lock) down(semaphore) spin_lock(mon_lock) Signal: while holding spinlock mon_lock if (wait_count > 0) { –up(semaphore) –wait_count -- } Question: is there a danger of a race condition between waiting and signalling?

Using synchronization primitives CS-3013 A-term Questions?

Using synchronization primitives CS-3013 A-term Barrier Synchronization User space May be simulated with pthread_mutex and pthread_cond_broadcast Kernel space May be simulated with completion variables See linux/completion.h and Robert Love, pp

Using synchronization primitives CS-3013 A-term Questions?