wait with priority An enhanced version of the wait operation accepts an optional priority argument:

Similar documents
Multi-threaded programming in Java

Example of use. Shared var mutex: semaphore = 1; Process i. begin.. P(mutex); execute CS; V(mutex);.. End;

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

Clock Synchronization. Synchronization. Clock Synchronization Algorithms. Physical Clock Synchronization. Tanenbaum Chapter 6 plus additional papers

Advanced Topics in Distributed Systems. Dr. Ayman A. Abdel-Hamid. Computer Science Department Virginia Tech

Concurrency in Java Prof. Stephen A. Edwards

Synchronization. Clock Synchronization

Models of concurrency & synchronization algorithms

Concurrent Programming using Threads

OUTLINE. Introduction Clock synchronization Logical clocks Global state Mutual exclusion Election algorithms Deadlocks in distributed systems

Synchronization. Chapter 5

Threads. Definitions. Process Creation. Process. Thread Example. Thread. From Volume II

CMSC 330: Organization of Programming Languages

Chapter 6 Synchronization

Concurrency User Guide

Synchronization Part 1. REK s adaptation of Claypool s adaptation of Tanenbaum s Distributed Systems Chapter 5

What is a thread anyway?

Multitasking. Multitasking allows several activities to occur concurrently on the computer Levels of multitasking: Process based multitasking

Distributed Synchronization. EECS 591 Farnam Jahanian University of Michigan

CMSC 330: Organization of Programming Languages

殷亚凤. Synchronization. Distributed Systems [6]

CIS233J Java Programming II. Threads

CS11 Java. Fall Lecture 7

CSE 5306 Distributed Systems. Synchronization

Midterm on next week Tuesday May 4. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 9

CS533 Concepts of Operating Systems. Jonathan Walpole

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

CMSC 714 Lecture 14 Lamport Clocks and Eraser

Time in Distributed Systems

MultiThreading. Object Orientated Programming in Java. Benjamin Kenwright

Summary Semaphores. Passing the Baton any await statement. Synchronisation code not linked to the data

CPS 506 Comparative Programming Languages. Programming Language Paradigms

Computation Abstractions. CMSC 330: Organization of Programming Languages. So, What Is a Thread? Processes vs. Threads. A computer.

COURSE 11 PROGRAMMING III OOP. JAVA LANGUAGE

Threads Questions Important Questions

Java: Pitfalls and Strategies

Last Class: Naming. Today: Classical Problems in Distributed Systems. Naming. Time ordering and clock synchronization (today)

Le L c e t c ur u e e 7 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Multithreading

Java Threads. COMP 585 Noteset #2 1

Concurrent Programming

Stacks and Queues

What's wrong with Semaphores?

Event Ordering. Greg Bilodeau CS 5204 November 3, 2009

Transactional Memory: Architectural Support for Lock-Free Data Structures Maurice Herlihy and J. Eliot B. Moss ISCA 93

Performance Throughput Utilization of system resources

1 Process Coordination

EECS 482 Introduction to Operating Systems

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

OpenMP and more Deadlock 2/16/18

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Java Threads. Written by John Bell for CS 342, Spring 2018

Advanced Concepts of Programming

Lecture 9: Introduction to Monitors

Implementing Scheduling Algorithms. Real-Time and Embedded Systems (M) Lecture 9

27/04/2012. We re going to build Multithreading Application. Objectives. MultiThreading. Multithreading Applications. What are Threads?

Real-Time and Concurrent Programming Lecture 4 (F4): Monitors: synchronized, wait and notify

Synchronization in Concurrent Programming. Amit Gupta

CSE 5306 Distributed Systems

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

Lecture 8: September 30

IT 540 Operating Systems ECE519 Advanced Operating Systems

DEPARTMENT OF INFORMATION TECHNOLOGY QUESTION BANK. UNIT I PART A (2 marks)

Overview. Processes vs. Threads. Computation Abstractions. CMSC 433, Fall Michael Hicks 1

Producing Production Quality Software. Lecture 12: Concurrent and Distributed Programming Prof. Arthur P. Goldberg Fall, 2004

Chapter 13. Concurrency ISBN

Threads Chate Patanothai

Chapter 32 Multithreading and Parallel Programming

CS A331 Programming Language Concepts

ArdOS The Arduino Operating System Reference Guide Contents

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

ICT 6544 Distributed Systems Lecture 7

Chapter 6: Process Synchronization

public class Shared0 { private static int x = 0, y = 0;

Chapter 5 Concurrency: Mutual Exclusion and Synchronization

Concurrency: Mutual Exclusion and Synchronization. Concurrency

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

Programming in Parallel COMP755

Need for synchronization: If threads comprise parts of our software systems, then they must communicate.

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

(b) External fragmentation can happen in a virtual memory paging system.

Clock and Time. THOAI NAM Faculty of Information Technology HCMC University of Technology

Design of Concurrent and Distributed Data Structures

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem

Dr. Rafiq Zakaria Campus. Maulana Azad College of Arts, Science & Commerce, Aurangabad. Department of Computer Science. Academic Year

Note: in this document we use process and thread interchangeably.

Chapter 5 Asynchronous Concurrent Execution

Java Threads. Introduction to Java Threads

CMSC 433 Programming Language Technologies and Paradigms. Composing Objects

Process Synchronization

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

OBJECT ORIENTED SIMULATION LANGUAGE. OOSimL Reference Manual - Part 2

Synchronization. CSCI 5103 Operating Systems. Semaphore Usage. Bounded-Buffer Problem With Semaphores. Monitors Other Approaches

Concurrency - Topics. Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads

EE458 - Embedded Systems Lecture 8 Semaphores

1) Discuss the mutual exclusion mechanism that you choose as implemented in the chosen language and the associated basic syntax

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems

THREADS AND CONCURRENCY

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

SYNCHRONIZATION. DISTRIBUTED SYSTEMS Principles and Paradigms. Second Edition. Chapter 6 ANDREW S. TANENBAUM MAARTEN VAN STEEN

Transcription:

wait with priority An enhanced version of the wait operation accepts an optional priority argument: syntax: <variable>.wait <parameter> the smaller the value of the parameter, the highest the priority When the variable is signaled, the process with highest priority in the queue is activated the base wait implementation used a First-In-First-Out (FIFO) discipline

Example: Smallest job first procedure startprint; begin if NOT printerisbusy then jobavailable.wait; printer-file := buffer; end startprint; <print printer-file> procedure endprint; begin printerisbusy := false; OKtoprint.signal; end endprint; procedure enqueuejob(file); begin if printerisbusy then OKtoprint.wait sizeof(file); printerisbusy := true; buffer := file; jobavailable.signal end;

Monitors: pros and cons Pros: encapsulation provides automatic serialization flexibility in blocking and unblocking process execution within monitor procedures Cons lack of concurrency if monitor encapsulates shared resources possibility of deadlock with nested monitor calls

Multi-threaded programming in Java Java allows program to specify multiple threads of execution Provides instructions to ensure mutual exclusion, and selective blocking/unblocking of threads

Lessons learned Encapsulation of critical section of code is desirable provides automatic mutual exclusion single copy of code, single point of synchronization however would be nice to have some form of controlled concurrency Blocking/unblocking of processes is powerful tool basic ingredient are named queues, enqueue and dequeue operations enqueue and dequeue operations usually subject to condition

What is a thread in Java? A thread is a program-counter and a stack All threads share the same memory space A running thread can Yield Sleep Wait for I/O or notification Be pre-empted A key feature: Synchronized methods Allow an exclusive lock, e.g., in an update method

Basic Syntax Build a thread by extending the class java.lang.thread Must have a public void run() method it is executed at the start of the thread, and when it finishes, the thread finishes Synchronized statement Synchronized (obj) { block } Obtains a lock on obj before executing block, releases lock after executing block Wait() gives up lock and suspends the thread Notifyall() resumes all threads waiting on object, resumed tasks must reacquire lock before continuing

Producer Consumer Example Public class ProdCons { private boolean ready ; private Object obj ; public ProducerConsumer() { ready = false ; } public ProducerConsumer (Object o) { obj = o ; ready = true ; } Synchronized Object consume() { while (!ready) wait() ; ready = false ; notifyall() ; return obj ; } Synchronized void produce (object o) { while (ready) wait() ; obj = o ; ready = true ; notifyall() ; } }

Introduction to Distributed Systems First two topics Conceptual Issue: Local and Global Clocks Practical Issue: Process Communication Distributed Mutual Exclusion

Absence of Global Clock Problem: synchronizing the activities of different part of the system (e.g. process scheduling) What about using a single shared clock? two different processes can see the clock at different times due to unpredictable transmission delays What about using radio synchronized clocks? Propagation delays are unpredictable Software approaches Clock synchronization algorithms Logical clocks

Cristian's Algorithm Basic idea: get the current time from a time server. Issues: Error due to communication delay - can be estimated as (T 1 -T 2 -I)/2 Time correction on client must be gradual

The Berkeley Algorithm a) The time daemon asks all the other machines for their clock values b) The machines answer c) The time daemon tells everyone how to adjust their clock

Logical clocks The need to order events in a distributed system has motivated schemes for logical clocks These artificial clocks provide some but not all of the functionality of a real global clock They build a clock abstraction based on underlying physical events of the system

Happened before relation: definitions Happened before relation ( ): a b if a and b are in the same process and a occurred before b a b if a is the event of sending a message and b is the event of receiving the same message by another process if a b and b c then a c, i.e. the relation is transitive The happened before relation is a way of ordering events based on the behavior of the underlying computation

Happened before relation: definitions (2) Two distinct events a and b are said to be concurrent (a b) if and For any two events in the system, either a b, b a or a b Example: a / b b / a P 1 e 11 e 12 e 13 e 14 e 11 e 21 e 22 e 13, e 13 e 14 thus e 22 e 14 P 2 e 21 e 22 e 23 e 24 Global Time

Lamport s Logical Clocks: definitions A logical clock C i at each process P i is a function that assigns a number C i (a) to any event a, called timestamp timestamps are monotonically increasing values example: C i (a) could be implemented as a counter We want to build a logical clock C(a) such that: if a b then C(a) < C(b)

Lamport s Logical Clocks: implementation If we want a logical clock C(a) to satisfy: if a b then C(a) < C(b) the following conditions must be met: if a and b are in the same process and a occurred before b, then C i (a) < C i (b) if a is the event of sending a message in process P i and b is the event of receiving the same message by process P j then C i (a) < C j (b)

Lamport s Logical Clocks: implementation (2) Two implementations rules that satisfy the previous correctness conditions are: clock C i is incremented by d at each event in process P i: C i := C i + d (d > 0) if event a is the sending of a message m by process P i, then message m is assigned the timestamp t m = C i (a) (C i (a) is obtained after applying previous rule). Upon receiving message m, process P j sets its clock to: C j := max(c j, t m + d) (d > 0)

Lamport s Logical Clocks: example Fill the blanks e 11 e 12 e 13 e 14 e 15 e 16 e 17 P 1 ( ) ( ) ( ) ( ) ( ) ( ) ( ) P 2 ( ) ( ) ( ) ( ) ( ) e 21 e 22 e 23 e 24 e 25 Global Time

Lamport s Logical Clocks: example e 11 e 12 e 13 e 14 e 15 e 16 e 17 P 1 (1) (2) (3) (4) (5) (6) (7) P 2 (1) (2) (3) (4) (7) e 21 e 22 e 23 e 24 e 25 Global Time