CSI110

 

Lecture 4

 

Process Control

and Management (3)

Reference for this lecture

http://www.afro.caribbean.co.uk/unix-unleashed/unx18.htm

Pipelines

• The fork/exec model allows pipelines to be created, which is a powerful feature of UNIX

• A pipeline consists of a series of commands with the output of one command forming the input to the next

• In a pipeline, UNIX redirects I/O so that the standard output from one command is connected to the standard input of the next

• Type ps -ef | grep <your user id>

 

What is grep?

• grep searches a file for a pattern

• grep pattern [file_list]

• By default, grep prints matching lines in its input

• Like most UNIX commands, if a file_list is not specified, grep takes its input from stdin

• When grep is used in a pipeline as in ps -ef | grep <your used id>, and if grep is not given a file_list, then the output of ps is used as the input to grep

• The output from the pipeline will be those lines from ps -ef that contain <your user id>

• E.g.,

babe% ps -ef | grep csta1

csta1 14249 14247 0 09:48:33 pts/7 0:00 -csh

csta1 15731 14249 0 15:05:44 pts/7 0:00 grep csta1

csta1 14303 14301 0 09:55:42 pts/9 0:00 -csh

• We can't see the ps process (because it's owned by root), so type ps to find out your terminal (TTY) number, and type ps -ef | grep "<your tty> "

• E.g.,

babe% ps -ef | grep "pts/7 "

csta1 14249 14247 0 09:48:33 pts/7 0:00 -csh

csta1 15784 14249 0 15:16:36 pts/7 0:00 grep pts/7

root 15783 14249 1 15:16:36 pts/7 0:00 ps -ef

• The following steps were followed in order to get this output:

1. fork (1). The login shell (PID 14249) forks a new process (15783) to execute the pipeline. This subprocess redirects standard output to create a pipe. (14249) waits for the pipeline to complete.

2. fork (2). (15783) forks a new process (15784) to help execute the pipeline. (15784) connects its standard input to the pipe that its parent (15783) is using for output.

3. exec (1). (15783) executes ps

4. exec (2). (15784) executes grep

Examining the status of processes

• ps -l gives long information about processes. Use it in conjunction with -e to get details of all processes

• You can pipe the output through more to see a screenful at a time.

babe% ps -el | more

F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD

19 T 0 0 0 0 0 SY ? 0 ? 0:03 sched

8 S 0 1 0 0 41 20 ? 93 ? ? 1:07 init

19 S 0 2 0 0 0 SY ? 0 ? ? 0:04 pageout

...

F: flags (obsolete)

UID: effective user id (numerical, except for -f)

PRI: priority (higher numbers = lower priority)

S: status

T: The process is stopped

R: The process is runnable

O: The process is running

S: The process is sleeping

Z: Zombie process (parent died)

(You can check the meaning of the other entries in the man pages for ps)