Linux/C2/Working-with-Linux-Process/English

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of script: Working with Linux Processes

Author : Anirban Roy Choudhury

Keywords: Process, shell process, PID, PPID, spawning, ps, parent, child, subshell, system process, init

Resources for the tutorial

Visual Cue
Narration
Display Slide 1 Welcome to this spoken tutorial on working with Linux Processes.
Display Slide 2 I am using Ubuntu 10.04. We assume that you know how to get started with the Linux operating system and have some basic idea about commands. If you are interested, it is available through another spoken tutorial, on the following website http://spoken-tutorial.org/ http://spoken-tutorial.org].
Also note that Linux is case sensitive. All the commands used in this tutorial are in lower case unless otherwise mentioned.
Display Slide 3


To understand what a process is let me give you an brief explanation.
Anything that is running in Linux is a process.
The shell that is running and taking our commands is a process.
The commands that we type on the terminal are processes when they are running.
The video in which you are seeing this tutorial is a process.
The browser running in which you have opened the spoken tutorial website is a process.
Shell scripts that are running are processes and so on.
Display Slide 4


A process can be defined as a program which is being executed i.e. which is running.
Processes are much like us. They are born, they die. They have parent and children.
Display Slide 5 Let us first learn about the shell process.
The shell process is started by the Linux kernel as soon as we login to the system.
At this point it would suffice to know that the Linux kernel is the core of the Linux operating system.
It consists of the most essential components that makes Linux run. The shell creates or gives birth to all the other user command processes.
Open a terminal Let us open a terminal.
point mouse to </$ sign We can see the command prompt on the terminal in the form of a dollar sign.
At the command prompt type “date”

press enter

This is the job of the shell process. Now let us type any command, say “date” and press enter.



As soon as we do this the shell process will create a process called date.
Now since the shell process has given birth to the date process, we can say that the shell process is the parent of the date process and the date process is the child of the shell process.
Once date process has shown the system date and time, it will die.
Display Slide 6 A shell can also give birth to another shell process. Giving birth to a process or creating a process is also called spawning a process.
At the command prompt type

“sh” press enter

To spawn another shell process, simply go to the terminal and type “sh” and press enter.
We would see a new prompt appearing in the terminal. Now our original shell , let us call it shell 1, has given birth to a child shell or sub shell,let us call it shell 2.
Now you may run a command in the new command prompt as well. Let us run ls command in this new command prompt.
At the new command prompt type “ls”

press enter

Now at the command prompt type “ls” and press enter.

We can see a list of files and directories.


Now a new process named ls is created.

Here, shell 2 is the parent of ls, while shell 1 is the grandparent of ls. ls is the child of shell 2, while shell 2 itself is the child of shell1
At the new prompt type “exit”

press enter


To kill shell 2 simply type at the new prompt “exit” and press enter

This will kill shell 2 and we will get back our original command prompt.

Display Slide 7


Continuing our analogy between us and processes ,we know that each of us have some attributes that identify us. Those attributes can be our name, parents' name , date of birth, PAN card no. etc.
Similarly processes also have attributes like PID(Process ID), PPID(Parent Process ID), Start time, etc. Most of these attributes are maintained by the kernel in a process table.
Each process is uniquely identified by a unique integer called the PID. PID is allotted by the kernel when the process is born.
The PID of the parent process that spawned a new process say P1 is called the PPID of P1 process.
At the command prompt type

“echo $$”

press enter

point mouse to PID number

To see the PID of the current shell at the prompt type

“echo space dollar dollar” and press enter

A number would be shown . This is the PID of the current shell.

Display Slide 8 The one command that we would use a lot while talking about processes is the ps command. ps or process status is a command which displays the processes running in the system.
Let us see what happens if we run this command without any options.
At the command prompt type

“ps”

press enter

Now at the command prompt and type “ps” and press enter

This way we would normally see the list of all the processes owned by the user running the program.

hover mouse over CMD column]hover mouse over PID column

hover mouse over TTY column ]hover mouse over TIME column

You can see the name of the process under CMD heading.

Other than this you can a see the PID, TTY or the console at which the process is running ,the TIME i.e. the total processor time that has been used since the process started



point mouse to PID of bash and output of "echo $$" command On my machine it displays two processes. One is bash, the shell process we are using. The other is the ps process itself.

The other important thing to note here is that the PID of the shell process is same as that shown by the echo space dollar dollar command.

At the command prompt type

“sh” press enter

If we spawn a subshell , let us see what happens.

At the terminal type “sh” and press enter.

At the new command prompt type“ps”

press enter

Now in the new prompt that appears on the new line, type “ps” and press enter.



Now we can see 3 processes in the list . The process sh has been added.

Here again notice that the PID of the bash process is same as that before.

ps comes with many options as we would see next. First option that we would see displays more attributes for the processes listed.
At the new command prompt type “ps -f”

press enter

Now type at the prompt “ps space minus f” and press enter

This again will list three processes as in the previous case. Bash, sh and ps -f.

point your mouse to UID, PPID and PID fields


The only difference is now more attributes are listed.

UID gives the user name of the user who has started the process. Also it shows the PPID that is the PID of the parent process that created the process.

point your mouse to the PID of bash and PPID of sh For example see that bash process is the parent of sh process , so the PID of bash is same as the PPID of sh process.
point your mouse to PID of sh and PPID of ps -f


Similarly since sh process is the parent of ps, the PID of sh process is the same as the PPID of the ps -f process. C stands for processor utilization. Currently, this is the integer value of the percent usage of the processor over the lifetime of the process. It will be displayed as 0 in

this case as the usage is negligible.

point your mouse to STIME The STIME field gives the time at which the process started, the rest we have already seen while running ps.
Display slide 9


Processes are of two types : First one is User processes. Those are started by the users. For example 'ps' or for that reason most commands that we run on the terminal.
Second one is system processes. Those processes are started by the system often during system startup or user login. Example of system process can be bash.
Display Slide 10 Sometimes we may want to see all the processes - both system processes as well as user processes. Then we use the -e narration - minus e] or the - capital A option.
At the new command prompt type “ps -e”

press enter

Go to the terminal and type at the prompt “ps space minus e” and press enter. We can see a large list of processes.
At the new command prompt type “ps -e | more”

press enter

To get a multipage display just type at the prompt

“ps space minus e space vertical bar space more” and press enter.

As we have seen before more will cause only as many processes to be listed as can fit in one window. As we press enter we scroll through the list of processes.
The first process in this list is interesting. It is called the init process. It is the process from which almost all other processes are spawned. It has a PID of 1.
Press "q" To come back to the prompt press q
Display slide 11 So, in this tutorial, we have learned about process, shell process, spawning of process, process attributes and different types of processes. We also learned the use of ps command. This brings me to the end of this tutorial.
Display slide 12 Spoken Tutorials is a part of the Talk to a Teacher project, supported by the National Mission on Education through ICT, MHRD, Government of India. More information on the same is available at the following link

http://spoken-tutorial.org/NMEICT-Intro. This is Prashant from IIT Bombay signing off. Thanks for joining.

Contributors and Content Editors

Pravin1389