CS370 Operating Systems

Similar documents
CS370 Operating Systems

CS370 Operating Systems

CS370 Operating Systems

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

CPU Scheduling. Daniel Mosse. (Most slides are from Sherif Khattab and Silberschatz, Galvin and Gagne 2013)

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

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

CPU Scheduling: Objectives

Process Synchronization

Chapter 6: CPU Scheduling. Operating System Concepts 9 th Edition

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

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

Chapter 5: CPU Scheduling. Operating System Concepts Essentials 8 th Edition

CS370 Operating Systems

Chapter 5: CPU Scheduling. Operating System Concepts 9 th Edit9on

Dept. of CSE, York Univ. 1

CHAPTER 6: PROCESS SYNCHRONIZATION

CS370 Operating Systems Midterm Review

Lesson 6: Process Synchronization

Chapter 6: Process Synchronization

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

CS420: Operating Systems. Process Synchronization

Chapter 5: CPU Scheduling

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

Process Synchronization

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

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

Chapter 5: Process Synchronization

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

CS307: Operating Systems

Process Synchronization (Part I)

Chapter 6: Process Synchronization

Chapter 5: CPU Scheduling

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

Operating Systems CS 323 Ms. Ines Abbes

CPU Scheduling Algorithms

CSC Operating Systems Spring Lecture - XII Midterm Review. Tevfik Ko!ar. Louisiana State University. March 4 th, 2008.

Properties of Processes

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Chapter 6: Process Synchronization

Chapter 6: CPU Scheduling

CS370 Operating Systems

CS370 Operating Systems Midterm Review. Yashwant K Malaiya Spring 2019

Chapter 7: Process Synchronization!

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

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

Chapter 5: Process Synchronization

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

Chapter 6: CPU Scheduling. Operating System Concepts 9 th Edition

Chapter 5: Synchronization 1

CSE 4/521 Introduction to Operating Systems

Course Syllabus. Operating Systems

LECTURE 3:CPU SCHEDULING

CPU Scheduling. Operating Systems (Fall/Winter 2018) Yajin Zhou ( Zhejiang University

Chapter 6: CPU Scheduling. Operating System Concepts 9 th Edition

Chapter 6: CPU Scheduling

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

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

Chapter 6: CPU Scheduling. Operating System Concepts 9 th Edition

General Objectives: To understand the process management in operating system. Specific Objectives: At the end of the unit you should be able to:

Chapter 6: CPU Scheduling

Chapter 5: Process Synchronization

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

CS370 Operating Systems

Process Synchronization

Module 6: Process Synchronization

Chapter 6: Process Synchronization

Synchronization Principles

Process- Concept &Process Scheduling OPERATING SYSTEMS

Process Synchronization

Midterm Exam. October 20th, Thursday NSC

Process Synchronization

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

Lecture 5 / Chapter 6 (CPU Scheduling) Basic Concepts. Scheduling Criteria Scheduling Algorithms

CS370 Operating Systems

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - VII CPU Scheduling - II. Louisiana State University

Roadmap. Multilevel Queue Scheduling. Multilevel Queue. Example of Multilevel Feedback Queue. Multilevel Feedback Queue. Tevfik Ko!

Synchronization for Concurrent Tasks

Process Synchronization - I

Chapter 7: Process Synchronization. Background. Illustration

Chapter 2 Processes and Threads. Interprocess Communication Race Conditions

Process Synchronization

Chapter 6: Process Synchronization

Operating System Concepts Ch. 5: Scheduling

Chapter 2 Processes and Threads

Process Synchronization

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

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

CSC501 Operating Systems Principles. Process Synchronization

CPU Scheduling. Basic Concepts. Histogram of CPU-burst Times. Dispatcher. CPU Scheduler. Alternating Sequence of CPU and I/O Bursts

Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

Introduction to Operating Systems

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

Process Coordination

OPERATING SYSTEMS CS3502 Spring Processor Scheduling. Chapter 5

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

Frequently asked questions from the previous class survey

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

Chapter 5: CPU Scheduling. Operating System Concepts 8 th Edition,

PROCESS SYNCHRONIZATION

Transcription:

CS370 Operating Systems Colorado State University Yashwant K Malaiya Spring 1018 L10 Synchronization Slides based on Text by Silberschatz, Galvin, Gagne Various sources 1 1

FAQ Development project: You need to get your own board/hardware and locate the software needed. What if processes have the same priority? Time quantum- what? How? Thread scheduling similar to process scheduling Multilevel Feedback Queue: implementations may vary Q0, Q1, Q2.. Does CPU go to Q1 only when Q0 is empty? If processes keep coming in, will it ever move to Q1? 2

Multilevel Feedback Queue A process can move between the various queues; aging can be implemented this way Multilevel-feedback-queue scheduler defined by the following parameters: number of queues scheduling algorithms for each queue method used to determine when to upgrade a process method used to determine when to demote a process method used to determine which queue a process will enter when that process needs service 3

Example of Multilevel Feedback Queue Three queues: Q 0 RR with time quantum 8 milliseconds Q 1 RR time quantum 16 milliseconds Q 2 FCFS (no time quantum limit) Scheduling A new job enters queue Q 0 which is served FCFS When it gains CPU, job receives 8 milliseconds If it does not finish in 8 milliseconds, job is moved to queue Q 1 At Q 1 job is again served FCFS and receives 16 additional milliseconds If it still does not complete, it is preempted and moved to queue Q 2 CPU-bound (long CPU burst): priority falls, quantum raised, I/O-bound: priority rises, quantum lowered 4

Virtualization and Scheduling Virtualization software schedules multiple guests onto CPU(s) Each guest doing its own scheduling Not knowing it doesn t own the CPUs Can effect time-of-day clocks in guests VMM has its own scheduler Various approaches have been used Workload aware, Guest OS cooperation, etc. 5

Operating System Examples Solaris scheduling: 6 classes, Inverse relationship between priorities and time quantum Windows XP scheduling: 32 priority levels (real-time, not real-time levels) Linux scheduling schemes have continued to evolve. Linux Completely fair scheduler (CFS, 2007): Variable time-slice based on number and priority of the tasks in the queue. Maximum execution time based on waiting processes (Q/n). Processes kept in a red-black binary tree with scheduling complexity of O(log N) Process with lowest weighted spent execution (virtual run time) time is picked next. Weighted by priority ( niceness ). 6

Algorithm Evaluation How to select CPU-scheduling algorithm for an OS? Determine criteria, then evaluate algorithms Deterministic modeling Type of analytic evaluation Takes a particular predetermined workload and defines the performance of each algorithm for that workload Consider 5 processes arriving at time 0: 7

Deterministic Evaluation For each algorithm, calculate minimum average waiting time Simple and fast, but requires exact numbers for input, applies only to those inputs FCS is 28ms: Non-preemptive SFJ is 13ms: RR is 23ms: 8

Probabilitistic Models Assume that the arrival of processes, and CPU and I/O bursts are random Repeat deterministic evaluation for many random cases and then average Approaches: Analytical: Queuing models Simulation: simulate using realistic assumptions 9

Queueing Models Describes the arrival of processes, and CPU and I/O bursts probabilistically Commonly exponential, and described by mean Computes average throughput, utilization, waiting time, etc Computer system described as network of servers, each with queue of waiting processes Knowing arrival rates and service rates Computes utilization, average queue length, average wait time, etc 10

Little s Formula n = average queue length W = average waiting time in queue λ = average arrival rate into queue Little s law in steady state, processes leaving queue must equal processes arriving, thus: n = λ x W Valid for any scheduling algorithm and arrival distribution For example, if on average 7 processes arrive per second, and normally 14 processes in queue, then average wait time per process = 2 seconds 11

Simulations Queueing models limited Simulations more accurate Programmed model of computer system Clock is a variable Gather statistics indicating algorithm performance Data to drive simulation gathered via Random number generator according to probabilities Distributions defined mathematically or empirically Trace tapes record sequences of real events in real systems 12

13 Evaluation of CPU Schedulers by Simulation

Implementation Even simulations have limited accuracy Just implement new scheduler and test in real systems High cost, high risk Environments vary Most flexible schedulers can be modified per-site or per-system Or APIs to modify priorities But again environments vary 14

CS370 Operating Systems Colorado State University Yashwant K Malaiya Spring 1018 L10 Synchronization Slides based on Text by Silberschatz, Galvin, Gagne Various sources 15 15

Process Synchronization: Objectives Concept of process synchronization. The critical-section problem, whose solutions can be used to ensure the consistency of shared data Software and hardware solutions of the criticalsection problem Classical process-synchronization problems Tools that are used to solve process synchronization problems 16 16

Process Synchronization EW Dijkstra Go To Statement Considered Harmful 17

Too Much Milk Example Person A Person B 12:30 Look in fridge. Out of milk. 12:35 Leave for store. 12:40 Arrive at store. Look in fridge. Out of milk. 12:45 Buy milk. Leave for store. 12:50 Arrive home, put milk away. Arrive at store. 12:55 Buy milk. 1:00 Arrive home, put milk away. Oh no! 18

Background Processes can execute concurrently May be interrupted at any time, 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 Illustration: we wanted to provide a solution to the consumer-producer problem that fills all the buffers. have an integer counter that keeps track of the number of full buffers. Initially, counter is set to 0. It is incremented by the producer after it produces a new buffer decremented by the consumer after it consumes a buffer. Will it work without any problems? 19

Consumer-producer problem Producer while (true) { /* produce an item*/ } while (counter == BUFFER_SIZE) ; /* do nothing */ buffer[in] = next_produced; in = (in + 1) % BUFFER_SIZE; counter++; Consumer while (true) { while (counter == 0); /* do nothing */ next_consumed = buffer[out]; out = (out + 1) % BUFFER_SIZ counter--; /* consume the item in next consumed */ } They run concurrently (or in parallel), and are subject to context switches at unpredictable times. In, out: indices of empty and filled items in the buffer. 20 20

Race Condition counter++ could be compiled as register1 = counter register1 = register1 + 1 counter = register1 counter-- could be compiled as register2 = counter register2 = register2-1 counter = register2 They run concurrently, and are subject to context switches at unpredictable times. Consider this execution interleaving with count = 5 initially: S0: producer execute register1 = counter {register1 = 5} S1: producer execute register1 = register1 + 1 {register1 = 6} S2: consumer execute register2 = counter {register2 = 5} S3: consumer execute register2 = register2 1 {register2 = 4} S4: producer execute counter = register1 {counter = 6 } S5: consumer execute counter = register2 {counter = 4} 21 Overwrites!

Critical Section Problem We saw race condition between counter ++ and counter Solution to the race condition problem: critical section Consider system of n processes {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 Race condition: when outcome depends on timing/order that is not predictable 22

23 Introductions

24 ICQ

Process Synchronization: Outline critical-section problem to ensure the consistency of shared data Software and hardware solutions of the critical-section problem Peterson s solution Atomic instructions Mutex locks and semaphores Classical process-synchronization problems Bounded buffer, Readers Writers, Dining Philosophers Another approach: Monitors 25

General structure: Critical section do { Request permission to enter entry section exit section critical section } while (true); remainder section Housekeeping to let processes to enter other 26

Algorithm for Process P i do { P i Waits to enter while (turn == j); critical section turn = j; remainder section } while (true); P i Executes critical section P i lets j enter critical section Process Pj is also trying to enter the critical section, and is also running similar code. 27

Solution to Critical-Section Problem 1. Mutual Exclusion - If process P i is executing in its critical section, then no other processes can be executing in their critical sections 2. Progress - If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely 3. Bounded Waiting - A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted Assume that each process executes at a nonzero speed No assumption concerning relative speed of the n processes 28

Critical-Section Handling in OS 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 29