CS 497C - Introduction to UNIX Lecture 26: - The Process Chin-Chih Chang chang@cs.twsu.edu Understanding the Process * A process is defined as an instance of a running program. * A process lives as long as the program is running. * The kernel makes sure that processes get their due time slice and are swapped out to disk as and when needed. * A process is identified by the process identifier (PID). Understanding the Process * A process has the following attributes: * The real user-id of the user who created the process. The user is said to be the owner of the process. * The real group-id of the owner process. Both user-id and group-id of the owner are stored in /etc/passwd. * The priority with which it runs. * The current directory from where the process was run. * A process is said to be born when the program starts execution. Understanding the Process * After execution is complete, the process is said to die. * Every process has a parent identified by the parent process identifier (PPID) except the ancestry of every process - the first process (PID 0) which is set up when the system is booted. * A process can spawn (generate) multiple children. How a Process Is Created * Every process comprises code and data. * The process image as viewed by the kernel runs in its own user address space. This address space has a number of segments: - Text Segment - This contains the executable code of a program. - Data Segment - All variables and arrays the program uses are held here. - User Segment - This contains all the process attributes. How a Process Is Created * There are three distinct phases in the creation of a process, using three important system calls - fork(), exec() and wait(): - Fork - A process is created by the parent with the fork system call, which creates a copy of itself. - Exec - The parent then overwrites the copy with the exec system call to create the child. - Wait - The parent then executes the wait system call to keep waiting for the child process to complete. How a Process Is Created * When a process is forked, it inherits the environment of its parent but changes in the child are not made available in the parent. * Built-in shell command like pwd and cd don't fork a separate process. * The shell also spawns a separate sub-shell to execute a shell script. The Login Shell: The First User Process * When you log on to a UNIX system, the shell process is immediately set up by the kernel. This program may be sh (Bourne shell), csh (C Shell), ksh (Korn shell), or bash (Bourne again shell). * The shell maintains a set of variables that are available to the user. The PID of the login shell is stored in the variable $$. * It is the parent of all commands and scripts run from the shell. The init Process * The parent of all login shells is the init process with PID 1. * init forks and execs a getty process which prints the login prompt and goes to sleep. * When a user tries to log in, getty wakes up and execs the login program to verify the login name and password. * On most systems, login forks and execs a shell. Internal and External Commands * The shell recognizes three types of commands: * External commands - These are the most commonly used utilities and programs such as cat, ls, and so forth. * Shell scripts - The shell executes these scripts by spawning another shell, which then executes the commands listed in the script. * Internal commands - The shell has a number of built-in commands such as cd and echo which don't generate a process. ps: Process Status * The ps command is used to list the attributes of processes. * By default, ps displays the processes associated with a user at the terminal: * ps is a highly variant command. Its actual output depends on the version of UNIX as well as the hardware used. * BSD and System V are two main brands of the ps command. ps: Process Status * Linux uses the BSD version of the ps command. ps in Linux uses two types of options - the BSD options that don't use a dash and the GNU-style options which use --- (2 hyphens). * The -e option shows all process including user and system process. * The -f (full) option gets a detailed listing showing the the parent of a process. ps: Process Status * The -u (user) option displays processes of a user. ps -f -u root * The -a (all) option displays all processes run by the user. * Linux uses the ax option to display system processes. * The ps u command approximates the ps -f. Running Jobs in Background * A multitasking system lets a user do more than one job at a time. A job can be run in the foreground and the background. * A job can be run in the background by affixing an & at the end of the command line or the nohup command. * nohup ensures that a background job remains alive even after the user has logged out. Running Jobs in Background * The & is the shell's operator used to run a process in the background. sort -o list list &; ps -f * C shell and bash shell allows you to log out without aborting the job. * The nohup (no hangup) command prefixing a command permits execution of the process even after the user logged out. nohup sort list &