int getIntegerArgument(String key) {
   try {
     return Integer.parseInt(getStringArgument(key));
   } catch (NumberFormatException e) {
     Lib.assertNotReached("getIntegerArgument(" + key + ") failed: " + "value is not an integer");
     return 0;
   }
 }
  private static void processArgs() {
    for (int i = 0; i < args.length; ) {
      String arg = args[i++];
      if (arg.length() > 0 && arg.charAt(0) == '-') {
        if (arg.equals("-d")) {
          Lib.assertTrue(i < args.length, "switch without argument");
          Lib.enableDebugFlags(args[i++]);
        } else if (arg.equals("-h")) {
          System.out.print(help);
          System.exit(1);
        } else if (arg.equals("-m")) {
          Lib.assertTrue(i < args.length, "switch without argument");
          try {
            numPhysPages = Integer.parseInt(args[i++]);
          } catch (NumberFormatException e) {
            Lib.assertNotReached("bad value for -m switch");
          }
        } else if (arg.equals("-s")) {
          Lib.assertTrue(i < args.length, "switch without argument");
          try {
            randomSeed = Long.parseLong(args[i++]);
          } catch (NumberFormatException e) {
            Lib.assertNotReached("bad value for -s switch");
          }
        } else if (arg.equals("-x")) {
          Lib.assertTrue(i < args.length, "switch without argument");
          shellProgramName = args[i++];
        } else if (arg.equals("-z")) {
          System.out.print(copyright);
          System.exit(1);
        }
        // these switches are reserved for the autograder
        else if (arg.equals("-[]")) {
          Lib.assertTrue(i < args.length, "switch without argument");
          configFileName = args[i++];
        } else if (arg.equals("--")) {
          Lib.assertTrue(i < args.length, "switch without argument");
          autoGraderClassName = args[i++];
        }
      }
    }

    Lib.seedRandom(randomSeed);
  }
  boolean getBooleanArgument(String key) {
    String value = getStringArgument(key);

    if (value.equals("1") || value.toLowerCase().equals("true")) {
      return true;
    } else if (value.equals("0") || value.toLowerCase().equals("false")) {
      return false;
    } else {
      Lib.assertNotReached("getBooleanArgument(" + key + ") failed: " + "value is not a boolean");
      return false;
    }
  }
Exemple #4
0
  private void threadroot() {
    // this should be running the current thread
    Lib.assertTrue(javaThread == Thread.currentThread());

    if (!isFirstTCB) {
      /* start() is waiting for us to wake it up, signalling that it's OK
       * to context switch to us. We leave the running flag false so that
       * we'll still run if a context switch happens before we go to
       * sleep. All we have to do is wake up the current TCB and then
       * wait to get woken up by contextSwitch() or destroy().
       */

      currentTCB.interrupt();
      this.yield();
    } else {
      /* start() called us directly, so we just need to initialize
       * a couple things.
       */

      currentTCB = this;
      running = true;
    }

    try {
      target.run();

      // no way out of here without going throw one of the catch blocks
      Lib.assertNotReached();
    } catch (ThreadDeath e) {
      // make sure this TCB is being destroyed properly
      if (!done) {
        System.out.print("\nTCB terminated improperly!\n");
        privilege.exit(1);
      }

      runningThreads.removeElement(this);
      if (runningThreads.isEmpty()) privilege.exit(0);
    } catch (Throwable e) {
      System.out.print("\n");
      e.printStackTrace();

      runningThreads.removeElement(this);
      if (runningThreads.isEmpty()) privilege.exit(1);
      else die();
    }
  }