CSS 430: Operating Systems - Process Synchronization

Slides:



Advertisements
Similar presentations
Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Advertisements

Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
CSCC69: Operating Systems
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
Thread synchronization Example:Producer/Consumer Relationship Buffer –Shared memory region Producer thread –Calls produce method to add item to buffer.
Thread Control methods The thread class contains the methods for controlling threads Thread() create default thread Thread(target: runnable ) creates new.
02/01/2007CSCI 315 Operating Systems Design1 Java Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook.
Definitions Process – An executing program
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Lecture 4 : JAVA Thread Programming
Internet Software Development More stuff on Threads Paul Krause.
Locks CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
Concurrent Programming in Java Dr. Zoltan Papp. Motivation: event driven, responsive systems Sequential approach: while ( true ) { do event = getEventId()
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Multithreading in JAVA
Threads II IS Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor.
Java Thread and Memory Model
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
Li Tak Sing COMPS311F. Threads A thread is a single sequential flow of control within a program. Many programming languages only allow you to write programs.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Java Producer-Consumer Monitor From: Concurrent Programming: The Java Programming Language By Steven J. Hartley Oxford University Press, 1998.
 2002 Prentice Hall, Inc. All rights reserved. Outline HoldIntegerUnsyn chronized.java Line 4 Lines 7-13 Lines // Fig. 15.6: HoldIntegerUnsynchronized.java.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
Multithreading and Garbage Collection Session 16.
Chapter 4: Threads Joe McCarthy CSS 430: Operating Systems - Threads1.
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Java Thread Programming
Threads in Java Two ways to start a thread
Multithreading / Concurrency
Chapter 13: Multithreading
Multithreading.
Final Project: ThreadOS File System
Chapter 5: Process Synchronization – Part 3
Java Concurrency.
System Programming Practical Session 7
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Threads Chate Patanothai.
Java Concurrency.
Threads II IS
Lecture 9: Process Synchronization
null, true, and false are also reserved.
Lecture 9: Process Synchronization
Condition Variables and Producer/Consumer
Multithreading.
Cancellation.
Condition Variables and Producer/Consumer
Semaphore Originally called P() and V() wait (S) { while S <= 0
Multithreading.
Multithreaded Programming
Programming with Shared Memory Java Threads and Synchronization
Programming with Shared Memory Java Threads and Synchronization
Chapter 15 Multithreading
Multithreading in java.
Threads and Multithreading
Monitors and Inter-Process Communication
More concurrency issues
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

CSS 430: Operating Systems - Process Synchronization Thread Synchronization in ThreadOS: Implementing SysLib methods join(), exit(), rawread(), rawwrite(), sync() Joe McCarthy CSS 430: Operating Systems - Process Synchronization

CSS 430: Operating Systems - Process Synchronization Monitors A high-level abstraction that provides a convenient and effective mechanism for process synchronization Only one process may be active within the monitor at a time CSS 430: Operating Systems - Process Synchronization

CSS 430: Operating Systems - Process Synchronization Syntax of a Monitor CSS 430: Operating Systems - Process Synchronization

Schematic view of a Monitor CSS 430: Operating Systems - Process Synchronization

CSS 430: Operating Systems - Process Synchronization Condition Variables Condition x, y; Operations on a condition variable: x.wait () – a process that invokes the operation is suspended x.signal () – resumes one of processes (if any) that invoked x.wait () CSS 430: Operating Systems - Process Synchronization

Monitor with Condition Variables CSS 430: Operating Systems - Process Synchronization

CSS 430: Operating Systems - Process Synchronization Java Synchronization Java provides synchronization at the language-level Thread-safe Every Java Object has an associated lock This lock is acquired by invoking a synchronized method This lock is released when exiting the synchronized method. Threads wanting to acquire the object lock are placed in the entry set for the object lock CSS 430: Operating Systems - Process Synchronization

CSS 430: Operating Systems - Process Synchronization Java Synchronization When a thread invokes wait(): 1. It releases the object lock 2. Its state is set to Blocked 3. It is placed in the wait set for the object When a thread invokes notify(): 1. A thread T from the wait set is selected 2. T is moved from the wait set to the entry set 3. The state of T is set to Runnable CSS 430: Operating Systems - Process Synchronization

CSS 430: Operating Systems - Process Synchronization http://download.oracle.com/javase/6/docs/api/java/lang/Object.html CSS 430: Operating Systems - Process Synchronization

CSS 430: Operating Systems - Process Synchronization Java Synchronization /usr/apps/CSS430/examples/os-book/ch6/java-synchronization/boundedbuffer CSS 430: Operating Systems - Process Synchronization

CSS 430: Operating Systems - Process Synchronization Java Synchronization notify() may not notify the correct thread! /usr/apps/CSS430/examples/os-book/ch6/java-synchronization/boundedbuffer CSS 430: Operating Systems - Process Synchronization

CSS 430: Operating Systems - Process Synchronization Test2e class Test2e extends Thread { public void run() { String[] args1 = SysLib.stringToArgs( "TestThread2 a 5000 0" ); String[] args2 = SysLib.stringToArgs( "TestThread2 b 1000 0" ); String[] args3 = SysLib.stringToArgs( "TestThread2 c 3000 0" ); String[] args4 = SysLib.stringToArgs( "TestThread2 d 6000 0" ); String[] args5 = SysLib.stringToArgs( "TestThread2 e 500 0" ); SysLib.exec( args1 ); SysLib.exec( args2 ); SysLib.exec( args3 ); SysLib.exec( args4 ); SysLib.exec( args5 ); for (int i = 0; i < 5; i++ ) { int tid = SysLib.join(); SysLib.cout( "Thread tid=" + tid + " done\n” ); SysLib.cout( "Test2e finished; total time = " + totalTime + "\n" ); SysLib.exit(); } CSS 430: Operating Systems - Process Synchronization

CSS 430: Operating Systems - Process Synchronization TestThread2 class TestThread2 extends Thread { … public void run() { long totalExecutionTime = 0; long totalWaitTime = 0; activationTime = new Date().getTime(); for ( int burst = cpuBurst; burst > 0; burst -= TIMEQUANTUM ) { totalExecutionTime += TIMEQUANTUM; SysLib.sleep( TIMEQUANTUM ); } completionTime = new Date().getTime( ); long responseTime = activationTime - submissionTime; totalWaitTime = completionTime - submissionTime - executionTime; long turnaroundTime = completionTime - submissionTime; SysLib.cout( String.format( "%05d: Thread[%s]: response: %5d; wait: %5d; execution: %5d; turnaround: %5d\n", completionTime % 100000, name, responseTime, totalWaitTime, totalExecutionTime, turnaroundTime ) ); SysLib.exit(); CSS 430: Operating Systems - Process Synchronization

CSS 430: Operating Systems - Process Synchronization Test2e output d-69-91-160-124:ThreadOS0 joe$ java Boot threadOS ver 1.0: Type ? for help threadOS: a new thread (thread=Thread[Thread-4,2,main] tid=0 pid=-1) -->l Shell l Shell threadOS: a new thread (thread=Thread[Thread-6,2,main] tid=1 pid=0) shell[1]% Test2e Test2e threadOS: a new thread (thread=Thread[Thread-9,2,main] tid=2 pid=1) threadOS: a new thread (thread=Thread[Thread-11,2,main] tid=3 pid=2) threadOS: a new thread (thread=Thread[Thread-13,2,main] tid=4 pid=2) threadOS: a new thread (thread=Thread[Thread-15,2,main] tid=5 pid=2) threadOS: a new thread (thread=Thread[Thread-17,2,main] tid=6 pid=2) threadOS: a new thread (thread=Thread[Thread-19,2,main] tid=7 pid=2) Thread[b]: response time = 3942 turnaround time = 4944 execution time = 1002 Thread tid=4 done Thread[e]: response time = 6944 turnaround time = 7445 execution time = 501 Thread tid=7 done Thread[a]: response time = 2941 turnaround time = 7948 execution time = 5007 Thread tid=3 done Thread[c]: response time = 4945 turnaround time = 7951 execution time = 3006 Thread tid=5 done Thread[d]: response time = 5944 turnaround time = 11953 execution time = 6009 Thread tid=6 done Test2e finished shell[2]% exit exit -->q b e d a c CSS 430: Operating Systems - Process Synchronization

SysLib.join(), Kernel.WAIT public class SysLib { … public static int join( ) { return Kernel.interrupt( Kernel.INTERRUPT_SOFTWARE, Kernel.WAIT, 0, null ); } public class Kernel { public static int interrupt( int irq, int cmd, int param, Object args ) { // Looks like myTcb is not used anywhere; newTcb declared & used below // TCB myTcb; switch( irq ) { case INTERRUPT_SOFTWARE: // System calls switch( cmd ) { case WAIT: // get the current thread id // let the current thread sleep in waitQueue under the // condition = this thread id // System.out.print( " * " ); return OK; // return a child thread id who woke me up CSS 430: Operating Systems - Process Synchronization

enqueueAndSleep(), dequeueAndWakeup() CSS 430: Operating Systems - Process Synchronization

SysLib.exit(), Kernel.EXIT public class SysLib { … public static int exit( ) { return Kernel.interrupt( Kernel.INTERRUPT_SOFTWARE, Kernel.EXIT, 0, null ); } public class Kernel { public static int interrupt( int irq, int cmd, int param, Object args ) { // Looks like myTcb is not used anywhere; newTcb declared & used below // TCB myTcb; switch( irq ) { case INTERRUPT_SOFTWARE: // System calls switch( cmd ) { case EXIT: // get the current thread's parent id // search waitQueue for and wakes up the thread under the // condition = the current thread's parent id // tell the Scheduler to delete the current thread // (since it is exiting) return OK; CSS 430: Operating Systems - Process Synchronization

SysLib.rawread(), Kernel.RAWREAD public class SysLib { … public static int rawread( int blkNumber, byte[] b ) { return Kernel.interrupt( Kernel.INTERRUPT_SOFTWARE, Kernel.RAWREAD, blkNumber, b ); } public class Kernel { public static int interrupt( int irq, int cmd, int param, Object args ) { // Looks like myTcb is not used anywhere; newTcb declared & used below // TCB myTcb; switch( irq ) { case INTERRUPT_SOFTWARE: // System calls switch( cmd ) { case RAWREAD: // read a block of data from disk while ( disk.read( param, ( byte[] )args ) == false ) ioQueue.enqueueAndSleep( COND_DISK_REQ ); while ( disk.testAndResetReady( ) == false ) ioQueue.enqueueAndSleep( COND_DISK_FIN ); return OK; CSS 430: Operating Systems - Process Synchronization

Disk.read(), Disk.testAndResetReady() public class Disk { … public synchronized boolean read( int blockId, byte buffer[] ) { if ( blockId < 0 || blockId > diskSize ) { SysLib.cerr( "threadOS: invalid blockId for read\n" ); return false; } if ( command == IDLE && readyBuffer == false ) { this.buffer = buffer; targetBlockId = blockId; command = READ; notify( ); return true; } else public synchronized boolean testAndResetReady( ) { if ( command == IDLE && readyBuffer == true ) { readyBuffer = false; CSS 430: Operating Systems - Process Synchronization

Disk.waitCommand(), Disk.finishCommand() public class Disk { … private synchronized void waitCommand( ) { while ( command == IDLE ) { try { wait( ); } catch ( InterruptedException e ) { SysLib.cerr( e.toString( ) + "\n" ); } readyBuffer = false; private synchronized void finishCommand( ) { command = IDLE; readyBuffer = true; SysLib.disk( ); // a disk interrupt CSS 430: Operating Systems - Process Synchronization

CSS 430: Operating Systems - Process Synchronization Disk.run() public class Disk { … public void run ( ) { while ( true ) { waitCommand( ); seek( ); switch( command ) { case READ: System.arraycopy( data, targetBlockId * blockSize, buffer, 0, blockSize ); break; case WRITE: case SYNC: } finishCommand( ); CSS 430: Operating Systems - Process Synchronization

Kernel.INTERRUPT_DISK public class SysLib { … public static int rawread( int blkNumber, byte[] b ) { return Kernel.interrupt( Kernel.INTERRUPT_SOFTWARE, Kernel.RAWREAD, blkNumber, b ); } public class Kernel { public static int interrupt( int irq, int cmd, int param, Object args ) { // Looks like myTcb is not used anywhere; newTcb declared & used below // TCB myTcb; switch( irq ) { case INTERRUPT_DISK: // Disk interrupts // wake up the thread waiting for a service completion first ioQueue.dequeueAndWakeup( COND_DISK_FIN ); // wake up a thread waiting for a request acceptance ioQueue.dequeueAndWakeup( COND_DISK_REQ ); return OK; CSS 430: Operating Systems - Process Synchronization