LL7 – Process Management

Required Reading

The Linux Command Line by Shotts – 1.10, 1.7 (for the sleep command)

Review previous material as necessary

Submission

As in the previous assignments, you will take periodic screenshots as directed below, paste the screenshots into a single .pdf document and submit this document in BB. You should also maximize your terminal screen as much as possible and preface each screenshot with the exercise number as listed in the tutorial.  As before you are responsible for researching these commands in further detail (minimally reread chapter 2.4 before beginning).

Now in this lab we will investigate processes… I’ll lead you through this but be careful not to stray from the directions as you could arbitrarily kill important processes like your shell … 🙂

Here is a nice quick supplemental presentation of process control: http://linuxcommand.org/lc3_lts0100.php

 

Intro ScreenShot

Open or launch your SSH application (e.g. Mac Terminal or Win PuTTy). Please resize your Terminal to make it larger to capture as much as possible but note you may not capture everything and this will be fine. If possible just take a screenshot/picture of your open terminal as if you include other items the text in the Terminal can be very small making it harder for me to see your commands. Paste this screenshot into your correctly named pdf document and label or title this screenshot “Intro Screenshot”.

 

7.0 Processes Introduction

First, recall our OS presentation as the commands we issue will be working with the scheduler and accessing the process table.  In the past we were working with files so we were working with the File Manager.

Recall in Linux there are files and processes.  A process is an executing program identified by a unique PID (process identifier). To see information about your processes, with their associated PID and status, use the ps command that is short for – report process status so please try the following

$ ps #this will show processes associated with your user id (uid)

Upon performing the ps you probably saw that only your shell (bash) and the ps command were running.

Now read the ps man page and then enter the following to see the difference:

$ ps -A #this will show all processes running on the system

Now this was overwhelming so lets use our skills and  look at it one screenful at a time using a pipe (if you do not remember how to exit less you will need to return to previous LLs and this is a good indication you are not putting in the requisite reading/research).

$ ps -A | less

But now we’re a budding sys admin and we wonder why our system/network is running slow so let’s look at running  browser processes

$ ps -A | grep chrome | less

Ok, nothing there since we no longer offer a graphical user interface with Acadnx so please see if ssh is running.

 BTW – another useful command is top.  I recommend you research and try it.

 

7.1 Foreground, background and suspended processes

A process may be in the foreground (default), in the background, or be suspended. In general the shell does not return the Linux prompt until the current foreground process has finished executing.  Some processes take a long time to run and hold up the terminal prompt…  Backgrounding a long process or one that requires user input (e.g. gedit) has the effect that the Linux prompt is returned immediately, and other tasks can be carried out while the original process continues executing.

Background processes

To background a process, type an & at the end of the command line. For example, the command sleep  waits a given number of seconds before continuing. Type the following:

$ sleep 10

This will execute/wait 10 seconds before returning the command prompt $. Until the command prompt is returned, you can do nothing except wait.

Now to observe the effect of running sleep in the background, type

$ sleep 10 & 

You will see the OS returns the job number in square brackets and the process id (pid) to you in the form of [1] 2950.  (Note that programmers could use this pid to coordinate activities.) Sometime later when you enter your next command or simply [enter] you will be notified the job completed.  Essentially the & runs the job in the background (multi-tasking) and returns the prompt straight away, allowing you do run other programs without waiting for that one to finish. Backgrounding is useful for jobs which will take a long time to complete.

Now if you are quick enough you can run sleep in the background and then perform a ps to see the sleep process actually running in the background.

Suspending and backgrounding a current foreground process (i.e. one that is running in the foreground at the outset)

At the prompt, type

$ sleep 100

You can suspend the process running in the foreground by holding down the [control] key and typing [z] (this is a lower case z but is written as ^Z). Then to put this suspended process in the background, type

$ bg

Note: do not background programs that require user interaction (e.g. command line interaction like pine and you should look that up if you are unfamiliar).

Exercise 7.1: Create a backgrounded sleep process where the process will sleep 100 seconds.  Perform a report process status.  Take a screenshot immediately and 100+ seconds later hit enter to ensure you capture the process termination and take another snapshot.  Paste both of these screenshots into your document.

Please clear your screen after taking your screenshot and before continuing.

 

7.2 Listing suspended and background processes

We’ve already seen this however when a process is running, backgrounded or suspended, it will be entered onto a list along with a job number (process table entries). To examine this list, create a background sleep 200 and then contrast (get familiar with) the ps and jobs commands by typing:

$ ps

$ jobs # the job number will be enclosed in square brackets []

To restart (foreground) a suspended processes, we use the fg command.  fg by itself will restart and return the last suspended process to the foreground.  fg and the job number retrieved from the jobs command will restart and return the specific job to the foreground.

Exercise 7.2:  Create a foreground (not backgrounded) sleep command.  Use ^Z to suspend the process.  Perform a jobs command to retrieve the suspended sleep command.  Return the sleep process to the foreground with the fg command and the job number.  Take a screenshot and paste this into your submission file (and of course wait for the sleep to expire so that you can move on).

Please clear your screen after taking your screenshot and before continuing.

 

7.3 Killing a process 

kill (terminate a process):  It is sometimes necessary to kill a process (for example, when an executing program is in an infinite loop (see CISS 110 content and infinite loops).  To kill a job running in the foreground, type control-c (This is represented as ^C) so as an example try:

$ sleep 100
^C

To kill a suspended or background process we enter kill followed by the job number. For example, run the following and kill the backgrounded sleep process using the pid

$ sleep 200 &

$ ps

$ kill pid # substitute pid of sleep pid returned by ps

Note – if a process refuses to be killed, use the -9 option, i.e. type $ kill -9 xxxx

Note: It is not possible to kill off other users’ processes unless you are root.

Exercise 7.3:  Create a backgrounded sleep 500 process, run a ps, kill the sleep 500 process using ps output, run a ps and then take a screenshot, paste it into the assignment file and submit the file.

Please clear your screen after taking your screenshot and before continuing.

skill & pstree commands

For completeness, please research the skill and pstree commands but we will not apply them here.

CTRL-C

Lastly note that ctrl-c (holding down the ctrl and c keys at the same time) should terminate the last command if it continues to run.

Summary

Ok, this was a short lab so this is a good opportunity to review the Linux commands covered to date as you will need a working knowledge of them for subsequent labs and the Final Project.

Linux Lab 7 Commands

CommandDescription
^Ckill the job running in the foreground
^Zsuspend the job running in the foreground
bgbackground the suspended job
command &run command in background
fg $1foreground job number 1
jobslist current jobs
kill 26152kill process number 26152
kill $1kill job number 1
pslist current processes
sleep makes a process sleep/wait a number of seconds
top
skill
pstree

Leave a Reply