Unix - Process in UNIX
1. What is a Process in UNIX?
-
A process is a program in execution.
-
When you run a command (e.g.,
ls
,cat file.txt
, or even a script), UNIX creates a process for it. -
Each process is an independent entity with:
-
Its own process ID (PID)
-
Its own memory space (code, data, stack)
-
Its own execution state (running, waiting, stopped, terminated)
-
2. Process States in UNIX
A process can be in different states:
-
Running – currently executing on CPU.
-
Ready – waiting in the queue to use CPU.
-
Sleeping (Waiting) – waiting for an event (like I/O).
-
Stopped – suspended (e.g., by Ctrl+Z).
-
Zombie – finished execution, but parent hasn’t collected exit status.
Example: Check process states with ps -aux
:
ps -aux | head -5
Output:
USER PID %CPU %MEM STAT COMMAND
root 1 0.0 0.1 Ss /sbin/init
user 123 0.1 0.3 S bash
user 456 1.0 2.1 R firefox
user 789 0.0 0.0 Z [vim] <defunct>
Here, S
= sleeping, R
= running, Z
= zombie.
3. Process Hierarchy in UNIX
-
UNIX processes form a tree structure.
-
The first process is
init
(orsystemd
in modern Linux), PID = 1. -
All other processes are children of
init
. -
Each process has:
-
PID – unique identifier
-
PPID – parent process ID
-
Example:
ps -ef | head -5
UID PID PPID CMD
root 1 0 /sbin/init
user 234 1 bash
user 567 234 vim file.txt
Here, bash
is a child of init
, and vim
is a child of bash
.
4. Creating and Managing Processes
(a) Process Creation
-
fork()
system call creates a new process (child). -
Child is an almost exact copy of the parent.
(b) Executing Programs
-
exec()
replaces the process image with a new program.
(c) Termination
-
exit()
ends a process. -
Parent collects child’s exit status with
wait()
.
5. Foreground and Background Processes
-
By default, a process runs in the foreground (blocking the shell).
-
Add
&
to run in background:
sleep 60 &
This runs sleep
in the background.
Check running jobs:
jobs
Bring a background job to foreground:
fg %1
6. Important Process Commands
Command | Purpose |
---|---|
ps |
Show process status |
top |
Real-time process info (CPU/memory) |
jobs |
List jobs started in current shell |
fg / bg |
Move job to foreground/background |
kill PID |
Kill a process by PID |
kill -9 PID |
Force kill a process |
nice / renice |
Change process priority |
pstree |
Show process tree |
7. Example: Process Management
# Run program in background
gedit &
# Find its PID
ps -ef | grep gedit
# Kill the process
kill -9 <PID>
8. Diagram – Process in UNIX
+--------------------+
| init (PID 1) |
+--------------------+
|
------------------------------------------
| | |
+-------------+ +-------------+ +-------------+
| bash (PID) | | sshd (PID) | | cron (PID) |
+-------------+ +-------------+ +-------------+
|
+-------------+
| vim (PID) |
+-------------+
Summary
-
A process is a running program with its own PID, memory, and state.
-
UNIX processes are organized in a tree, starting from
init
/systemd
. -
Key actions: creation (fork), execution (exec), termination (exit), waiting (wait).
-
Processes can run in foreground or background.
-
Tools:
ps
,top
,jobs
,kill
,nice
,pstree
.