Friday 29 November 2013

Unix System Process Management

What is Process?

When you run a program(command) on your UNIX system , the system creates a special environment for it. This environment contains everything needed for the system to run as if no other programs running on the system the program.

Every time you run a command the system will initiate a process.In other words a process is a basic unit of execution in UNIX (and many other operating systems).

If, for example, three people are running the same program simultaneously, there are three processes there, not just one. In fact, we might have more than one process running even with only person executing the program, because (you will see later) the program can “split into two,” making two processes out of one.

Keep in mind that all Unix commands, e.g. cc and mail, are programs, and thus contribute processes to the system when they are running. If 10 users are running mail right now, that will be 10 processes. At any given time, a typical Unix system will have many active processes, some of which were set up when the machine was first powered up

Overciew:

The operating system process followed by an identification number known as the five-digit identifier or pid . Each process in the system has a unique PID.

PID can be repeated , because all possible numbers have been exhausted and the next pid rolls or back . At any given time , there are no two processes with the same PID in the system.

Finally each process  has its own parent process (PPID) from where its running.There will be there case where multiple processes will be initiated for a UNIX programme.

A process under unix consists of an address space and a set of data structures in the kernel to keep track of that process. The address space is a section of memory that contains the code to execute as well as the process stack.

The kernel must keep track of the following data for each process on the system:
  • the address space map,
  • the current status of the process,
  • the execution priority of the process,
  • the resource usage of the process,
  • the current signal mask,
  • the owner of the process.
  • How to start a Process:
When ever you run a command the system will start a process.There are two ways to  start a process depending upon  the visibility of the process.
  • Foreground Process
  • Background Process

Foreground processes:

By default, all processes will start running in the background . It takes its input from the keyboard , and transmits its output to the screen.

Eg: if you run  ls command  on directory /home/um  , the system will list the contents of the directory "home/um" ,

$ ls /home/um
 check.txt  pink.doc umdir1 umdir2 umdir3

Even if a program is running in the foreground and take some time , its means you can't run the commands on the same terminal as it is waiting for the process to complete which  gives prompt.

Background processes :

Suppose we want to execute a command but do not want to wait for its completion, i.e. we want to be able to issue other commands in the mean time. We can do this by specifying that the command be executed in
the background.

In order to run a command process as  background , we can do by appending an ampersand (‘&’) to the end of the command.

For example, suppose we have a very large program, which will take a long time to compile. We could give the command
sh syssnap.sh

which will execute the compilation while allowing me to submit other commands for execution while the compile is running.

If you press the Enter key now, you see the following:
[1]   +   Done                 sh syssnap.sh &
$
This will be tells us the end of the background process.

The advantage of running a background process is that it can  run other commands, you do not have to wait until the end to start another one!

Listing of  processes :

Its is easy to list the process  by using the command ps.

user097@sshell ~ $ ps                                                           
  PID TTY          TIME CMD
31314 pts/4    00:00:00 bash
31401 pts/4    00:00:00 ps

if you want  elaborate information about processes use option "-f"  with 'ps'.

user097@sshell ~ $ ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
user097  30929     1  0 08:36 ?        00:00:00 /usr/local/bin/anytermd -p 2397
user097  31314 30929  0 08:37 pts/4    00:00:00 /bin/bash
user097  31511 31314  0 08:37 pts/4    00:00:00 ps -ef
user097@sshell ~ $  

Here is the description of all the fields displayed by ps -f command:
ColumnDescription
UIDUser ID that this process belongs to (the person running it).
PIDProcess ID.
PPIDParent process ID (the ID of the process that started it).
CCPU utilization of process.
STIMEProcess start time.
TTYTerminal type associated with the process
TIMECPU time taken by the process.
CMDThe command that started this process.
There are other options which can be used along with ps command:
OptionDescription
-aShows information about all users
-xShows information about processes without terminals.
-uShows additional information like -f option.
-eDisplay extended information.

Ending/Stopping a processes:

Ending a process can be done in several different ways. Often, from a console-based command, sending a CTRL + C keystroke (the default interrupt character) will exit the command. This works when process is running in foreground mode.

If a process is running in background mode then first you would need to get its Job ID using ps command and after that you can use kill command to kill the process as follows:

user097@sshell ~ $ ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
user097  30929     1  0 08:36 ?        00:00:00 /usr/local/bin/anytermd -p 2397
user097  31314 30929  0 08:37 pts/4    00:00:00 /bin/bash
user097  31511 31314  0 08:37 pts/4    00:00:00 ps -ef

$kill 30929
Terminated
Here kill command would terminate first_one process. If a process ignores a regular kill command, you can use kill -9 followed by the process ID as follows:

$kill -9 30929
Terminated

Parent and Child Processes:

Each unix process has two ID numbers assigned to it: Process ID (pid) and Parent process ID (ppid). Each user process in the system has a parent process.

Most of the commands that you run have the shell as their parent. Check ps -f example where this command listed both process ID and parent process ID.

Zombie and Orphan Processes:

Normally, when a child process is killed, the parent process is told via a SIGCHLD signal. Then the parent can do some other task or restart a new child as needed. However, sometimes the parent process is killed before its child is killed. In this case, the "parent of all processes," init process, becomes the new PPID (parent process ID). Sometime these processes are called orphan process.

When a process is killed, a ps listing may still show the process with a Z state. This is a zombie, or defunct, process. The process is dead and not being used. These processes are different from orphan processes.They are the processes that has completed execution but still has an entry in the process table.

Daemon Processes:

Daemons are system-related background processes that often run with the permissions of root and services requests from other processes.

A daemon process has no controlling terminal. It cannot open /dev/tty. If you do a "ps -ef" and look at the tty field, all daemons will have a ? for the tty.

More clearly, a daemon is just a process that runs in the background, usually waiting for something to happen that it is capable of working with, like a printer daemon is waiting for print commands.

0 blogger-disqus:

Post a Comment