Computer Core Practice1: Operating System Week10. locking Protocol & Atomic Operation in Linux

Similar documents
Prof. Dr. Hasan Hüseyin

Linux Synchronization Mechanisms in Driver Development. Dr. B. Thangaraju & S. Parimala

C09: Process Synchronization

Operating System Design and Implementation

MULTITHREADING AND SYNCHRONIZATION. CS124 Operating Systems Fall , Lecture 10

Part A Interrupts and Exceptions Kernel preemption CSC-256/456 Operating Systems

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

SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T A N D S P R I N G 2018

Synchronization COMPSCI 386

Locks. Dongkun Shin, SKKU

CMSC421: Principles of Operating Systems

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall

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

10/17/ Gribble, Lazowska, Levy, Zahorjan 2. 10/17/ Gribble, Lazowska, Levy, Zahorjan 4

More on Synchronization and Deadlock

Shared Memory Parallel Programming with Pthreads An overview

Introduction to PThreads and Basic Synchronization

Kernel Korner Kernel Locking Techniques

Concept of a process

Chapter 6 Process Synchronization

Lecture. DM510 - Operating Systems, Weekly Notes, Week 11/12, 2018

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

Synchronization Principles

Synchronization I. Jo, Heeseung

Synchronization for Concurrent Tasks

Thread and Synchronization

Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011

Lecture 5: Synchronization w/locks

Operating Systems. Synchronization

Pre-lab #2 tutorial. ECE 254 Operating Systems and Systems Programming. May 24, 2012

Synchronization. Dr. Yingwu Zhu

Kernels and Locking. Luca Abeni

CSE 4/521 Introduction to Operating Systems

Synchronization Spinlocks - Semaphores

CSE 120 Principles of Operating Systems

LSN 13 Linux Concurrency Mechanisms

CS 3723 Operating Systems: Final Review

250P: Computer Systems Architecture. Lecture 14: Synchronization. Anton Burtsev March, 2019

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

Advance Operating Systems (CS202) Locks Discussion

Operating systems. Lecture 3 Interprocess communication. Narcis ILISEI, 2018

Problem Set 2. CS347: Operating Systems

CSE Traditional Operating Systems deal with typical system software designed to be:

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019

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

Operating Systems: William Stallings. Starvation. Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall

Lecture 9: Midterm Review

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks

[537] Locks. Tyler Harter

Solving the Producer Consumer Problem with PThreads

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

POSIX PTHREADS PROGRAMMING

Synchronization for Concurrent Tasks

Dealing with Issues for Interprocess Communication

Operating Systems, Assignment 2 Threads and Synchronization

RCU in the Linux Kernel: One Decade Later

Chapter 2 Processes and Threads

Kernel Critical Sections

Review: Processes. A process is an instance of a running program. POSIX Thread APIs:

Concurrency, Thread. Dongkun Shin, SKKU

Synchronization. CSE 2431: Introduction to Operating Systems Reading: Chapter 5, [OSC] (except Section 5.10)

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

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

OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL. Chap. 2.2 Interprocess Communication

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

Chapter 6: Process Synchronization

Condition Variables. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Kernel Synchronization I. Changwoo Min

Process Synchronization

Synchronization I. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Programming in Parallel COMP755

Processes Prof. James L. Frankel Harvard University. Version of 6:16 PM 10-Feb-2017 Copyright 2017, 2015 James L. Frankel. All rights reserved.

Lecture 9: Multiprocessor OSs & Synchronization. CSC 469H1F Fall 2006 Angela Demke Brown

PROCESSES & THREADS. Charles Abzug, Ph.D. Department of Computer Science James Madison University Harrisonburg, VA Charles Abzug

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

CS510 Advanced Topics in Concurrency. Jonathan Walpole

CS 153 Design of Operating Systems Winter 2016

Lesson 6: Process Synchronization

Precept 2: Non-preemptive Scheduler. COS 318: Fall 2018

IV. Process Synchronisation

CSE 153 Design of Operating Systems

Process Synchronization(2)

Concurrency: Mutual Exclusion (Locks)

Synchronising Threads

Timers 1 / 46. Jiffies. Potent and Evil Magic

Chapter 6 Concurrency: Deadlock and Starvation

Synchronization I q Race condition, critical regions q Locks and concurrent data structures q Next time: Condition variables, semaphores and monitors

Atomic Instructions! Hakim Weatherspoon CS 3410, Spring 2011 Computer Science Cornell University. P&H Chapter 2.11

EECE.4810/EECE.5730: Operating Systems Spring 2017 Homework 2 Solution

IT 540 Operating Systems ECE519 Advanced Operating Systems

Process Synchronization (Part I)

Lecture Outline. CS 5523 Operating Systems: Concurrency and Synchronization. a = 0; b = 0; // Initial state Thread 1. Objectives.

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

This exam paper contains 8 questions (12 pages) Total 100 points. Please put your official name and NOT your assumed name. First Name: Last Name:

TCSS 422: OPERATING SYSTEMS

CS 105, Spring 2015 Ring Buffer

CHAPTER 6: PROCESS SYNCHRONIZATION

Threading and Synchronization. Fahd Albinali

CS 537 Lecture 11 Locks

Chapter 6: Process Synchronization

Transcription:

1 Computer Core Practice1: Operating System Week10. locking Protocol & Atomic Operation in Linux Jhuyeong Jhin and Injung Hwang

Race Condition & Critical Region 2 Race Condition Result may change according to implementation order Critical Region Section of code that must be completely executed before another kernel control path can enter it Kernel Preemption Planned process switch & forced process switch Thread1 Thread2 Thread1 Thread2 Read value i (7) Increase value i (8) Save value i (8) Read value i (8) Increase value i (9) Save value i (9) Read value i (7) Increase value i (8) Save value i (8) Read value i (7) Increase value i (8) Save value i (8)

Synchronization 3 Synchronization is required to protect the critical region Ex) System call accessing the shared data structure Disable kernel preemption Locking protocol is used to synchronize the critical section in kernel

Per-CPU Variables 4 Per-CPU variables One element per each CPU in the system No need to be locked Usage DEFINE_PER_CPU(type, name) : per CPU array call with a specific type and name per_cpu (var, cpu) : get the value of var allocated in per-cpu section per_cpu_ptr (ptr, cpu): get the point of var allocated in per-cpu section get_cpu_var (var) : operate get_cpu_var(var) get_cpu_var (var) : get the value of var allocated in per_cpu of current cpu

Atomic Operation 5 Atomic Operations Every operation must be executed in a single instruction Read-Modify-Write access a memory twice To avoid race condition Linux provide: atomic_t type & special functions & macros atomic_read(v): return v atomic_inc(v): Add 1 to v atomic_set(v, i): set v to i atomic_add_return(i, v): Add i to v and return v atomic_add(i, v): add i to v atomic_sub(i, v): subtract i from v atomic_sub_and_test(i, v): subtract i from v, if result is 0 return 1

Optimization and Memory Barrier 6 Barrier Guarantee access order of instruction Optimization barrier limit instruction order change by optimization Barrier macro: asm volatile( ::: memory ) This prevents the code from being executed in a interleaved way

Spin Lock 7 Spin Locks While loop until kernel control path get Lock (busy wait) Waste time use when releasing CPU costly Preemption disable! Spinlock_t <linux/spinlock_types.h> Slock [1 unlocked state, below 0 locked state] Break_lock: flag signaling that process is busy waiting for the lock Close door and lock P2 wait without sleeping Door open Critical region Critical P1 region P2 Critical P1 region Macro Description spin_lock_init() Set the spin lock to 1 (unlocked) spin_lock() Cycle until spin lock become 1, then set it to 0 spin_unlock() Set the spin lock to 1 spin_unlock_wait() Wait until the spin lock becomes 1 spin_is_locked() Return 0 if the spin lock is set to 1

Semaphore 8 Semaphore Only for process that can sleep Struct semaphore Count (atomic_t) [ >0 : free, =0 : busy, no waiting process, <0 : busy, waiting process ] Wait: Address of wait queue list Sleeps: Flag whether processes are sleeping Close door and lock P2 sleep and store in wait queue Door open Critical region Critical P1 region Critical P1 region

Mutex 9 Mutex have more limitation than semaphore Mutex count 1 Getting and releasing lock in same context Can t exit program while having mutex Interrupt handler can t use mutex Better to use mutex than semaphore (much simpler) Semaphore can become mutex, but not vice versa Semaphore cannot be owned, but mutex is owned Mutex can be released by its owner, but semaphore can be released by one without ownership Semaphore exist as a file in a file system and in a system-wide, while mutex exist in a process-wide

Read-Copy Update (RCU) 10 RCU Good at protecting Read dominated data structure No lock Read wait free & overhead is small Limitation Only protect the data structure allocated dynamically and referred by pointer Can t sleep inside critical region Deallocate when there is no reader.

Pthread Mutex 11 Use pthread_mutex_t Get mutex lock with pthread_mutex_lock() before critical section Release using pthread_mutex_unlock() after

Practice #5 12 Apply mutex lock to synchronize the critical section Use pthread_mutex_t Note: don t care about pthread_join() Code is executed one by one Code is executed in a interleaved way

Homework #4 Compare the performance of locking Spin lock, mutex Measure the time with gettimeofday() Do at least 3 scenarios From simple jobs, to complex works E.g., simple add, do complex operations taking some time Submit the source code and document summarizing the result with the table Simple add operation mutex spinlock s:us