Exemple #1
0
  /*
   * if the input arguments are negative, return -1
   * @return the child process Id
   */
  private int handleExec(int file, int argc, int argv) {
    if (file < 0 || argc < 0 || argv < 0) {
      return -1;
    }
    String fileName = readVirtualMemoryString(file, 256);

    if (fileName == null) {
      return -1;
    }

    String args[] = new String[argc];

    int byteReceived, argAddress;
    byte temp[] = new byte[4];
    for (int i = 0; i < argc; i++) {
      byteReceived = readVirtualMemory(argv + i * 4, temp);
      if (byteReceived != 4) {
        return -1;
      }

      argAddress = Lib.bytesToInt(temp, 0);
      args[i] = readVirtualMemoryString(argAddress, 256);

      if (args[i] == null) {
        return -1;
      }
    }
    UserProcess child = UserProcess.newUserProcess();
    childProcess newProcessData = new childProcess(child);
    child.myChildProcess = newProcessData;

    if (child.execute(fileName, args)) {
      map.put(child.process_id, newProcessData);
      return child.process_id;
    }

    return -1;
  }