Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

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.

Exercise

Create a file print.args.R containing the following R code.

args <- commandArgs(trailingOnly=TRUE)
for(arg in args) print(arg)

Now run this script from the terminal using

cat print.args.R | R --no-echo --args hello world

Can you work out what is happening here ?

Exercise

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

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

Download the this file to your local directory.

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 ?

  • Redirect the ouput to a file

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

Process IDs

top

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 !!

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>

Exercise

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

Find its PID.

kill it !!

Check it is dead.

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.

Exercise

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

What is the PID for your process ?

ps

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

Exercise

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

ps -u <username>

where is your username.

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 !!

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 ?