3 Process Control

1 minute read

Published:

ps axf     # display process tree
ps axfj    # with more info
ps axfjww  # even if lines wrap around terminal

Processes

  • Orphan: parents terminate before children
    • init adopts it and periodically calls waitpid()
  • Zombie: child terminated, but parent never calls waitpid()
    • It will show in the process tree as <defunct>

Memory mappings

When a process fork()s, its mappings are duplicated for the child

Copy-On-Write:

  • Even pieces in stack/text/code are shared and marked as read only
  • Once when memory is written, OS splits it to two
  • Delays duplication of memory space.
  • Efficient since fork() is typically followed by exec(), which will trash the memory anyway
  • exec() loads a new program code into memory by mapping its executable file to the text region of the address space

Process groups job control

proc1 | proc2

proc1 and proc2 share the same group ID (PGID)

proc1 | proc2 &    # & sends pipeline to background
# [1] 7106         # [<job number>] <leading PID>
proc3 | proc4 | proc5

All share the same session ID (SID)

alt

If you send a signal, it’s delivered to the foreground process group

  • jobs: List all jobs
  • Ctrl-Z: Suspend (SIGSTOP) foreground job and send to the background
  • bg <job>: Resume <job> in the background
  • fg <job>: Bring backgrouded <job> into the foreground