Deadlock Victim. dimanche 6 mai 12

Size: px
Start display at page:

Download "Deadlock Victim. dimanche 6 mai 12"

Transcription

1 Deadlock Victim by Dr Heinz Kabutz && Olivier Croisier The Java Specialists Newsletter && The Coder's Breakfast && 1

2 You discover a race condition 2

3 Data is being corrupted 3

4 What ya gonna do? 4

5 Who ya gonna call? 5

6 Ghostbusters? 6

7 you do what we all do 7

8 you use synchronized 8

9 synchronized 9

10 synchronized 9

11 synchronized can be bad medicine 10

12 deadlock awaits you! 11

13 12

14 Deadlock Victim 13

15 Correctness fix can lead to liveness fault 14

16 lock ordering deadlock 15

17 at least two locks 16

18 and at least two threads 17

19 locked in different orders 18

20 can cause a deadly embrace 19

21 requires global analysis to fix 20

22 resource deadlocks 21

23 bounded queues 22

24 bounded thread pools 23

25 semaphores 24

26 class loaders 25

27 lost signals 26

28 The Drinking Philosophers 27

29 @ the Java Specialist Symposium in Crete 28

30 Symposium == sym + poto Literally - to drink together 29

31 our philosophers need two cups 30

32 they either want to drink or think 31

33 Table is ready, all philosophers are thinking

34 philosophers 5 wants to drink, takes right cup

35 Philosopher 5 is now drinking with both cups

36 philosophers 3 wants to drink, takes right cup

37 Philosopher 3 is now drinking with both cups

38 philosophers 2 wants to drink, takes right cup

39 Philosopher 3 finished drinking, returns right cup

40 Philosopher 2 is now drinking with both cups

41 Philosopher 3 returns Left cup

42 philosophers can get stuck 41

43 if they all pick up the right cup 42

44 A deadlock can easily happen with this design

45 Philosopher 5 wants to drink, takes right cup

46 Philosopher 1 wants to drink, takes right cup

47 Philosopher 2 wants to drink, takes right cup

48 Philosopher 3 wants to drink, takes right cup

49 Philosopher 4 wants to drink, takes right cup

50 Deadlock!

51 they will all stay thirsty

52 this is a deadly embrace

53 this can be detected automatically 52

54 search the graph of call stacks 53

55 and look for circular dependencies 54

56 ThreadMXBean does that for us 55

57 simple deadlocks are quickly found 56

58 but what do you DO with them? 57

59 what will the dog do with the car if he ever catches it? 58

60 databases choose deadlock victim 59

61 but what does Java do? 60

62 it should probably throw an Error 61

63 but the threads simply hang up 62

64 it is best to avoid causing them! 63

65 global order of locks in program 64

66 public class LeftRightDeadlock { private final Object left = new Object(); private final Object right = new Object(); public void leftright() { synchronized (left) { synchronized (right) { dosomething(); } } } public void rightleft() { synchronized (right) { synchronized (left) { dosomethingelse(); } } } } 65

67 Interleaving causes deadlock lock left trying to lock right A leftright() BLOCKED lock right trying to lock left B rightleft() BLOCKED 66

68 we define a fixed global order 67

69 e.g. left before right 68

70 deadlock is solved! public void rightleft() { synchronized (left) { synchronized (right) { dosomethingelse(); } } } 69

71 our drinking philosophers 70

72 each cup gets a unique number 71

73 lock the highest number cup first 72

74 then lock the lower number cup 73

75 drink 74

76 unlock lower order cup 75

77 unlock higher order cup 76

78 think 77

79 Global Lock ordering We start with all the philosophers thinking

80 Philosopher 5 takes cup 5 Cup 5 has higher number 1 Remember our rule!

81 Philosopher 1 takes cup 2 Must take the cup with the higher number first 1 2 In this case cup

82 Philosopher 2 takes cup

83 Philosopher 3 takes cup

84 Philosopher 1 takes cup 1 - Drinking

85 Philosopher 1 returns Cup 1 Cups are returned in the opposite order to what they are acquired

86 Philosopher 5 takes cup 1 - Drinking

87 Philosopher 5 returns cup

88 Philosopher 1 returns cup

89 Philosopher 2 takes cup 2 - Drinking

90 Philosopher 5 returns cup

91 Philosopher 4 takes cup

92 Philosopher 2 returns cup

93 Philosopher 2 returns cup

94 Philosopher 3 takes cup 3 - Drinking

95 Philosopher 3 Returns cup

96 Philosopher 3 Returns cup

97 Philosopher 4 takes cup 4 - Drinking

98 Philosopher 4 Returns cup

99 Philosopher 4 Returns cup

100 sorting locks by property is easy 99

101 e.g. account IBAN number 100

102 or employee number 101

103 or some other characteristic 102

104 maybe even System.identityHashCode() 103

105 use "open calls" 104

106 Vector synchronizes writeobject() private synchronized void writeobject(objectoutputstream s) throws IOException { s.defaultwriteobject(); } 105

107 synchronized writeobject() is invoking s.defaultwriteobject() private synchronized void writeobject(objectoutputstream s) throws IOException { s.defaultwriteobject(); } 106

108 this is not an "open call" private synchronized void writeobject(objectoutputstream s) throws IOException { s.defaultwriteobject(); } 107

109 but can it cause problems? private synchronized void writeobject(objectoutputstream s) throws IOException { s.defaultwriteobject(); } 108

110 yes it can! private synchronized void writeobject(objectoutputstream s) throws IOException { s.defaultwriteobject(); } 109

111 serialize both vectors at same time Vector v1 = new Vector(); Vector v2 = new Vector(); v1.add(v2); v2.add(v1); 110

112 this could happen with nested data structures 111

113 it happened to an IBM client 112

114 private void writeobject(objectoutputstream stream) throws IOException { Vector<E> cloned = null; // this specially fix is for a special dead-lock in customer // program: two vectors refer each other may meet dead-lock in // synchronized serialization. Refer CMVC synchronized (this) { try { cloned = (Vector<E>) super.clone(); cloned.elementdata = elementdata.clone(); } catch (CloneNotSupportedException e) { // no deep clone, ignore the exception } } cloned.writeobjectimpl(stream); } private void writeobjectimpl(objectoutputstream stream) throws IOException { stream.defaultwriteobject(); } 113

115 Java 7 uses an "open call" private void writeobject(objectoutputstream s) throws IOException { final ObjectOutputStream.PutField fields = s.putfields(); final Object[] data; synchronized (this) { fields.put("capacityincrement", capacityincrement); fields.put("elementcount", elementcount); data = elementdata.clone(); } fields.put("elementdata", data); s.writefields(); } 114

116 More synchronized writeobject()s File StringBuffer Throwable URL Hashtable and others 115

117 we have seen the deadly embrace 116

118 are there other deadlocks? 117

119 semaphore deadlocks 118

120 Bounded DB Connection Pools For example, say you have two DB connection pools Some tasks might require connections to both databases Thus thread A might hold semaphore for D1 and wait for D2, whereas thread B might hold semaphore for D2 and be waiting for D1 119

121 public class DatabasePool { private final Semaphore connections; public DatabasePool(int connections) { this.connections = new Semaphore(connections); } public void connect() { connections.acquireuninterruptibly(); System.out.println("DatabasePool.connect"); } public void disconnect() { System.out.println("DatabasePool.disconnect"); connections.release(); } } 120

122 ThreadMXBean does not show this as a deadlock! 121

123 JVM does not detect this deadlock 122

124 JVM does not detect this deadlock 122

125 Avoiding and diagnosing deadlocks Avoiding Liveness Hazards 123

126 Avoiding and diagnosing deadlocks You cannot get a lock-ordering deadlock with a single lock Easiest way to avoid deadlocks But not always practical 124

127 Avoiding and diagnosing deadlocks For multiple locks we need to define a lock order specify and document possible lock sequences Identify where multiple locks could be acquired Do a global analysis to ensure that lock ordering is consistent This can be extremely difficult in large programs 125

128 Use open calls whenever possible 126

129 Unit Testing for lock ordering deadlocks Code typically has to be called many times before a deadlock happens How many times do you need to call it to prove that there is no deadlock? Nondeterministic unit tests are bad - they should either always pass or always fail 127

130 Testing the Bank In the transfermoney() method, a deadlock occurs if after the first lock is granted, the first thread is swapped out and another thread requests the second lock public class Bank { public boolean transfermoney(account from, Account to, Amount amount) { synchronized (from) { // if thread is swapped out here, we can end up with a deadlock synchronized (to) { return doactualtransfer(from, to, amount); } } } } 128

131 We can force the context switch 129

132 simply add a sleep after first lock 130

133 Adding a sleep to cause deadlocks public class Bank { public boolean transfermoney(account from, Account to, Amount amount) { synchronized (from) { sleepawhilefortesting(); synchronized (to) { return doactualtransfer(from, to, amount); } } } protected void sleepawhilefortesting() {} } 131

134 In our unit test we use a SlowBank public class SlowBank extends Bank { private final long timeout; private final TimeUnit unit; public SlowBank(long timeout, TimeUnit unit) { this.timeout = timeout; this.unit = unit; } protected void sleepawhilefortesting() { try { unit.sleep(timeout); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } 132

135 Won't this cause problems in production? 133

136 In production we only use Bank 134

137 HotSpot will optimize away empty sleepawhilefortesting() 135

138 it is thus a zero cost 136

139 as long as SlowBank is never used 137

140 How can you verify the lockordering deadlock? 138

141 CTRL+Break, jstack, kill -3, jconsole, etc. 139

142 but that won't be a good unit test 140

143 Better via Java's ThreadMXBean 141

144 ThreadMXBean Get instance with ManagementFactory.getThreadMXBean() findmonitordeadlockedthreads() Includes only "monitor" locks, i.e. synchronized Only way to find deadlocks in Java 5 142

145 ThreadMXBean Better is finddeadlockedthreads() Includes "monitor" and "owned" (Java 5) locks Preferred method to test for deadlocks But, does not find deadlocks between semaphores See 143

146 public class BankDeadlockTest { private final static ThreadMXBean tmb = ManagementFactory.getThreadMXBean(); private boolean isthreaddeadlocked(long tid) { long[] ids = tmb.finddeadlockedthreads(); if (ids == null) return false; for (long id : ids) { if (id == tid) return true; } return false; } 144

147 private void checkthatthreadterminates(thread thread) throws InterruptedException { for (int i = 0; i < 2000; i++) { thread.join(50); if (!thread.isalive()) return; if (isthreaddeadlocked(thread.getid())) { fail("deadlock detected!"); } } fail(thread + " did not terminate in time"); } 145

148 @Test public void testfortransferdeadlock() throws InterruptedException { final Account alpha = new Account(new Amount(1000)); final Account ubs = new Account(new Amount( )); final Bank bank = new SlowBank(100, TimeUnit.MILLISECONDS); Thread alphatoubs = new Thread("alphaToUbs") { public void run() { bank.transfermoney(alpha, ubs, new Amount(100)); } }; Thread ubstoalpha = new Thread("ubsToAlpha") { public void run() { bank.transfermoney(ubs, alpha, new Amount(100)); } }; alphatoubs.start(); ubstoalpha.start(); checkthatthreadterminates(alphatoubs); } } 146

149 Output with broken transfermoney() method We see the deadlock within about 100 milliseconds junit.framework.assertionfailederror: Deadlock detected! at BankDeadlockTest.checkThatThreadTerminates(BankDeadlockTest.java:20) at BankDeadlockTest.testForTransferDeadlock(BankDeadlockTest.java:55) 147

150 Output with broken transfermoney() method If we fix the transfermoney() method, it also completes within about 100 milliseconds This is the time that we are sleeping for testing purposes Remember that the empty sleepawhilefortesting() method will be optimized away by HotSpot 148

151 Timed lock attempts Another technique for solving deadlocks is to use the timed trylock() method of explicit Java locks 149

152 However, if trylock() fails it may be 150

153 a deadlock 151

154 or some thread taking a long time 152

155 or a thread in an infinite loop 153

156 But ThreadMXBean shows it as deadlocked 154

157 even if it is a temporary condition 155

158 The JVM has support for trylock with synchronized 156

159 but it is hidden away 157

160 So here it is 158

161 159

162 Told you it was hidden! 160

163 It is hidden for a good reason 161

164 if you need trylock functionality 162

165 rather use explicit locks (ReentrantLock) 163

166 trylock with synchronized Technically possible in Sun JVM using Unsafe.getUnsafe().tryMonitorEnter(monitor) Unsafe.getUnsafe().monitorExit(monitor) 164

167 Deadlock analysis with thread dumps 165

168 Deadlock analysis with thread dumps We can also cause a thread dump in many ways: Ctrl+Break on Windows or Ctrl-\ on Unix Invoking "kill -3" on the process id Calling jstack on the process id Only shows deadlocks since Java 6 Intrinsic locks show more information of where they were acquired 166

169 Can a deadlock be resolved? 167

170 NO with synchronized 168

171 YES with ReentrantLock 169

172 synchronized() goes into BLOCKED state 170

173 ReentrantLock.lock() goes into WAITING state 171

174 We can write a DealockArbitrator 172

175 This stops one of the threads with a DeadlockVictimError 173

176 Our special Error for deadlocks public class DeadlockVictimError extends Error { private final Thread victim; public DeadlockVictimError(Thread victim) { super("deadlock victim: " + victim); this.victim = victim; } } public Thread getvictim() { return victim; } 174

177 public class DeadlockArbitrator { private static final ThreadMXBean tmb = ManagementFactory.getThreadMXBean(); public boolean tryresolvedeadlock( int attempts, long timeout, TimeUnit unit) throws InterruptedException { for (int i = 0; i < attempts; i++) { long[] ids = tmb.finddeadlockedthreads(); if (ids == null) return true; Thread t = findthread(ids[i % ids.length]); if (t == null) throw new IllegalStateException("Could not find thread"); t.stop(new DeadlockVictimError(t)); unit.sleep(timeout); } return false; } 175

178 finding threads by id is tricky public boolean tryresolvedeadlock() throws InterruptedException { return tryresolvedeadlock(3, 1, TimeUnit.SECONDS); } } private Thread findthread(long id) { for (Thread thread : Thread.getAllStackTraces().keySet()) { if (thread.getid() == id) return thread; } return null; } 176

179 177

180 Applicability of DeadlockArbitrator Only use in extreme circumstances Code that is outside your control and that deadlocks Where you cannot prevent the deadlock Remember, it only works with ReentrantLock 178

181 Deadlock Victim Questions? by Dr Heinz Kabutz && Olivier Croisier The Java Specialists Newsletter && The Coder's Breakfast && 179

Thread states. Java threads: synchronization. Thread states (cont.)

Thread states. Java threads: synchronization. Thread states (cont.) Thread states Java threads: synchronization 1. New: created with the new operator (not yet started) 2. Runnable: either running or ready to run 3. Blocked: deactivated to wait for something 4. Dead: has

More information

TDA - Thread Dump Analyzer

TDA - Thread Dump Analyzer TDA - Thread Dump Analyzer TDA - Thread Dump Analyzer Published September, 2008 Copyright 2006-2008 Ingo Rockel Table of Contents 1.... 1 1.1. Request Thread Dumps... 2 1.2. Thread

More information

CS11 Java. Fall 2014-2015 Lecture 7

CS11 Java. Fall 2014-2015 Lecture 7 CS11 Java Fall 2014-2015 Lecture 7 Today s Topics! All about Java Threads! Some Lab 7 tips Java Threading Recap! A program can use multiple threads to do several things at once " A thread can have local

More information

To start the new thread running you simply execute:

To start the new thread running you simply execute: Java: Threads and Monitors Java supports concurrency via threads. These threads are lightweight processes and they synchronise via monitors. Threads A Java thread is a lightweight process that has its

More information

CMSC 433 Section 0101 Fall 2012 Midterm Exam #2

CMSC 433 Section 0101 Fall 2012 Midterm Exam #2 Name: CMSC 433 Section 0101 Fall 2012 Midterm Exam #2 Directions: Test is closed book, closed notes, no electronics. Answer every question; write solutions in spaces provided. Use backs of pages for scratch

More information

Comprehensive support for general-purpose concurrent programming; partitioned into three packages:

Comprehensive support for general-purpose concurrent programming; partitioned into three packages: Java 1.5 Concurrency Utilities Comprehensive support for general-purpose concurrent programming; partitioned into three packages: java.util.concurrent support common concurrent programming paradigms, e.g.,

More information

Programming Language Concepts: Lecture 12

Programming Language Concepts: Lecture 12 Programming Language Concepts: Lecture 12 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2009 PLC 2009, Lecture 12, 04 March 2009 Concurrent

More information

Threads, Part II. Thread Synchronisa3on CS3524 Distributed Systems Lecture 05

Threads, Part II. Thread Synchronisa3on CS3524 Distributed Systems Lecture 05 Threads, Part II Thread Synchronisa3on CS3524 Distributed Systems Lecture 05 Threads Mul3-threaded execu3on is an essen3al feature of Java A thread is a single sequen3al flow of control within a program

More information

Monitors & Condition Synchronization

Monitors & Condition Synchronization INF2140 Modeling and programming parallel systems Lecture 5 Feb. 13, 2013 Plan for Today: Concepts Monitors: encapsulated data + access procedures Mutual exclusion + condition synchronization Single access

More information

Effective Java Programming. measurement as the basis

Effective Java Programming. measurement as the basis Effective Java Programming measurement as the basis Structure measurement as the basis benchmarking micro macro profiling why you should do this? profiling tools Motto "We should forget about small efficiencies,

More information

Outline of this lecture G52CON: Concepts of Concurrency

Outline of this lecture G52CON: Concepts of Concurrency Outline of this lecture G52CON: Concepts of Concurrency Lecture 10 Synchronisation in Java Natasha Alechina School of Computer Science nza@cs.nott.ac.uk mutual exclusion in Java condition synchronisation

More information

Monitors, Java, Threads and Processes

Monitors, Java, Threads and Processes Monitors, Java, Threads and Processes 185 An object-oriented view of shared memory A semaphore can be seen as a shared object accessible through two methods: wait and signal. The idea behind the concept

More information

Lecture 7: Class design for security

Lecture 7: Class design for security Lecture topics Class design for security Visibility of classes, fields, and methods Implications of using inner classes Mutability Design for sending objects across JVMs (serialization) Visibility modifiers

More information

Introduction to Java A First Look

Introduction to Java A First Look Introduction to Java A First Look Java is a second or third generation object language Integrates many of best features Smalltalk C++ Like Smalltalk Everything is an object Interpreted or just in time

More information

3C03 Concurrency: Condition Synchronisation

3C03 Concurrency: Condition Synchronisation 3C03 Concurrency: Condition Synchronisation Mark Handley 1 Goals n Introduce concepts of Condition synchronisation Fairness Starvation n Modelling: Relationship between guarded actions and condition synchronisation?

More information

Java Memory Model: Content

Java Memory Model: Content Java Memory Model: Content Memory Models Double Checked Locking Problem Java Memory Model: Happens Before Relation Volatile: in depth 16 March 2012 1 Java Memory Model JMM specifies guarantees given by

More information

Building a Multi-Threaded Web Server

Building a Multi-Threaded Web Server Building a Multi-Threaded Web Server In this lab we will develop a Web server in two steps. In the end, you will have built a multi-threaded Web server that is capable of processing multiple simultaneous

More information

Performance Improvement In Java Application

Performance Improvement In Java Application Performance Improvement In Java Application Megha Fulfagar Accenture Delivery Center for Technology in India Accenture, its logo, and High Performance Delivered are trademarks of Accenture. Agenda Performance

More information

SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (I)

SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (I) SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (I) Shan He School for Computational Science University of Birmingham Module 06-19321: SSC Outline Outline of Topics

More information

Monitors & Condition Synchronization

Monitors & Condition Synchronization Chapter 5 Monitors & Condition Synchronization 1 monitors & condition synchronization Concepts: monitors: encapsulated data + access procedures mutual exclusion + condition synchronization nested monitors

More information

Threads 1. When writing games you need to do more than one thing at once.

Threads 1. When writing games you need to do more than one thing at once. Threads 1 Threads Slide 1 When writing games you need to do more than one thing at once. Threads offer a way of automatically allowing more than one thing to happen at the same time. Java has threads as

More information

G52CON: Concepts of Concurrency

G52CON: Concepts of Concurrency G52CON: Concepts of Concurrency Lecture 11 Synchronisation in Java" Brian Logan School of Computer Science bsl@cs.nott.ac.uk Outline of this lecture" mutual exclusion in Java condition synchronisation

More information

Overview Motivating Examples Interleaving Model Semantics of Correctness Testing, Debugging, and Verification

Overview Motivating Examples Interleaving Model Semantics of Correctness Testing, Debugging, and Verification Introduction Overview Motivating Examples Interleaving Model Semantics of Correctness Testing, Debugging, and Verification Advanced Topics in Software Engineering 1 Concurrent Programs Characterized by

More information

How to Write AllSeen Alliance Self- Certification Test Cases September 25, 2014

How to Write AllSeen Alliance Self- Certification Test Cases September 25, 2014 How to Write AllSeen Alliance Self- Certification Test Cases September 25, 2014 This work is licensed under a Creative Commons Attribution 4.0 International License. http://creativecommons.org/licenses/by/4.0/

More information

Multithreading in Java

Multithreading in Java Multithreading in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park Problem Multiple tasks for computer Draw & display images on screen Check keyboard

More information

Correct and Efficient Synchronization of Java Technologybased

Correct and Efficient Synchronization of Java Technologybased Correct and Efficient Synchronization of Java Technologybased Threads Doug Lea and William Pugh http://gee.cs.oswego.edu http://www.cs.umd.edu/~pugh 1 Audience Assume you are familiar with basics of Java

More information

Lecture 8: Safety and Liveness Properties

Lecture 8: Safety and Liveness Properties Concurrent Programming 19530-V (WS01) 1 Lecture 8: Safety and Liveness Properties Dr. Richard S. Hall rickhall@inf.fu-berlin.de Concurrent programming December 11, 2001 Safety Properties 2 A safety property

More information

Monitors to Implement Semaphores

Monitors to Implement Semaphores (Chapter 5.2) Monitors to Implement Semaphores Concurrency: monitors & condition synchronization 1 Monitors & Condition Synchronization Concepts: monitors: encapsulated data + access procedures mutual

More information

Threads & Tasks: Executor Framework

Threads & Tasks: Executor Framework Threads & Tasks: Executor Framework Introduction & Motivation WebServer Executor Framework Callable and Future 12 April 2012 1 Threads & Tasks Motivations for using threads Actor-based Goal: Create an

More information

CSC System Development with Java Threads

CSC System Development with Java Threads CSC 308 2.0 System Development with Java Threads Department of Statistics and Computer Science 1 Multitasking and Multithreading Multitasking: Computer's ability to perform multiple jobs concurrently More

More information

Synchronization of Java Threads

Synchronization of Java Threads Synchronization of Java Threads The previous examples shows independent, asynchronous threads 1. each thread has all the data and methods it needs 2. threads run at their own pace, without concern for

More information

Introduction to Java Threads

Introduction to Java Threads Object-Oriented Programming Introduction to Java Threads 4003-243 1 What is a Process? Here s what happens when you run this Java program and launch 3 instances while monitoring with top On a single CPU

More information

Operating Systems and Networks

Operating Systems and Networks recap Operating Systems and Networks How OS manages multiple tasks Virtual memory Brief Linux demo Lecture 04: Introduction to OS-part 3 Behzad Bordbar 47 48 Contents Dual mode API to wrap system calls

More information

Getting Started with the Internet Communications Engine

Getting Started with the Internet Communications Engine Getting Started with the Internet Communications Engine David Vriezen April 7, 2014 Contents 1 Introduction 2 2 About Ice 2 2.1 Proxies................................. 2 3 Setting Up ICE 2 4 Slices 2

More information

Unit 6: Conditions, Barriers, and RendezVous

Unit 6: Conditions, Barriers, and RendezVous SPP (Synchro et Prog Parallèle) Unit 6: Conditions, Barriers, and RendezVous François Taïani So far We have seen several synchronisation mechanisms locks (plain, re-entrant, read/write) semaphores monitors

More information

key points from previous lecture Overview of Presentation Disables and Spin-locks revisited Interrupts when to disable them

key points from previous lecture Overview of Presentation Disables and Spin-locks revisited Interrupts when to disable them Overview of Presentation key points from previous lecture Spin-locks, Interrupts and Exclusion reasonable use of interrupt routines and disabling spin locks revisited, and sleep/wakeup exclusion Semaphores

More information

OPERATING SYSTEMS. IIIT-Hyderabad

OPERATING SYSTEMS. IIIT-Hyderabad OPERATING SYSTEMS IIIT-Hyderabad OVERVIEW Introduction What is an OS/Kernel? Bootstrap program Interrupts and exceptions Volatile and Non volatile storage!!! Process Management What is a process/system

More information

Synchronization and Coordination between Threads

Synchronization and Coordination between Threads Synchronization and Coordination between Threads Part I Producer/consumer problems: Synchronization and coordination in between threads Synchronization and Coordination of Threads In the last class, we

More information

MonitorExplorer: A State Exploration-Based Approach to Testing Java Monitors

MonitorExplorer: A State Exploration-Based Approach to Testing Java Monitors Department of Computer Science and Engineering University of Texas at Arlington Arlington, TX 76019 MonitorExplorer: A State Exploration-Based Approach to Testing Java Monitors Y. Lei, R. Carver, D. Kung,

More information

CPU I/O Burst Cycle Process execution consists of a cycle of CPU execution and I/O wait

CPU I/O Burst Cycle Process execution consists of a cycle of CPU execution and I/O wait Unit II Process Scheduling and Synchronization Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling Thread Scheduling Operating Systems Examples Java

More information

Deadlocks CHAPTER. Practice Exercises. 7.1 List three examples of deadlocks that are not related to a computersystem. Answer:

Deadlocks CHAPTER. Practice Exercises. 7.1 List three examples of deadlocks that are not related to a computersystem. Answer: 7 CHAPTER Deadlocks Practice Exercises 7.1 List three examples of deadlocks that are not related to a computersystem environment. Two cars crossing a single-lane bridge from opposite directions. A person

More information

Oracle WebLogic Server Monitoring and Performance Tuning

Oracle WebLogic Server Monitoring and Performance Tuning Oracle WebLogic Server Monitoring and Performance Tuning Duško Vukmanović Principal Sales Consultant, FMW Stuck Threads A Label given to threads not returned to thread pool after

More information

Object Oriented Design with UML and Java PART XII: Threads

Object Oriented Design with UML and Java PART XII: Threads Object Oriented Design with UML and Java PART XII: Threads Copyright David Leberknight and Ron LeMaster Version 2011 Java Threads A Thread is an execution process. Only one thread at a time may be running

More information

Chapter 6, The Operating System Machine Level

Chapter 6, The Operating System Machine Level Chapter 6, The Operating System Machine Level 6.1 Virtual Memory 6.2 Virtual I/O Instructions 6.3 Virtual Instructions For Parallel Processing 6.4 Example Operating Systems 6.5 Summary Virtual Memory General

More information

Fundamentals of Java Programming

Fundamentals of Java Programming Fundamentals of Java Programming This document is exclusive property of Cisco Systems, Inc. Permission is granted to print and copy this document for non-commercial distribution and exclusive use by instructors

More information

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Processes and Non-Preemptive Scheduling. Otto J. Anshus Processes and Non-Preemptive Scheduling Otto J. Anshus 1 Concurrency and Process Challenge: Physical reality is Concurrent Smart to do concurrent software instead of sequential? At least we want to have

More information

Concurrent Programming (COMP 150-CCP) Spring 2008 Dr. Richard S. Hall April 24 th, 2008

Concurrent Programming (COMP 150-CCP) Spring 2008 Dr. Richard S. Hall April 24 th, 2008 Concurrent Programming (COMP 150-CCP) Spring 2008 Dr. Richard S. Hall April 24 th, 2008 Write your name on each sheet of paper you hand in. Make sure that mobile phones are turned off. This is a closed-book

More information

Sophos Mobile Control Web service guide

Sophos Mobile Control Web service guide Sophos Mobile Control Web service guide Product version: 3.5 Document date: July 2013 Contents 1 About Sophos Mobile Control... 3 2 Prerequisites... 4 3 Server-side implementation... 5 4 Client-side implementation...

More information

Threads and Synchronization

Threads and Synchronization Threads and Synchronization Mark Allen Weiss Copyright 2000 1 What threads are Outline of Topics The Thread class and starting some threads Synchronization: keeping threads from clobbering each other Deadlock

More information

CONCURRENT PROGRAMMING WITH THREADS R I C K M E R C E R, R I C K S N O D G R A S S, I V A N V A S Q U E Z W I T H H E L P F R O M J O S H B L O C K

CONCURRENT PROGRAMMING WITH THREADS R I C K M E R C E R, R I C K S N O D G R A S S, I V A N V A S Q U E Z W I T H H E L P F R O M J O S H B L O C K CONCURRENT PROGRAMMING WITH THREADS R I C K M E R C E R, R I C K S N O D G R A S S, I V A N V A S Q U E Z W I T H H E L P F R O M J O S H B L O C K OUTLINE Basic concepts Processes Threads Java: Thread

More information

Why Threads Are A Bad Idea (for most purposes)

Why Threads Are A Bad Idea (for most purposes) Why Threads Are A Bad Idea (for most purposes) John Ousterhout Sun Microsystems Laboratories john.ousterhout@eng.sun.com http://www.sunlabs.com/~ouster Introduction Threads: Grew up in OS world (processes).

More information

Tutorial: Getting Started

Tutorial: Getting Started 9 Tutorial: Getting Started INFRASTRUCTURE A MAKEFILE PLAIN HELLO WORLD APERIODIC HELLO WORLD PERIODIC HELLO WORLD WATCH THOSE REAL-TIME PRIORITIES THEY ARE SERIOUS SUMMARY Getting started with a new platform

More information

Monitors and Exceptions : How to Implement Java efficiently. Andreas Krall and Mark Probst Technische Universitaet Wien

Monitors and Exceptions : How to Implement Java efficiently. Andreas Krall and Mark Probst Technische Universitaet Wien Monitors and Exceptions : How to Implement Java efficiently Andreas Krall and Mark Probst Technische Universitaet Wien 1 Outline Exceptions in CACAO Exception implementation techniques CACAO s implementation

More information

1.1. Over view. 1.2. Example 1: CPU Slot Distribution A example to test the distribution of cpu slot.

1.1. Over view. 1.2. Example 1: CPU Slot Distribution A example to test the distribution of cpu slot. 1. JVM, Threads, and Monitors See also: http://java.sun.com/docs/books/vmspec/2nd-edition/html/vmspec- TOC.doc.html I have copied material from there. Thanks to Tim Lindholm Frank Yellin. 1.1. Over view

More information

Replication on Virtual Machines

Replication on Virtual Machines Replication on Virtual Machines Siggi Cherem CS 717 November 23rd, 2004 Outline 1 Introduction The Java Virtual Machine 2 Napper, Alvisi, Vin - DSN 2003 Introduction JVM as state machine Addressing non-determinism

More information

While we wait for Brian to figure out the web-page model www.diku.dk/~vinter/xmp And we are looking for a larger room! Threading Thread packages and threadprogramming Processes and Threads Threads are

More information

Hoare-Style Monitors for Java

Hoare-Style Monitors for Java Hoare-Style Monitors for Java Theodore S Norvell Electrical and Computer Engineering Memorial University February 17, 2006 1 Hoare-Style Monitors Coordinating the interactions of two or more threads can

More information

Operating Systems for Parallel Processing Assistent Lecturer Alecu Felician Economic Informatics Department Academy of Economic Studies Bucharest

Operating Systems for Parallel Processing Assistent Lecturer Alecu Felician Economic Informatics Department Academy of Economic Studies Bucharest Operating Systems for Parallel Processing Assistent Lecturer Alecu Felician Economic Informatics Department Academy of Economic Studies Bucharest 1. Introduction Few years ago, parallel computers could

More information

III. Process Scheduling

III. Process Scheduling Intended Schedule III. Process Scheduling Date Lecture Hand out Submission 0 20.04. Introduction to Operating Systems Course registration 1 27.04. Systems Programming using C (File Subsystem) 1. Assignment

More information

III. Process Scheduling

III. Process Scheduling III. Process Scheduling 1 Intended Schedule Date Lecture Hand out Submission 0 20.04. Introduction to Operating Systems Course registration 1 27.04. Systems Programming using C (File Subsystem) 1. Assignment

More information

OBJECT ORIENTED PROGRAMMING LANGUAGE

OBJECT ORIENTED PROGRAMMING LANGUAGE UNIT-6 (MULTI THREADING) Multi Threading: Java Language Classes The java.lang package contains the collection of base types (language types) that are always imported into any given compilation unit. This

More information

Essentials of Java Performance Tuning. Dr Heinz Kabutz Kirk Pepperdine Sun Java Champions

Essentials of Java Performance Tuning. Dr Heinz Kabutz Kirk Pepperdine Sun Java Champions Essentials of Java Performance Tuning Dr Heinz Kabutz Kirk Pepperdine Sun Java Champions Our Typical Story Customer JoGoSlo Ltd calls us in desperation > Millions of Rands invested > Users complain about

More information

Topics. Producing Production Quality Software. Concurrent Environments. Why Use Concurrency? Models of concurrency Concurrency in Java

Topics. Producing Production Quality Software. Concurrent Environments. Why Use Concurrency? Models of concurrency Concurrency in Java Topics Producing Production Quality Software Models of concurrency Concurrency in Java Lecture 12: Concurrent and Distributed Programming Prof. Arthur P. Goldberg Fall, 2005 2 Why Use Concurrency? Concurrent

More information

Monitors & Condition Synchronisation

Monitors & Condition Synchronisation Chapter 5 Monitors & Condition Synchronisation controller 1 Monitors & Condition Synchronisation Concepts: monitors (and controllers): encapsulated data + access procedures + mutual exclusion + condition

More information

Class 305 Focus On Threads: An Introduction Michel de Champlain ABSTRACT

Class 305 Focus On Threads: An Introduction Michel de Champlain ABSTRACT Class 305 Focus On Threads: An Introduction Michel de Champlain ABSTRACT Threads are now supported by many new object-oriented languages. Java provides language-level support for multi-threading, resulting

More information

Concurrent Programming

Concurrent Programming Concurrent Programming Shared Memory Passing the baton Monitors Verónica Gaspes www.hh.se/staff/vero CERES, November 2, 2007 ceres Outline 1 Recap 2 Readers & Writers Readers & writers (mutual exclusion)

More information

Development Environment and Tools for Java. Brian Hughes IBM

Development Environment and Tools for Java. Brian Hughes IBM Development Environment and Tools for Java Brian Hughes IBM 1 Acknowledgements and Disclaimers Availability. References in this presentation to IBM products, programs, or services do not imply that they

More information

Structuring the Control

Structuring the Control Structuring the Control Hints on control-in-the-small Essential elements of control-in-the-large (parallel execution) Ghezzi&Jazayeri: Ch 4 1 Control in-the-small From if-then-else, while, case, for,...

More information

! Past few lectures: Ø Locks: provide mutual exclusion Ø Condition variables: provide conditional synchronization

! Past few lectures: Ø Locks: provide mutual exclusion Ø Condition variables: provide conditional synchronization 1 Synchronization Constructs! Synchronization Ø Coordinating execution of multiple threads that share data structures Semaphores and Monitors: High-level Synchronization Constructs! Past few lectures:

More information

MASSACHUSETTS INSTITUTE OF TECHNOLOGY. 6.033 Computer Systems Engineering: Spring 2013. Quiz I Solutions

MASSACHUSETTS INSTITUTE OF TECHNOLOGY. 6.033 Computer Systems Engineering: Spring 2013. Quiz I Solutions Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY 6.033 Computer Systems Engineering: Spring 2013 Quiz I Solutions There are 11 questions and 8 pages in this

More information

Process Synchronization

Process Synchronization Process Synchronization 5 CHAPTER Now that we have provided a grounding in synchronization theory, we can describe how Java synchronizes the activity of threads, allowing the programmer to develop generalized

More information

Monitoring and Managing a JVM

Monitoring and Managing a JVM Monitoring and Managing a JVM Erik Brakkee & Peter van den Berkmortel Overview About Axxerion Challenges and example Troubleshooting Memory management Tooling Best practices Conclusion About Axxerion Axxerion

More information

Chapter 7: Deadlocks!

Chapter 7: Deadlocks! The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still

More information

END TERM Examination, May 2014 (Model Test Paper) B.Tech (IT) IV Semester

END TERM Examination, May 2014 (Model Test Paper) B.Tech (IT) IV Semester END TERM Examination, May 2014 (Model Test Paper) B.Tech (IT) IV Semester Paper Code: ETCS - 212 Subject: Operating Systems Time: 3 hrs Maximum Marks: 75 Note: Attempt five questions including Q. No. 1

More information

Monitor-Konzepte. nach Brinch Hansen. Konzepte von Betriebssystem-Komponenten: Konkurrenz und Koordinierung in Manycore-Systemen.

Monitor-Konzepte. nach Brinch Hansen. Konzepte von Betriebssystem-Komponenten: Konkurrenz und Koordinierung in Manycore-Systemen. Konzepte von Betriebssystem-Komponenten: Konkurrenz und Koordinierung in Manycore-Systemen Monitor-Konzepte nach Brinch Hansen 18.07.2013 Krzysztof Mazur 2 Motivation Writing to a log file???? 3 Motivation

More information

How to use IBM HeapAnalyzer to diagnose Java heap issues

How to use IBM HeapAnalyzer to diagnose Java heap issues IBM Software Group How to use IBM HeapAnalyzer to diagnose Java heap issues Jinwoo Hwang (jinwoo@us.ibm.com) IBM HeapAnalyzer Architect/Developer WebSphere Support Technical Exchange Introduction Java

More information

pthread Tutorial August 31, Creating and Destroying Threads Creating Threads Returning Results from Threads...

pthread Tutorial August 31, Creating and Destroying Threads Creating Threads Returning Results from Threads... pthread Tutorial c Copyright 2008 by Peter C. Chapin August 31, 2008 Contents 1 Introduction 2 2 Creating and Destroying Threads 3 2.1 Creating Threads........................... 3 2.2 Returning Results

More information

Semaphores and Monitors: High-level Synchronization Constructs

Semaphores and Monitors: High-level Synchronization Constructs Semaphores and Monitors: High-level Synchronization Constructs 1 Synchronization Constructs Synchronization Coordinating execution of multiple threads that share data structures Past few lectures: Locks:

More information

Synchronization in Java

Synchronization in Java Synchronization in Java We often want threads to co-operate, typically in how they access shared data structures. Since thread execution is asynchronous, the details of how threads interact can be unpredictable.

More information

CDP Threads in Java

CDP Threads in Java CDP 2013 Threads in Java 1 Threads: reminder Basic sequential unit of execution. Multiple threads run in the context of single process: all threads within a process share common memory space (but not their

More information

Java Troubleshooting and Performance

Java Troubleshooting and Performance Java Troubleshooting and Performance Margus Pala Java Fundamentals 08.12.2014 Agenda Debugger Thread dumps Memory dumps Crash dumps Tools/profilers Rules of (performance) optimization 1. Don't optimize

More information

Last Class: Semaphores

Last Class: Semaphores Last Class: Semaphores A semaphore S supports two atomic operations: S Wait(): get a semaphore, wait if busy semaphore S is available. S Signal(): release the semaphore, wake up a process if one is waiting

More information

No no-argument constructor. No default constructor found

No no-argument constructor. No default constructor found Every software developer deals with bugs. The really tough bugs aren t detected by the compiler. Nasty bugs manifest themselves only when executed at runtime. Here is a list of the top ten difficult and

More information

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions COMP209 Object Oriented Programming Designing Classes 2 Mark Hall Programming by Contract (adapted from slides by Mark Utting) Preconditions Postconditions Class invariants Programming by Contract An agreement

More information

Why Study Deadlocks? Overview of Presentation. Commodity vs General Resources. Types of Deadlocks

Why Study Deadlocks? Overview of Presentation. Commodity vs General Resources. Types of Deadlocks Overview of Presentation Overview of dead-locks Deadlock avoidance (advance reservations) Deadlock prevention (four necessary conditions) Related subjects Other types of hangs Detection and recovery Priority

More information

Brekeke PBX Version 2 IVR Script Developer s Guide Brekeke Software, Inc.

Brekeke PBX Version 2 IVR Script Developer s Guide Brekeke Software, Inc. Brekeke PBX Version 2 IVR Script Developer s Guide Brekeke Software, Inc. Version Brekeke PBX Version 2 IVR Script Developer s Guide Revised: February 2010 Copyright This document is copyrighted by Brekeke

More information

JAVA - MULTITHREADING

JAVA - MULTITHREADING JAVA - MULTITHREADING http://www.tutorialspoint.com/java/java_multithreading.htm Copyright tutorialspoint.com Java is amulti threaded programming language which means we can develop multi threaded program

More information

END TERM Examination(Model Test Paper) Fourth Semester[B.Tech]

END TERM Examination(Model Test Paper) Fourth Semester[B.Tech] END TERM Examination(Model Test Paper) Fourth Semester[B.Tech] Paper Code: ETCS - 212 Subject: Operating System Time: 3 hrs Maximum Marks: 75 Note: Q.No.1 is compulsory. Attempt any four questions of remaining

More information

(Pessimistic) Timestamp Ordering. Rules for read and write Operations. Pessimistic Timestamp Ordering. Write Operations and Timestamps

(Pessimistic) Timestamp Ordering. Rules for read and write Operations. Pessimistic Timestamp Ordering. Write Operations and Timestamps (Pessimistic) stamp Ordering Another approach to concurrency control: Assign a timestamp ts(t) to transaction T at the moment it starts Using Lamport's timestamps: total order is given. In distributed

More information

Digital Cable TV. User Guide

Digital Cable TV. User Guide Digital Cable TV User Guide T a b l e o f C o n T e n T s DVR and Set-Top Box Basics............... 2 Remote Playback Controls................ 4 What s on TV.......................... 6 Using the OK Button..................

More information

There are two distinct types of Multitasking i.e. Processor-Based and Thread-Based multitasking.

There are two distinct types of Multitasking i.e. Processor-Based and Thread-Based multitasking. Multithreading Multithreading is a conceptual programming concept where a program (process) is divided into two or more subprograms (process), which can be implemented at the same time in parallel. A multithreaded

More information

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

Last Class: Synchronization Problems. Need to hold multiple resources to perform task. CS377: Operating Systems. Real-world Examples Last Class: Synchronization Problems Reader Writer Multiple readers, single writer In practice, use read-write locks Dining Philosophers Need to hold multiple resources to perform task Lecture 10, page

More information

B M C S O F T W A R E, I N C. BASIC BEST PRACTICES. Ross Cochran Principal SW Consultant

B M C S O F T W A R E, I N C. BASIC BEST PRACTICES. Ross Cochran Principal SW Consultant B M C S O F T W A R E, I N C. PATROL FOR WEBSPHERE APPLICATION SERVER BASIC BEST PRACTICES Ross Cochran Principal SW Consultant PAT R O L F O R W E B S P H E R E A P P L I C AT I O N S E R V E R BEST PRACTICES

More information

Part III Synchronization Critical Section and Mutual Exclusion

Part III Synchronization Critical Section and Mutual Exclusion Part III Synchronization Critical Section and Mutual Exclusion Fall 2016 The question of whether computers can think is just like the question of whether submarines can swim 1 Edsger W. Dijkstra Process

More information

Real Time Programming: Concepts

Real Time Programming: Concepts Real Time Programming: Concepts Radek Pelánek Plan at first we will study basic concepts related to real time programming then we will have a look at specific programming languages and study how they realize

More information

Java API Essentials (Part II) Web Languages Course 2009 University of Trento

Java API Essentials (Part II) Web Languages Course 2009 University of Trento Java API Essentials (Part II) Web Languages Course 2009 University of Trento Lab Objective More exercises on Object serialization Client-Server application through sockets Refresh basic Java concepts on

More information

www.virtualians.pk CS506 Web Design and Development Solved Online Quiz No. 01 www.virtualians.pk

www.virtualians.pk CS506 Web Design and Development Solved Online Quiz No. 01 www.virtualians.pk CS506 Web Design and Development Solved Online Quiz No. 01 Which of the following is a general purpose container? JFrame Dialog JPanel JApplet Which of the following package needs to be import while handling

More information

Chapter 8 Implementing FSP Models in Java

Chapter 8 Implementing FSP Models in Java Chapter 8 Implementing FSP Models in Java 1 8.1.1: The Carpark Model A controller is required for a carpark, which only permits cars to enter when the carpark is not full and does not permit cars to leave

More information

University of Waterloo Midterm Examination

University of Waterloo Midterm Examination University of Waterloo Midterm Examination Spring, 2006 Student Name: Student ID Number: Section: Course Abbreviation and Number Course Title CS350 Operating Systems Sections 01 (14:30), 02 (11:30) Instructor

More information

Java Interview Questions and Answers

Java Interview Questions and Answers 1. What is the most important feature of Java? Java is a platform independent language. 2. What do you mean by platform independence? Platform independence means that we can write and compile the java

More information