1 of 28

Processes

Kevin Kuehler

Live Q&A: on Piazza

2 of 28

What’s a Process?

  • Basically, an instance of a program being executed
  • Hundreds of processes running on your system at any moment
  • Every process has a unique PID

3 of 28

Basic Commands

  • ps: print running processes
    • Default: print processes in your current session
    • -e: print all processes
    • -f: print with full information
    • -H: print a tree
    • -u (--user): print a certain user’s processes
  • kill: send a signal to a process (to end it)
  • nice, ionice: process resource access
  • pgrep: search for a process by its name
  • htop: show live process stats

4 of 28

Process IO

  • Every process on a Unix system initialized with three open file handles:
  • Often referred to as streams
  • stdin (fd 0): where a process looks for input
  • stdout (fd 1): where a process can write output
  • stderr (fd 2): where a process can write errors

5 of 28

Review: Process IO Examples

  • $ echo “stdout is the terminal”
  • $ echo “stdout is the terminal” >&1
  • $ echo “writing to stderr, also the terminal!” >&2
  • $ ./program.sh 1>outfile 2>errfile
  • stdin, new way to use the cat command?
    • $ cat < somefile.txt

6 of 28

Pipes Re-imagined: IPC

  • Remember pipes from lecture 2?
  • Now that we know about processes, we can think of pipes as a form of interprocess communication
  • $ ps -ef | grep vim
  • stdout of ps cmd becomes stdin for grep cmd

7 of 28

The Tree of Life

  • Every process has a parent process
  • Initially, the parent of a process is the process which launched it
  • Process 1 is at the very top of the tree

8 of 28

supernova Process Tree (feat. Shicheng)

9 of 28

Signals

  • A means of sending messages to other processes
  • SIGTERM (15): tell a process to exit now
  • SIGKILL (9): kill a process without giving it the chance to clean up
  • SIGINT (2): the signal sent when you press Ctrl+C
  • SIGHUP (1): the signal sent when the user closes the terminal window
  • SIGSTOP (varies; 19 on GNU/Linux): suspend a process
  • SIGCONT (varies; 18 on GNU/Linux): resume a process
  • The signals SIGKILL and SIGSTOP cannot be handled
  • Can send using the kill, pkill, and killall commands�e.g. kill -STOP 3276, kill -9 7283

10 of 28

Orphans!

Orphan Processes

  • Usually the result of a of the parent process crashing or being terminated by a signal like SIGKILL that doesn’t give the process time to clean up
  • The orphan child process is “adopted” by the init process (PID 1)
  • Called re-parenting

11 of 28

Process Priority

  • How much resources and time the CPU allocate for a process
    • Determined by nice value on a scale from -20 to 19
    • Set the niceness value for a new process:
    • $ nice -n -20 ./sync_porn.sh & >/dev/null
    • Set niceness value for existing process: renice -n -5 -p 30000
  • Set priority for IO (disk usage)
    • ionice, like nice, but for disk resources

12 of 28

Background Processes (Primitive Daemons)

  • Noninteractive processes
  • Started on the shell by appending an ‘&’ to the command
  • Give me an example of a long running command
    • rsync
  • $ rsync -ahz rsync://anime.girls.moe/waifus /home/keur/trash &
  • We’ll try with something simpler...

13 of 28

yes, background me senpai

  • Everyone open a terminal
  • Run the command yes
  • Review: How do we stop this command; what signal to we send it?
    • Ctrl+C, which sends SIGINT
  • Let’s background it: yes &
  • Why is our terminal still getting spammed?!?! That doesn’t seem like a background process...
  • Let’s investigate...

14 of 28

yes, background me senpai

  • keur@supernova $ ps -fu

keur 30903 0.6 0.0 41468 5760 pts/4 Ss 01:58 0:00 -zsh

keur 30938 99.4 0.0 5840 704 pts/4 RN 01:59 1:28 \_ yes

keur 31013 0.0 0.0 29868 1552 pts/4 R+ 02:00 0:00 \_ ps -fu

  • One quirk: stdout and stderr inherited from parent shell, only stdin detached
  • keur@supernova $ yes > ~/yesfile.out &
  • This is the behavior we’d expect of a background job, or is it…?

15 of 28

Unix Job Control

  • Ahh, time to leave my job running in the background and relax...
  • keur@supernova $ logout

zsh: you have running jobs.

  • WTF?!?!! (╯°□°)╯︵ ┻━┻
  • keur@supernova $ jobs

[1] + running yes > ~/yesfile.out

  • Run disown, removes this from shell’s job control
  • Now we can logout and leave our job running

16 of 28

Unix Job Control

  • What would happen if we force logged out without disowning?
    • Shell would send SIGHUP, killing its children
  • This seems like a lot of work… is there a tool for this?
  • Introducing nohup
  • $ nohup sleep 1000 &
    • Redirects stdout and stderr to file nohup.out
    • Process handles SIGHUP (hence the name)

17 of 28

More Job Control

  • Let’s see the power of job control
  • $ ping -c5 ocf.berkeley.edu
  • Cntl+Z to suspend the process
  • bg to have the process resume in the background
  • fg to bring this to foreground
  • You will get to play around with this more in lab!

18 of 28

Some Theory: Process Creation on Unix

  • Two important system calls
  • fork(): create child process that is a copy of current task, differing only in its PID (some other minor differences that we won’t get into)
  • exec(): load new executable in memory, replacing the current process image
  • These two commands work in tandem to create new processes on Unix

19 of 28

Some Theory: Process Creation on Unix

20 of 28

Some Theory: Process Creation

keur 30916 0.0 0.0 4040 732 pts/17 S 17:06 0:00 \_ ./a.out

keur 30921 0.0 0.0 4288 720 pts/17 S 17:06 0:00 \_ [sh]

keur 30924 0.0 0.0 28196 1632 pts/17 R+ 17:06 0:00 \_ ps -fu

21 of 28

What’s a Daemon?

  • Noninteractive background process
  • Etymology: daemon < Gk. δαίμων “spirit”
  • Examples:
    • crond: schedule commands to run at regular times
    • sshd: run a command once at specific time
    • Apache2: http server waiting for incoming http connections
    • And more
  • Daemon names usually end in d

22 of 28

Cron

  • A daemon that lets you run tasks at scheduled times.
    • Tasks are defined in crontabs.
  • Example crontab entry:
    • 26 4 */2 * * /opt/bin/find_aliens.sh

    • asterisk (*) = always

Minute (0 - 59)

Hour

(0 - 23)

Day of month

(1 - 31)

Month

(1 - 12)

Day of week

(0 - 6), Sunday = 0, 7

23 of 28

Editing Cron jobs

  • Use the crontab command
    • The command allows you to have per-user crontabs
    • Use crontab -e to edit
    • Use crontab -l to show
    • The -u option lets you operate on another user’s crontab if you’re root
  • System wide crontab in /etc/crontab

24 of 28

Cron is hard!

That’s okay! Just visit https://crontab.guru

25 of 28

But crontab.guru is easy!

26 of 28

Init System

  • A daemon which manages other daemons
  • First process to be launched, has PID 1
    • Started by the kernel using a hardcoded filename
    • If initialization daemon fails, a kernel panic ensues :(
  • Functions:
    • Start daemons
    • Stop daemons
    • Coordinate system startup and shutdown
    • Monitor status of daemons
    • Restart daemons if they crash

27 of 28

Examples of Init Systems

  • sysvinit (also called “init”)
    • Developed for proprietary System V Unix systems by AT&T in the 1980s
    • Largely replaced by systemd in 2015
  • Upstart (discontinued)
  • systemd
    • Default init system on all major GNU/Linux distros except Gentoo
    • Had controversial beginnings because people hate change… and Lennart Poettering
      • Monolith that breaks the *nix philosophy of “small, sharp tools”
  • OpenRC
    • Popular alternative in anti-systemd circles
  • runit
  • s6
  • GNU Shepherd

28 of 28

Process Monitoring

  • Essential job of a sysadmin
  • Many tools for this:
    • ps, top and htop
    • iotop: show processes using the most I/O
    • iftop: show processes generating the most network traffic
    • vmstat: show statistics about memory
    • iostat: show statistics about I/O