MODULE-5 Introduction to UNIX System process: Mechanism of process creation.
Parent and
child process. The ps command with its options. Executing a command at a specified point of
time: at command. Executing a command periodically: cron command and the crontab file
Signals
✅ Processes in UNIX
a) Explain the concept of a process in UNIX. (6 marks)
● A process is an instance of a program in execution.
● It consists of:
○ Executable code
○ Memory space
○ Process ID (PID)
○ File descriptors
○ Execution context (registers, stack, heap)
● UNIX is a multi-tasking OS, meaning many processes can run concurrently.
Types of Processes:
● Foreground: Interacts with user.
● Background: Runs silently in the background.
● Daemon: Runs continuously to handle services.
b) Describe the mechanism of process creation in UNIX. (8 marks)
UNIX uses two main system calls to create and run a new process:
1. fork():
○ Creates a copy of the current (parent) process.
○ Returns:
■ 0 to the child
■ PID of child to the parent
■ -1 on failure
2. exec() family:
○ Replaces the process memory with a new program.
○ Used by child processes to run different programs than the parent.
Steps in Process Creation:
1. Parent calls fork()
2. Child process is created
3. Child may call exec() to load a new program
4. Parent may use wait() to synchronize
c) What is the difference between a parent and a child process? (6 marks)
Feature Parent Process Child Process
Origin Existing process Created via fork() by parent
PID Has its own unique PID Also has unique PID; parent gets it
Execution flow Continues after fork() Starts from same point after
fork()
Relationship Controls or monitors the child May be independent or controlled
Termination Can wait for child to complete May terminate independently
✅ Process Monitoring (ps Command)
a) What is the ps command in UNIX? What information does it display? (7
marks)
ps (process status) displays information about active processes.
Displays:
● PID – Process ID
● TTY – Terminal associated
● TIME – CPU time used
● CMD – Command that started the process
Example Output:
PID TTY TIME CMD
2021 pts/0 00:00 bash
2050 pts/0 00:00 ps
b) Explain the commonly used options of the ps command. (8 marks)
Option Description
-e or -A Lists all processes
-f Full-format listing (with PPID, UID, etc.)
-u Shows processes owned by a specific user
[user]
-x Includes processes not attached to terminals
-aux Shows all running processes with memory and CPU
usage
-o Customize output format (e.g., ps -o pid,cmd)
Example:
ps -ef # Full details of all processes
ps aux # BSD style detailed listing
ps -u student # All processes by 'student'
c) Write the command to display all processes running for the current user
with full format. (5 marks)
ps -fu $USER
● -f: full format
● -u $USER: processes of current user
✅ Job Scheduling with at Command
a) What is the at command used for in UNIX? (6 marks)
● The at command schedules a one-time execution of a command at a specified future
time.
● Unlike cron, which is repetitive, at is for single-run tasks.
Use cases:
● Schedule backups
● Send reminders
● Run reports at off-peak hours
b) How do you schedule a command to execute at 5:30 PM today using at?
(6 marks)
Command:
at 5:30 PM today
Then type the command you want to run, for example:
echo "Backup started" >> backup.log
<Ctrl+D>
This will schedule the job and return a job ID.
c) Write the steps to list, remove, and execute scheduled jobs using the at
command. (8 marks)
Task Command
Schedule job at 10:00 AM tomorrow +
<Ctrl+D>
List jobs atq or atrm -l
Remove job atrm [job number]
Execute atd service must be running
(auto)
Example Session:
$ at 9:00 AM
> echo "Reminder: Meeting at 9" >> notes.txt
> <Ctrl+D>
$ atq
3 Thu Jun 20 09:00:00 2025 a user
$ atrm 3 # Cancels job ID 3
●
Question 4
a) Explain the use of the cron daemon in UNIX. (6 marks)
b) What is a crontab file? How does it relate to cron? (7 marks)
c) Write an example entry in a crontab file to run a backup script every day at midnight. (7
marks)
Answer 4
a)
cron is a daemon that runs commands periodically at fixed times, dates, or intervals.
b)
A crontab file contains user-specific cron jobs with scheduling syntax. The cron daemon
reads these files and executes commands accordingly.
c)
Example crontab entry:
0 0 * * * /home/user/backup.sh
This runs backup.sh every day at 00:00 (midnight).
✅ Crontab and Job Scheduling
a) Explain the five fields of a crontab schedule entry. (10 marks)
A crontab entry consists of five time fields followed by the command to run:
* * * * * command-to-execute
│ │ │ │ │
│ │ │ │ └──── Day of the week (0-7; 0 or 7 = Sunday)
│ │ │ └────────── Month (1–12)
│ │ └─────────────── Day of the month (1–31)
│ └───────────────────── Hour (0–23)
└─────────────────────────── Minute (0–59)
Example:
15 8 * * 1 /home/user/script.sh
Runs script at 8:15 AM every Monday.
b) How do you edit your user’s crontab file? (5 marks)
Use the command:
crontab -e
● Opens the user’s crontab file in the default editor (e.g., vi, nano).
● You can add, remove, or modify scheduled jobs.
● Each line represents one scheduled task.
c) Write a crontab entry to run a script every Monday at 8:15 AM. (5 marks)
15 8 * * 1 /home/user/script.sh
Explanation:
● 15 → minute
● 8 → hour
● 1 → Monday (day of week)
✅ UNIX Signals
a) Define UNIX signals. What are they used for? (6 marks)
UNIX signals are software interrupts sent to a process to notify it of events like:
● Termination
● Segmentation fault
● Child process completion
● External interrupts (e.g., Ctrl+C)
Uses:
● Control or manage process behavior.
● Graceful shutdown or debugging.
b) Name and explain any four common UNIX signals. (8 marks)
Signal Number Description
SIGIN 2 Interrupt from keyboard (Ctrl+C)
T
SIGTE 15 Terminate a process gracefully
RM
SIGKI 9 Forcefully kills a process
LL
SIGHU 1 Hang-up signal; often restarts
P daemon
SIGST 19 Pauses a process (cannot be
OP ignored)
c) How can a process handle or ignore signals? (6 marks)
A process can handle or ignore signals using:
trap command in shell scripts:
trap "echo Caught SIGINT" SIGINT
1.
Signal handlers in C programs:
signal(SIGINT, my_handler);
2.
To ignore a signal in a script:
trap "" SIGINT # Ignores Ctrl+C
3.
✅ Sending Signals with kill and killall
a) How do you send a signal to a process? Explain the kill command with
examples. (7 marks)
The kill command sends a signal to a process using its PID.
Syntax:
kill -SIGNAL PID
Examples:
kill -9 1234 # Sends SIGKILL to process 1234
kill -SIGTERM 5678 # Sends SIGTERM
kill 9102 # Sends default SIGTERM
You can find the PID using ps, pgrep, or top.
b) What is the difference between kill and killall commands? (6 marks)
Feature kill killall
Target Specific PID All processes with the given name
Example kill -9 1234 killall -9 firefox
Use When you know the PID When you want to kill all of a program
Case
c) Write a command to send SIGTERM to a process with PID 1234. (7
marks)
kill -15 1234
Or using the signal name:
kill -SIGTERM 1234
● This sends SIGTERM (signal number 15), which politely requests the process to
terminate.
✅ Zombie Processes in UNIX
a) What is a zombie process? How does it occur? (7 marks)
A zombie process is a terminated child process whose exit status has not been read by its
parent.
How It Occurs:
1. A child process finishes execution and exits.
2. The process remains in the process table until the parent reads its exit status using
wait() or waitpid().
3. If the parent does not read the status, the process stays in a "zombie" (defunct) state.
Characteristics:
● Does not consume CPU/memory but occupies a PID.
● Appears with <defunct> in the process list.
b) How does UNIX handle zombie processes? (6 marks)
UNIX handles zombies through the following mechanisms:
1. Parent process is responsible for calling wait() to clean up the zombie.
2. If the parent fails or dies, the zombie is adopted by init (PID 1) or systemd, which
then performs the cleanup.
3. Properly written programs use signal handlers (SIGCHLD) to handle child termination.
System Cleans Zombies:
● If too many zombies accumulate, it may exhaust the PID space and affect system
performance.
c) What command can be used to check for zombie processes? (7 marks)
You can use any of the following:
1. Using ps:
ps aux | grep 'Z'
● STAT column shows Z for zombie.
● ps -el may show status Z and <defunct> in the command name.
2. Using top:
● Zombies are shown as Z in the STAT column.
● Look at the summary line for zombie count.
3. Example:
ps -ef | grep defunct
✅ Cron Job Scheduling
a) Describe how you can schedule a command to run repeatedly every 5
minutes using cron. (7 marks)
To run a command every 5 minutes, add the following to the crontab:
*/5 * * * * /path/to/command.sh
Explanation:
● */5: Every 5 minutes
● *: Every hour, day, month, and weekday
● This line ensures the command runs every 5 minutes, all day, every day.
Steps:
1. Run crontab -e
2. Add the line above
3. Save and exit
b) What is the significance of @reboot in a crontab file? (6 marks)
@reboot is a special string in crontab that schedules a job to run once at system startup.
Use Cases:
● Start services or applications after boot
● Run maintenance scripts
● Resume background tasks
Example:
@reboot /home/user/startup.sh
Note: The cron service must be running, and the script should be executable.
c) Write a crontab entry using @reboot to start a service after system
restart. (7 marks)
Example:
@reboot /usr/bin/systemctl start apache2
Or for a custom script:
@reboot /home/user/start_service.sh
● This entry ensures that the specified service (e.g., Apache) is started automatically
after reboot.
Practical UNIX Process & Scheduling on
Ubuntu
1. Check Running Processes
Open terminal and type:
ps -ef
● Shows all running processes with full info.
Filter by your user:
ps -u $USER -f
2. Create a Child Process (Using fork() in C)
First, install build tools if you don’t have them:
sudo apt update
sudo apt install build-essential
Create a file fork_example.c with this content:
#include <stdio.h>
#include <unistd.h>
int main() {
pid_t pid = fork();
if (pid < 0) {
perror("fork failed");
return 1;
} else if (pid == 0) {
printf("Child process: PID=%d, Parent PID=%d\n", getpid(), getppid());
} else {
printf("Parent process: PID=%d, Child PID=%d\n", getpid(), pid);
return 0;
Compile & run:
gcc fork_example.c -o fork_example
./fork_example
3. Schedule One-Time Command Using at
First, install at if missing:
sudo apt install at
sudo systemctl start atd
sudo systemctl enable atd
Schedule a job to run in 2 minutes:
echo "date > ~/at_test.txt" | at now + 2 minutes
● This writes current date to at_test.txt in your home directory after 2 minutes.
Check scheduled jobs:
atq
Remove a job (replace job_number with actual number from atq):
atrm job_number
4. Schedule Repeated Jobs Using cron
Edit your crontab:
crontab -e
If asked, select an editor (choose nano if unsure).
Add this line to run a script every day at 6:30 AM:
30 6 * * * /home/yourusername/myscript.sh
Save and exit (Ctrl+O, Enter, Ctrl+X in nano).
View cron jobs:
crontab -l
5. Send Signals to Processes
Find a process PID, e.g., for gedit:
pidof gedit
Send SIGTERM:
kill -15 <PID>
Send SIGKILL (force kill):
kill -9 <PID>
Send signal to all gedit processes:
killall gedit
6. Trap Signals in Shell Scripts
Create a file trap_example.sh:
nano trap_example.sh
Paste this:
#!/bin/bash
trap "echo 'SIGINT received, exiting...'; exit" SIGINT
echo "Running... Press Ctrl+C to exit."
while true; do
sleep 1
done
Save and exit (Ctrl+O, Enter, Ctrl+X).
Make it executable and run:
chmod +x trap_example.sh
./trap_example.sh
Press Ctrl+C to test signal trap.
7. Find Zombie Processes
List zombies:
ps aux | grep 'Z'
Zombies will show status Z in the output.
Summary: Ubuntu Terminal Commands
Task Command Example
List all processes ps -ef
Compile & run fork program gcc fork_example.c -o fork_example &&
./fork_example
Schedule one-time job `echo "date > ~/file.txt"
List at jobs atq
Edit cron jobs crontab -e
List cron jobs crontab -l
Send SIGTERM to PID 1234 kill -15 1234
Force kill (SIGKILL) PID 1234 kill -9 1234
Kill all processes by name killall gedit
Run script with signal trap ./trap_example.sh
Find zombies `ps aux