Skip to content

Instantly share code, notes, and snippets.

@aerodame
aerodame / SyncQueueWAIT_EXIT.java
Last active August 29, 2015 14:20
SyncQueue WAIT and EXIT
case WAIT:
// get the current thread id (use Scheduler.getMyTcb( ) )
// let the current thread sleep in waitQueue under the condition
// = this thread id (= tcb.getTid( ) )
return OK; // return a child thread id who woke me up
case EXIT:
// get the current thread's parent id (= tcb.getPid( ) )
// search waitQueue for and wakes up the thread under the condition
// = the current thread's parent id
return OK;
@aerodame
aerodame / Kernel_Interrupt.java
Last active August 29, 2015 14:20
ThreadOS Disk I/O Interrupt
public static int interrupt( int irq, int cmd, int param, Object args ) {
TCB myTcb;
switch( irq ) {
case INTERRUPT_SOFTWARE: // System calls
...
...
case INTERRUPT_DISK: // Disk interrupts
// wake up the thread waiting for a service completion
ioQueue.dequeueAndWakeup( 2 );
@aerodame
aerodame / Kernel_Disk.java
Last active August 29, 2015 14:20
SyncQueue for Disk I/O
Disk disk = new Disk( 1000 );
SyncQueue ioQueue = new SyncQueue( );
...
...
// a disk read operation:
while ( disk.read( blockId, buffer ) == false ) {
ioQueue.enqueueAndSleep( 1 ); // relinquish CPU to another ready thread
// now check to ensure disk is not busy
while ( disk.testAndResetReady( ) == false )
ioQueue.enqueueAndSleep( 2 ); // relinquish CPU to another ready thread
@aerodame
aerodame / KernelSnippet.java
Last active August 29, 2015 14:20
ThreadOS Kernel Snippet
import java.util.*;
import java.lang.reflect.*;
import java.io.*;
public class Kernel {
// Interrupt requests
public final static int INTERRUPT_SOFTWARE = 1; // System calls
public final static int INTERRUPT_DISK = 2; // Disk interrupts
public final static int INTERRUPT_IO = 3; // Other I/O interrupts
@aerodame
aerodame / DiskIO_Snippet.java
Last active August 29, 2015 14:20
Disk IO Sync Snippet
Disk disk = new Disk( 1000 );
// a disk read operation:
while ( disk.read( blockId, buffer ) == false )
; // busy wait
while ( disk.testAndResetReady( ) == false )
; // another busy wait
// now you can access data in buffer
@aerodame
aerodame / gist:c1dc6c7a1b6b28815361
Last active August 29, 2015 14:19
ThreadOS Single Queue debugger
// ----------------------- Start Debugger Method ---------------------------
// Provide a simple debugger of the TCBs in queue
// Format of output is = Q[]-> { [p,t] [p,t] [p,t] ... }
// Where p = Process ID
// t = Thread ID
// -------------------------------------------------------------------------
private void dumpQueue(int time) {
if (DEBUG) {
System.out.print("\nT("+time+"): ");
// how many TCBs are in queue
@aerodame
aerodame / PingPong.java
Created April 1, 2015 07:13
CSS430 : Program 1 - PingPong Infinite
public class PingPong extends Thread {
private String word;
private int loop;
public PingPong( String[] args ) {
word = args[0];
loop = Integer.parseInt( args[1] );
}
public void run( ) {
for ( int j = 0; j < 100; j++ ) {
SysLib.cout( word + " " );
@aerodame
aerodame / PingPong.java
Last active August 29, 2015 14:18
CSS430 - Program 1 - PingPong
public class PingPong extends Thread {
private String word;
private int loop;
public PingPong( String[] args ) {
word = args[0];
loop = Integer.parseInt( args[1] );
}
public void run( ) {
for ( int j = 0; j < 100; j++ ) {
SysLib.cout( word + " " );
@aerodame
aerodame / ThreadDriver.java
Last active August 29, 2015 14:18
CSS430 - Program1 Thread Main
public class ThreadDriver {
public static void main( String[] args ) {
String args[2];
args[0] = "ping"; args[1] = "10000";
new PingPong( args ).start( );
args[0] = "PING"; args[1] = "90000";
new PingPong( args ).start( );
}
// Provide a quick debugger of the TCBs in queue
// Format of output is = Q[i]-> { [p,t] [p,t] [p,t] ... }
private void dumpQueue(int time) {
int N;
if (DEBUG) {
System.out.print("T("+time+"): ");
// scan through each multilevel queue
for (int i=0; i < 3; i++) {
// how many TCBs are in queue
N = queue[i].size();