Unix Process Management:
When you execute a program on your UNIX system, the system creates a special environment for that program. This environment contains everything needed for the system to run the program as if no other program were running on the system.
The operating system tracks processes through a five digit ID number known as the pid or process ID. Each process in the system has a unique pid.
Pids eventually repeat because all the possible numbers are used up and the next pid rolls or starts over. At any one time, no two processes with the same pid exist in the system because it is the pid that UNIX uses to track each process.
Starting a Process:
When you start a process (run a command), there are two ways you can run it:
- Foreground Processes
- Background Processes
Foreground Processes:
By default, every process that you start runs in the foreground. It gets its input from the keyboard and sends its output to the screen.
Example:
cat /etc/passwd | grep ^root
While a program is running in foreground and taking much time, we cannot run any other commands (start any other processes) because prompt would not be available until program finishes its processing and comes out.
Background Processes:
A background process runs without being connected to your keyboard. If the background process requires any keyboard input, it waits.
The advantage of running a process in the background is that you can run other commands; you do not have to wait until it completes to start another!
The simplest way to start a background process is to add an ampersand ( &) at the end of the command.
firefox &
Listing Running Processes:
It is easy to see your own processes by running the ps (process status) command as follows:
[senthil]$ps PID TTY TIME CMD 18358 pts0 00:00:00 sh 18361 pts0 00:01:31 vi test 18789 pts0 00:00:00 ps |
One of the most commonly used flags for ps is the -f ( f for full) option, which provides more information as shown in the following example:
[senthil]$ps -f UID PID PPID C STIME TTY TIME CMD senthil 6738 3662 0 10:23:03 pts/6 0:00 first_one senthil 6739 3662 0 10:22:54 pts/6 0:00 second_one senthil 3662 3657 0 08:10:53 pts/6 0:00 -ksh senthil 6892 3662 4 10:51:50 pts/6 0:00 ps -f |
Here is the description of all the fileds displayed by ps -f command:
Column | Description |
UID | User ID that this process belongs to (the person running it). |
PID | Process ID. |
PPID | Parent process ID (the ID of the process that started it). |
C | CPU utilization of process. |
STIME | Process start time. |
TTY | Terminal type associated with the process |
TIME | CPU time taken by the process. |
CMD | The command that started this process. |
There are other options which can be used along with ps command:
Option | Description |
-a | Shows information about all users |
-x | Shows information about processes without terminals. |
-u | Shows additional information like -f option. |
-e | Display extended information. |
Stopping 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 pscommand and after that you can use kill command to kill the process as follows:
[senthil]$ps -f UID PID PPID C STIME TTY TIME CMD senthil 6738 3662 0 10:23:03 pts/6 0:00 first_one senthil 6739 3662 0 10:22:54 pts/6 0:00 second_one senthil 3662 3657 0 08:10:53 pts/6 0:00 -ksh senthil 6892 3662 4 10:51:50 pts/6 0:00 ps -f [senthil]$kill 6738 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:
[senthil]$kill -9 6738 Terminated |
kill command sends signal.
To list the signals:
kill –l
trap –l
For example:
signal 1 --- NOHUP
signal 2 --- ctrl+c
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 commad 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," initprocess, 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.
If you have a program which needs to do long processing then it’s worth to make it a daemon and run it in background.
Job ID versus Process ID:
Background and suspended processes are usually manipulated via job number (job ID). This number is different from the process ID and is used because it is shorter.
In addition, a job can consist of multiple processes running in series or at the same time, in parallel, so using the job ID is easier than tracking the individual processes.
To list the running and stopped jobs:
jobs
Change the background process to a foreground:
jobs
fg ID%
Change the foreground process to background:
Ctrl + Z --- stop the process
jobs
bg id%
Nice and Renice:
Priority value
The nice value is -19 to +20
Default Nice Value is 0
To change the running process nice value:
renice -2 PID
To run a process with new nice value:
nice -1 sh backup.sh
Other commands relate to the process:
top --- to list top processess
topas (Only AIX)
Senthilkumar Muthusamy
No comments:
Post a Comment