questions about unix processes

Hello! Here are some questions & answers. The goal isn't to get all the questions "right". Instead, the goal is to learn something! If you find a topic you're interested in learning more about, I'd encourage you to look it up and learn more.

does every process have a PID?

yes!

a process's PID never changes while it's running, and when it exits it gets recycled so that it can be used later by another process.

is it possible to start a process without a binary executable?

no!

even if the program is in Python or Java, processes always need a binary executable (like the Python interpreter/JVM/bash) to start. You can set which binary should run a program with a #! line like this:

#!/usr/bin/python3

print("banana")
        

does every process have a parent?

yes! (except for PID 1)

every process except for the one with PID 1 has exactly 1 parent process. child processes inherit a lot of attributes from their parents.

when you run
$ ls ../file.py
in your shell, how does the OS decide what directory to interpret the path file.py relative to?

the process's working directory!

every process has a working directory, and child processes inherit their working directory from their parent.

when you sent a signal to a process (for example with kill PID), can the process stop itself from being killed?

yes, based on its signal handlers!

any process can set signal handlers functions for a given signal (like SIGINT). You can either use a default handler (like "ignore") or run a custom function. (kill -9 can't be ignored though)

when you execute a program (like cat), what directories are looked in to find it?

every directory in the PATH environment variable

lots of programs use environment variables for configuration -- for example git uses the EDITOR environment variable.

child processes inherit environment variables from their parent.

are a process's environment variables private?

not very!

you can see any process's environment variables with

$ ps eww $PID | tr '\0' '\n'
# or on Linux:
$ cat /proc/$PID/environ

if that process is running as the same user as you. This is good to be aware of if you're putting passwords in environment variables.

are a process's command line arguments private?

no!

any user can see every process's command line arguments, like this:

$ ps -F $PID
# or on Linux:
$ cat /proc/$PID/cmdline

so they're even less secret than environment variables

can two processes have the same file open at the same time?

yes!

you can run two programs which are both appending log lines to the same file.

if your process is in the middle of reading a file, how does it keep its place in the file?

the offset!

every open file has an associated offset, and when you read bytes from the file it'll advance the offset

does every process have an array of command line arguments?

yes!

when you start a process on Linux, you have to specify an array of command line arguments (though the array can be empty).

this is why every programming language has the same core interface for command line arguments (an array/list of strings).

can command line arguments be unicode?

yes!

a command line argument can be any sequence of bytes (as long as it doesn't contain the 0 byte). Technically if you wanted you could put weird binary data in a command line argument, but usually they're strings.

more reading