joe mccarthy

Post on 04-Jan-2016

42 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Program 3 Implementing Java Monitors: Kernel support for SysLib methods join(), exit(), rawread(), rawwrite(), sync(). Joe McCarthy. File System (Ch. 11). File system interface provides applications with various system calls and commands such as open, write, read, seek, etc.. - PowerPoint PPT Presentation

TRANSCRIPT

Program 3Implementing Java Monitors:

Kernel support for SysLib methodsjoin(), exit(),

rawread(), rawwrite(), sync()

Joe McCarthy

CSS 430: Operating Systems - Program 3 1

File System (Ch. 11)

CSS 430: Operating Systems - Program 3 2

File system interface provides applications with various system calls and commands such as open, write, read, seek, etc..

File system maintains disk space in blocks and allocates available blocks to each stream-oriented file.

Basic file system (BIOS) maintains data in physical blocks

Device driver reads / writes disk blocks which consists of one (or more) sector(s).

Disk maintains physical block locations indexed by drive#, cylinder#, track# and sector#

track

sector

cylinder

File System (Ch. 11)

CSS 430: Operating Systems - Program 3 3

SysLib.rawread(), Kernel.RAWREAD

CSS 430: Operating Systems - Program 3 4

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 ) ; // busy wait while ( disk.testAndResetReady() == false ) ; // busy wait; return OK;

SysLib.rawread(), Kernel.RAWREAD

CSS 430: Operating Systems - Program 3 5

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( CONDDISK_FIN ); return OK;

Disk.read(),Disk.testAndResetReady()

CSS 430: Operating Systems - Program 3 6

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; }

Disk.run()

CSS 430: Operating Systems - Program 3 7

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(); } }

Disk.waitCommand(), Disk.finishCommand()

CSS 430: Operating Systems - Program 3 8

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(); // generate a disk interrupt }

SysLib.disk(), Kernel.INTERRUPT_DISK

CSS 430: Operating Systems - Program 3 9

public class SysLib { … public static int disk() {

return Kernel.interrupt( Kernel.INTERRUPT_DISK, 0, 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_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; …

Test2e

CSS 430: Operating Systems - Program 3 10

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(); }}

TestThread2

CSS 430: Operating Systems - Program 3 11

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(); }}

Test2e output

CSS 430: Operating Systems - Program 3 12

d-69-91-160-124:ThreadOS0 joe$ java BootthreadOS ver 1.0:Type ? for helpthreadOS: a new thread (thread=Thread[Thread-4,2,main] tid=0 pid=-1)-->l Shelll ShellthreadOS: a new thread (thread=Thread[Thread-6,2,main] tid=1 pid=0)shell[1]% Test2eTest2ethreadOS: 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 = 1002Thread tid=4 doneThread[e]: response time = 6944 turnaround time = 7445 execution time = 501Thread tid=7 doneThread[a]: response time = 2941 turnaround time = 7948 execution time = 5007Thread tid=3 doneThread[c]: response time = 4945 turnaround time = 7951 execution time = 3006Thread tid=5 doneThread[d]: response time = 5944 turnaround time = 11953 execution time = 6009Thread tid=6 doneTest2e finishedshell[2]% exitexit-->q

b

e

d

a

c

SysLib.join(), Kernel.WAIT

CSS 430: Operating Systems - Program 3 13

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…

enqueueAndSleep(),dequeueAndWakeup()

CSS 430: Operating Systems - Program 3 14

SysLib.exit(), Kernel.EXIT

CSS 430: Operating Systems - Program 3 15

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;

top related