The labs on this site need JavaScript to run. The same content is written out below.
Fundamentals › Architecture and operations
What is a process, and what is separate when you run a program twice?
Counter.class, made by compiling a program, is a file stored on disk. When you run this file with the command java Counter, something that is running comes into being. That is a process. The operating system gives each process a number (PID) and gives it memory that only that process uses. In the code, static int count is a variable in that memory, and add() raises count by 3 and then prints the PID of this process and count. The flow that runs code inside a process is called a thread. In this lab you run the same Counter.class in two terminals, run it again after it ends, or run it once and let threads inside it run add one after another. You see how many processes are created and what count each one saw. (A program such as a web server is also one process when you run it with the java command. static is not covered in this lab.)
Checked against real execution · Ran all six combinations as real javac and java processes on JDK 21 and checked against the results
# Counter.java
public class Counter {
// A variable in the memory of this process
static int count = 0;
// Raise count by 3, and print the PID of this process and count
static void add() {
count++;
count++;
count++;
System.out.println("PID " + ProcessHandle.current().pid()
+ " count " + count);
}
public static void main(String[] args) throws Exception {
add();
// Keep the run going for 2 seconds
Thread.sleep(2000);
}
}
# Terminal
# Compile: the file Counter.class is created
javac Counter.java
# Run it in terminal 1
java Counter
# In terminal 2, run it before the earlier run ends
java Counter
The environment is JDK 21.0.12.1 (javac 21, java 21) on Windows. All six combinations were checked against real execution. For each combination, the same text as the code on the screen (the # Counter.java section and the # Terminal section) was written into an empty folder, the source file and the commands were read from that text, and the real javac program and java program were started as processes, and the results were compared. For each java Counter line in the terminal section, the line directly above it was read as a comment. If it said "after the earlier run ends", the check waited for all earlier processes to end before starting it, and otherwise it started it at once without waiting. For "several terminals", the check did not open several terminals but started the processes one after another instead. What was checked is the number of processes actually started, the count printed by each process, whether any process ever saw a count larger than 3 (that is, whether memory is shared), the maximum number of live processes counted each time a process was started, whether the PID that was printed equals the process ID seen by the side that ran it, whether the PIDs of the processes alive together are different from each other, the exit code and the standard error, and whether Counter.class is still 1 file after running. The PIDs 4120, 7352, and 9081 on the screen are example numbers, and the real numbers differ on every run. On the PC used to write this lab, the PID of a process that was run again after the earlier one ended was different from all the earlier ones. But the Java SE 21 API documentation of ProcessHandle (the description of pid()) says that the operating system may reuse the number of an ended process and does not fix when it does so, so the screen does not say that "the number of the process that is run again is always different". That a program such as a web server is also one process when you run it with the java command was checked by running a small server program that receives requests (com.sun.net.httpserver of the JDK) with the java command and sending it two requests. The PIDs in the two responses were the same and equal to the process ID seen by the side that ran it, and count continued as 1, 2. This server is not shown on the screen, and a real server product such as Tomcat was not run. The documentation was opened and checked directly. From "Processes and Threads" in The Java Tutorials, the lab follows that a process has its own execution environment and memory space (the Processes section), that a thread lives inside a process and shares the resources of the process, including memory (the Threads section), and that most implementations of the Java virtual machine run as a single process. The distinction between a program file and a process (the file is on disk and the process is that file being run) is not defined by this document. It is an explanation that follows the execution results above and the general use of the terms. The code on the screen uses a static variable because it is meant to show that the variable count inside a process is in the memory of the process, and it makes the threads run one after another with join() to avoid the problem that occurs when two or more threads raise count at the same time (a race condition). Thread.sleep(2000) is there so that a process does not end at once and several processes are alive at the same time, and a real program does not need it. The code on the screen needs no import lines, so nothing was left out. This lab covers only that the operating system gives each process separate memory on one computer. It does not cover how processes pass values to each other, what it costs to create a process, or what happens to other processes when one process dies. These are covered in the in-depth lab "Processes and threads", and containers, which isolate processes and run them as a bundle, are covered in the in-depth lab "Containers and virtual machines". What could not be checked by opening the documentation: how the operating system decides a process number (PID), and how the operating system takes back memory when a process ends. The documentation was not opened for these, and the screen does not say anything about them.
Previous lab: Input and injection · Next lab: Calling another server
Struck-through text was removed from the first version of the code; highlighted text was added.