Explain briefly how starvation may occur in process scheduling. (2 marks)

Similar documents
Concept of a process

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

Chapter 6: Process Synchronization

Dealing with Issues for Interprocess Communication

Midterm Exam. October 20th, Thursday NSC

CS370 Operating Systems

Concurrency and Synchronisation

Process Synchronization

Two hours. Question ONE is COMPULSORY UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Friday 25th January 2013 Time: 14:00-16:00

Concurrency and Synchronisation

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

Learning Outcomes. Concurrency and Synchronisation. Textbook. Concurrency Example. Inter- Thread and Process Communication. Sections & 2.

PROCESS SYNCHRONIZATION

Chapter 2 Processes and Threads. Interprocess Communication Race Conditions

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

Concurrency: a crash course

Lecture 3: Intro to Concurrent Processing using Semaphores

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

Semaphores INF4140. Lecture 3. 0 Book: Andrews - ch.04 ( ) INF4140 ( ) Semaphores Lecture 3 1 / 34

Programming Languages

Operating Systems Comprehensive Exam. Spring Student ID # 3/16/2006

Process Management And Synchronization

Reintroduction to Concurrency

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

IV. Process Synchronisation

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

CS3502 OPERATING SYSTEMS

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

Process Synchronisation (contd.) Operating Systems. Autumn CS4023

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018

Deadlock CS 241. March 19, University of Illinois

Deadlocks: Part I Prevention and Avoidance Yi Shi Fall 2017 Xi an Jiaotong University

Processes The Process Model. Chapter 2. Processes and Threads. Process Termination. Process Creation

Concurrent Object Oriented Languages

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

Review. Preview. Three Level Scheduler. Scheduler. Process behavior. Effective CPU Scheduler is essential. Process Scheduling

Synchronization Principles

CHAPTER 6: PROCESS SYNCHRONIZATION

Last Class: Deadlocks. Today

High Performance Computing Lecture 21. Matthew Jacob Indian Institute of Science

Today: Synchronization. Recap: Synchronization


Chapter 6: Process Synchronization

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

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

Concurrency and Synchronisation. Leonid Ryzhyk

Lesson 6: Process Synchronization

Lecture 9: Midterm Review

Last Class: Synchronization Problems. Need to hold multiple resources to perform task. CS377: Operating Systems. Real-world Examples

Paralleland Distributed Programming. Concurrency

CS450 OPERATING SYSTEMS FINAL EXAM ANSWER KEY

Interprocess Communication By: Kaushik Vaghani

CS370 Operating Systems

Interprocess Communication and Synchronization

Performance Throughput Utilization of system resources

CS370 Operating Systems

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

CS370 Operating Systems

Processes The Process Model. Chapter 2 Processes and Threads. Process Termination. Process States (1) Process Hierarchies

Roadmap. Readers-Writers Problem. Readers-Writers Problem. Readers-Writers Problem (Cont.) Dining Philosophers Problem.

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

Sections 01 (11:30), 02 (16:00), 03 (8:30) Ashraf Aboulnaga & Borzoo Bonakdarpour

Chapter 2 Processes and Threads

PESIT Bangalore South Campus

Multicore and Multiprocessor Systems: Part I

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

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

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

Chapter 7: Process Synchronization!

Last Class: Monitors. Real-world Examples

Lecture Topics. Announcements. Today: Concurrency (Stallings, chapter , 5.7) Next: Exam #1. Self-Study Exercise #5. Project #3 (due 9/28)

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

Process Synchronization

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

CSC501 Operating Systems Principles. Process Synchronization

Operating Systems. Thread Synchronization Primitives. Thomas Ropars.

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

Concurrent & Distributed Systems Supervision Exercises

CSE 153 Design of Operating Systems

ECE 462 Object-Oriented Programming using C++ and Java. Scheduling and Critical Section

Concurrent Programming

Threads and concurrency

Threads and concurrency

The dining philosophers problem. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 13

Multithreaded Programming Part II. CSE 219 Stony Brook University, Department of Computer Science

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

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

Synchronization Basic Problem:

Deadlocks. Copyright : University of Illinois CS 241 Staff 1

Process Synchronization

Page 1. Challenges" Concurrency" CS162 Operating Systems and Systems Programming Lecture 4. Synchronization, Atomic operations, Locks"

CS 537 Lecture 8 Monitors. Thread Join with Semaphores. Dining Philosophers. Parent thread. Child thread. Michael Swift

Introduction to Operating Systems

Operating systems and concurrency (B08)

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 8: Semaphores, Monitors, & Condition Variables

Semaphores. To avoid busy waiting: when a process has to wait, it will be put in a blocked queue of processes waiting for the same event

Semaphores. Semaphores. Semaphore s operations. Semaphores: observations

5 Classical IPC Problems

Transcription:

OMP25111 Lecture 8 1/40 From last time Explain briefly how starvation may occur in process scheduling. (2 marks) In round-robin scheduling, new processes are typically placed at the end of the ready-state queue rather than at the beginning. Suggest a good reason for this. (2 marks) A scheduler uses a time-slice of 4.5msec, and a context switch takes 0.5msec. What percentage of CPU time is spent on executing process instructions: (a) if processes use the whole time-slice? (b) if processes only need 0.5msec CPU-bursts? In general, how would you improve the percentage of CPU time spent on executing process instructions? (3 marks)

OMP25111 Lecture 8 2/40 COMP25111: Operating Systems Lecture 8: Process/Thread Synchronisation John Gurd School of Computer Science, University of Manchester Autumn 2014

OMP25111 Lecture 8 3/40 Overview & Learning Outcomes Process Synchronisation Semaphores Deadlocks Dining Philosophers Message-passing Everything in this handout about process synchronisation also applies to thread synchronisation

OMP25111 Lecture 8 Process Synchronisation 6/40 Problem: Too much milk Time Person 1 Person 2 Person 3 8:00 sleeping sleeping check fridge - no milk 8:15 wakes up sleeping leave for lecture 8:30 check - no milk sleeping travelling 8:45 leave for store wakes up arrive at University 9:00 arrive at store check - no milk go to lecture 9:15 bought milk leave for store (lecture) 9:30 return home arrive at store (lecture) 9:45 bought milk go to store 10:00 return home arrive at store 10:15 bought milk 10:45 return home

OMP25111 Lecture 8 Process Synchronisation 8/40 Data inconsistency Concurrent access to shared data static int i; // shared by A & B // process or thread A // process or thread B i = 0; i = 0; while (i < 100) { while (i > -100) { i++; i--; } } System.out.println("A"); System.out.println("B"); (assume both access & assignment are atomic i.e. indivisible) Questions: which will finish first? will they ever finish? if one finishes, will the other also finish? does it help if one gets a head start?

OMP25111 Lecture 8 Process Synchronisation 10/40 Race Condition Several processes manipulate shared data concurrently & outcome depends on precise order of what happens when Q: what are a CPU s atomic operations? e.g.: shared variable i in memory, initial value 4 process A: i++; i.e. load Reg from i; Reg = Reg + 1; store Reg to i process B: i--; i.e. load Reg from i; Reg = Reg 1; store Reg to i Question: what can the final value of i be?

OMP25111 Lecture 8 Process Synchronisation 12/40 Definitions Data inconsistency: disagreement about data values Synchronisation: using appropriate policies and mechanisms to ensure the correct operation of cooperating processes Critical section (Critical region): section of code in which shared data is used Mutual exclusion (mutex): at most 1 process can be in its critical section at once i.e. if more than one process tries to enter a critical section simultaneously, only one can succeed others must wait

OMP25111 Lecture 8 Semaphores 15/40 Semaphores Dijkstra, 1965: An integer variable (e.g. S) accessed via two atomic operations (with Dutch names!): P(S) ( try-to-reduce, down, wait, aquire, probe, procure) while (S<=0) ; /*no action*/ S--; V(S) ( increase, up, signal, release, vacate) S++; Initialise S appropriately = number of processes allowed in critical section at once (usually 1)

OMP25111 Lecture 8 Semaphores 17/40 Don t loop yield In practice, it is silly to busy-wait in P() P() adds process to a queue & gives up CPU V() takes process from queue & makes it ready

OMP25111 Lecture 8 Semaphores 19/40 Example 1 semaphore Two processes sharing A[100]; initialise S=1;...... P(S); P(S); r1=a[100]; r2=a[100]; critical r1++; r2++; A[100]=r1; A[100]=r2; section V(S); V(S);......

OMP25111 Lecture 8 Semaphores 21/40 Example 2 semaphores Initialise: S1=0; S2=0; I=0;... /* A */... /* B */... /* C */ P(S1); P(S2); for(j=0;j<10;j++) I=I*2; I=I+5; I++; V(S1); V(S1); V(S2);......... Question: What is the the sequence of events?

Deadlock: everyone waiting for everyone else COMP25111 Lecture 8 Semaphores 23/40

OMP25111 Lecture 8 Semaphores 25/40 Deadlock A set of waiting processes, where each process is waiting for something that can only be provided by another of the processes e.g. S1, S2 initialised to 1 // A // B P(S1); P(S2); P(S2); P(S1);...... V(S1); V(S2); V(S2); V(S1); Detection complicated Once occurred, almost impossible for OS to solve

COMP25111 Lecture 8 Semaphores 26/40 Prevention e.g. Deadlock (trajectory) Diagram Process-2 s progress V(S1) V(S2) S1 P(S1) S2 DEAD- P(S2) -LOCK P(S1) P(S2) V(S1) V(S2) Process-1 s progress

Benjamin D. Esham / Wikimedia Commons COMP25111 Lecture 8 Dining Philosophers 29/40

OMP25111 Lecture 8 Dining Philosophers 31/40 The Dining Philosophers 5 philosophers, 5 plates of spaghetti, 5 forks, 1 circular table Alternately think (alone) or eat with 2 forks (from right and left) Semaphore fork[5]; // initialise to 1 philosopher (int i) { while(true) { System.out.println (i + " hungry"); P(fork[i]); P(fork[(i+1)mod 5]); System.out.println (i + " eating"); V(fork[i]); V(fork[(i+1)mod 5]); System.out.println (i + " thinking"); // delay? } } This may deadlock

OMP25111 Lecture 8 Higher-level Synchronisation 33/40 Message-passing Shared data Semaphore(s) control access No shared data send copy to other processes Wait for message instead of semaphore from known source(s), or from any source Con: slower(?) Pro: more general/flexible RPC multi-cpu Distributed/Network

OMP25111 Lecture 8 Higher-level Synchronisation 34/40 Summary of key points Process Synchronisation Semaphores Deadlocks may occur in a variety of situations, usually fatal Dining Philosophers many solutions Message-passing Everything in this handout about process synchronisation also applies to thread synchronisation Next: Threads in Java.

Your Questions OMP25111 Lecture 8 Higher-level Synchronisation 35/40

OMP25111 Lecture 8 Higher-level Synchronisation 36/40 For next time Explain briefly how a deadlock may occur (2 marks) shared variables x, y, s; initial values S1=S2=0 x=y=1 s=0 line Thread A Thread B Explain the purpose 1. do{ do{ of the semaphores in: 2. V(S1) P(S1) lines 2 & 3 3. P(S2) V(S2) of both threads 4. x=x+y P(S1) line 4 of B 5. V(S1) y=x-y & line 5 of A 6. V(S1) s=s+1 lines 6 & 7 of A 7. P(S2) P(S1) and 7 & 8 of B 8. print s,y V(S2) 9. }while(s<7) }while(s<7) (1 mark each) Will A ever terminate? Justify your answer. (1 mark) What is output by the print statement in line 8 of A? (3 marks)

OMP25111 Lecture 8 Higher-level Synchronisation 37/40 Exam Questions shared variables x1,x2,x3,x4,x5,x6; initially S1=S2=S3=0 Thread A Thread B Thread C 2. x1= 1 x2= 2 x3= 3 3. V(S1) V(S1) P(S1); P(S1) 4. P(S2) P(S3) V(S2); V(S3) 5. x4= x2+x3 x5= x1+x3 x6= x1+x2 6. V(S1) V(S1) P(S1); P(S1) 7. P(S2) P(S3) V(S2); V(S3) 8. x2= x5+x6 x3= x4+x6 x1= x4+x5 continued...

OMP25111 Lecture 8 Higher-level Synchronisation 38/40 Exam Question ctd. Is it possible that: line 5 of A executes before line 2 of B? line 5 of C executes before line 2 of A? line 5 of B executes before line 5 of C? line 5 of A executes before line 5 of B? line 5 of A executes after line 5 of C? line 8 of A executes before line 5 of B? line 8 of A executes before line 5 of C? line 8 of C executes before line 5 of B? What are the final values of x1, x2, x3, x4, x5, x6? If S3 is replaced by S2 throughout, can you find a pattern of execution which gives different answers to the questions above?

OMP25111 Lecture 8 Higher-level Synchronisation 39/40 Glossary Shared data Concurrent access Data (in)consistency Atomic action/operation Race condition Synchronisation Critical section/region Mutual exclusion (mutex) Semaphore P() V() Busy-wait Deadlock Deadlock trajectory diagram Dining Philosphers Message-passing

OMP25111 Lecture 8 Higher-level Synchronisation 40/40 Reading OSC/J: 6.1, 6.2, 6.5 (skim 6.3, 6.4, 6.6) older OSC/J: 7.1, 7.2, 7.5 (skim 7.3, 7.4, 7.6) MOS: 2.3 (opening paragraphs), 2.3.1, 2.3.2, 2.3.5 (skim 2.3, 2.4, intro of MOS2 ch.3 or MOS3 ch.6) Dining philosphers: AOS 7.6.3, MOS2 2.4.1) Traffic deadlock: http://www.glommer.net/blogs/?p=189