105 Process states and Management
Here’s a complete guide on Process Management – Process States, focused on FAANG-level interview preparation, with clear explanations, real-world examples, and 20 high-quality Q&A to test and reinforce your understanding.
π§ 1. Key Topics to Cover (Process Management & Process States)
Category | Topics |
---|---|
Process Basics | PID, PPID, UID, GID, nice, renice |
Process Lifecycle | Creation, Execution, Termination |
Process States (Linux/Unix) | Running , Waiting , Stopped , Zombie , Sleeping , Dead |
State Transitions | How and when process moves between states |
Signals | kill , SIGKILL , SIGTERM , SIGSTOP , SIGCONT , etc. |
Parent-Child Relationship | Orphan and Zombie processes |
Foreground/Background Jobs | fg , bg , jobs , & , nohup |
Process Scheduling | nice , renice , priority (niceness level), time slicing |
Troubleshooting Tools | ps , top , htop , pstree , strace , lsof , kill , nice , renice , watch |
π¦ 2. Process States in Linux (FAANG-focused)
State | Description |
---|---|
R (Running) |
Actively executing on CPU |
S (Sleeping) |
Waiting for I/O (interruptible sleep) |
D (Uninterruptible sleep) |
Waiting for disk/network (not killable easily) |
T (Stopped) |
Suspended (via signal like SIGSTOP) |
Z (Zombie) |
Process completed but parent didn’t call wait() |
X (Dead) |
Terminated, not seen often |
I (Idle) |
Kernel threads only, rarely shown |
π 3. FAANG-Ready Interview Questions + Answers
✅ Basic to Intermediate
Q1. What are the different states a process can be in? Explain.
Answer:
-
Running (R): Actively on CPU or ready to run.
-
Sleeping (S): Waiting for I/O; can be interrupted by signals.
-
Uninterruptible Sleep (D): Waiting on resources like disk; cannot be interrupted.
-
Stopped (T): Halted by a signal (e.g.,
SIGSTOP
). -
Zombie (Z): Terminated but not reaped by parent.
-
Dead (X): Process terminated and removed from system.
Q2. How can you identify zombie processes on Linux?
Answer:
ps aux | grep 'Z'
Or:
ps -eo pid,ppid,state,cmd | grep Z
Zombie processes show as state Z and can only be cleared if the parent process is terminated or calls wait().
Q3. What is the difference between zombie and orphan process?
Zombie | Orphan |
---|---|
Child terminated, parent alive but didn’t call wait() | Child alive, parent terminated |
Consumes PID | Reassigned to init (PID 1) |
Can’t be killed | Kernel adopts it |
Q4. How does a process move from running to waiting (sleeping)?
Answer:
A process transitions from running to sleeping when it requests I/O or is waiting for a resource (disk, network, user input). The scheduler then switches to another process until I/O is ready.
Q5. Explain the lifecycle of a Linux process.
Answer:
-
Created – via
fork()
orexec()
-
Ready – added to scheduler queue
-
Running – executes instructions
-
Waiting – for I/O or other resource
-
Terminated – exits using
exit()
-
Zombie – if not reaped by parent
✅ Advanced FAANG-Level
Q6. How do you handle a zombie process in a production server?
Answer:
-
Identify with
ps aux | grep Z
-
Check
PPID
-
Kill parent process to allow init (PID 1) to adopt and reap the zombie.
Q7. How to find which syscall a process is waiting on?
Answer:
Use strace
:
strace -p <pid>
This shows the system calls being waited on.
Q8. What does the D
state (Uninterruptible sleep) mean and why is it dangerous?
Answer:
It indicates the process is waiting for I/O and cannot be killed or interrupted (even with kill -9
). This can be a sign of hardware issues, NFS hang, or I/O lock.
Q9. How does the Linux kernel schedule processes?
Answer:
-
Linux uses the CFS (Completely Fair Scheduler).
-
It maintains a red-black tree to assign CPU time.
-
Each process gets a fair share of time based on niceness and priority.
Q10. What happens when you run kill -9 <pid>
on a zombie?
Answer:
Nothing. Zombie processes are already terminated. They can’t be killed. You need to terminate the parent process so the kernel can clean up the zombie.
✅ Real-World + Debugging
Q11. How do you debug high CPU usage from a process?
Answer:
top or htop
pidstat -p <pid> 1
strace -p <pid>
lsof -p <pid>
These tools help track what the process is doing, system calls, and open files.
Q12. How do you change the priority of a running process?
Answer:
renice -n 10 -p <pid>
Niceness range: -20 (highest priority) to 19 (lowest priority).
Q13. What’s the use of the pstree
command?
Answer:
Shows process hierarchy, useful for visualizing parent-child relationships:
pstree -p
Q14. What signal is sent by default using kill
command?
Answer:
SIGTERM (15)
– allows process to gracefully shut down.
Q15. When would you use nohup
?
Answer:
To run a process after logout/session close:
nohup ./my_script.sh &
✅ Behavioral + Situational
Q16. You found a process in D
state for hours. What do you do?
Answer:
-
Use
strace
ordmesg
to check blocked I/O. -
Check if NFS, disk, or DB is hung.
-
Consider restarting the service or rebooting if stuck.
Q17. A user reports background jobs disappearing after logout. Why?
Answer:
-
Use
nohup
ordisown
to detach processes from terminal session.
nohup script.sh &
disown
Q18. How to monitor all states of processes?
Answer:
ps -eo pid,user,state,cmd | sort
Also use top
, htop
, watch
.
Q19. What causes a process to become a zombie?
Answer:
Child process exits, but parent hasn’t called wait()
to read its exit status. Kernel keeps it to return info when parent requests.
Q20. How can you force the cleanup of multiple zombie processes?
Answer:
Find the parent with:
ps -eo ppid,pid,state,cmd | grep Z
Then kill or restart the parent to allow init to reap the child processes.
π Summary Cheat Sheet
Process Tool | Purpose |
---|---|
top / htop |
Real-time resource usage |
ps |
List process info |
strace |
Trace system calls |
lsof |
List open files/sockets |
kill / pkill / killall |
Send signals |
nice / renice |
Adjust priority |
jobs / bg / fg |
Background job control |
pstree |
Visualize process hierarchy |
No comments :
Post a Comment