Download Review of Ch6 + Program 3

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Thread Synchronization
in ThreadOS:
Implementing SysLib methods
join(), exit(),
rawread(), rawwrite(), sync()
Joe McCarthy
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/CSS430/examples/os-book/ch6/java-synchronization/boundedbuffer
CSS 430: Operating Systems - Process Synchronization
10
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
11
Test2e
class Test2e extends Thread {
public void run() {
String[] args1 = SysLib.stringToArgs( "TestThread2 a 5000
String[] args2 = SysLib.stringToArgs( "TestThread2 b 1000
String[] args3 = SysLib.stringToArgs( "TestThread2 c 3000
String[] args4 = SysLib.stringToArgs( "TestThread2 d 6000
String[] args5 = SysLib.stringToArgs( "TestThread2 e 500
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
SysLib.exit();
}
}
CSS 430: Operating Systems - Process Synchronization
0"
0"
0"
0"
0"
);
);
);
);
);
+ "\n" );
12
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
13
Test2e output
b
e
d
a
c
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
CSS 430: Operating Systems - Process Synchronization
14
exit
-->q
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
15
enqueueAndSleep(),
dequeueAndWakeup()
CSS 430: Operating Systems - Process Synchronization
16
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
17
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
18
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
return false;
}
…
public synchronized boolean testAndResetReady( ) {
if ( command == IDLE && readyBuffer == true ) {
readyBuffer = false;
return true;
} else
return false;
}
CSS 430: Operating Systems - Process Synchronization
19
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
20
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
21
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
22
Related documents