OS6 (1)
OS6 (1)
OS6 (1)
Linux memory management includes allocating and freeing physical memory – pages,
groups of pages, and small blocks of main memory and handling virtual memory that is
memory mapped into the address space of running processes.
Memory management in the Linux system is a sophisticated and efficient process that involves
handling physical and virtual memory to ensure optimal performance. Here's an overview:
1. Virtual Memory Management: Linux provides each process with its own virtual
address space, allowing processes to operate independently. The kernel translates these
virtual addresses to physical memory using page tables.
2. Paging: Memory is divided into fixed-size pages. When a process requests memory,
pages are mapped to available physical memory or swapped out to disk if RAM is
insufficient.
3. Demand Paging: Pages are loaded into memory only when accessed, reducing initial
load times and conserving memory.
4. Swapping: When RAM is full, inactive pages are moved to swap space (a designated
disk area). This allows Linux to manage memory efficiently but may slow performance
due to disk access.
5. Memory Allocation: Linux uses allocators like kmalloc and vmalloc for kernel
memory and malloc for user-space memory. These ensure efficient memory use while
minimizing fragmentation.
6. Caching and Buffers: Linux uses a portion of free memory for disk caching and
buffers to speed up file operations, improving system performance.
7. Shared Memory: Multiple processes can share memory regions for efficient
communication, reducing redundancy.
8. OOM Killer: If the system runs out of memory, the Out-of-Memory (OOM) Killer
terminates processes to free resources, prioritizing less critical ones.
10. Transparent Huge Pages (THP): Linux combines small memory pages into larger
ones to enhance performance for memory-intensive applications.
System calls in Linux are classified into five types based on the operation they perform.
These are the categories –
1. Process Management
2. File Management
3. Device Management
5. Memory Management
6. Networking
7. Time Management
8. Miscellaneous
Linux Unix
This operating system was launched by Linus Torvalds at This operating system was launched in
the University of Helsinki in 1991. 1960 and released by AT&T Bell
Labs.
It supports more file systems than Unix. It also supports less than Linux.
It is used in several systems like desktop, smartphones, Unix is majorly used on workstations
mainframes and servers. and servers.
Some examples of Linux are: Fedora, Debian, Red Hat, Some examples of unix are IBM AIX,
Ubuntu, Android, etc. Darwin, Solaris, HP-UX, macOS X,
etc.
Linux is free and its corporate support is available at a Unix is not totally free. There are
price. some Unix versions that are free, other
than that UNIX is expensive.
The Linux file system is the structure in which files and directories are organized on Linux-
based operating systems. It provides a standardized way for the operating system and users to
interact with stored data.
Linux file system has a hierarchal file structure as it contains a root directory and its
subdirectories. All other directories can be accessed from the root directory. A partition usually
has only one file system, but it may have more than one file system.
Linux file system contains two-part file system software implementation architecture. Consider
the below image:
The file system requires an API (Application programming interface) to access the function
calls to interact with file system components like files and directories. API facilitates tasks such
as creating, deleting, and copying the files. It facilitates an algorithm that defines the
arrangement of files on a file system.
The first two parts of the given file system together called a Linux virtual file system. It
provides a single set of commands for the kernel and developers to access the file system. This
virtual file system requires the specific system driver to give an interface to the file system.
Directory Structure
The directories help us to store the files and locate them when we need them. Also, directories
are called folders as they can be assumed of as folders where files reside in the form of a
physical desktop analogy. Directories can be organized in a tree-like hierarchy in Linux and
several other operating systems.
The Linux file system begins at the root directory /. Common subdirectories include:
Directory Purpose
Shell is a program that acts as an interface between the user and the operating system. It forms
the connection between the operating system and human users by taking user-written
commands and converting them into instructions that the OS can understand and execute.
.
Kernal:
It is a core program of the Linux OS, responsible for managing many routine tasks and Linux
resources like processes management, file handling, I/O operations, memory, devices, etc
The Linux shell is another integral part of the Linux OS, providing a command-line
interface that allows users to directly interact with the operating system by accepting
human-readable commands.
Users type these human-readable commands in a terminal, where the Linux shell
interprets these commands.
The Linux terminal includes many built-in commands that users can utilize to perform
various tasks.
The Linux shell converts these commands for the kernel to understand and execute.
All in all, the Linux shell serves as a bridge between the user and the kernel by facilitating the
execution of human-understandable commands. It is the primary interface that provides users
with a way to communicate with the Linux operating system at a more fundamental level than
what is offered by a GUI.
Different shells offer slightly different features and syntax, but they all serve the same core
purpose. Some common Linux shells include:
Bash (Bourne Again Shell): The most widely used shell, default on many Linux
distributions.
Zsh (Z Shell): Offers advanced features like spell checking and auto-completion.
Fish (Friendly Interactive Shell): A user-friendly shell with features like syntax
highlighting and better completion.
o BIOS
BIOS is an acronym for Basic Input/Output System. In other words, the BIOS can load
and run the MBR (Master Boot Record) boot loader. When we first turn on our system,
the BIOS first implements a few integrity checks of the SSD or HDD.
After that, the BIOS finds, loads, and runs the boot loader function, which can be
detected in the MBR. Sometimes, the MBR is on a CD-ROM or USB stick, like with a
live Linux installation. Then, the boot loader function is loaded into memory, and BIOS
provides system control to it once it is detected.
o MBR
MBR is an acronym for Master Boot Record and is liable to load and run the GRUB
boot loader. MBR is placed in the first bootable disk sector, which is generally /dev/sda,
relying on our hardware. Also, the MBR includes details of GRUB, or LILO is an older
system.
o GRUB
GRUB is sometimes known as GNU GRUB, which stands for GNU GRand Unified
Bootloader. It is the classic bootloader for almost all the latest Linux systems. The
splash screen of GRUB is often the initial thing we see when we boot our system. It
contains a general menu where we can choose some portions.
We can use our keyboard to choose the one we wish our system to initiate with if we
have multiple installed kernel images. The latest kernel image is chosen by default. The
splash screen will delay for some seconds for us to choose options. It will load the
kernel image (default) if we don't. In several systems, we can see the GRUB
configuration file at /etc/grub/conf or /boot/grub/grub.conf.
o Kernel
Often, the kernel is called the code of an operating system. It contained full control on
everything in our system. In this boot process stage, the kernel mounts the base file
system that was chosen that is set up in the file, i.e., grub.conf. Then, it runs the
/sbin/init function, which is always the initial function to be run. We can confirm it with
its PID (process id), which should be always 1. Then, the kernel creates a temporary
base file system with the help of initrd (Initial RAM Disk) until the actual file system
is mounted.
o Init
At this stage, our system runs runlevel programs. It would find an init file, generally
detected at /etc/inittab, to determine the run level of Linux. Modern Linux systems
utilize systemd to select a run level rather. Then, systemd will start running runlevel
programs
Runlevel programs
We can see distinct services getting started depending on which distribution of Linux we
have installed. They are called runlevel programs and are run from distinct directories
depending on our run level.
Open Source: Provide a free and open-source operating system where users can view,
modify, and distribute the source code.
Stability and Reliability: Offer a robust operating system suitable for servers, desktops,
and embedded systems.
Multiuser Support: Allow multiple users to use the system simultaneously without
interference.
Security: Implement strong security mechanisms to protect data and system resources.
Networking Capabilities: Provide advanced networking features for internet and intranet
connectivity.
Interfaces to Linux
o Users can interact with the OS using windows, icons, and menus.
o Used in C/C++ programming for tasks like file operations, process control, etc.
o Tools like SSH, Telnet, and FTP allow remote interaction with Linux systems.
o Kernel is the core part of an OS(Operating system); hence it has full control over
everything in the system. Each operation of hardware and software is managed and
administrated by the kernel.
o It acts as a bridge between applications and data processing done at the hardware level.
It is the central component of an OS.
o It is the part of the OS that always resides in computer memory and enables the
communication between software and hardware components.
o It is the computer program that first loaded on start-up the system (After the
bootloader). Once it is loaded, it manages the remaining start-ups. It also manages
memory, peripheral, and I/O requests from software. Moreover, it translates all I/O
requests into data processing instructions for the CPU. It manages other tasks also
such as memory management, task management, and disk management.
o A kernel is kept and usually loaded into separate memory space, known as protected
Kernel space. It is protected from being accessed by application programs or less
important parts of OS.
5. System Calls and Security: Provides a mechanism for applications to request services
and ensures secure access to resources.
fork() − A parent process always uses a fork for creating a new child process. The child process
is generally called a copy of the parent. After execution of fork, both parent and child execute
the same program in separate processes.
exec() − This function is used to replace the program executed by a process. The child
sometimes may use exec after a fork for replacing the process memory space with a new
program executable making the child execute a different program than the parent.
wait() − The parent uses a wait function to suspend execution till a child terminates. Using
wait the parent can obtain the exit status of a terminated child.
Define the components of LINUX system with diagram. What is the responsibility of
kernel in LINUX operating system?
Linux Operating System has primarily three components
Kernel − Kernel is the core part of Linux. It is responsible for all major activities of
this operating system. It consists of various modules and it interacts directly with the
underlying hardware. Kernel provides the required abstraction to hide low level
hardware details to system or application programs.
System Library − System libraries are special functions or programs using which
application programs or system utilities accesses Kernel's features. These libraries
implement most of the functionalities of the operating system and do not requires kernel
module's code access rights.
. Hardware
User Applications
Software programs that run in the user space, such as browsers, text editors, and media
players.
The kernel's primary responsibility is to act as a bridge between hardware and software. Its key
roles include:
1. Resource Management:
o Allocates CPU time, memory, and I/O resources efficiently among processes.
2. Process Management:
3. Memory Management:
4. Device Management:
8. Networking:
What are different Process management system calls in Linux. Explain exec() and brk()
in detail.
The exec() family of system calls is used to replace the current process image with a new
process image. Essentially, when a process calls exec(), it runs a different program while using
the same process ID (PID).
Variants of exec()
There are several variants of the exec() call, allowing different ways to specify arguments and
environment variables:
execl(): Executes a program, passing a list of arguments.
execle(): Similar to execl(), but also allows specifying the environment variables.
execve(): A low-level system call that executes a program, passing both arguments and
environment variables (the most commonly used variant).
execvp() and execvpe(): Similar to execv(), but search for the program in the
directories listed in the PATH environment variable.
The brk() system call allows a process to increase or decrease the size of its data
segment (heap), which is the memory area where dynamically allocated variables are
stored (e.g., memory allocated by malloc()).
When a process needs more memory on the heap, it can use brk() to extend the heap
by setting a new end address.
If the new address is above the current end of the heap, the system allocates more
memory.
If the new address is below the current heap end, the system releases memory.
A process is an instance of a program that is being executed. It has its own memory space, a
unique Process ID (PID), and system resources such as CPU time and file descriptors.
Processes are managed by the kernel.
The creation of a new process in Linux typically happens using the fork() system call. When a
process calls fork(), a new child process is created, which is a copy of the parent process. The
child gets a unique PID, but the memory space is initially shared.
o Child Process: Receives a unique PID and executes its own code.
wait() / waitpid(): Waits for the termination of a child process and retrieves its exit
status.
A thread is the smallest unit of execution within a process. Multiple threads within a process
share the same memory space but execute independently. Threads are lighter than processes in
terms of memory overhead and are useful for tasks that can be parallelized.
Thread Creation in Linux
Linux uses pthreads (POSIX threads) for managing threads. Threads can be created using
the pthread_create() function, which creates a new thread within a process.
o The new thread shares the same address space (memory) as the parent thread.
pthread_join(): Blocks the calling thread until the specified thread finishes execution.
Process Thread
The process takes more time to terminate. The thread takes less time to terminate.
It takes more time for creation. It takes less time for creation.