Class Kernel

Object
  extended byKernel

public class Kernel
extends Object

A simple kernel simulation.

There is only one public interface to this class: interrupt(). System calls, disk notification, and power on messages all arrive by way of this function.

See the list of SYSCALL_XXX constants to learn what system calls are currently supported.

See Also:
Disk, Library

Nested Class Summary
private static class Kernel.Launcher
          A Launcher instance represents one atomic command being run by the Kernel.
 
Field Summary
private static BufferedReader br
           
private static int cacheSize
          The size of the disk cache
private static Disk disk
          The disk to be used
static int ERROR_BAD_ARGUMENT
          An error code indicating that one of the system call parameters made no sense.
static int ERROR_BAD_COMMAND
          An error code indicating some unspecified problem running the class passed SYSCALL_EXEC.
static int ERROR_END_OF_FILE
          An error code indicating that end of file was reached.
static int ERROR_IN_CHILD
          An error code indicating that a child program caused an exception and crashed.
static int ERROR_IO
          An error code indicating that somthing went wrong during an I/O operation.
static int ERROR_NO_CLASS
          An error code indicating that the class name passed to SYSCALL_EXEC could not be found.
static int ERROR_NO_MAIN
          An error code indicating that the class name passed to SYSCALL_EXEC named a class with no appropriate main() method.
static int ERROR_NO_SUCH_PROCESS
          An error code indicating an attempt to join with a non-existant process
static int ERROR_OUT_OF_RANGE
          An error code indicating that one parameter was too big or too small
static int INTERRUPT_DISK
          An interrupt kind indicating that a disk caused the interrupt.
static int INTERRUPT_POWER_ON
          An interrupt kind indicating that the system just started.
static int INTERRUPT_USER
          An interrupt kind indicating that a user program caused the interrupt.
static int SYSCALL_EXEC
          System call to execute a new program.
static int SYSCALL_INPUT
          System call to read text from the console.
static int SYSCALL_JOIN
          System call to wait for a process to terminate.
static int SYSCALL_OUTPUT
          System call to output text on the console.
 
Constructor Summary
Kernel()
           
 
Method Summary
private static int doExec(String command, String[] args)
          Loads a program and runs it in the background.
private static int doExecAndWait(String command, String[] args)
          Loads a program and runs it.
private static int doInput(StringBuffer sb)
          Reads a line from the console into a StringBuffer.
private static int doJoin(int pid)
          Waits for a program previous started by doExec to terminate.
private static int doOutput(String msg)
          Displays a message on the console.
private static void doPowerOn(int i1, Object o1, Object o2)
          Performs the actions associated with a POWER_ON interrupt.
private static void doShutdown()
          Does any "shutdown" activities required after all activities started by a POWER_ON interrupt have completed.
static int interrupt(int kind, int i1, int i2, Object o1, Object o2, byte[] a)
          This is the only entry into the kernel.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INTERRUPT_USER

public static final int INTERRUPT_USER
An interrupt kind indicating that a user program caused the interrupt. Other parameters depend on the call number.

See Also:
Constant Field Values

INTERRUPT_DISK

public static final int INTERRUPT_DISK
An interrupt kind indicating that a disk caused the interrupt. All other parameters will be null or zero.

See Also:
Constant Field Values

INTERRUPT_POWER_ON

public static final int INTERRUPT_POWER_ON
An interrupt kind indicating that the system just started. The Kernel should set up any internal state and begin executing the first program.

See Also:
Constant Field Values

SYSCALL_OUTPUT

public static final int SYSCALL_OUTPUT
System call to output text on the console.

See Also:
Constant Field Values

SYSCALL_INPUT

public static final int SYSCALL_INPUT
System call to read text from the console. This function returns when the user presses [Enter].

See Also:
Constant Field Values

SYSCALL_EXEC

public static final int SYSCALL_EXEC
System call to execute a new program. The new program will run in parallel to the current program.

See Also:
Constant Field Values

SYSCALL_JOIN

public static final int SYSCALL_JOIN
System call to wait for a process to terminate. This call will not return until the indicated process has run to completion.

See Also:
Constant Field Values

ERROR_BAD_ARGUMENT

public static final int ERROR_BAD_ARGUMENT
An error code indicating that one of the system call parameters made no sense.

See Also:
Constant Field Values

ERROR_NO_CLASS

public static final int ERROR_NO_CLASS
An error code indicating that the class name passed to SYSCALL_EXEC could not be found.

See Also:
Constant Field Values

ERROR_NO_MAIN

public static final int ERROR_NO_MAIN
An error code indicating that the class name passed to SYSCALL_EXEC named a class with no appropriate main() method.

See Also:
Constant Field Values

ERROR_BAD_COMMAND

public static final int ERROR_BAD_COMMAND
An error code indicating some unspecified problem running the class passed SYSCALL_EXEC.

See Also:
Constant Field Values

ERROR_OUT_OF_RANGE

public static final int ERROR_OUT_OF_RANGE
An error code indicating that one parameter was too big or too small

See Also:
Constant Field Values

ERROR_END_OF_FILE

public static final int ERROR_END_OF_FILE
An error code indicating that end of file was reached.

See Also:
Constant Field Values

ERROR_IO

public static final int ERROR_IO
An error code indicating that somthing went wrong during an I/O operation.

See Also:
Constant Field Values

ERROR_IN_CHILD

public static final int ERROR_IN_CHILD
An error code indicating that a child program caused an exception and crashed.

See Also:
Constant Field Values

ERROR_NO_SUCH_PROCESS

public static final int ERROR_NO_SUCH_PROCESS
An error code indicating an attempt to join with a non-existant process

See Also:
Constant Field Values

disk

private static Disk disk
The disk to be used


cacheSize

private static int cacheSize
The size of the disk cache


br

private static BufferedReader br
Constructor Detail

Kernel

public Kernel()
Method Detail

interrupt

public static int interrupt(int kind,
                            int i1,
                            int i2,
                            Object o1,
                            Object o2,
                            byte[] a)
This is the only entry into the kernel.

A user may call this function to perform a system call. In that case, set kind to INTERRUPT_USER and set i1 to the system call number. Other parameters should be set as the system call requires.

A disk may call this function to indicate the current operation has completed. In that case, kind will be INTERRUPT_DISK and all parameters will be zero or null.
Important: If the Disk calls interrupt(), the Kernel should take care of business and return from the interrupt as soon as possible. All Disk I/O is halted while the interrupt is being processed.

The boot code may call this function to indicate that the computer has been turned on and it is time to start the first program and use the disk. In that case, kind will be INTERRUPT_POWER_ON, o1 will point to the Disk to be used, o2 will be a String containing the name of the shell to use, i1 will indicate the size of the buffer cache, and all other parameters will be zero or null.

Since different system calls require different parameters, this method has a variety of arguments of various types. Any one system call will use at most a few of them. The others should be zero or null.

Parameters:
kind - the kind of system call, one of the INTERRUPT_XXX codes.
i1 - the first integer parameter. If kind == INTERRUPT_USER, i1 should be one of the SYSTEM_XXX codes to indicate which system call is being invoked.
i2 - another integer parameter.
o1 - a parameter of some object type.
o2 - another parameter of some object type.
a - a byte-array parameter (generally used for binary input/output).
Returns:
a negative number indicating an error code, or other values depending on the system call.

doPowerOn

private static void doPowerOn(int i1,
                              Object o1,
                              Object o2)
Performs the actions associated with a POWER_ON interrupt.

Parameters:
i1 - the first int parameter to the interrupt (the disk cache size)
o1 - the first Object parameter to the interrupt (the Disk).
o2 - the second Object parameter to the interrupt (the shell command-line).

doShutdown

private static void doShutdown()
Does any "shutdown" activities required after all activities started by a POWER_ON interrupt have completed.


doOutput

private static int doOutput(String msg)
Displays a message on the console.

Parameters:
msg - the message to display

doInput

private static int doInput(StringBuffer sb)
Reads a line from the console into a StringBuffer.

Parameters:
sb - a place to put the line of input.

doExecAndWait

private static int doExecAndWait(String command,
                                 String[] args)
Loads a program and runs it. Blocks the caller until the program has terminated.

Parameters:
command - the program to run.
args - command-line args to pass to the program.
Returns:
the program's return code on success, ERROR_NO_CLASS, ERROR_NO_MAIN, or ERROR_BAD_COMMAND if the command cannot be run, or ERROR_IN_CHILD if the program throws an uncaught exception.

doExec

private static int doExec(String command,
                          String[] args)
Loads a program and runs it in the background. Does not wait for the program to terminate.

Parameters:
command - the program to run.
args - command-line args to pass to the program.
Returns:
a process id on success or ERROR_NO_CLASS, ERROR_NO_MAIN, or ERROR_BAD_COMMAND if the command cannot be run.

doJoin

private static int doJoin(int pid)
Waits for a program previous started by doExec to terminate.

Parameters:
pid - the process id of the program.
Returns:
the return code returned by the program.