Process & Thread Management II CIS 657

Similar documents
Process & Thread Management II. Queues. Sleep() and Sleep Queues CIS 657

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

OS Structure. User mode/ kernel mode (Dual-Mode) Memory protection, privileged instructions. Definition, examples, how it works?

Resource management. Real-Time Systems. Resource management. Resource management

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

OS Structure. User mode/ kernel mode. System call. Other concepts to know. Memory protection, privileged instructions

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

Multiprocessor Systems. COMP s1

Process Synchronisation (contd.) Operating Systems. Autumn CS4023

Locks. Dongkun Shin, SKKU

Multiprocessor System. Multiprocessor Systems. Bus Based UMA. Types of Multiprocessors (MPs) Cache Consistency. Bus Based UMA. Chapter 8, 8.

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

Concept of a process

Chapter 6: Process Synchronization

Lecture 5: Synchronization w/locks

C09: Process Synchronization

Introduction to Real-Time Operating Systems

Chapter 6: Process Synchronization

Real-Time Systems. Lecture #4. Professor Jan Jonsson. Department of Computer Science and Engineering Chalmers University of Technology

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

Interrupts, Etc. Operating Systems In Depth V 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

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

Dealing with Issues for Interprocess Communication

Lesson 6: Process Synchronization

Synchronization I. Jo, Heeseung

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

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

CS420: Operating Systems. Process Synchronization

Chapter 6: Process Synchronization

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

CMSC421: Principles of Operating Systems

Chapter 6: Process Synchronization. Operating System Concepts 9 th Edit9on

Synchronising Threads

Mutex Implementation

Multiprocessor Systems. Chapter 8, 8.1

Last Class: Synchronization

Today: Synchronization. Recap: Synchronization

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

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

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

Threads. Concurrency. What it is. Lecture Notes Week 2. Figure 1: Multi-Threading. Figure 2: Multi-Threading

CHAPTER 6: PROCESS SYNCHRONIZATION

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

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

Synchronization Principles

Module 6: Process Synchronization

Operating Systems: Quiz2 December 15, Class: No. Name:

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

CS533 Concepts of Operating Systems. Jonathan Walpole

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

Problem Set 2. CS347: Operating Systems

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

MULTITHREADING AND SYNCHRONIZATION. CS124 Operating Systems Fall , Lecture 10

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

Prof. Dr. Hasan Hüseyin

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Operating Systems. Synchronization

Lecture 9: Midterm Review

Midterm Exam Amy Murphy 19 March 2003

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

Administrivia. p. 1/20

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Chapter 6: Process Synchronization

CSE 153 Design of Operating Systems

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

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

Preemptive Scheduling and Mutual Exclusion with Hardware Support

CS 333 Introduction to Operating Systems. Class 4 Concurrent Programming and Synchronization Primitives

Recap. Mutual exclusion. Peterson s algorithm. Synchronization instructions. s/w solution Assume program order execution. Test&set

Implementing Locks. Nima Honarmand (Based on slides by Prof. Andrea Arpaci-Dusseau)

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

Concurrency, Thread. Dongkun Shin, SKKU

Kernels and Locking. Luca Abeni

Chapter 5: Process Synchronization

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

Lecture #7: Implementing Mutual Exclusion

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

Tasks. Task Implementation and management

Real-Time Programming

Synchronization for Concurrent Tasks

Module 1. Introduction:

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers

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

Introduction to OS Synchronization MOS 2.3

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

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

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

What are they? How do we represent them? Scheduling Something smaller than a process? Threads Synchronizing and Communicating Classic IPC problems

Mutual Exclusion and Synchronization

Concurrency: Deadlock and Starvation. Chapter 6

Last Class: Deadlocks. Today

CS370 Operating Systems

Intro to Threads. Two approaches to concurrency. Threaded multifinger: - Asynchronous I/O (lab) - Threads (today s lecture)

Process Management And Synchronization

Fast and Lock-Free Concurrent Priority Queues for Multi-Thread Systems

Course Syllabus. Operating Systems

Timers 1 / 46. Jiffies. Potent and Evil Magic

Advanced Topic: Efficient Synchronization

Chapter 6 Process Synchronization

Transcription:

Process & Thread Management II CIS 657

Queues Run queues: hold threads ready to execute Not a single ready queue; 64 queues All threads in same queue are treated as same priority Sleep queues: hold threads waiting on events Hashed data structure, optimized for searching on wait channel In older versions of the OS, these held processes, not threads

Sleep() and Sleep Queues LOOKUP(x) hashes an identifier to a sleep queue (kern_synch( kern_synch.c) Ident can be a void *, int,, or other (generally a pointer). The sleep routines do not determine how many identifiers there are in the system Threads can give up the CPU by making a sleep() call (passing wait channel ID and priority at which to be awakened)

Algorithm for Sleep 1. Acquire sched_mutex lock 2. Record the wait channel in the thread structure, and hash the channel to find a sleep queue 3. Set the thread s s priority for when it is awakened, set SLEEPING flag 4. Place thread at end of sleep queue 5. Call mi_switch() to context switch (sched_lock automatically released)

Wakeup() & Wakeup_one() Wakeup awakens all processes on a channel Front to back Wakeup_one() awakens approximately one thread

Algorithm for Wakeup() 1. Remove thread from sleep queue 2. Recompute user-mode scheduling priority if sleep time > 1 sec 3. If in SLEEPING state, 1. Make thread RUNNABLE 2. If in memory, put in run queue; else start swapin process to get it 4. If in STOPPED state, do nothing

Context Switching Involuntary context switch: time slice expired or higher-priority process is ready to run Voluntary context switch: call to sleep(). The routine mi_switch() does the dirty work, selecting the next process to run (see /usr/src/sys/kern/kern_synch.c).

Thread State A context switch requires changing both kernel- and user-mode context Saved in the thread structure (see proc.h) mi_switch() Selects thread from highest-priority scheduling queue w/runnable thread Loads process s s context from the TCB

Historic Synchronization: Uniprocessor Architecture Kernel process was best priority No other process could enter top half of kernel (no preemption) Coordination by ensuring data in stable state before sleep Mask interrupts (if needed) to block bottom half processes from data conflicts

FreeBSD 3.0 The origin of the Big Giant Lock (BGL) Acquired by every process on entry to the kernel. Serialized access to the kernel (good) but limited to 1 process in kernel at a time (bad on multiprocessor)

FreeBSD 5.0 First SMP-ready FreeBSD Most kernel access now outside the BGL 5 levels of locking

The 5 Lock Levels Level Type Can Sleep Description Lowest Hardware no Memory-interlocked test-and- set Spin mutex no Spin lock Sleep mutex no Spin for a while, then sleep Lock manager yes Sleep lock Highest Witness yes Partially ordered sleep locks

Hardware: Memory-interlocked test-and-set Memory-interlocked: only allow one processor to use this instruction on memory at one time Two component operations, done atomically Read value of memory location, then Write value to same location

Swap vs.. Test-and-set Test-and-set(lock) { int one = 1; swap(one, lock); }

Spin Lock Using Test-and-set spinlock(lock) { while (test-and-set(lock)); } spinunlock(lock) { lock = 0; }

Use of Spin Locks Busy waiting is BAD Only for low-contention resources Lock typically held for short time Result should be that very little time is wasted in the lock over the system Do spin locks allow deadlock? Starvation?

Sleep Locks Used when we expect resources to be held for significant lengths of time E.g., disk buffers during I/O So, queue up using sleep() instead of spinning. Releasing the lock must wake up one thread

Sleep Mutex Sometimes we don t t know how long the resource will be held Expect short Could be long Spin a while, then sleep Lets other processes run Inverse exponential distribution

The Witness Module Above the other types of locks is a set of partially-ordered sleep locks This avoids deadlock Recall: Circular wait, no preemption, hold-and-wait, mutual exclusion Which condition does this avoid? Also makes sure thread doesn t t hold any spin locks

Rules for Acquiring Witness Locks A thread may acquire only one lock in each class A thread may only acquire a lock in a higher-numbered class than the highest- numbered lock the thread already holds