UNit 2 Unix Kernel Level Services
UNit 2 Unix Kernel Level Services
The UNIX kernel is the core component of the operating system responsible for managing hardware
and system resources. It provides essential services such as process management, memory
management, file system handling, device management, and system calls.
Process management
Process Management
● Key functions: Process creation (fork()), execution (exec()), termination (exit()), and scheduling.
Commands:
#include <stdio.h>
#include <unistd.h>
int main() {
pid_t pid = fork(); // Create a child process
if (pid > 0) {
printf("Parent process, PID: %d\n", getpid());
} else if (pid == 0) {
printf("Child process, PID: %d, Parent PID: %d\n", getpid(), getppid());
} else {
printf("Fork failed!\n");
}
return 0;
}
Process management
./fork_example
Output
#include <stdio.h>
#include <unistd.h>
int main() {
printf("Before exec()\n");
execlp("ls", "ls", "-l", NULL); // Execute 'ls -l' command
printf("This line will not execute if exec() succeeds\n");
return 0;
}
Executing Process Execution (exec())
Step 2: Compile and Run the Program
Output
Before exec()
total 12
-rw-r--r-- 1 user user 234 Feb 17 12:00 file1.txt
-rw-r--r-- 1 user user 456 Feb 17 12:05 file2.txt
./exit_example
#include <stdio.h>
#include <stdlib.h>
Output
int main() {
Process is running...
printf("Process is running...\n");
The process terminates immediately after printing the
exit(0); // Terminate the process message.
}
Managing and Monitoring Processes
jobs
Scenario:
A system administrator notices that a process (python3 script.py) is consuming 90% CPU. The system performance is
degrading.
Problem Statement:
How can we reduce the CPU priority of a process without stopping it?
Steps to Solve:
Find the Process ID (PID) of python3 script.py:
Example Output:
user 4567 90.3 2.5 134256 4532 ? S 12:00 45:23 python3 script.py
renice 10 -p 4567
Terminating a Hung Process
Scenario:
A user’s terminal becomes unresponsive after running a poorly optimized program. The
process does not terminate normally.
Problem Statement:
Solution:
Steps to Solve:
Example Output:
kill 6789
kill -9 6789
Confirm termination:
Scenario:
A user runs a script that takes 10 minutes to complete but needs to continue working in the terminal.
Problem Statement:
How can the user continue using the terminal while keeping the process running?
Solution:
Run the script in the background and manage it using jobs, fg, and bg.
Steps to Solve:
Example Output:
[1] 12345
Example Output:
fg %1
bg %1
Memory Management
Commands:
Usage:
free -h
Output:
Additional Options:
● free -m → Show output in MB.
● free -g → Show output in GB.
● free -t → Display total memory, including swap.
Memory Management
Output:
Usage:
Explanation:
ulimit -a
● -m: Maximum memory a process can use.
● -u: Maximum number of user processes.
Output: ● -n: Maximum number of open file descriptors.
java
CopyEdit Set Memory Limits for a Process
core file size (blocks, -c) unlimited
data seg size (kbytes, -d) unlimited ulimit -m 1048576 # Limit memory to 1GB (1024 *
scheduling priority (-e) 0 1024 KB)
file size (blocks, -f) unlimited ulimit -n 2048 # Allow up to 2048 open files
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
max user processes (-u) 4096
Key Differences Between User Process and System Process
A system administrator notices that the system is slowing down significantly. Applications are
lagging, and there are frequent system hangs. The administrator suspects high memory
usage.
Problem Statement:
How can the administrator determine if the system is running low on memory and identify the
available free memory?
Solution:
Output:
Analysis:
Resolution:
Problem Statement:
How can the administrator monitor memory usage along with CPU and I/O statistics to detect any
bottlenecks?
Solution:
Use the vmstat command to analyze memory usage, CPU load, and disk I/O.
vmstat
Analysis:
Resolution:
A user reports that their laptop is running very slowly, with frequent freezing. They suspect an application is
using too much memory.
Problem Statement:
How can the user identify and terminate the process consuming excessive memory?
Solution:
Use the top command to find the process with the highest memory usage and kill it.
top
Analysis:
Resolution:
Analysis:
Resolution:
A shared Linux server is being used by multiple developers. One of them runs a script that consumes
excessive memory, affecting other users.
Problem Statement:
How can the administrator limit memory usage for individual users to prevent a single user from consuming
too much RAM?
Solution:
ulimit -a
Output:
Issue: The memory limit is currently unlimited, meaning a single process can use all available memory.
ulimit -m 2048000
ulimit -m
Output:
2048000
File System Management
Commands:
Definition Core services of the OS that directly interact Services running on top of the kernel to
with hardware provide system functionality
Example Process scheduling, memory paging, file SSH, FTP, logging, web servers, firewall, cron
Services
system operations, device drivers jobs
Example ps, kill, df, free, mount systemctl, crontab, netstat, tail -f
Commands /var/log/syslog
When Used? When interacting with hardware or system When running system services, applications,
resources and networking tools