Process in Unix
Process in Unix
2
• A process is a program in execution in memory or in other words, an
instance of a program in memory. Any program executed creates a
process. A program can be a command, a shell script, or any binary
executable or any application. However, not all commands end up in
creating process, there are some exceptions. Similar to how a file
created has properties associated with it, a process also has lots of
properties associated to it.
3
• A program/command when executed, a special instance is provided
by the system to the process. This instance consists of all the
services/resources that may be utilized by the process under
execution.
• Whenever a command is issued in unix/linux, it creates/starts a new
process. For example, pwd when issued which is used to list the
current directory location the user is in, a process starts.
• The operating system tracks processes through a five-digit ID number
known as the pid or the process ID. Each process in the system has a
unique pid.
4
• Through a 5 digit ID number unix/linux keeps account of the
processes, this number is call process id or pid. Each process in the
system has a unique pid.
• Used up pid’s can be used in again for a newer process since all the
possible combinations are used.
• At any point of time, no two processes with the same pid exist in the
system because it is the pid that Unix uses to track each process.
5
Types of Processes
• Parent and Child process : The 2nd and 3rd column of the ps –f
command shows process id and parent’s process id number. For each
user process there’s a parent process in the system, with most of the
commands having shell as their parent.
• Zombie and Orphan process : After completing its execution a child
process is terminated or killed and SIGCHLD updates the parent
process about the termination and thus can continue the task
assigned to it. But at times when the parent process is killed before
the termination of the child process, the child processes becomes
orphan processes, with the parent of all processes “init” process,
becomes their new ppid.
6
• A process which is killed but still shows its entry in the process status
or the process table is called a zombie process, they are dead and are
not used.
• Daemon process : They are system-related background processes
that often run with the permissions of root and services requests
from other processes, they most of the time run in the background
and wait for processes it can work along with for ex print daemon.
When ps –ef is executed, the process with ? in the tty field are
daemon processes
• cron is a daemon which runs on UNIX systems.
7
zombie
• When a program forks and the child finishes before the parent, the
kernel still keeps some of its information about the child in case the
parent might need it - for example, the parent may need to check the
child's exit status. To be able to get this information, the parent calls
`wait()'; In the interval between the child terminating and the parent
calling `wait()', the child is said to be a `zombie' (If you do `ps', the
child will have a `Z' in its status field to indicate this.)
8
Process attributes:
A process has some properties associated to it:
PID : Process-Id. Every process created in Unix/Linux has an
identification number associated to it which is called the process-id.
This process id is used by the kernel to identify the process similar to
how the inode number is used for file identification. The PID is unique
for a process at any given point of time. However, it gets recycled.
PPID : Parent Process Id: Every process has to be created by some other
process. The process which creates a process is the parent process, and
the process being created is the child process. The PID of the parent
process is called the parent process id(PPID).
9
• TTY: Terminal to which the process is associated to. Every command is
run from a terminal which is associated to the process. However, not
all processes are associated to a terminal. There are some processes
which do not belong to any terminal. These are called daemons.
• UID: User Id- The user to whom the process belongs to. And the user
who is the owner of the process can only kill the process(Of course,
root user can kill any process). When a process tries to access files,
the accessibility depends on the permissions the process owner has
on those files.
10
Process Management Commands
• Fg
• Top
• PS
• Kill
• NICE
• DF
11
Command Description
bg To send a process to the background
fg To run a stopped process in the foreground
top Details on all Active Processes
ps Give the status of processes running for a user
ps PID Gives the status of a particular process
pidof Gives the Process ID (PID) of a process
kill PID Kills a process
nice Starts a process with a given priority
renice Changes priority of an already running process
df Gives free hard disk space on your system
free Gives free RAM on your system
12
Process States
• There are two categories of processes in Unix, namely
• User processes: They are operated in user mode.
• Kernel processes: They are operated in kernel mode.
• As a process executes it changes state according to its circumstances.
Unix processes have the following states:
Running : The process is either running or it is ready to run .
Waiting : The process is waiting for an event or for a resource.
Stopped : The process has been stopped, usually by receiving a signal.
Zombie : The process is dead but have not been removed from the
process table.
13
14
• Created-Process is newly created by system call, is not ready to run
• User running-Process is running in user mode which means it is a user
process.
• Kernel Running-Indicates process is a kernel process running in kernel
mode.
• Zombie- Process does not exist/ is terminated.
• Pre-empted- When process runs from kernel to user mode, it is said
to be preempted.
15
• Ready to run in memory- It indicated that process has reached a state
where it is ready to run in memory and is waiting for kernel to
schedule it.
• Ready to run, swapped– Process is ready to run but no empty main
memory is present
• Sleep, swapped- Process has been swapped to secondary storage
and is at a blocked state.
• Asleep in memory- Process is in memory(not swapped to secondary
storage) but is in blocked state.
16
Process Transitions
• The working of Process is explained in following steps:
• User-running: Process is in user-running.
• Kernel-running: Process is allocated to kernel and hence, is in kernel
mode.
• Ready to run in memory: Further, after processing in main memory
process is rescheduled to the Kernel. The process is not executing but
is ready to run as soon as the kernel schedules it.
• Asleep in memory: Process is sleeping but resides in main memory. It
is waiting for the task to begin.
17
• Ready to run, swapped: Process is ready to run and be swapped by
the processor into main memory, thereby allowing kernel to schedule
it for execution.
• Sleep, Swapped: Process is in sleep state in secondary memory,
making space for execution of other processes in main memory. It
may resume once the task is fulfilled.
• Pre-empted: Kernel preempts an on-going process for allocation of
another process, while the first process is moving from kernel to user
mode.
18
• Created: Process is newly created but not running. This is the start
state for all processes.
• Zombie: Process has been executed thoroughly and exit call has been
enabled. The process, thereby, no longer exists. But, it stores a
statistical record for the process.
19