LL7 – Processes

Introduction

For the assignment, you will take periodic screenshots as directed below (Screenshot instructions provided in the previous assignments) and paste the screenshots into a single LastnameFirstnameLL7.odt LibreOffice Word document.  You should also preface each screenshot with the exercise number as listed in the tutorials.  When complete, please upload this file to BB as directed in the LM.

Now in this lab we will investigate processes… please don’t arbitrarily kill important processes like your shell (i.e. bash) – Smile1

7.1 Processes and Jobs

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

$ ps

A process may be in the foreground, 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 has the effect that the Linux prompt is returned immediately, and other tasks can be carried out while the original process continues executing.

Running 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.  At some point in the future you will also be informed the process completed or is “done”.

To run 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.  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 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.

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] (written as ^Z) Then to put it in the background, type

$ bg

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

Exercise 7a:

Create a backgrounded sleep process where the process will sleep 200 seconds.  Perform a report process status.  Take a screenshot and paste it into your .odt document.

7.2 Listing suspended and background processes

When a process is running, backgrounded or suspended, it will be entered onto a list along with a job number. To examine this list, create a background sleep 200 and then contrast (get familiar with) ps and jobs commands by typing:

$ ps

$ jobs

An example of a job list could be

[1] Suspended sleep 100
[2] Running netscape
[3] Running pine

To restart (foreground) a suspended processes, type

$ fg $jobnumber

For example, to restart my sleep 200 if I suspended it  I would type

$ fg $1

Note – Typing fg with no job number foregrounds the last suspended process so this would also work but we need to learn about absolute control.

7.3 Killing a process using Jobs and Process numbers

kill (terminate or signal 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)

To kill a job running in the foreground, type ^c (control c) so as an example try:

$ sleep 100
^C

To kill a suspended or background process, type

$ kill $jobnumber

For example, run

$ sleep 100 &
$ jobs

If it is job number 4, type

$ kill $4

To check whether this has worked, examine the job list again to see if the process has been removed.

Using ps and a pid to kill a process

Alternatively, processes can be killed by finding their process numbers (PIDs) and using kill pid as in the following example

$ sleep 100 &
$ ps

PID TT S TIME COMMAND
20077 pts/5 S 0:05 sleep 100
21563 pts/5 T 0:00 netscape
21873 pts/5 S 0:25 pine

To kill off the process sleep 100, type

$ kill 20077

and then type ps again to see if it has been removed from the list.

If a process refuses to be killed, uses the -9 option, i.e. type

$ kill -9 20077

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

Exercise 7b

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 .odt file and submit the file.

Summary

 

command & run command in background
^C kill the job running in the foreground
^Z suspend the job running in the foreground
bg background the suspended job
jobs list current jobs
fg $1 foreground job number 1
kill $1 kill job number 1
ps list current processes
kill 26152 kill process number 26152

Leave a Reply