Learning Linux: Linux Processes

Learning Linux: Linux Processes

These notes cover the basics of processes, signals and their control keys and some concept on processes.

  • add & at the end of a command to run the process in the background
  • Process IDs referee to processes in the entire system.
  • Job numbers refers to processes running in the background of the current shell.
    • Job numbers are simple (ex. 1, 2, 3…), which Process IDs are not.
  • When jobs completes with 0 exit status Done, along with the job number and command.
  • Jobs completing with a non-zero exit status prints Exit followed by the exit status number, instead of Done.

 

  • fg command brings a job into foreground; meaning, you can interact with it and have no access to the shell command line.
    • No arguments with fg, brings most recent job to foreground. Same as fg %+ (plus means the most recent job).
    • The next/second most recent job is assigned , which you can get with fg %-.
    • When you get the list of jobs with jobs command you will see the + and – after job number for the most and second most recent jobs.
  • To bring specific jobs to foreground:
    • fg %command_name  – brings the most recent process that uses command_name.
    • fg %job_number – brings the specified job_number.
    • fg process_ID – brings the specified process_ID. (**Note: process ID has no % before it)
    • fg %?string – You can type the first part of a command and the rest will be predicted. For example, if command name is python you can type fg %? pyt.
  • bg command sends process to the background.

 

  • jobs command lists all jobs currently running.
    • -p prints only process ID.
    • -l prints with process ID

Signals

NAME         CTRL KEY         MEANING

INT                CTRL-C                interrupt
TSTP             CTRL-Z                 terminal stop; you can resume again
QUIT             CTRL-\                 quit
KILL              –                             terminates the process without cleaning up

  • Use CTRL-C first, if it doesn’t work use CTRL-\, then if that fails too use KILL signal. However, KILL is not linked to a control key, so it cannot be used on processes that are running.

 

  • stty – sets signal names to control keys.
    • syntax: stty signal_name ^key, where key a key on the keyboard and signal_name is not always the same as the signal name.
    • stty -a displays all the signals assigned to control keys.
    • changing signal keys is not recommended.

 

  • kill  command sends specific signals to a process.
    • by default sends TERM signal to a process.
    • TERM and QUIT give process time to clean up, while KILL does not.
    • kill uses the same syntax as fg to select process by ID, job number or command.
  • killall – same as kill but sends signal to all processes with the name given by the first argument.

 

  • ps – displays information about processes running on the system.
    • ps with no options only gives processes list for the current terminal.
    • ps -a shows processes running on all the terminals.
    • ps output example:
username:~$ ps
  PID TTY          TIME CMD
 4673 pts/0    00:00:00 bash
 4698 pts/0    00:00:00 ps
  • “Zombies” or “Orphans” are processes that have information about what shell they came from missing.
    • “Zombies” are hidden with ps -a so you have to use ps -e (In the list, column TTY shows “?” for zombies).
    • daemons are “zombie” processes which handle system services.

 

  • trap command
    • trap command sig1 sig2
    • if any of the signals are received, run command, which can be a function or script.
    • trap without arguments prints lists of traps set
    • trap can be used to clean up, if user sends signal. This is similar to finally in try/except/finally in Python.
    • trap  signals resets signals defined.
  • trap functions
func () {
        trap command1 sig1
}

trap command2 sig1
func()
  • Since the signals are the same and the function is called after the trap outside the function, trap inside the function will be used. (func() redefines the previous trap)
  • $$ is a special variable “$” that contained the process ID of the current shell process.
    • Can be to create temporary files.
  • $! is another special variable “!” contains process ID of the most recent background job.

 

  • Coroutines are two programs that run together and possibly communicate. For example ls | more.
  • wait command waits for background jobs to finish.
    • With wait courotines that don’t communicate don’t keep running after the end/ they don’t become “orphans”.
  • Coroutines that don’t communicate can have a performance advantage, but could also cause thrashing, which is when processes compete for system resources.
  • Parallelization uses multiple hardware (CPU) to split processes between the CPUs, allowing the processes to run faster.
  • Subshells inherit current directory (PWD), environment variables, file descriptors (stdin, stdout and stderr) and ignored signals (An example is setting trap second argument to null string).
  • Nested Subshells are like command blocks, but with parenthesis and have properties of a subshell.
  • Process substitution:
command <(command1)
  • command is some command and with syntax <(command1) the output from command1 is set as the first argument for command. (Pretty useful!)