6. Processes#

Each time you run a command or a program in Unix it starts a new process. This process has a unique ID which can be used to control and manage the process. There are several Unix commands to help you do this.

6.1. Exercise#

Use wget to download the following file to your work directory.

http://www.mathsbox.com/introduction-to-unix/matrix.exp.R

6.2. Exercise#

Have a look at the code in the file matrix.exp.R. Can you work out how you could run this R code from the shell ?

Can you work out what the code does ?

How does the execution time depend on the first parameter to the program ?

6.3. Process IDs#

6.3.1. top#

6.3.2. Exercise#

Start another terminal (so that you now have access to two terminals). In the first terminal run the matrix.exp.R code setting the first parameter to 2000 and the second to 10.

While matrix.exp.R is running type the following in the second terminal

top

What do you see ?

Note - to stop the top command running use Ctrl-C

The PID column displayed by top command is the ID of the listed process. You can use this to control the process. In most cases, this means stopping it !!

6.3.3. kill#

In Unix, stopping a process is usually referred to as killing it !! To kill a process, use the kill command with the -9 option (the -9 option means kill it - no questions asked !!).

kill -9 <PID>

6.3.4. Exercise#

Make sure you have the matrix.exp.R code running.

Find its PID.

kill it !!

Check it is dead.

6.4. Background jobs#

The runtime of the matrix.exp.R code can become quite long depending on the value of the first two parameters. To allow the shell to continue operating after the matrix.exp.R process is started the process can be run in the background. This is done by adding an & at the end of the command.

6.4.1. Exercise#

Run the matrix.exp.R code in the background.

What is the PID for your process ?

6.4.2. ps#

The ps command can be used to find out information about running processes.

6.4.3. Exercise#

Run the matrix.exp.R code in the background Now run

ps -u <username>

where is your username.

6.5. Batch processing#

Being able to run jobs in the background allows for a basic form of parallel processing. You can run many (independent) “jobs” at the same time and the Unix system will (where possible) assign each process to a different CPU (core,processor). The easiest way to do this is to write a shell script that starts each “job” in the background from within a loop. You should now have all of the necessary knowledge to do just this !!

6.5.1. Exercise#

Write a shell script that runs the matrix.exp.R code multiple times for different values of the first parameter. Each process should run in the background.

How will you store the results of the above process for future use ?