CSC 140: Introduction to IT Processes CIT 140: Introduction to IT
CIT 140: Introduction to IT Topics What is a process? Process states and multitasking How does the shell execute processes? Foreground and background processes Job control Process tree Process commands CIT 140: Introduction to IT
CIT 140: Introduction to IT A process is a program in execution. A process is created every time you run an external command and is removed after the command finishes its execution. CIT 140: Introduction to IT
CIT 140: Introduction to IT Multitasking Only one process is using a CPU at a time. A process uses the CPU until: It makes an I/O request, or It reaches the end of its time slice, or It is terminated. The technique used to choose the process that gets to use the CPU next is called Process scheduling. CIT 140: Introduction to IT
CIT 140: Introduction to IT Process State As a process executes, it changes state new: The process is being created. running: Instructions are being executed. waiting: The process is waiting for some event to occur. ready: The process is waiting to be assigned to a CPU. terminated: The process has finished execution. zombie: Process has been terminated and is waiting for its resources to be recovered. CIT 140: Introduction to IT
CIT 140: Introduction to IT Process States A UNIX process can be in one of many states, typically ready, running, or waiting. CIT 140: Introduction to IT
CIT 140: Introduction to IT Process Scheduling First come, First serve(FCFS) Process that enters system first is assigned highest priority. Priority Scheduling Assign priorities based on accumulated CPU time. New and I/O bound processes have highest priorities. Round Robin (RR) A process gets to use the CPU for one time slice, then the CPU is given to another process, the next process in the queue of processes waiting to use the CPU. CIT 140: Introduction to IT
Execution of shell Commands A shell command can be external or internal. Internal (built-in) command: is part of the shell: ex: bg, cd, continue, echo, exec External command: separate program or script ex: grep, more, cat, mkdir, rmdir, ls CIT 140: Introduction to IT
Execution of shell Commands A UNIX process is created by the fork() system call, which creates an exact copy of the original process. The forking process is known as the parent process. The created (forked) process is called the child process. CIT 140: Introduction to IT
Execution of shell Commands To execute an external command, the exec() system call is used after fork. exec() replaces the current process in memory with the specified file and begins running it. CIT 140: Introduction to IT
Execution of shell Commands CIT 140: Introduction to IT
Execution of Shell Scripts Shell script: a series of shell commands in a file Execution different from binary programs. Current shell creates a child shell and lets the child shell execute commands in the shell script, one by one. Child shell creates a child process for every command it executes. While the child shell is executing commands in the script file, the parent shell waits for the child to terminate, after which it comes out of waiting state and resumes execution. Only purpose of child shell is to execute commands and eof means no more commands. CIT 140: Introduction to IT
Execution of shell Commands CIT 140: Introduction to IT
CIT 140: Introduction to IT Process Attributes Processes have a variety of attributes: User owner Group owner Process name Process ID (PID) Parent process ID (PPID) Process state Length of time running Amount of memory used CIT 140: Introduction to IT
CIT 140: Introduction to IT Process Status ps [options] (System V version) Purpose Report process status Output Attributes of process running on the system Commonly used options/features: -a Display Information about the processes executing on your terminal except the session header (your login shell) -e Display information about all the processes running on the system -f Display full list (8 columns) of status report. -l Display long list (14 columns) of status report. -u uidlist Display information about processes belonging to the users with UIDs in the ‘uidlist’ (UIDs separated by commas) CIT 140: Introduction to IT
CIT 140: Introduction to IT Examples of ps > ps PID TTY TIME CMD 24621 pts/13 0:00 bash 24740 pts/13 0:00 ps > ps -f UID PID PPID C STIME TTY TIME CMD waldenj 24621 24615 0 14:51:55 pts/13 0:00 -bash waldenj 24745 24621 0 14:55:06 pts/13 0:00 /usr/bin/ps -f > ps -l F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 8 S 1249 24621 24615 0 50 20 ? 331 ? pts/13 0:00 bash 8 O 1249 24746 24621 0 50 20 ? 123 pts/13 0:00 ps > ps -ef|head root 0 0 0 Jun 16 ? 0:01 sched root 1 0 0 Jun 16 ? 1:39 /etc/init - root 2 0 0 Jun 16 ? 0:01 pageout root 3 0 1 Jun 16 ? 970:56 fsflush root 332 1 0 Jun 16 ? 0:00 /usr/lib/saf/sac -t 300 root 312 1 0 Jun 16 ? 0:00 /usr/lib/snmp/snmpdx -y -c /etc/snmp/conf root 325 1 0 Jun 16 ? 0:05 /usr/sbin/vold root 50 1 0 Jun 16 ? 0:00 /usr/lib/sysevent/syseventd root 60 1 0 Jun 16 ? 0:01 /usr/lib/picl/picld CIT 140: Introduction to IT
Process and Job control Shell manages your processes for you: Process creation Process termination Suspending processes Running foreground processes. Running background processes. Switching processes to foreground/background. CIT 140: Introduction to IT
Example of Background Processes > find / -name foo 2>/dev/null & [1] 25299 > bzip2 bash.man & [2] 25304 > ps PID TTY TIME CMD 24621 pts/13 0:00 bash 25299 pts/13 0:03 find 25305 pts/13 0:00 ps > [2]+ Done bzip2 bash.man CIT 140: Introduction to IT
CIT 140: Introduction to IT Suspending a Process What if you forgot to background a process? Suspend the process with <Ctrl-z> Use fg or bg to unsuspend process. Use stop to suspend background processes. If you don’t have a stop command, use alias stop=“kill –STOP” Example CIT 140: Introduction to IT
Managing Background Processes: fg fg[%jobid] Purpose: Resume execution of the process with job number ‘jobid’ in the foreground or move background processes to the foreground. Commonly used values for ‘%jobid’ % or %+ Curent job %- Previous job %N Job Number N %Name Job beginning with ‘Name’ CIT 140: Introduction to IT
Managing Background Processes: bg bg[%jobid-list] Purpose: Resume execution of suspended processes/jobs with job numbers in ‘jobid-list’ in the background. Commonly used values for ‘%jobid’: % or %+ Curent job %- Previous job %N Job Number N %Name Job beginning with ‘Name’ %?Name Command containing ‘Name’ CIT 140: Introduction to IT
CIT 140: Introduction to IT UNIX Daemons A daemon is a system process running in the background. Most system services are daemons: E-mail Printing Ssh Web CIT 140: Introduction to IT
Sequential and Parallel Execution cmd1; cmd2; … ; cmdN Purpose: Execute the ‘cmd1’, ‘cmd2’, ‘cmd3’,…,’cmdN’ commands in sequence. cmd1 & cmd2 & … & cmdN & Purpose: Execute commands ‘cmd1’,cmd2’,…’cmdN’ in parallel as separate processes. CIT 140: Introduction to IT
CIT 140: Introduction to IT Terminating a Process Can terminate a foreground process by <Ctrl-C> Can terminate a background process: Find the PID with ps, then use kill PID. Bring process into foreground with fg, then use <Ctrl-C> CIT 140: Introduction to IT
CIT 140: Introduction to IT Terminating a Process > find / -name foo 2>/dev/null >foo.txt & [4] 28344 > sort bigFile | bzip2 -c >sortFile.bz2 & [5] 28350 > jobs [2]+ Stopped vim smallFile [3] Running sleep 10000 & [4] Running find / -name foo 2>/dev/null >foo.txt & [5]- Running sort bigFile | bzip2 -c >sortFile.bz2 & > kill %5 [5]- Terminated sort bigFile | bzip2 -c >sortFile.bz2 > ps PID TT S TIME COMMAND 28022 pts/4 S 0:00 -bash 28132 pts/4 T 0:00 vim smallFile 28137 pts/4 S 0:00 sleep 10000 28344 pts/4 R 0:05 find / -name foo > kill 28344 [4]- Terminated find / -name foo 2>/dev/null >foo.txt CIT 140: Introduction to IT
CIT 140: Introduction to IT Job Numbers vs PIDs PIDs Every process has a PID. PIDs are global (shared by every user.) Job Numbers Every job has a job number. Jobs may contain multiple processes. Job numbers are local (every user has own %1...) CIT 140: Introduction to IT
UNIX Process Hierarchy System starts init process on boot. Init is PID 1. Init starts system processes System daemons. Graphical login: xdm, kdm, gdm Text login: getty -> login Network login: sshd Login runs User shell CIT 140: Introduction to IT
Process Hierarchy in UNIX CIT 140: Introduction to IT