// COM3336 Operating Systems // Homework 1 part 2 // John Sung // Started: 10/9/1999 // import java.io.*; import java.util.*; import CommandExecutor; import ThreadMutex; import ShellSystem; // The hw1 class. This class is the highest level class. public class hw2_1 { // the mutex that counts the number of command // threads that are currently running. private static ThreadMutex RunningThreadCount; // the file system for this shell private static ShellSystem FileSystem; public static void main(String[] args) { // initialize the running thread count RunningThreadCount = new ThreadMutex(0); // input from the standard in. BufferedReader cmdLineIn = new BufferedReader(new InputStreamReader(System.in)); // the file system. FileSystem = new ShellSystem(); // loop forever. we'll exit by calling System.exit(). while (true) { // output the prompt System.out.print("\n>"); try { // read in a line and get input from the // user, i.e. commands String cmd = cmdLineIn.readLine(); // the input has terminated so just exit if (cmd == null) System.exit(0); // Attempt to execute the command executeCommand(cmd); // wait for all of the threads to be finished. while(!RunningThreadCount.areThreadsFinished()); } catch (IOException ex) { } } } // this is the function that will take in a cmdAndArgs // and attempt to execute the command by calling the runtime. public static void executeCommand(String cmdAndArgs) { // attempt to find the '&' int ampersandIndex = cmdAndArgs.indexOf('&'); // if the ampersand is there then do a sub string and // recursively call the execute command on both parts. if (ampersandIndex > -1) { // break up the commands into 2 substrings // and call this method again recursively. // This is for when you hae multiple &'s. String cmd1 = cmdAndArgs.substring(0,ampersandIndex); String cmd2 = cmdAndArgs.substring(ampersandIndex+1); // execute the two commands executeCommand(cmd2); executeCommand(cmd1); } // if there is no ampersand then just execute the sigle // command that's been passed else { // Now create a new command executor object to // execute the command. CommandExecutor executor = new CommandExecutor(cmdAndArgs,RunningThreadCount, FileSystem); // Now start the thread. executor.start(); } } }