MS-373
Advanced Unix
Lab 3

Date:    Thursday, December 15, 2005
Due:     Thursday, January 5, 2006

Prelude

This lab is meant to familiarize the student with the basics of process control.  If you were in my MS-371 class you probably are already familiar with process control from the command line.  In this case consider this lab to be a review.  In either case, complete the tasks in the next section on process control and then complete the sed task at the end of this lab.  As usual, be sure to script your work and produce a readable HTML report to contain both the questions below and your answers.  Be sure to email the completed work to blessing@msoe.edu by the above due date.

Process Control

Read Chapter 4 in the textbook on controlling processes under Unix/Linux and answer the following questions by capturing the output from a command window on your system.  Use the online manual and info pages as well as your textbook in formulating your answers.

Suppose that a user has started a long-running process that is consuming significant amounts of CPU time and I/O resources.

  1. How would you notice a process that is "hogging" system resources?
  2. Assume that this misbehaving process is legitimate. How would you temporarily put it "on ice" while you investigate what it's up to?
  3. Later, you discover that the process belongs to your boss and must run to completion. How would you allow this process to resume "hogging" resources?
  4. Even later, your boss tells you it's okay for you to terminate the process. What signal should you send it to all it to terminate gracefully? What is the command line to issue such a command.  How would you guarantee that the process will be terminated regardless of the state it is in?
  5. How could your boss establish a lower priority for a process started from the command line? What would the command line look like?
  6. How would your boss go about adjusting the priority of an already executing process from the command line?

Introduction to sed

sed is a non-interactive stream editor which is used for text. The command to invoke sed is:

	sed [-n] [-e command] [-f edfile] [input_file]

For example:

%  sed "s/UNIX/Unix/g" thesis > thesis.new

This will process the file thesis line by line, outputting each line to the file thesis.new and replacing each occurrence of the string "UNIX" with "Unix".

In the above example every line of thesis will be output to thesis.new, irrespective of whether it has been changed or not. This is because the default output for sed is every line of the input. Using the -n option supresses the default output, and only specified lines are output. In the above example, the following command would mean that no lines would be output (regardless of how many substitutions are made):

%  sed -n "s/UNIX/Unix/g" thesis > thesis.new

In othe words, substitutions, but no output has been specified by the above command. If a print command is added, as follows:

%  sed  -n  "s/UNIX/Unix/gp"  thesis  >  thesis.new

then only those lines in which "UNIX" had been changed to "Unix" would be output.  It is quite common to use the -n command option in combination with the p (print) editor command to output only those lines changed by the edit action (in this case, a substitution).

As you also see in the example, the -e option is not necessary when there is only one editor command. It is possible to specify more than one edit command, and in this case each must be preceded by -e. For example:

%  sed  -e  "s/a/A/"  -e  "s/b/B/"  file1  >  file2

This command will carry out the two substitutions on each line of file1, sending the output to file2.

The -f option enables the user to use a file containing editor commands, instead of typing out a series of commands with the -e option.   The equivalent command to carry out the two substitutions above would be:

%  sed  -f editcommands  file1  >  file2

where the file named editcommands contains:

s/a/A/
s/b/B/

sed examples

The sed command to list only files (exclude directories) is shown below.  It uses a regular expression within the double quotes to only print lines that start with a dash (-).

%  ls  -l  |  sed  -n  "/^-/p"
-rw------- 1 lnp5jb 1765 mbox
-rw------- 1 lnp5jb 320 example1

The sed command to extract a list of usernames from the password file is:

%  sed  "s/:.*//"  /etc/passwd  |  more

What this does is to remove everything from the output that comes after the first ':' from each line in the password file.


Exercise

1. Reproduce the effects of the above sed examples using grep instead. Note that grep is generally better for searches, such as this, while sed can be used to make changes to files.