1 of 2

Name:

PID: (page 2)

A process is what our computer keeps track of for each command while it is running. This includes:

  • Where its standard input comes from (typing or file) and where its standard output should go (terminal or file)
  • Its memory (like where all the variables and objects are stored) and code (Java bytecode, bash script code)
  • The command line arguments it started with
  • Any ports it is listening on, any files it is reading from or writing to.... and lots more!

​

When a process ends, it has a return code or exit code. The rule is that 0 means success and non-0 means error.

In bash, the exit code of the last process that ran is stored in the special variable: $? (yes, that's a dollar sign then a question mark)

set -e

javac ExitCode.java

java ExitCode 0

echo $?

java ExitCode 1

echo $?

java ExitCode 3

echo $?

class ExitCode {

public static void main(String[] args) {

System.out.println("Exiting with code " + args[0]);

System.exit(Integer.parseInt(args[0]));

}

}

run-exit-code.sh

ExitCode.java

bash-3.2$ bash run-exit-code.sh

# what's the output? why?

​

​

​

​

​

​

​

​

​

bash-3.2$ ls does-not-exist.txt

ls: does-not-exist: No such file or directory

bash-3.2$ echo $? # fill in a guess/the result below

​

_________________________________

bash-3.2$ cat BadFile.java

class Bad {

private statik void mane(Strong[] orgs) {}

}

bash-3.2$ javac BadFile.java

BadFile.java:2: error: <identifier> expected

private statik void mane(Strong[] orgs) {}

^

1 error

bash-3.2$ echo $?

1

bash-3.2$ echo $? # fill in a guess/the result below

​

________________________________

bash-3.2$ cat GoodFile.java

class Main {

public static void main(String[] args) { }

}

bash-3.2$ javac GoodFile.java

bash-3.2$ echo $? $ fill in a guess/the result below

​

​

________________________________

​

​

​

​

​

​

​

Where might it be useful to use or check an exit code?

​

​

​

​

​

​

​

​

​

What happens when you submit a PA in CSE12?

2 of 2

Name:

PID: (page 1)

class CompileError {

public static void main(String[] args) {

int x = "not-a-number";

}

}

CompileError.java

$ javac CompileError.java >error-output.txt

CompileError.java:3: error: incompatible types:

String cannot be converted to int

int x = "not-a-number";

^

1 error

$ cat error-output.txt # nothing in this file!

$ javac CompileError.java ________________________________

$ cat error-output.txt

CompileError.java:3: error: incompatible types:

String cannot be converted to int

int x = "not-a-number";

^

1 error

class PrintThenThrow {

public static void main(String[] args) {

System.out.println("Hello!");

System.err.println("Error!");

throw new RuntimeException("This is an error!");

}

}

PrintThenThrow.java

$ java PrintThenTrow >out.txt

Error!

Exception in thread "main" java.lang.RuntimeException: This is an error!

at PrintThenTrow.main(PrintThenThrow.java:4)

$ cat out.txt

​

_________________________________

$ java PrintThenTrow >out.txt 2>&1

$ cat out.txt

Hello!

Error!

Exception in thread "main" java.lang.RuntimeException: This is an error!

at PrintThenTrow.main(PrintThenThrow.java:4)

Processes actually have two different default places to print: standard output and standard error

​

Programming languages typically have a way to select one or the other when printing. In Java, there's System.err.print

​

Many programs, when they report an error message, print to standard error (also called stderr).

​

We can observe this difference with redirection: cmd 2>file.txt redirects the output of the command's stderr to file.txt

cmd >file.txt 2>&1 redirects stderr to stdout and then both to file.txt

Brainstorm: What makes a good autograder script? How might it work?

​

Hint – imagine this setup. Gradescope runs:

​

$ bash grade.sh <student-github-url>

​

and whatever the text output of that command is gets sent back to the student. What are the steps to, say, grade a PA.

​

Imagine students submitted their DocSearchServer that we used in lab that searches files, for example. What should the grader do?