Thread Synchronization in Thread OS Implementing Sys Lib
Thread Synchronization in Thread. OS: Implementing Sys. Lib methods join(), exit(), rawread(), rawwrite(), sync() Joe Mc. Carthy CSS 430: Operating Systems - Process Synchronization 1
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 2
Syntax of a Monitor CSS 430: Operating Systems - Process Synchronization 3
Schematic view of a Monitor CSS 430: Operating Systems - Process Synchronization 4
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 5
Monitor with Condition Variables CSS 430: Operating Systems - Process Synchronization 6
Java Synchronization • Java provides synchronization at the languagelevel – 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 7
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 8
http: //download. oracle. com/javase/6/docs/api/java/lang/Object. html CSS 430: Operating Systems - Process Synchronization 9
Java Synchronization /usr/apps/CSS 430/examples/os-book/ch 6/java-synchronization/boundedbuffer CSS 430: Operating Systems - Process Synchronization 10
Java Synchronization notify() may notify the correct thread! /usr/apps/CSS 430/examples/os-book/ch 6/java-synchronization/boundedbuffer CSS 430: Operating Systems - Process Synchronization 11
Test 2 e class Test 2 e extends Thread { public void run() { String[] args 1 = Sys. Lib. string. To. Args( "Test. Thread 2 a 5000 String[] args 2 = Sys. Lib. string. To. Args( "Test. Thread 2 b 1000 String[] args 3 = Sys. Lib. string. To. Args( "Test. Thread 2 c 3000 String[] args 4 = Sys. Lib. string. To. Args( "Test. Thread 2 d 6000 String[] args 5 = Sys. Lib. string. To. Args( "Test. Thread 2 e 500 Sys. Lib. exec( args 1 ); Sys. Lib. exec( args 2 ); Sys. Lib. exec( args 3 ); Sys. Lib. exec( args 4 ); Sys. Lib. exec( args 5 ); for (int i = 0; i < 5; i++ ) { int tid = Sys. Lib. join(); Sys. Lib. cout( "Thread tid=" + tid + " donen” ); Sys. Lib. cout( "Test 2 e finished; total time = " + total. Time Sys. Lib. exit(); } } CSS 430: Operating Systems - Process Synchronization 0" 0" 0" ); ); ); + "n" ); 12
Test. Thread 2 class Test. Thread 2 extends Thread { … public void run() { long total. Execution. Time = 0; long total. Wait. Time = 0; activation. Time = new Date(). get. Time(); for ( int burst = cpu. Burst; burst > 0; burst -= TIMEQUANTUM ) { total. Execution. Time += TIMEQUANTUM; Sys. Lib. sleep( TIMEQUANTUM ); } completion. Time = new Date(). get. Time( ); long response. Time = activation. Time - submission. Time; total. Wait. Time = completion. Time - submission. Time - execution. Time; long turnaround. Time = completion. Time - submission. Time; Sys. Lib. cout( String. format( "%05 d: Thread[%s]: response: %5 d; wait: %5 d; execution: %5 d; turnaround: %5 dn", completion. Time % 100000, name, response. Time, total. Wait. Time, total. Execution. Time, turnaround. Time ) ); Sys. Lib. exit(); } } CSS 430: Operating Systems - Process Synchronization 13
Test 2 e output b e d a c d-69 -91 -160 -124: Thread. OS 0 joe$ java Boot thread. OS ver 1. 0: Type ? for help thread. OS: a new thread (thread=Thread[Thread-4, 2, main] tid=0 pid=-1) -->l Shell thread. OS: a new thread (thread=Thread[Thread-6, 2, main] tid=1 pid=0) shell[1]% Test 2 e thread. OS: a new thread (thread=Thread[Thread-9, 2, main] tid=2 pid=1) thread. OS: a new thread (thread=Thread[Thread-11, 2, main] tid=3 pid=2) thread. OS: a new thread (thread=Thread[Thread-13, 2, main] tid=4 pid=2) thread. OS: a new thread (thread=Thread[Thread-15, 2, main] tid=5 pid=2) thread. OS: a new thread (thread=Thread[Thread-17, 2, main] tid=6 pid=2) thread. OS: 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 Test 2 e finished shell[2]% exit CSS 430: Operating Systems - Process Synchronization 14 -->q
Sys. Lib. join(), Kernel. WAIT public class Sys. Lib { … public static int join( ) { return Kernel. interrupt( Kernel. INTERRUPT_SOFTWARE, Kernel. WAIT, 0, null ); } public class Kernel { … public static interrupt( int irq, int cmd, int param, Object args ) { // Looks like my. Tcb is not used anywhere; new. Tcb declared & used below // TCB my. Tcb; switch( irq ) { case INTERRUPT_SOFTWARE: // System calls switch( cmd ) { … case WAIT: // get the current thread id // let the current thread sleep in wait. Queue 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 15
enqueue. And. Sleep(), dequeue. And. Wakeup() CSS 430: Operating Systems - Process Synchronization 16
Sys. Lib. exit(), Kernel. EXIT public class Sys. Lib { … public static int exit( ) { return Kernel. interrupt( Kernel. INTERRUPT_SOFTWARE, Kernel. EXIT, 0, null ); } public class Kernel { … public static interrupt( int irq, int cmd, int param, Object args ) { // Looks like my. Tcb is not used anywhere; new. Tcb declared & used below // TCB my. Tcb; switch( irq ) { case INTERRUPT_SOFTWARE: // System calls switch( cmd ) { … case EXIT: // get the current thread's parent id // search wait. Queue 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 17
Sys. Lib. rawread(), Kernel. RAWREAD public class Sys. Lib { … public static int rawread( int blk. Number, byte[] b ) { return Kernel. interrupt( Kernel. INTERRUPT_SOFTWARE, Kernel. RAWREAD, blk. Number, b ); } public class Kernel { … public static interrupt( int irq, int cmd, int param, Object args ) { // Looks like my. Tcb is not used anywhere; new. Tcb declared & used below // TCB my. Tcb; 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 ) io. Queue. enqueue. And. Sleep( COND_DISK_REQ ); while ( disk. test. And. Reset. Ready( ) == false ) io. Queue. enqueue. And. Sleep( COND_DISK_FIN ); return OK; CSS 430: Operating Systems - Process Synchronization 18
Disk. read(), Disk. test. And. Reset. Ready() public class Disk { … public synchronized boolean read( int block. Id, byte buffer[] ) { if ( block. Id < 0 || block. Id > disk. Size ) { Sys. Lib. cerr( "thread. OS: invalid block. Id for readn" ); return false; } if ( command == IDLE && ready. Buffer == false ) { this. buffer = buffer; target. Block. Id = block. Id; command = READ; notify( ); return true; } else return false; } … public synchronized boolean test. And. Reset. Ready( ) { if ( command == IDLE && ready. Buffer == true ) { ready. Buffer = false; return true; } else return false; } CSS 430: Operating Systems - Process Synchronization 19
Disk. wait. Command(), Disk. finish. Command() public class Disk { … private synchronized void wait. Command( ) { while ( command == IDLE ) { try { wait( ); } catch ( Interrupted. Exception e ) { Sys. Lib. cerr( e. to. String( ) + "n" ); } ready. Buffer = false; } } … private synchronized void finish. Command( ) { command = IDLE; ready. Buffer = true; Sys. Lib. disk( ); // a disk interrupt } CSS 430: Operating Systems - Process Synchronization 20
Disk. run() public class Disk { … public void run ( ) { while ( true ) { wait. Command( ); seek( ); switch( command ) { case READ: System. arraycopy( data, target. Block. Id * block. Size, buffer, 0, block. Size ); break; case WRITE: … case SYNC: … } finish. Command( ); } } CSS 430: Operating Systems - Process Synchronization 21
Kernel. INTERRUPT_DISK public class Sys. Lib { … public static int rawread( int blk. Number, byte[] b ) { return Kernel. interrupt( Kernel. INTERRUPT_SOFTWARE, Kernel. RAWREAD, blk. Number, b ); } public class Kernel { … public static interrupt( int irq, int cmd, int param, Object args ) { // Looks like my. Tcb is not used anywhere; new. Tcb declared & used below // TCB my. Tcb; switch( irq ) { … case INTERRUPT_DISK: // Disk interrupts // wake up the thread waiting for a service completion first io. Queue. dequeue. And. Wakeup( COND_DISK_FIN ); // wake up a thread waiting for a request acceptance io. Queue. dequeue. And. Wakeup( COND_DISK_REQ ); return OK; CSS 430: Operating Systems - Process Synchronization 22
- Slides: 22