To become familiar with the multitasking capabilities of UNIX.
|
Corning Community College UNIX/Linux Fundamentals |
To become familiar with the multitasking capabilities of UNIX.
Information provided in the book(s) on the ps utility will not be valid on Linux systems, as they focus on the SystemV-style of UNIX, where most Linux systems use the BSD-style. So be sure to consult the actual manual pages on Lab46.
In "UNIX for the Beginning Mage", please read:
In "Harley Hahn's Guide to UNIX and Linux", please read:
In "Learning the UNIX Operating System, 5th Edition", please read:
UNIX is a multitasking, multiuser operating system.
So far in this course you have dealt with the multiuser aspect of the system (through the who(1) utility from Lab #0 and with the write(1), talk(1), and ytalk(1) utilities in this week's UNIX Messaging Utilities Case Study). Through this usage you have seen that many users can be on the system simultaneously, each doing individual tasks.
So what is multitasking? Multitasking is the ability of the system's processor to allocate cycles of CPU time efficiently enough in order to give the appearance that many processes are running simultaneously.
What is a process? A process is the term given to a running UNIX application (ie a "program in action").
UNIX provides a mechanism for viewing these processes running in memory. The top(1) and ps(1) utilities allow you to view the process list.
Terminology - instead of saying "list of running programs", you would say "the process list". You can manipulate the process, as it is a unique instance of a real program on the system.
In a way, the multiuser aspect of UNIX already proves it is multitasking, as each user is doing their own task independent of any other user.
However, each user can multitask their own session. This ability to run and manage more than one process within your login terminal is known as Job Control.
More Terminology:
Process ID (PID)- a unique number assigned to each running
process on the system.
Foreground process- a process that is running in the foreground (currently using STDIN/STDOUT/STDERR).
Background process- a process that is running independently in the background, allowing you to use your shell (which is in your foreground) or start additional applications.
killing a process- in UNIX, you can terminate processes with the kill(1) utility.
Viewing Processes:
The ps(1) is used to list the running processes
on the system. Running with no arguments will show the current
processes attached to your login session.
| | List your current processes with ps. How did you do this (Provide the output)? |
| | If you were logged onto the system multiple times, would your invocation of ps(1) also show any processes being run in your other shells? |
| | How would you instruct ps(1) to give you this information? |
ps can also be used to list all the processes on the system.
| | Using ps(1), list all processes on the system in user-oriented format. Tell me how you did it. |
| | What PID is the inetd daemon? Include the entire matching line from ps |
| | Run top(1), and check it out for a while. Any observations? What are the most active processes? (q to quit) |
Running in the background:
When used by itself following a command on the command-line, the &
will instruct the shell to run the command in the background.
For example:
lab46:~$ head -n4 /etc/motd > file1 &Will take the first 4 lines of /etc/motd and redirect it to file1. This will run in the background, and will give the PID of the process. This particular command-line should complete quickly.
| | What type of file is this? How did you determine this? |
| | Follow the instructions inside on how to compile it to create an executable. |
| | Did it work? How do you run it? |
| | What could you do to make your $PATH see this file? Explain. |
NOTE: If you have problems, there is an existing binary (called count) located in the same place you found count.c.
Now let us compare the benefits of using the & to background processes:
| | Run: time ./count > output and wait for it to finish. What is in "output"? How long did it take? |
| | Do the same, but this time add an & to the end. Anything change? Get a process listing with top, does it show up? What is its CPU utilization? |
The command separator is another useful feature, represented as a semi-colon (;). You can use this to string commands together to execute one after the other.
For example:
lab46:~$ ls -l; cat /etc/motd
Will execute ls with the -l argument, then once it is complete, will go and cat out the contents of /etc/motd. Each command is run separately, and therefore has its own unique PID.
Finally, the parenthesis come in particular use. If you enclose an entire command-line in parenthesis (excluding the &, as seen on page 132 in Learning the UNIX Operating System, 5th Edition) will cause the shell to treat that sequence of commands as one unique command, and give ALL commands involved just one unique PID!
Task 1
Devise a command-line to do the following:
Confused? Be sure to ask questions.
| | Show me the command-line incantation you came up with, and explain your reasoning. |
Job Control: Suspending a foreground process
Not only can you background processes, but you can suspend and manipulate foreground processes. By using the SUSPEND character, you can cause most applications running in the foreground to stop, giving you back control of you prompt.
When at the prompt using the bourne shell, you can use the built-in command jobs to list any suspended jobs that can be manipulated. If you have any, they will be preceded by a job number, allowing you to foreground (fg) or background (bg) that job.
Task 2
Set up the following situation:
So far we've put some things in the background, and if we're lucky, we'll just have gotten a message from the shell.
| | Try logging out by hitting CTRL-D or typing "logout". |
| | Are you able to immediately log out? |
| | What happens? |
| | What might be the purpose behind this action? |
| | What number is the links job? |
| | Type fg # to foreground it. Did it work? Suspend it again. |
| | Is links still listed in the jobs list? |
| | Foreground the cat job and tell me how you did it. |
| | With cat running in the foreground, end it with the end of file character. |
| | Is cat still listed in the jobs list? |
| | Foreground and end links (you may have to use the interrupt character) |
| | Is the jobs list empty? |
Once the jobs list is empty, you should be able to logout again without a problem. Test this then log back in again to continue on with the lab.
Killing Processes:
Finally, we are going to determine how to terminate processes on the system using the kill(1) utility.
The kill utility works on a series of signals that applications use to communicate with the operating system. The most common signals you will encounter are the Hangup (HUP), Kill (KILL), and Terminate (TERM) signals.
Hangup is one of the lowest level signals. It instructs the process to re-run, often used to get programs to re-read configuration files.
Kill is among the most powerful of the signals as far as normal processes go. When used on a running process, it basically means "kill process with no mercy" -- instant and immediate termination.
Terminate is the default signal, when none are supplied. It informs the process that it is to terminate, allowing the process to clean up and take care of any matters it needs to attend to before it exits.
kill understands a series of signals, but for the purposes of this class we will only be dealing with the above 3 on a regular basis.
| | What number is the Hangup signal? |
| | What number is the Terminate signal? |
| | What number is the Kill signal? |
| | What number is the Interrupt signal? |
| | Log in again, so you have two instances of your user logged into the system. Are both users listed in a who(1) output? Show me your output. |
| | Using the tty(1) utility, determine the terminal device of both login sessions. What are they? |
| | Using the x argument to ps, determine the PID of both your login shells. What are they? (Associate PID with terminal device). |
Now that we've viewed the current situation, let's change it up a bit:
| | Using kill, terminate one of your login sessions. |
| | Which one did you kill and how did you do it? |
| | Any messages generated & what ones? |
| | Using who, are you still logged in twice? |
Process management is an important feature of any multitasking operating system. Having a handle on how it works not only helps you to better understand how to use UNIX, but to better manage your resources on a system.
Putting these new skills together with those learned last week, we can start to make the system work for us:
| | All the processes on the system that are owned by user rwhod. |
| | The PID of the init process. Anything interesting about this process? |
| | Who owns the cron process and what is its PID? What purpose does cron(1) serve on the system? |
HINT: pipes are good. And be sure to list your incantations.
All questions in this assignment require an action or response. Please organize your answers into an easily readable format and be prepared to submit the final results to your instructor.
Your assignment is expected to be performed and submitted in a clear and organized fashion- messy or unorganized assignments may have points deducted. Be sure to adhere to the submission policy.
When complete, electronically submit your assignment using the "Assignment Submitter", located here:
As always, the class mailing list is available for assistance, but not answers.