Os Notes Fullllllll
Os Notes Fullllllll
COURSE CONTENT
1. Abraham Silberschatz, Peter Baer Galvin, Greg Gagne; “Operating Systems Concepts”, 9th
Edition, 201 6 India, Wiley.
2. William Stallings, "Operating Systems and Design Principles", Pearson, thEdition,2018
Reference Books
1. D M Dhamdhere; operating systems - A concept Based Approach, 3'd Edition, Tata McGraw
- Hill.
2. Sumitabha Das: "I-INIX Concepts and Applications", 4th Edition, TataMcGraw Hill' 2006
3. MGVenkateshmurthy,"uNlXandShellProgramming", Pearson Edition Asia'2005
*****
TABLE OF CONTENT
Module 1 – Introduction
1 to Operating System 3 - 67
Module 2 - Process
2 68 - 91
Synchronization
Module 3 - Memory
3 91 - 157
Management Strategies
Module 4 - Introduction
4 158 - 165
to Linux Programming
UNIT - 1
1. Introduction
An operating system (OS) is a program that interfaces the system hardware and the user.
Moreover, it handles all the interactions between the software and the hardware. A computer
system's workings depend on the OS at the base level. Further, it performs all the functions like
handling memory, processes, the interaction between hardware and software, etc. Now, let us look
at the functions of the operating system.
Operating System
Objectives of OS
2. Efficiency – An operating system enables the efficient use of resources. This is due
to less time spent configuring the system.
• User interface
• Program execution
• Job Accounting
• I/O operations
• File System manipulation
• Communication
• Error Detection
• Resource Allocation
• Protection
User interface: The operating system acts as an interface between the user and system hardware.
It makes it easy and convenient for users to access system resources with the help of different
interface mechanisms. UI, GUI, and CLI are the common ways of interacting with the system.
Program execution: Operating systems handle many kinds of activities from user programs to
system programs. The system must be able to load a program into memory and run that
program. The program must be able to end its execution, either normally or abnormally.
Provides a mechanism for process synchronization, process communication, and deadlock
handling.
1. Job Accounting – The operating system keeps track of all the functions of a computer
system. Hence, it makes a record of all the activities taking place on the system. It has
an account of all the information about the memory, resources, errors, etc. Therefore,
this information can be used as and when required.
2. I/O Operation
An I/O subsystem comprises I/O devices and their corresponding driver software. An
Operating System manages the communication between user and device drivers.
3. File system manipulation:
A file represents a collection of related information. Computers can store files on the
disk. Many operating systems provide a variety of file systems. The operating system
provides various activities for file management.
4. Communication
In the case of distributed systems which are a collection of processors that do not share
memory, peripheral devices, or a clock, the operating system manages communications
between all the processes. Multiple processes communicate with one another through
communication lines in the network.
5. Error handling
Errors can occur anytime and anywhere. An error may occur in the CPU, in I/O devices,
or in the memory hardware. OS constantly checks for possible errors and takes
appropriate action to ensure correct and consistent computing.
6. Resource Management
In the case of a multi-user or multi-tasking environment, resources such as main
memory, CPU cycles, and file storage are to be allocated to each user or job. The OS
manages all kinds of resources using schedulers.
7. Protection
It provides a mechanism or a way to control the access of programs, processes, or users
to the resources defined by a computer system. OS ensures that all access to system
resources is controlled.
Types of Operating System
1. Batch Operating System:
In this system, the OS does not forward the jobs/tasks directly to the CPU. It works by
grouping similar types of jobs under one category. Further, we name this group as a ‘batch’.
Hence, the name batch OS.
Advantages
• Multiple programs can be executed simultaneously.
• High and efficient CUP utilization
• Increases the Throughput of the System.
• It helps in reducing the response time.
Disadvantages
• There is not any facility for user interaction of system resources with the system.
• CPU scheduling is required.
• Many management is also required to handle many jobs.
• Synchronization and IPC mechanism is needed.
Advantages
• Each task gets an equal opportunity.
• Provide user an interface to interact with system.
Disadvantages
• More complex to implement.
• One must have to take care of the security and integrity of user programs and data.
• Reliability problem.
• Data communication problem.
Advantages
• Efficient resource sharing.
• Uses load balancing to share workload
• Reliability of the network
• Exchange of information through communication
• Maintain coordination among programs using IPC mechanism.
Disadvantages
• Complexity in implantation.
• Protection to shared resources is needed.
• Require memory and resource management.
Real-time systems are used when there are rigid time requirements needed for each
operations of a processor.
Ex: missile systems, air traffic control systems, robots, etc
• Types of Real-Time Operating Systems:
A. Hard Real-Time Systems
Hard Real-Time OSs are meant for applications where time constraints are very strict and
even the shortest possible delay is not acceptable.
Advantages
1. Memory Management
It is the management of the main or primary memory. Whatever program is executed, it has to be
present in the main memory. Main memory is a quick storage area that may be accessed directly
by the CPU. When the program is completed, the memory region is released and can be used by
other programs. Therefore, there can be more than one program present at a time. Hence, it is
required to manage the memory.
Every software that runs on a computer, whether in the background or in the frontend, is a process.
Processor management is an execution unit in which a program operates. The operating system
determines the status of the processor and processes, selects a job and its processor, allocates the
processor to the process, and de-allocates the processor after the process is completed.
When more than one process runs on the system the OS decides how and when a process will use
the CPU. Hence, the name is also CPU Scheduling. The OS:
• Proper utilization of CPU. Since the proper utilization of the CPU is necessary.
Therefore, the OS makes sure that the CPU should be as busy as possible.
• Since every device should get a chance to use the processor. Hence, the OS makes
sure that the devices get fair processor time.
• Increasing the efficiency of the system.
3. Device Management
An operating system regulates device connection using drivers. The processes may require devices
for their use. This management is done by the OS. The OS:
• Decides which process can use which device for how much time.
4. File Management
The operating system manages resource allocation and de-allocation. It specifies which process
receives the file and for how long. It also keeps track of information, location, uses, status, and so
on. These groupings of resources are referred to as file systems. The files on a system are stored
in different directories. The OS:
• Storage management is a procedure that allows users to maximize the utilization of storage
devices while also protecting data integrity on whatever media on which it lives. Network
virtualization, replication, mirroring, security, compression,
1. Process Management:
Processor management is an execution unit in which a program operates. The operating system
determines the status of the processor and processes, selects a job and its processor, allocates
the processor to the process, and de-allocates the processor after the process is completed.
It is the management of the main or primary memory. Whatever program is executed, it has to
be present in the main memory. Main memory is a quick storage area that may be accessed
directly by the CPU. There can be more than one program present at a time. Hence, it is required
to manage the memory.
The OS is responsible for the following activities in connection with memory management.
• Keeping track of which parts of memory are currently being used & by whom.
• Deciding which processes are to be loaded into memory when memory space becomes
available.
3. File Management:
File management is one of the most important components of an OS computer can store
information on several different types of physical media magnetic tape, magnetic disk & optical
disk are the most common media.
One of the purposes of an OS is to hide the peculiarities of specific hardware devices from the
user. For example, in UNIX the peculiarities of I/O devices are hidden from the bulk of the OS
itself by the I/O subsystem.
• A general device- driver interfaces drivers for specific hardware devices. Only the device
The main purpose of a computer system is to execute programs. These programs with the data
they access must be in main memory during execution. The main memory is too small to
accommodate all data & programs & because the data that it holds are lost when power is lost.
The computer system must provide secondary storage to back up the main memory. Most
modern computer systems use disks as the storage medium to store data & programs.
The operating system is responsible for the following activities of disk management.
• Storage allocation.
• Disk scheduling
7. Networking:
A distributed system is a collection of processors that don’t share memory peripheral devices
or a clock. Each processor has its own local memory & clock and the processor communicate
with one another through various communication lines such as high speed buses or networks.
The processors in the system are connected through communication networks which are
configured in a number of different ways. The communication network design must consider
message routing & connection strategies are the problems of connection & security.
8. Protection or security:
If a computer system has multi users & allow the concurrent execution of multiple processes
then the various processes must be protected from one another’s activities. For that purpose,
mechanisms ensure that files, memory segments, CPU & other resources can be operated on
by only those processes that have gained proper authorization from the OS.
System Calls:
System calls provide the interface between a process & the OS. These are usually available in
the form of assembly language instruction. Some systems allow system calls to be made
directly from a high-level language program like C, BCPL PERL, etc. Systems calls occur in
different ways depending on the computer in use. System calls can be roughly grouped into 5
major categories.
1. Process Control:
• End, abort: A running program needs to be able to has its execution either normally
(end) or abnormally (abort).
• Load, execute: A process or job executing one program may want to load and executes
another program.
• Create Process, terminate process: There is a system call specifying for the purpose of
creating a new process or job (create process or submit job). We may want to terminate
a job or process that we created (terminates process, if we find that it is incorrect or no
longer needed).
• Get process attributes, set process attributes: If we create a new job or process we should
able to control its execution. This control requires the ability to determine & reset the
attributes of a job or processes (get process attributes, set process attributes).
2. File Manipulation:
• Create file, delete file: We first need to be able to create & delete files. Both the system
calls require the name of the file & some of its attributes.
• Open file, close file: Once the file is created, we need to open it & use it. We close the
file when we are no longer using it.
• Read, write, reposition file: After opening, we may also read, write or reposition the file
(rewind or skip to the end of the file).
• Get file attributes and set file attributes: We need to be able to determine the values of
various attributes for files or directories and reset them if necessary. Two system calls,
get file attributes and set file attributes, are required for this purpose.
3. Device Management:
• Request device, release device: If the system has multiple users, we first request the
device. After we finish using it, we must release it.
• Read, write, reposition: Once the device has been requested & allocated to us, we can
read, write & reposition the device.
4. Information maintenance:
• Get time or date, set time or date: Most systems have a system call to return the current
date & time or set the current date & time.
• Get system data, set system data: Other system calls may return information about the
system like number of current users, version number of OS, amount of free memory
etc.
• Get process attributes, set process attributes: The OS keeps information about all its
processes & there are system calls to access this information.
Communication:
• Shared memory model: processes use map memory system calls to access regions of memory
owned by other processes. They exchange information by reading & writing data in the shared
areas. The processes ensure that they are not writing to the same location simultaneously.
The operating systems are large and complex. A common approach is to partition the task into
small components, or modules, rather than have one monolithic system. The structure of an
operating system can be defined the following structures.
• Simple structure
• Layered approach
• Microkernels
• Modules
• Hybrid systems
Simple structure:
The Simple structured operating systems do not have a well-defined structure. These systems
will be simple, small and limited systems.
Example: MS-DOS
o There are four layers that make up the MS-DOS operating system, and each has its own
set of features.
o These layers include ROM BIOS device drivers, MS-DOS device drivers, application
programs, and system programs.
o The MS-DOS operating system benefits from layering because each level can be
defined independently and, when necessary, can interact with one another.
o If the system is built in layers, it will be simpler to design, manage, and update. Because
of this, simple structures can be used to build constrained systems that are less complex.
o When a user program fails, the operating system as whole crashes.
o Because MS-DOS systems have a low level of abstraction, programs and I/O
procedures are visible to end users, giving them the potential for unwanted access.
MONOLITHIC STRUCTURE
The monolithic operating system controls all aspects of the operating system's operation,
including file management, memory management, device management, and operational
operations.
The core of an operating system for computers is called the kernel (OS). All other System
components are provided with fundamental services by the kernel. The operating system and
the hardware use it as their main interface. When an operating system is built into a single piece
of hardware, such as a keyboard or mouse, the kernel can directly access all of its resources.
The monolithic operating system is often referred to as the monolithic kernel. Multiple
programming techniques such as batch processing and time-sharing increase a processor's
usability. Working on top of the operating system and under the complete command of all
hardware, the monolithic kernel performs the role of a virtual computer. This is an old operating
system that was used in banks to carry out simple tasks like batch processing and time-sharing,
which allows numerous users at different terminals to access the Operating System.
o Because layering is unnecessary and the kernel alone is responsible for managing all
operations, it is easy to design and execute.
o Because functions like memory management, file management, process scheduling,
etc., are implemented in the same address area, the monolithic kernel runs rather quickly
when compared to other systems. Utilizing the same address speeds up and reduces the
time required for address allocation for new processes.
o The monolithic kernel's services are interconnected in address space and have an impact
on one another, so if any of them malfunctions, the entire system does as well.
o It is not adaptable. Therefore, launching a new service is difficult.
LAYERED STRUCTURE
The OS is separated into layers or levels in this kind of arrangement. Layer 0 (the lowest layer)
contains the hardware, and layer 1 (the highest layer) contains the user interface (layer N).
These layers are organized hierarchically, with the top-level layers making use of the
capabilities of the lower-level ones.
The functionalities of each layer are separated in this method, and abstraction is also an option.
Because layered structures are hierarchical, debugging is simpler, therefore all lower-level
layers are debugged before the upper layer is examined. As a result, the present layer alone has
to be reviewed since all the lower layers have already been examined.
o Work duties are separated since each layer has its functionality, and there is some
amount of abstraction.
o Debugging is simpler because the lower layers are examined first, followed by the top
layers.
MICRO-KERNEL STRUCTURE
The operating system is created using a micro-kernel framework that strips the kernel of any
unnecessary parts. Systems and user applications are used to implement these optional kernel
components. So, Micro-Kernels is the name given to these systems that have been developed.
Each Micro-Kernel is created separately and is kept apart from the others. As a result, the
system is now more trustworthy and secure. If one Micro-Kernel malfunctions, the remaining
operating system is unaffected and continues to function normally.
Process Concept
A process is a program in execution. The execution of a process must progress sequentially.
When a program is loaded into the memory and becomes a process, it can be divided into
four sections ─ stack, heap, text, and data. The following image shows a simplified layout
of a process inside the main memory.
1 Stack
The process Stack contains the temporary data such as method/function parameters, return
address and local variables.
2 Heap
This is dynamically allocated memory to a process during its run time.
3 Text
This includes the current activity represented by the value of the Program Counter and the
contents of the processor's registers.
4 Data
Process State:
Context switch
• Context switching is a technique or method used by the operating system to switch a
process from one state to another to execute its function using CPUs in the system.
• It is a method to store/restore the state of a CPU in a PCB. So that process execution
can be resumed from the same point at a later time. The context-switching method is
important for multitasking OS.
Process scheduling:
• It allows the OS to allocate a time interval of CPU execution for each process. Another
important reason for using a process scheduling system is that it keeps the CPU busy
all the time.
• This allows you to get the minimum response time for programs.
• Thread is a lightweight process that holds less number of resources. In multithreaded
OS a process can have single or many no. of threads.
• The objective of multiprogramming is to have some process running at all times, to
maximize CPU utilization.
• The objective of time sharing is to switch the CPU among processes so frequently that
users can interact with each program while it is running.
• Process scheduler manages the scheduling of execution of all the processes with the
help of queues.
• Process Scheduling Queues help you to maintain a distinct queue for each and every
process state and PCB.
Three types of operating system queues are:
1. Job queue – It helps you to store all the processes in the system.
2. Ready queue – This type of queue helps you to set every process residing in the main
memory, which is ready and waiting to execute.
3. Device queues – It is a process that is blocked because of the absence of an I/O device.
Categories of Scheduling:
➢ Non-Preemptive Scheduling:
• In this scheduling, once the resources (CPU cycles) are allocated to a process, the
process holds the CPU till it gets terminated or reaches a waiting state. In the case of
non-preemptive scheduling does not interrupt a process running CPU in the middle
of the execution. Instead, the Process scheduler manages the scheduling of execution
of all the processes with the help of queues.
• Process Scheduling Queues help you to maintain a distinct queue for each and every
process state and PCBs.
Three types of operating system queues are:
4. Job queue – It helps you to store all the processes in the system.
5. Ready queue – This type of queue helps you to set every process residing in the main
memory, which is ready and waiting to execute.
6. Device queues – It is a process that is blocked because of the absence of an I/O device.
Categories of Scheduling:
➢ Non-Preemptive Scheduling:
• In this scheduling, once the resources (CPU cycles) are allocated to a process, the
process holds the CPU till it gets terminated or reaches a waiting state. In the case of
non-preemptive scheduling does not interrupt a process running CPU in the middle
of the execution. Instead, the Process scheduler manages the scheduling of execution
of all the processes with the help of queues.
• Process Scheduling Queues help you maintain a distinct queue for every process state
and PCB.
Three types of operating system queues are:
7. Job queue – It helps you to store all the processes in the system.
8. Ready queue – This type of queue helps you to set every process residing in the main
memory, which is ready and waiting to execute.
9. Device queues – It is a process that is blocked because of the absence of an I/O device.
Categories of Scheduling:
➢ Non-Preemptive Scheduling:
In this scheduling, once the resources (CPU cycles) are allocated to a process, the
process holds the CPU till it gets terminated or reaches a waiting state. In the case of
non-preemptive scheduling does not interrupt a process running CPU in the middle
of the execution. Instead, Implementation is difficult and high cost.
❑ Process Schedulers:
A scheduler is a type of system software that allows the OS to handle and monitor process
scheduling.
There are mainly three types of Process Schedulers:
✓ Long Term Scheduler
✓ Short Term Scheduler
✓ Medium Term Scheduler
I/O-bound process – spends more time doing I/O than computations, many short CPU bursts
CPU-bound process – spends more time doing computations; few very long CPU bursts
3. Medium-term scheduler
• It can be added if degree of multiple programming needs to decrease
• Remove process from memory, store on disk, bring back in from disk to continue
execution: swapping
Process Scheduling
Definition
Process scheduling is the activity of the process manager that handles the removal of the
running process from the CPU and the selection of another process on the basis of a particular
strategy.
• In multiprogramming operating systems allow more than one process to be loaded into
the executable memory at a time and the loaded process shares the CPU using time
multiplexing.
Scheduling Queues
As processes enter the system, they are put into a job queue, which consists of all processes in
the system.
• The processes that are residing in main memory and are ready and waiting to execute
are kept on a list called the ready queue.
• This queue is generally stored as a linked list. A ready-queue header contains pointers
to the first and final PCBs in the list. Each PCB includes a pointer field that points to the
next PCB in the ready queue.
• The system also includes other queues. When a process is allocated the CPU, it executes
for a while and eventually quits, is interrupted, or waits for the occurrence of a particular
event, such as the completion of an I/O request.
• Suppose the process makes an I/O request to a shared device, such as a disk. Since there
are many processes in the system, the disk may be busy with the I/O request of some other
process. The process therefore may have to wait for the disk.
• The list of processes waiting for a particular I/O device is called a device queue. Each
device has its own device queue as shown in the below figure.
Note:
Below figure shows process creation using above mentioned system calls.
Process Termination
• A process terminates when it finishes executing its final statement and asks the
operating system to delete it by using the exit() system call.
• At that point, the process may return a status value (typically an integer) to its parent
process (via the wait() system call).
• All the resources of the process—including physical and virtual memory, open files,
and I/O buffers—are deallocated by the operating system.
Termination can occur in other circumstances as well. A process can cause the termination of
another process via an appropriate system call.
• Usually, such a system call can be invoked only by the parent of the process that is to
be terminated. Otherwise, users could arbitrarily kill each other’s jobs. Note that a parent
needs to know the identities of its children if it is to terminate them.
• Thus, when one process creates a new process, the identity of the newly created process
is passed to the parent.
• A parent may terminate the execution of one of its children for a variety of reasons,
such as these
• The child has exceeded its usage of some of the resources that it has been allocated. (To
determine whether this has occurred, the parent must have a mechanism to inspect the state
of its children.)
• The task assigned to the child is no longer required.
• The parent is exiting, and the operating system does not allow a child to continue if its
parent terminates.
Cascading termination
Some systems do not allow a child to exist if its parent has terminated. In such systems, if a
process terminates (either normally or abnormally), then all its children must also be terminated
and this phenomenon, referred to as cascading termination, is normally initiated by the
operating system.
Zombie process
A process that has terminated, but whose parent has not yet called wait(), is known as a zombie
process. When the child process becomes orphans then OS addresses the issue by assigning the
in it process as the new parent to orphan processes.
Interprocess Communication
• Information sharing.
• Since several users may be interested in the same piece of information (for
instance, a shared file), we must provide an environment to allow concurrent access to
such information.
• Computation speedup.
• If we want a particular task to run faster, we must break it into subtasks, each of
which will be executing in parallel with the others. Notice that such a speedup can be
achieved only if the computer has multiple processing cores.
• Modularity.
• We may want to construct the system in a modular fashion, dividing the system
functions into separate processes or threads.
• Convenience.
• Even an individual user may work on many tasks at the same time. For instance,
a user may be editing, listening to music, and compiling in parallel.
Shared-Memory Systems
• Typically, a shared-memory region resides in the address space of the process creating
the shared-memory segment.
• Other processes that wish to communicate using this shared-memory segment must
attach it to their address space.
• Normally, the operating system tries to prevent one process from accessing another
process’s memory.
• Shared memory requires that two or more processes agree to remove this restriction.
• They can then exchange information by reading and writing data in the shared areas.
The form of the data and the location are determined by these processes and are not under
the operating system’s control.
• The processes are also responsible for ensuring that they are not writing to the same
location simultaneously.
• Processes that want to communicate must have a way to refer to each other.
Synchronization
• Communication between processes takes place through calls to send() and receive()
primitives.
• There are different design options for implementing each primitive. Message passing
may be either blocking or non-blocking also known as synchronous and asynchronous.
• Blocking send. The sending process is blocked until the message is received by the
receiving process or by the mailbox.
• Non-blocking send. The sending process sends the message and resumes operation.
• Blocking receive. The receiver blocks until a message is available.
• Non-blocking receive. The receiver retrieves either a valid message or a null.
Buffering
The queue has a maximum length of zero; thus, the link cannot have any messages waiting
in it. In this case, the sender must block until the recipient receives the message.
• Bounded capacity.
The queue has a finite length of n; thus, at most n messages can reside in it. If the queue is not
full when a new message is sent, the message is placed in the queue (either the message is
copied or a pointer to the message is kept), and the sender can continue execution without
waiting. The link’s capacity is finite, however. If the link is full, the sender must block until
space is available in the queue.
• Unbounded capacity. The queue’s length is potentially infinite; thus, any number of
messages can wait in it and the sender never blocks. The process could be removed forcibly
from the CPU, as a result of an interrupt, and be put back in the ready queue.
• In the first two cases, the process eventually switches from the waiting state to the ready
state and is then put back in the ready queue.
• A process continues this cycle until it terminates, at which time it is removed from all
queues and has its PCB and resources deallocated.
Schedulers
Schedulers are special system software that handles process scheduling in various ways. Their
main task is to select the jobs to be submitted into the system and to decide which process to
run.
• A process migrates among the various scheduling queues throughout its lifetime.
• The operating system must select, for scheduling purposes, processes from these queues
in some fashion. The selection process is carried out by the appropriate scheduler.
Schedulers are of three types as below
• Long-Term Scheduler
• Short-Term Scheduler
• Medium-Term Scheduler
Degree of multiprogramming
The degree of multiprogramming describes the maximum number of processes that a single
processor system can accommodate efficiently. The primary factor affecting the degree of
multiprogramming is the amount of memory available to be allocated to executing processes.
• In general, most processes can be described as either I/O bound or CPU bound.
• An I/O-bound process spends more of its time doing I/O than it spends doing
computations.
• A CPU-bound process, in contrast, generates I/O requests infrequently, using more of
its time doing computations.
Comparison among Scheduler
Speed is lesser than short- Speed is the fastest Speed is in between both
term scheduler among the other two short and long-term
schedulers.
Context switching
• Context switching is the procedure of storing the state of an active process for the CPU
when it has to start executing a new one.
• Context switches are resource-intensive and most operating system designers try to
reduce the need for a context switch. They can be software or hardware governed depending
upon the CPU architecture.
• Context switches can relate to either a process switch, a thread switch within a process,
or a register switch. The major need for a context switch arises when CPU has to switch
between user mode and kernel mode but some OS designs may obviate it.
Ex: Process A with its address space and stack is currently being executed by the CPU and
there is a system call to jump to a higher priority process B; the CPU needs to remember the
current state of process A so that it can suspend its operation, begin executing the new process
B and when done, return to its previously executing process A.
Operations on Processes
The processes in most systems can execute concurrently, and they may be created and deleted
dynamically.
Process Creation
• During the course of execution, a process may create several new processes. As
mentioned earlier, the creating process is called a parent process, and the new processes
are called the children of that process.
• Each of these new processes may in turn create other processes, forming a tree of
processes.
• Most operating systems (including UNIX, Linux, and Windows) identify processes
according to a unique process identifier (or pid), which is typically an integer number.
• The pid provides a unique value for each process in the system, and it can be used as
an index to access various attributes of a process within the kernel.
• Below figure illustrates a typical process tree for the Linux operating system, showing
the name of each process and its pid.
In general, when a process creates a child process, that child process will need certain resources
(CPU time, memory, files, I/O devices) to accomplish its task.
• When a process creates a new process, two possibilities for execution exist
• The parent continues to execute concurrently with its children.
• The parent waits until some or all of its children have terminated.
• There are also two address-space possibilities for the new process
• The child process is a duplicate of the parent process (it has the same program and data
as the parent).
• The child process has a new program loaded into it.
Thread
The benefits of multithreaded programming can be broken down into four major categories
• Responsiveness
• Multithreading an interactive application may allow a program to continue
running even if part of it is blocked or is performing a lengthy operation, thereby
increasing responsiveness to the user.
• This quality is especially useful in designing user interfaces.
• Resource sharing
• Processes can only share resources through techniques such as shared memory
and message passing.
• Such techniques must be explicitly arranged by the programmer. However,
threads share the memory and the resources of the process to which they belong by
default.
• The benefit of sharing code and data is that it allows an application to
• have several different threads of activity within the same address space.
• Economy
• Threads share the resources of the process to which they belong, it is more
economical to create and context-switch threads.
• Concurrent systems that we create using multicore programming have multiple tasks
executing in parallel.
• This is known as concurrent execution. When multiple parallel tasks are executed by a
processor, it is known as multitasking.
• A CPU scheduler, handles the tasks that execute in parallel. The CPU implements tasks
using operating system threads. So that tasks can execute independently but have some data
transfer between them, such as data transfer between a data acquisition module and
controller for the system. Data transfer occurs when there is a data dependency.
• Identifying tasks.
that can be divided into separate, concurrent tasks. Ideally, tasks are independent of one another
and thus can run in parallel on individual cores.
• Balance.
While identifying tasks that can run in parallel, programmers must also ensure that the tasks
perform equal work of equal value. In some instances, a certain task may not contribute as
much value to the overall process as other tasks. Using a separate execution core to run that
task may not be worth the cost.
• Data splitting.
Just as applications are divided into separate tasks, the
data accessed and manipulated by the tasks must be divided to run on separate cores.
• Data dependency.
The data accessed by the tasks must be examined for
dependencies between two or more tasks. When one task depends on data from another,
programmers must ensure that the execution of the tasks is synchronized to accommodate the
data dependency.
Parallelism
Parallelism is when tasks literally run at the same time, eg. on a multicore processor. The term
Parallelism refers to techniques to make programs faster by performing several computations
in parallel. This requires hardware with multiple processing units.
Types of Parallelism
Task parallelism.
Data parallelism
• Data parallelism focuses on distributing subsets of the same data across multiple
computing cores and performing the same operation on each core.
[N/2] . . . [N − 1]. The two threads would be running in parallel on separate computing cores.
Task parallelism
• Task parallelism involves distributing not data but tasks (threads) across multiple
computing cores. Each thread performs a unique operation.
• Different threads may be operating on the same data, or they may be operating on
different data. Consider again our example above.
• In contrast to that situation, an example of task parallelism might involve two threads,
each performing a unique statistical operation on the array of elements.
• The threads again are operating in parallel on separate computing cores, but each is
performing a unique operation.
•
Note: Data parallelism involves the distribution of data across multiple cores and task
parallelism on the distribution of tasks across multiple cores.
Multithreading Models
Support for threads may be provided either at the user level, for user threads, or by the kernel,
for kernel threads.
• User threads are supported above the kernel and are managed without kernel support.
• Kernel threads are supported and managed directly by the operating system.
• Virtually all contemporary operating systems—including Windows, Linux, Mac OS X,
and Solaris— support kernel threads.
• Ultimately, a relationship must exist between user threads and kernel threads. In this
section, we look at three common ways of establishing such a relationship
• The many-to-one model
• The one-to-one model
• The many-to-many model
Many-to-One Model
• The many-to-one model maps many user-level threads to one kernel thread.
One-to-One Model
• It provides more concurrency than the many-to-one model by allowing another thread
to run when a thread makes a blocking system call.
• It also allows multiple threads to run in parallel on multiprocessors.
• The only drawback to this model is that creating a user thread requires creating the
corresponding kernel thread.
• Because the overhead of creating kernel threads can burden the performance of an
application, most implementations of this model restrict the number of threads supported
by the system.
• Linux, along with the family of Windows operating systems, implements the one-to-
one model.
Many-to-Many Model
Many-to-Many Model
Two-level model
CPU Scheduler
• Whenever the CPU becomes idle, the operating system must select one of the processes
in the ready queue to be executed. The selection process is carried out by the short-term
scheduler, or CPU scheduler.
• The scheduler selects a process from the processes in memory that are ready to execute
and allocates the CPU to that process.
• Note that the ready queue is not necessarily a first-in, first-out (FIFO) queue.
• As we shall see when we consider the various scheduling algorithms, a ready queue can
be implemented as a FIFO queue, a priority queue, a tree, or simply an unordered linked
list. Conceptually, however, all the processes in the ready queue are lined up waiting for a
chance to run on the CPU.
• The records in the queues are generally process control blocks (PCBs) of the processes.
Pre-emptive Scheduling
• CPU-scheduling decisions may take place under the following four circumstances
• When a process switches from the running state to the waiting state (for example, as
the result of an I/O request or an invocation of wait() for the termination of a child process)
• When a process switches from the running state to the ready state (for example, when
an interrupt occurs)
• When a process switches from the waiting state to the ready state (for example, at
completion of I/O)
• When a process terminates
• Scheduling schemes are classified as follows
• Non-pre-emptive or cooperative.
• Pre-emptive.
• Under non-pre-emptive scheduling, once the CPU has been allocated to a process, the
process keeps the CPU until it releases the CPU either by terminating or by switching to
the waiting state.
• Pre-emptive scheduling can result in race conditions when data are shared among
several processes. Consider the case of two processes that share data. While one process is
updating the data, it is pre-empted so that the second process can run. The second process
then tries to read the data, which are in an inconsistent state.
Dispatcher
• The dispatcher is the module that gives control of the CPU to the process selected by
the short-term scheduler. This function involves the following
• Switching context
• Switching to user mode
• Jumping to the proper location in the user program to restart that program
• The dispatcher should be as fast as possible since it is invoked during every process
switch.
• The time it takes for the dispatcher to stop one process and start another running is
known as the dispatch latency.
Scheduling Criteria
1. CPU utilization
We want to keep the CPU as busy as possible. Conceptually, CPU utilization can range from
0 to 100 percent. In a real system, it should range from 40 percent (for a lightly loaded system)
to 90 percent (for a heavily loaded system).
2. Throughput.
done. One measure of work is the number of processes that are completed per time unit, called
throughput. For long processes, this rate may be one process per hour; for short transactions, it
may be ten processes per second.
3. Turnaround time.
important criterion is how long it takes to execute that process. The interval from the time of
submission of a process to the time of completion is the turnaround time. Turnaround time is
the sum of the periods spent waiting to get into memory, waiting in the ready queue, executing
on the CPU, and doing I/O.
4. Waiting time.
The CPU-scheduling algorithm does not affect the amount of time during which a process
executes or does I/O. It affects only the amount of time that a process spends waiting in the
ready queue. Waiting time is the sum of the periods spent waiting in the ready queue.
5. Response time.
the best criterion. Often, a process can produce some output fairly early and can continue
computing new results while previous results are being output to the user. Thus, another
measure is the time from the submission of a request until the first response is produced. This
measure, called response time, is the time it takes to start responding, not the time it takes to
output the response. The turnaround time is generally limited by the speed of the output device.
Scheduling Algorithms
Shortest-Job-First Scheduling
Using SJF scheduling, we would schedule these processes according to the following Gantt
chart
• The waiting time is 3 milliseconds for process P1, 16 milliseconds for process • P2,
9 milliseconds for process P3, and 0 milliseconds for process P4.
• Thus, the average waiting time is (3 + 16 + 9 + 0)/4 = 7 milliseconds.
• By comparison, if we were using the FCFS scheduling scheme, the average waiting
time would be 10.25 milliseconds.
Priority Scheduling
• Each process is assigned a priority. The process with the highest priority is to be
executed first and so on.
• Processes with the same priority are executed on a first-come first-served basis.
• Priority can be decided based on memory requirements, time requirements, or any other
resource requirement.
Note that we discuss scheduling in terms of high priority and low priority. Priorities are
generally indicated by some fixed range of numbers, such as 0 to 7 or 0 to 4,095.
• Consider the following set of processes, assumed to have arrived at time 0 in the order
P1, P2, ···, P5, with the length of the CPU burst given in milliseconds
Multiple-level queues are not an independent scheduling algorithm. They make use of other
existing algorithms to group and schedule jobs with common characteristics.
• The idea is to separate processes according to the characteristics of their CPU bursts.
• If a process uses too much CPU time, it will be moved to a lower-priority queue.
• This scheme leaves I/O-bound and interactive processes in the higher-priority queues.
• In addition, a process that waits too long in a lower-priority queue may be moved to a
higher-priority queue. This form of aging prevents starvation.
Thread Scheduling
Thread scheduling is done by thread library, which consists of below contention scope.
• The library chooses which unbound thread will be put on which LWP.
• The scheduling of the LWP is (of course) still global and independent of the local
scheduling. While this does mean that unbound threads are subject to a sort of funny, two-
tiered scheduling architecture, in practice, you can ignore the scheduling of the LWP and
deal solely with the local scheduling algorithm.
SCS (System Contention Scope)
• This scheme is used by the kernel to decide which kernel-level thread to schedule onto
a CPU, wherein all threads (as opposed to only user-level threads, as in the Process
Contention Scope scheme) in the system compete for the CPU.
• Operating systems that use only the one-to-one model, such as Windows, Linux, and
Solaris, schedule threads using only System Contention Scope
Multiple-Processor Scheduling
In multiple-processor scheduling multiple CPUs are available and hence Load Sharing
becomes possible.
Asymmetric Multiprocessing
One approach is when all the scheduling decisions and I/O processing are handled by a single
processor which is called the Master Server and the other processors executes only the user
code. This is simple and reduces the need of data sharing.
Symmetric Multiprocessing
A second approach uses Symmetric Multiprocessing where each processor is self scheduling.
All processes may be in a common ready queue or each processor may have its own private
queue for ready processes. The scheduling proceeds further by having the scheduler for each
processor examine the ready queue and select a process to execute.
Processor Affinity
• Processor Affinity means a process has an affinity for the processor on which it is
currently running.
• When a process runs on a specific processor there are certain effects on the cache
memory. The data most recently accessed by the process populate the cache for the
processor and as a result successive memory access by the process are often satisfied in the
cache memory. Now if the process migrates to another processor, the contents of the cache
memory must be invalidated for the first processor and the cache for the second processor
must be repopulated. Because of the high cost of invalidating and repopulating caches, most
of the SMP(symmetric multiprocessing) systems try to avoid migration of processes from
one processor to another and try to keep a process running on the same processor. This is
known as PROCESSOR AFFINITY.
There are two types of processor affinity
• Soft Affinity
When an operating system has a policy of attempting to keep a process running on the same
processor but not guaranteeing it will do so, this situation is called soft affinity.
• Hard Affinity
Hard Affinity allows a process to specify a subset of processors on which it may run. Some
systems such as Linux implements soft affinity but also provide some system calls like
sched_setaffinity() that supports hard affinity.
Load Balancing
Load Balancing is the phenomenon that keeps the workload evenly distributed across all
processors in an SMP system.
• Load balancing is necessary only on systems where each processor has its own private
queue of processes that are eligible to execute. Load balancing is unnecessary because once
a processor becomes idle it immediately extracts a runnable process from the common run
queue.
• On SMP(symmetric multiprocessing), it is important to keep the workload balanced
among all processors to fully utilize the benefits of having more than one processor else
one or more processors will sit idle while other processors have high workloads along with
lists of processors awaiting the CPU.
There are two general approaches to load balancing:
• Push Migration
In push migration a task routinely checks the load on each processor and if it finds an imbalance
then it evenly distributes the load on each processor by moving the processes from overloaded
to idle or less busy processors.
• Pull Migration
Pull Migration occurs when an idle processor pulls a waiting task from a busy processor for its
execution.
Multicore Processors
• In multicore processors multiple processor cores are placed on the same physical chip.
Each core has a register set to maintain its architectural state and thus appears to the
operating system as a separate physical processor.
• SMP systems that use multicore processors are faster and consume less power than
systems in which each processor has its own physical chip.
• However multicore processors may complicate the scheduling problems. When a
processor accesses memory then it spends a significant amount of time waiting for the data
to become available. This situation is called MEMORY STALL. It occurs for various
reasons such as cache miss, which is accessing the data that is not in the cache memory. In
such cases, the processor can spend up to fifty percent of its time waiting for data to become
available from the memory.
• To solve this problem recent hardware designs have implemented multithreaded
processor cores in which two or more hardware threads are assigned to each core. Therefore
if one thread stalls while waiting for the memory, core can switch to another thread.
There are two ways to multithread a processor:
Coarse-Grained Multithreading
Fine Grained Multithreading: This multithreading switches between threads at a much finer
level mainly at the boundary of an instruction cycle. The architectural design of fine-grained
systems includes logic for thread switching and as a result, the cost of switching between
threads is small.
Solaris Scheduling
• Threads in the real-time class are given the highest priority. A real-time process
will run before a process in any other class.
• Solaris uses the system class to run kernel threads, such as the scheduler and
paging daemon.
• Below figure illustrates how the six scheduling classes relate to one another and
how they map to global priorities.
Algorithm Evaluation
The first problem is defining the criteria to be used in selecting an algorithm and criteria are
often defined in terms of CPU utilization,
• To select an algorithm, we must first define the relative importance of these elements.
• Our criteria may include several measures, such as these:
• Maximizing CPU utilization under the constraint that the maximum response time is 1
second.
• Maximizing throughput such that turnaround time is (on average) linearly proportional
to total execution time.
• Once the selection criteria have been defined, we want to evaluate the algorithms under
consideration.
• We have below evaluation methods.
Deterministic Modeling
• One major class of evaluation methods is analytic evaluation. Analytic evaluation uses
the given algorithm and the system workload to produce a formula or number to evaluate
the performance of the algorithm for that workload.
Queueing Models
• Each server has a queue of waiting processes. The CPU is a server with its ready queue,
as is the I/O system with its device queues. Knowing arrival rates and service rates, we can
compute utilization, average queue length, average wait time, and so on.
• This area of study is called queueing-network analysis.
• The above equation, known as Little’s formula, is particularly useful because it is valid
for any scheduling algorithm and arrival distribution.
Where n be the average queue length (excluding the process being serviced) W be the average
waiting time in the queue,
Simulations
The only completely accurate way to evaluate a scheduling algorithm is to code it up, put it in
the operating system, and see how it works.
• This approach puts the actual algorithm in the real system for evaluation under real
operating conditions.
UNIT – 2
PROCESS SYNCHRONIZATION.
Process Synchronization
• Process Synchronization was introduced to handle problems that arose during multiple
process executions. Some of the problems are discussed below.
Race Condition
• A race condition is a situation that may occur inside a critical section. This happens
when the result of multiple thread execution in a critical section differs according to the
order in which the threads execute.
• Race conditions in critical sections can be avoided if the critical section is treated as an
atomic instruction. Also, proper thread synchronization using locks or atomic variables can
prevent race conditions.
Critical Section
• The critical section in a code segment where the shared variables can be accessed.
• Atomic action is required in a critical section i.e. only one process can execute in its
critical section at a time.
• All the other processes have to wait to execute in their critical sections.
do
Entry Section
Critical Section
Exit Section
Remainder Section
} while (TRUE);
• In the above diagram, the entry sections handle the entry into the critical section. It
acquires the resources needed for the execution by the process. The exit section handles
the exit from the critical section. It releases the resources and also informs the other
processes that the critical section is free.
• The critical section problem needs a solution to synchronize the different processes.
The solution to the critical section problem must satisfy the following conditions.
Mutual Exclusion
• Mutual exclusion implies that only one process can be inside the critical section at any time.
If any other processes require the critical section, they must wait until it is free.
Progress
• Progress means that if a process is not using the critical section, then it should not stop
any other process from accessing it.
Bounded waiting means that each process must have a limited waiting time. It should not wait
endlessly to access the critical section.
Peterson’s Solution
do {
flag[i] = true;
turn = j;
while (true);
• Mutual Exclusion is assured as only one process can access the critical section at any
time.
• Progress is also assured, as a process outside the critical section does not block other
processes from entering the critical section.
• Bounded Waiting is preserved as every process gets a fair chance.
Synchronization Hardware
• In TestAndSet, we have a shared lock variable that can take either of the two values, 0
or 1.
• We follow below conventions
0 Unlock 1 Lock
• Before entering into the critical section, a process inquires about the lock. If it is locked,
it keeps on waiting till it becomes free and if it is not locked, it takes the lock and executes
the critical section.
• In TestAndSet, Mutual exclusion and progress are preserved but bounded waiting
cannot be preserved.
Semaphores
• Semaphores are integer variables that are used to solve the critical section problem by
using two atomic operations, wait and signal that are used for process synchronization.
while (S<=0);
S--;
• Signal
• The signal operation increments the value of its argument S.
signal(S)
S++;
Types of Semaphores
There are two main types of semaphores i.e. counting semaphores and binary semaphores.
Details about these are given as follows
Counting Semaphores
• These are integer value semaphores and have an unrestricted value domain. These
semaphores are used to coordinate resource access, where the semaphore count is the
number of available resources.
• If the resources are added, the semaphore count is automatically incremented and if the
resources are removed, the count is decremented.
Binary Semaphores
• The binary semaphores are like counting semaphores but their value is restricted to 0
and 1. The wait operation only works when the semaphore is 1 and the signal operation
succeeds when the semaphore is 0.
• It is sometimes easier to implement binary semaphores than counting semaphores.
Advantages of Semaphores
• Semaphores are complicated so the wait and signal operations must be implemented in
the correct order to prevent deadlocks.
Semaphores are impractical for last-scale use as their use leads to loss of modularity. This
happens because the wait and signal operations prevent the creation of a structured layout for
the system.
• Semaphores may lead to a priority inversion where low-priority processes may access
the critical section first and high-priority processes later.
Classic Problems of Synchronization
• Because the buffer pool has a maximum size, this problem is often called the Bounded
buffer problem.
• The solution to this problem is, creating two counting semaphores full and empty to
keep track of the current number of full and empty buffers, respectively.
• Need to follow the below conventions.
• a buffer that can store n items.
• a producer process that creates the items (1 at a time).
• a consumer process that processes them (1 at a time).
Note:
• In this problem there are some processes(called readers) that only read the shared data,
and never change it, and there are other processes(called writers) who may change the data
in addition to reading.
• There are various types of readers-writer problems, most centered on the relative
priorities of readers and writers.
• A data item such as a file is shared among several processes. Each process is classified
as either a reader or a writer. Multiple readers may access the file simultaneously.
• A writer must have exclusive access (i.e., cannot share with either a reader or another
writer).
• Solution gives priority to either readers or writers.
• Reader’s priority: no reader is kept waiting unless a writer has already obtained
permission to access the database.
• Writer’s priority: if a writer is waiting to access the database, no new readers can start
reading.
• Solution to either version may cause starvation
• In the reader's priority version, writers may starve
• In the writer’s priority version, readers may starve
The dining philosopher’s problem states that 5 philosophers are sharing a circular table and
they eat and think alternatively.
• There is a bowl of rice for each of the philosophers and 5 chopsticks. A philosopher
needs both their right and left chopsticks to eat.
• A hungry philosopher may only eat if there are both chopsticks available. Otherwise, a
philosopher puts down their chopstick and begin thinking again.
• The dining philosopher is a classic synchronization problem as it demonstrates a large
class of concurrency control problems.
Solution of Dining Philosophers' Problem
• Initially the elements of the chopstick are initialized to 1 as the chopsticks are on the
table and not picked up by a philosopher.
THINKING.
} while(1);
• In the above structure, the first wait operation is performed on chopstick[i] and
chopstick[ (i+1) % 5].
• This means that the philosopher has picked up the chopsticks on his sides. Then the
eating function is performed.
After that, signal operation is performed on chopstick[i] and chopstick[ (i+1) % 5].
• This means that the philosopher has eaten and put down the chopsticks on his sides.
Then the philosopher goes back to thinking.
Difficulty with the solution
The above solution makes sure that no two neighboring philosophers can eat at the same time.
But this solution can lead to a deadlock.
• This may happen if all the philosophers pick their left chopsticks simultaneously. Then
none of them can eat and deadlock occurs.
• Some of the ways to avoid deadlock are as follows • There should be at most
four philosophers on the table.
• An even philosopher should pick the right chopstick and then the left chopstick while
an odd philosopher should pick the left chopstick and then the right chopstick.
• A philosopher should only be allowed to pick their chopstick if both are available at the
same time.
Monitors in Process Synchronization
• The monitor is one of the ways to achieve Process synchronization. The monitor is
supported by programming languages to achieve mutual exclusion between processes.
For example Java Synchronized methods. Java provides wait() and notify() constructs.
variables;
condition variables;
Procedure p1{…}
Procedure p2{…}
• Condition Variables
• Two different operations are performed on the condition variables of the monitor.
• Wait.
• signal.
• let say we have 2 condition variables
• condition x, y; // Declaring variable
• Wait operation
• [Link]() : Process performing wait operation on any condition variable are suspended.
The suspended processes are placed in block queue of that condition variable.
Note: Each condition variable has its unique block queue.
• Signal operation
• [Link](): When a process performs signal operation on condition variable, one of the
blocked processes is given chance.
• If (x block queue empty) // Ignore signal else
• // Resume a process from block queue.
Advantages of Monitor
• Monitors have the advantage of making parallel programming easier and less error-
prone than using techniques such as semaphore.
Disadvantages of Monitor
• This gives the compiler the additional burden of having to know what operating system
facilities are available to control access to critical sections in concurrent processes.
• Some languages that do support monitors are Java, C#, Visual Basic, Ada, and
concurrent Euclid.
DEADLOCK
Definition
A deadlock is a situation where a set of processes are blocked because each process is holding
a resource and waiting for another resource acquired by some other process.
• In the above diagram, process 1 has resource 1 and needs to acquire resource 2.
• Similarly process 2 has resource 2 and needs to acquire resource 1.
• Process 1 and process 2 are in a deadlock as each of them needs the other’s resources
to complete their execution but neither of them is willing to relinquish their resources.
Deadlock Characterization
A deadlock occurs if the four Coffman conditions hold. But these conditions are not mutually
exclusive.
Mutual Exclusion
There should be a resource that can only be held by one process at a time. In the diagram
below, there is a single instance of Resource 1 and it is held by Process 1 only.
A process can hold multiple resources and still request more resources from other processes
that are holding them. In the diagram given below, Process 2 holds Resource 2 and Resource 3
and is requesting Resource 1 which is held by Process 1.
No Pre-emption
A resource cannot be pre-empted from a process by force. A process can only release a resource
voluntarily. In the diagram below, Process 2 cannot preempt Resource 1 from Process 1. It will
only be released when Process 1 relinquishes it voluntarily after its execution is complete.
Circular Wait
A process is waiting for the resource held by the second process, which is waiting for the
resource held by the third process, and so on, till the last process is waiting for a resource held
by the first process. This forms a circular chain. For example: Process 1 is allocated Resource
2 and it is requesting Resource 1. Similarly, Process 2 is allocated Resource 1 and it is
requesting Resource 2. This forms a circular wait loop.
Resource-Allocation Graph
Deadlock can be described more precisely in terms of a directed graph called a system
Resource allocation graph.
• This graph consists of a set of verities V and a set into two different types of nodes P=
{P 1, P 2 ,…P n }, the set consisting of all the active processes in the system.
• R= {R 1, R 2 ,… R n }, the set consisting of all the resource types in the system.
• A directed edge from process Pi to resources type R j is denoted by P I → R j. It signifies
that process Pi to resource type R j and is currently waiting for that resource.
• A directed edge Pi → R j is called request edge.
• A directed edge from resource type R j to process Pi is denoted by R j →Pi. It signifies
that an instance of resource type has been allocated to process Pi. A directed edge R j →
Pi is called an assignment edge.
• Pictorially, we represent each process a circle, and each resource type Rj as a square
since resource type Rj may have more than one instance, we represent each such instance
as a dot within the square.
Note: A request edge points to only the square Rj. Whereas an assignment edge must also
designate one of the dots in the square.
• Resource Instance
• One instance of resource type R1.
• Two instances of resource type R2.
• One instance of resource type R3.
• Three instances of resource type R4.
Note:
When process Pi requests an instance of resource type R j a request edge is inserted in the
resource allocation graph. When this request can be fulfilled, the request edge is transferred to
an assignment edge is doted.
• Process states
• If the cycle invokes only a set of resource types each of which has only a single instance,
then deadlock has occurred. Each process involved in the cycle is deadlock.
• If a resource type has several instances, then a cycle doesn’t necessarily imply that a
deadlock has occurred.
• In this case the cycle in the graph is necessary but not sufficient for the deadlock.
Deadlock Prevention
• A deadlock may occur when Mutual exclusion, hold and wait. No pre-emption, circular
wait occurs simultaneously. A system can be prevented from deadlock if these conditions
are not allowed to occur.
Mutual Exclusion
• Shareable resources like read-only files can be shared among processes. Thus, a process
never needs to wait for a sharable resource.
Hold and Wait
• To ensure that the hold and wait condition never occurs in the system, we must
guarantee that whenever process requests a resource, it does not hold any other resources.
• First product that can be used requires each process to request and be allocated all its
resources before it begins execution.
• An alternative protocol allows a process to request resources only when the process has
none.
• A process may request some resources and use them. Before it can request any
additional resources. It must release all the resources that is currently allocated.
No pre-emption
• To ensure that this condition does not hold, we can use the following protocol.
• If a process is holding some resources and requests another resource that cannot be
immediately allocated to it (i.e. the process must wait), then all resources currently being
held are pre-empted.
• The pre-empted resources are added to the list of resources for which the process is
waiting the process will be re-started only when it can re-gain its old resources, as well as
the new ones that it is requesting.
• Alternatively, if the process requests some resources, we first check whether they are
available.
• If they are, we allocate them. If they aren’t available, we check whether they are
allocated to another process that is waiting for additional resources.
Circular Wait
• One way to ensure that this condition never holds is to impose a total that each process
requests resources in increasing order of enumeration.
F (pen drive) =1
F (disk drive) =5
F (printer) = 12
Where F is a function.
• Allocation of resources can be made in any of the orders. Tape drive, disk drive for all
F (tape drive) < F (disk drive)
• Tape drive, printer for all F (tape drive) <F (printer) Disk drive, printer for all F (disk
drive) < F (printer)
Being in the increasing order of the enumeration and thus deadlock can be prevented.
Deadlock Avoidance
• A state is safe if the system can allocate resources to each process in some order and
still avoid a deadlock. More precisely a system is in a safe state only if there exists a safe
sequence.
• A sequence of processes <P1, P2 , P3 …Pn >is in a safe state only if for each Pi the
resources that Pi can still request can be satisfied by the currently available resources plus
the resources held by all the P j, with j < i.
Resource-Allocation-Graph Algorithm
• Along with the request and assignment edges, we introduce a new type of edge, called
claim edge.
• A claim edge Pi → Rj indicates that process Pi may request resources R j at some time
in the future. They are represented in a dashed line.
• If no cycle exists, then the allocation of the resource will put the system in a safe state.
On the other hand, if the cycle is found, the allocation will put the system in an unsafe state.
• The Below graph represents the resource allocation graph for deadlock avoidance.
• Consider the resource allocation graph, suppose that P2 requests r2 although R2 is
currently free, we cannot allocate it to P2, since this action will create a cycle in the graph.
• A cycle indicates that the system is in an unsafe state. If P1 requests R2, and P2 requests
R1, then a deadlock will occur.
Banker’s Algorithm
The resource allocation graph algorithm does not apply to a resource • allocation system
with multiple instances of each resource type.
Available
A vector (array) of length m indicates the number of available resources of each type.
Available[j]=K, indicates that there are K instances to resource type R j is available.
MAX
An n*m matrix defines the maximum demand to each process. If Max{i,j}=K, then Pi may
request the most K instances of resource type Rj.
Allocation
An n*m matrix defines the number of resources of each type currently allocated to each
process. If allocation[i,j]=K, then process Pi currently allocated K instances of resource type
Rj.
Need
An n*m matrix indicates the renaming resource need of each process. If need[i,j]=K then Pi
may need K more instances of resource type Rj, to complete its task and Need [ i , j] = Max[ I
, j]
Allocation [ i , j]
X= (1,7,3,2) and Y= (0,3,2,1) Then Y≤X, Y<X only if Y≤X and Y≠X.
Safety Algorithm
Safety algorithm for finding out whether a system is in a safe state. This algorithm can be
described as follows.
Resource-Request Algorithm
• Next, we describe the algorithm for determining whether requests can be safely
granted.
• Let Requesti be the request vector for process Pi . If Requesti [j] == k, then
process Pi wants k instances of resource type Rj . When a request for resources is
made by a process
Pi , the following actions are taken
• Have the system pretend to have allocated the requested resources to process Pi
by modifying the state as follows:
• Available = Available–Requesti ; Allocationi = Allocationi + Requesti ;
Needi = Needi –Requesti ;
• This dead-lock detection algorithm uses a different type of resource allocation graph
called the wait-for graph. It consists of the edge.
• Available
A vector (Array) of length m indicates the number of available resources of each type.
• Allocation
An n*m matrix defines the number of resources of each type currently allocated to each
process.
• Request
An n*m matrix indicates the current request of each process and the algorithm is like a banker’s
algorithm with slight variation
• Let Work and Finish be vectors of length m and n, respectively. Initialize Work =
Available. For i = 0, 1, ..., n–1, if Allocationi != 0, then Finish[i] = false. Otherwise,
Finish[i] = true.
• Find an index i such that both
Finish[i] == false
Requesti ≤ Work
The above algorithm requires an order of m × n2 operations to detect whether the system is in
a deadlocked state.
UNIT – 3
Memory Management
Basic Hardware
• It should be noted that from the memory chips point of view, all memory accesses
are equivalent. The memory hardware doesn't know what a particular part of memory is
being used for, nor does it care. This is almost true of the OS as well, although not entirely.
• The CPU can only access its registers and main memory. It cannot, for example,
direct access to the hard drive, so any data stored there must first be transferred into the
main memory chips before the CPU can work.(Device drivers communicate with their
hardware via interrupts and "memory" accesses, sending short instructions for example to
transfer data from the hard drive to a specified location in main memory. The disk
controller monitors the bus for such instructions, transfers the data, and then notifies the
CPU that the data is there with another interrupt, but the CPU never gets direct access to
the disk.)
• Memory accesses to registers are very fast, generally one clock tick, and a CPU
may be able to execute more than one machine instruction per clock tick
• Memory accesses to main memory are comparatively slow and may take several
clock ticks to complete. This would require intolerable waiting by the CPU if it were not
for an intermediary fast memory cache built into most modern CPUs. The basic idea of the
cache is to transfer chunks of memory at a time from the main memory to the cache, and
then to access individual memory locations one at a time from the cache.
• User processes must be restricted so that they only access memory locations that
"belong" to that particular process. This is usually implemented using a base register and
a limit register for each process, as shown in below figures a and b. Every memory access
made by a user process is checked against these two registers, and if a memory access is
attempted outside the valid range, then a fatal error is generated. The OS has access to all
existing memory locations, as this is necessary to swap users' code and data in and out of
memory. It should also be obvious that changing the contents of the base and limit registers
is a privileged activity, allowed only to the OS kernel.
Figure a: A base and a limit register define a logical address space
Address Binding
• User programs typically refer to memory addresses with symbolic names such as
"i", "count", and "average Temperature". These symbolic names must be mapped or bound
to physical memory addresses, which typically occur in several stages.
• Load Time - If the location at which a program will be loaded is not known at
compile time, then the compiler must generate relocatable code, which references
addresses relative to the start of the program. If that starting address changes, then the
program must be reloaded but not recompiled.
• Addresses bound at compile time or load time have identical logical and physical
addresses.
• Addresses created at execution time, however, have different logical and physical
addresses.
In this case, the logical address is also known as a virtual address, and the two terms are
used interchangeably in our text.
• The set of all logical addresses used by a program composes the logical address
space, and the set of all corresponding physical addresses composes the physical address
space.
• The MMU can take on many forms. One of the simplest is a modification of the
base register scheme described earlier.
• The base register is now termed a relocation register, whose value is added to every
memory request at the hardware level.
• Note that user programs never see physical addresses. User programs work entirely
in logical address space, and any memory references or manipulations are done using
purely logical addresses. Only when the address gets sent to the physical memory chips is
the physical memory address generated.
The below figure shows dynamic relocation using a relocation register
•
•
Dynamic Loading
• Rather than loading an entire program into memory at once, dynamic loading loads up
each routine as it is called. The advantage is that unused routines need never be loaded,
reducing total memory usage and generating faster program startup times. The downside
is the added complexity and overhead of checking to see if a routine is loaded every time
it is called and then loading it up if it is not already loaded.
Dynamic Linking and Shared Libraries
• With static linking library modules get fully included in executable modules,
wasting both disk space and main memory usage, because every program that included a
certain routine from the library would have to have its copy of that routine linked into their
executable code.
• With dynamic linking, however, only a stub is linked into the executable module,
containing references to the actual library module linked in at run time.
• This method saves disk space because the library routines do not need to be fully
included in the executable modules, only the stubs.
• We will also learn that if the code section of the library routines is re-entrant(
meaning it does not modify the code while it runs, making it safe to re-enter it ), then the
main memory can be saved by loading only one copy of dynamically linked routines into
memory and sharing the code amongst all processes that are concurrently using it. (Each
process would have its copy of the data section of the routines, but that may be small
relative to the code segments.) The OS must manage shared routines in memory.
• An added benefit of dynamically linked libraries (DLLs, also known as shared
libraries or shared objects on UNIX systems) involves easy upgrades and updates.
When a program uses a routine from a standard library and the routine changes, then the
program must be rebuilt (re-linked) to incorporate the changes. However, if DLLs are used,
then as long as the stub does not change, the program can be updated merely by loading
new versions. a program can specify a version of the DLL if necessary.
• In practice, the first time a program calls a DLL routine, the stub will recognize the
fact and will replace itself with the actual routine from the DLL library. Further calls to the
same routine will access the routine directly and not incur the overhead of the stub access.
of the DLLs onto the system. Version information is maintained in both the program and
the
Swapping
• If there is not enough memory available to keep all running processes in memory at the
same time, then some processes that are not currently using the CPU may have their
memory swapped out to a fast local disk called the backing store.
Standard Swapping
• much it might use. Programmers can help with this by freeing up dynamic memory that
they are no longer using.
• It is important to swap processes out of memory only when they are idle, or more to the
point, only when there are no pending I/O operations. ( Otherwise, the pending I/O
operation could be written into the wrong process's memory space. ) The solution is to
either swap only idle processes or do I/O operations only into and out of OS buffers,
which are then transferred to or from the process's main memory as a second step.
• Most modern OSes no longer use swapping, because it is too slow and there are faster
alternatives available. ( e.g. Paging. ) However, some UNIX systems will still invoke
swapping if the system gets extremely full, and then discontinue swapping when the load
reduces again.
Windows 3.1 would use a modified version of swapping that was somewhat controlled by
the user, swapping processes out if necessary and then only swapping them back in when
the user focused on that particular window.
• Below figure shows the swapping of two processes using a disk as a backing store.
Memory Allocation
• One method of allocating contiguous memory is to divide all available memory
into equal-sized partitions and to assign each process to its partition. This restricts both the
number of simultaneous processes and the maximum size of each process and is no longer
used.
There are many different strategies for finding the "best" allocation of memory to
processes, including the three most commonly discussed
• First fit - Search the list of holes until one is found that is big enough to satisfy the
request and assign a portion of that hole to that process. Whatever fraction of the hole not
needed by the request is left on the free list as a smaller hole. Subsequent requests may
start looking either from the beginning of the list or from the point at which this search
ended.
• Best fit—Allocate the smallest hole big enough to satisfy the request. This saves
large holes for other process requests that may need them later, but the resulting unused
portions of holes may be too small to be of any use and will, therefore, be wasted. Keeping
the free list sorted can speed up finding the right hole.
• Worst fit - Allocate the largest hole available, thereby increasing the likelihood that
the remaining portion will be usable for satisfying future requests.
• Simulations show that either the first or best fit is better than the worst fit regarding
both time and storage utilization. The first and best fits are about equal in terms of storage
utilization, but the first fit is faster.
Fragmentation
• All the memory allocation strategies suffer from external fragmentation, though
the first and best fits experience the problems more so than the worst fit. External
fragmentation means that the available memory is broken up into lots of little pieces, none
of which is big enough to satisfy the next memory requirement, although the sum total
could.
• The amount of memory lost to fragmentation may vary with algorithm, usage
patterns, and some design decisions such as which end of a hole to allocate and which end
to save on the free list.
• Statistical analysis of the first fit, for example, shows that for N blocks of allocated
memory, another 0.5 N will be lost to fragmentation.
• Internal fragmentation also occurs, with all memory allocation strategies. This is
caused by the fact that memory is allocated in blocks of a fixed size, whereas the actual
memory needed will rarely be that exact size.
For a random distribution of memory requests, on average 1/2 block will be wasted per
memory request, because on average the last allocated block will be only half full.
• Note that the same effect happens with hard drives and that modern hardware gives
us increasingly larger drives and memory at the expense of ever larger block sizes, which
translates to more memory lost to internal fragmentation.
• Some systems use variable-size blocks to minimize losses due to internal
fragmentation.
• If the programs in memory are relocatable, ( using execution-time address binding
), then the external fragmentation problem can be reduced via compaction, i.e. moving all
processes down to one end of physical memory. This only involves updating the relocation
register for each process, as all internal work is done using logical addresses.
• Another solution as we will see in upcoming sections is to allow processes to use
non-contiguous blocks of physical memory, with a separate relocation register for each
block.
Segmentation
• Segmentation is a memory management technique in which each job is divided into
several segments of different sizes, one for each module that contains pieces that perform
related functions.
• Each segment is a different logical address space of the program.
Basic Method
• Most users ( programmers ) do not think of their programs as existing in one
continuous linear address space.
• Rather they tend to think of their memory in multiple segments, each dedicated to
a particular use, such as code, data, the stack, the heap, etc.
• Memory segmentation supports this view by providing addresses with a segment
number ( mapped to a segment base address ) and an offset from the beginning of that
segment.
• For example, a C compiler might generate 5 segments for the user code, library
code, global ( static ) variables, the stack, and the heap, as shown in the below figure.
Segmentation Hardware
A segment table maps segment-offset addresses to physical addresses, and simultaneously
checks for invalid addresses, using a system like the page tables and relocation base
registers discussed previously. (Note that at this point in the discussion of segmentation,
each segment is kept in contiguous memory and may be of different sizes, but that
segmentation can also be combined with paging as we shall see shortly.)
Paging
• A computer can address more memory than the amount physically installed on the
system. This extra memory is actually called virtual memory and it is a section of a hard
that's set up to emulate the computer's RAM. The paging technique plays an important role
in implementing virtual memory.
• Paging is a memory management technique in which process address space is
broken into blocks of the same size called pages (size is power of 2, between 512 bytes
and 8192 bytes). The size of the process is measured in the number of pages.
• Due to the equal size of the pages and frames, swapping becomes very easy. • Page
table requires extra memory space, so may not be good for a system having small RAM.
Virtual memory
• Virtual memory involves the separation of logical memory as perceived by users from
physical memory.
• Code needs to be in memory to execute, but the entire program is rarely used.
• Error code, unusual routines, large data structures
• Entire program code is not needed at the same time.
• Consider the ability to execute partially-loaded
• Program no longer constrained by limits of physical memory
• Each program takes less memory while running -> more programs run at the same time
or turnaround time
• Less I/O needed to load or swap programs into memory ->each user program runs faster
• Separation of user logical memory from physical memory
• Only part of the program needs to be in memory for execution
• Logical address space can therefore be much larger than physical address space
• Allows address spaces to be shared by several processes
• When a context switch occurs, the operating system does not copy any of the old
program’s pages out to the disk or any of the new program’s pages into the main memory
Instead, it just begins executing the new program after loading the first page and fetches
that program’s pages as they are referenced.
• While executing a program, if the program references a page which is not available
in the main memory because it was swapped out a little ago, the processor treats this invalid
memory reference as a page fault and transfers control from the program to the operating
system to demand the page back into the memory.
Advantages
Page fault
• A page fault occurs when a program attempts to access a block of memory that is not
stored in the physical memory, or RAM.
• The fault notifies the operating system that it must locate the data in virtual memory, and
then transfer it from the storage device, such as an HDD or SSD, to the system RAM.
• Paging happens whenever a page fault occurs and a free page cannot be used for
allocation purposes accounting to the reason that pages are not available or the number of
free pages is lower than the required pages.
• When the page that was selected for replacement and was paged out, is referenced
again, it has to read in from disk, and this requires I/O completion. This process determines
the quality of the page replacement algorithm: the lesser the time waiting for page-ins, the
better is the algorithm.
• A page replacement algorithm looks at the limited information about accessing the
pages provided by hardware, and tries to select which pages should be replaced to
minimize the total number of page misses while balancing it with the costs of primary
storage and processor time of the algorithm itself.
Copy-on-write
• Copy on write sometimes referred to as implicit sharing or shadowing, is a resource
management technique used in computer programming to efficiently implement a
"duplicate" or "copy" operation on modifiable resources.
• Modifications must still create a copy, hence the technique: the copy operation is
deferred to the first write.
Allocation of Frames
• An important aspect of operating systems, virtual memory is implemented using demand
paging. Demand paging necessitates the development of a pagereplacement algorithm
and a frame allocation algorithm.
• Frame allocation algorithms are used if you have multiple processes; it help decide how
many frames to allocate to each process.
• There are various constraints to the strategies for the allocation of frames.
• You cannot allocate more than the total number of available frames.
• At least a minimum number of frames should be allocated to each process. This
constraint is supported by two reasons. The first reason is, as less number of frames are
allocated, there is an increase in the page fault ratio, decreasing the performance of the
execution of the process. Secondly, there should be enough frames to hold all the
different pages that any single instruction can reference.
Thrashing
• Thrashing is a condition or a situation when the system is spending a major portion
of its time servicing the page faults, but the actual processing done is very negligible.
• The basic concept involved is that if a process is allocated too few frames, then
there will be too many and too frequent page faults. As a result, no useful work would be
done by the CPU and the CPU utilization would fall drastically.
• The long-term scheduler would then try to improve the CPU utilization by loading
some more processes into the memory thereby increasing the degree of multiprogramming.
This would result in a further decrease in the CPU utilization triggering a chained reaction
of higher page faults followed by an increase in the degree of multiprogramming, called
Thrashing.
File Management System File
• File system is the most visible aspect of an operating system. It provides the mechanism
for online.
• storage and access to both data and programs of the operating system and all the users of
the operating system.
• A directory structure- which organizes and provides information about all the files in the
system.
File concept
• A file is a named collection of related information that is recorded on the secondary
• storage
. • A file directory is a group of files organized together an entry within a directory refers
to
• the directory or another file. Hence, a tree structure or hierarchy can be formed. • The
directories are used to group files belonging to different applications or users
• The Directory enables files to be separated based on user and user application. Thus
simplifying system management issues like backup recovery, security, integrity, name
collision problems (file name clashes), housekeeping of files, etc.
File Attributes
• A file’s attributes vary from one operating system to another but typically consist of the
following
• Name: the symbolic file name is the only information kept in human-readable form.
• Identifier: this unique tag, usually a number, identifies the file within the file system. It
is the non-human-readable form.
• Type: this information is needed for systems that supports different types of files.
• Location: This information is a pointer to a device and to the location of the file on that
device.
• Size: the current size of the file (in bytes, words, or blocks) and possibly the maximum
allowed size is included in this attribute.
• Time, Date, and user identification: this information may be kept for creation,
last
• modification, and last use. These data can be useful for protection, security, and usage
monitoring.
File Operations
To define a file properly, we need to consider the following operations that can be
performed on files.
• Creating a file
• Two steps are necessary to create a file. First, space in the file system
• must be found for the file. Second, an entry for a new file must be made in the directory.
• Writing a file:
To write a file, we make a system call specifying both the name of the file
• Repositioning within a file need not involve any actual I/O. This file operation is also
called a file seek.
• Deleting a file
• Truncating a file
• The user may want to erase the contents of the file but keep its attributes.
• Rather than forcing the user to delete the file and then recreate it, this function allows all
attributes to remain unchanged except for file length.
File Types
• A common technique for implementing file types is to include the type as part of the file
• name.
• The name is split into two parts: a name and an extension, usually separated by a period
character.
• In this way, the user and the operating system can tell from the name alone what type of
the file is.
• File name examples include [Link], [Link], and Reader thread.c. The system
uses the extension to indicate the type of operations that can be done on that file.
• Only a file with .com, .exe, or .bat extension can be executed, for instance. The .com and
.exe files are two forms of binary executable files, whereas a .bat file is a batch file
containing, in ASCII format, commands to the operating system
• A sequential access is that in which the records are accessed in some sequence, i.e., the
information in the file is processed in order, one record after the other.
This access method is the most primitive one.
• The idea of Sequential access is based on the tape model which is a sequential access
device. We consider the Sequential access method to be best because most of the records
in a file are to be processed. For example, transaction files.
• The disk is a direct access device which gives us the reliability to random access
of any file block. In the file, there is a collection of physical blocks and the records of those
blocks.
• Ex: Databases are often of this type since they allow query processing that involves
immediate access to large amounts of information. All reservation systems fall into this
category.
• Not all operating systems support direct access files. The sequential and direct
access of the file is defined at the time of creation and accessed accordingly later. Direct
access to a sequential file is not possible but Sequential access to a direct access file is
possible
• The key is an attribute that uniquely identifies a record. We can say that If more
than one index is present the other ones are alternate indexes. The creation of the indexes
is done with the file but maintained by the system.
• The main idea of this method is to first access the file directly and then it accesses
sequentially. In this access method, it is necessary to maintain an index. The index is
nothing but a pointer to a block. The direct access of the index is made to access a record
in a file.
• The information which is obtained from this access is used to access the file.
Sometimes the indexes are very big. So to maintain all these hierarchies of indexes are
built in which one direct access of an index leads to information of another index access.
• The main advantage in this type of access is that both direct and sequential access
of files is possible with the help of this method.
Directory overview
• A file directory is a group of files organized together on entry within a directory or
another file. Hence, a tree structure or hierarchy can be formed.
• The directories are used to group files belonging to different applications or users.
• Directories enable files to be separated based on user and user applications. Thus,
simplifying system management issues like backup recovery, security, integrity, name
collision problems (file name clashes), etc • The device directory records information
like name, location, size, and type for all the files on the partition. A root refers to the
part of the disk from where the root directory begins, which points to the user directories,
the root directory is distinct from sub-directories in that it is in a fixed position and of
fixed size.
• Following are the operations that can be performed on a directory or file system. • Search
for file: we need to be able to search a directory structure to find the entry for a particular
file. Since files have symbolic names and similar names may indicate a relationship
between files, we may want to be able to find all files whose names match a particular
pattern.
• Create a file: new files need to be created and added to the directory.
• Delete a file: when a file is no longer needed, we want to be able to remove it from the
directory.
• List a directory: we need to be able to list the files in a directory and the contents of the
directory entry for each file in the list.
• Rename a file: because the name of the file represents its contents to its users, we must
be able to change the name when the contents or use of the file changes. • Renaming a
file: This may also allow its position within the directory structure to be changed.
• Traverse the file system: we may wish to access every directory and every file within a
directory structure. Often, we do this by copying all files to magnetic tape. This technique
provides a backup copy in case of system failure.
Single-level directory
Two-level directory
Tree-structured directory
• The location in VFS where the newly mounted medium was registered is called the
mount point; when the mounting process is completed, the user can access files and
directories on the medium from there.
• Normally, when the computer is shutting down, every mounted storage will
undergo an unmounting process to ensure that all queued data got written, and to preserve
integrity of file system structure on the media.
File sharing is the public or private sharing of computer data or space in a network with
various levels of access privilege.
• While files can easily be shared outside a network (for example, simply by handing
or mailing someone your file on a diskette), the term file sharing almost always means
sharing files in a network, even if in a small local area network.
• File sharing allows a number of people to use the same file or file by some
combination of being able to read or view it, write to or modify it, copy it, or print it.
• Typically, a file sharing system has one or more administrators. Users may all have
the same or may have different levels of access privilege. File sharing can also mean
having an allocated amount of personal file storage in a common file system.
• FTP can be used to access (read and possibly write to) files shared among a
particular set of users with a password to gain access to files shared from an FTP server
site.
• Most Web site developers use FTP to upload new or revised Web files to a Web
server, and indeed the World Wide Web itself can be thought of as large-scale file sharing
in which requested pages or files are constantly being downloaded or copied down to the
Web user.
• File sharing implies a system in which users write to as well as read files or in
which users are allotted some amount of space for personal files on a common server,
giving access to other users as they see fit.
• Any multi-user operating system will provide some form of file sharing. Among
the best known network file systems is (not surprisingly) the Network File System (NFS).
• Originally developed by Sun Microsystems for its UNIX-based systems, it lets you
read and, assuming you have permission, write to sharable files as though they were on
your own personal computer.
• Files can also be shared in file systems distributed over different points in a
network. File sharing is involved in groupware and several other types of applications.
Protection
• When information is stored in a computer system, we want to keep it safe from physical
damage (the issue of reliability) and improper access (the issue of protection).
• Bugs in the file-system software can also cause file contents to be lost.
• Protection can be provided in many ways. For a single-user laptop system, we might
protect by locking the computer in a desk drawer or file cabinet.
In a larger multiuser system, however, other mechanisms are needed.
• Delete. Delete the file and free its space for possible reuse.
• Contiguous Allocation
• Linked Allocation
• Indexed Allocation
All three methods have their advantages and disadvantages as discussed below:
1. Contiguous Allocation
In this scheme, each file occupies a contiguous set of blocks on the disk. For example, if a
file requires n blocks and is given a block b as the starting location, then the blocks
assigned to the file will be b, b+1, b+2,……b+n-1. This means that given the starting block
address and the length of the file (in terms of blocks required), we can determine the blocks
occupied by the file.
The file ‘mail’ in the following figure starts from block 19 with length = 6 blocks.
Advantages:
• Both the Sequential and Direct Accesses are supported by this. For direct access,
the address of the kth block of the file which starts at block b can easily be obtained
as (b+k).
• This is extremely fast since the number of seeks are minimal because of contiguous
allocation of file blocks.
Disadvantages:
• This method suffers from both internal and external fragmentation. This makes it
inefficient in terms of memory utilization.
• Increasing file size is difficult because it depends on the availability of contiguous
memory at a particular instance.
2. Linked List Allocation
• In this scheme, each file is a linked list of disk blocks that need not be contiguous.
The disk blocks can be scattered anywhere on the disk. The directory entry contains
a pointer to the starting and ending file blocks. Each block contains a pointer to the
next block occupied by the file.
• The file ‘jeep’ in the following image shows how the blocks are randomly
distributed. The last block (25) contains -1 indicating a null pointer and does not
point to any other block.
Advantages:
• This is very flexible in terms of file size. File size can be increased easily since the
system does not have to look for a contiguous chunk of memory.
• This method does not suffer from external fragmentation. This makes it relatively
better in terms of memory utilization.
Disadvantages:
• Because the file blocks are distributed randomly on the disk, a large number of
seeks are needed to access every block individually. This makes linked allocation
slower.
• It does not support random or direct access. We can not directly access the blocks
of a file. A block k of a file can be accessed by traversing k blocks sequentially
(sequential access ) from the starting block of the file via block pointers.
• Pointers required in the linked allocation incur some extra overhead.
3. Indexed Allocation
In this scheme, a special block known as the Index block contains the pointers to all the
blocks occupied by a file. Each file has its index block. The ith entry in the index block
contains the disk address of the ith file block. The directory entry contains the address of
the index block as shown in the image:
Advantages:
• This supports direct access to the blocks occupied by the file and therefore provides
fast access to the file blocks.
• It overcomes the problem of external fragmentation.
Disadvantages:
• The pointer overhead for indexed allocation is greater than linked allocation.
• For very small files, say files that expand only 2-3 blocks, the indexed allocation
would keep one entire block (index block) for the pointers which
• is inefficient in terms of memory utilization. However, in linked allocation, we lose
the space of only 1 pointer per block.
A Bitmap or Bit Vector is a series or collection of bits where each bit corresponds to a disk
block. The bit can take two values: 0 and 1: 0 indicates that the block is free and 1 indicates
an allocated block. The given instance of disk blocks on the disk in Figure 1 (where green
blocks are allocated) can be represented by a bitmap of 16 bits as 1111000111111001.
2. Linked List
In this approach, the free disk blocks are linked together i.e. a free block contains a pointer
to the next free block. The block number of the very first disk block is stored at a separate
location on the disk and is also cached in memory.
In Figure-2, the free space list head points to Block 5 which points to Block 6, the next
free block and so on. The last free block would contain a null pointer indicating the end of
free list. A drawback of this method is the I/O required for free space list traversal.
Advantages:
• Dynamic allocation in Linked List is easy, thus can add the space as per the
requirement dynamically.
Disadvantages:
• When the size of the Linked List increases, the headache of miniating pointers also
increases.
• This method is not efficient during iteration of each block of memory.
Grouping
This approach stores the address of the free blocks in the first free block. The first free
block stores the address of some, say n free blocks. Out of these n blocks, the first n-1
blocks are free and the last block contains the address of the next free n blocks. An
advantage of this approach is that the addresses of a group of free disk blocks can be
found easily.
Advantage:
• Finding free blocks in massive amounts can be done easily using this method.
Disadvantage:
• The only disadvantage is, that we need to alter the entire list if any of the blocks of
the list is occupied.
Counting
This approach stores the address of the first free disk block and a number n of free
contiguous disk blocks that follow the first block. Every entry in the list would contain:
• A number n.
Advantages:
• Using this method, a group of entire free blocks can take place easily and Fastly.
• The list formed in this method is especially smaller in size.
Disadvantage:
• The first free block in this method, keeps account of other free blocks. Thus, due
to that one block the space requirement is more.
Recovery
FSCK is one approach still used by older Linux-based systems to find and repair
inconsistencies. It is not a complete solution and may still have inodes pointing to garbage
data. The major focus is to make the metadata internally consistent.
The following are the checks that FSCK performs to achieve consistency:
• SuperblockChecks:
FSCK performs a sanity check to see if the file size is greater than the number of
blocks allocated. In this case, it tries to find the suspect superblock and use an
alternate copy instead.
• FreeBlockChecks:
FSCK also scans inodes to ensure that blocks in inodes are marked allocated.
• InodeStateChecks:
FSCK checks inodes for corruption. Corrupted inodes are simply cleared.
• InodeLinkChecks:
FSCK counts the number of links to an inode and modifies the inode count. If an
allocated inode has no directory or file referring to it, FSCK moves it to the lost
and found directory.
• DuplicatePointers:
FSCK checks duplicate pointers. For example, if two inodes have pointers to the
same data block, one of the inodes can be deleted.
• BadBlocks:
A bad pointer is simply one that points to a memory address that is out of range. In
this case, FSCK deletes the pointer.
• DirectoryChecks:
FSCK makes sure the directory format is correct, e.g. they should start with “.” and
“..”.
Advantages of FSCK:
Disadvantages of FSCK:
• Scanning the disk, again and again, is slow and infeasible for large disk sizes.
• It requires a heavy understanding and prior knowledge of the file system. As file
systems continue to evolve, it is difficult to keep track of every nuance.
data – documents, media files, configuration and registry files, machine images, etc.
required to perform the workload on your server. In essence, any data that you desire to
keep can be saved as backup data.
The main goal of backup is to generate a copy of the data that can be recovered if the
primary data fails. Failure can be – hardware or software failures, data corruption, or a
human-initiated event such as an attack (virus or malware) or data deletion by an accident.
The act of backing up your data in the case of a loss and putting up secure mechanisms
that allow you to recover your data, as a result, this process is known as data backup and
recovery. It copies and preserves data in order to keep it available in the event of data loss
or damage. Suppose you have backed up your data, so you can only recover data from a
previous point in time. Data backup is a type of disaster recovery that should be included
in every plan for disaster recovery.
Backup copies should be made on a constant, regular basis for optimal outcomes since this
will reduce the amount of data lost between backups. When recovering from a backup, the
longer the gap between backup copies, the greater the risk of data loss.
Recovery
Data recovery, often known as a restore, is required when data of any sort is no longer
readable or has been corrupted by a malicious alteration. The act, process, or occurrence
of recovering data following inadvertent loss or corruption is known as recovery. The cost
of recovering data is high.
In this hierarchy, all the storage devices are arranged according to speed and cost. The
higher levels are expensive, but they are fast. As we move down the hierarchy, the cost per
bit generally decreases, whereas the access time generally increases. The storage systems
above the Electronic disk are Volatile, whereas those below are Non-Volatile.
Magnetic Disk
A magnetic Disk is a type of secondary memory that is a flat disc covered with a magnetic
coating to hold information. It is used to store various programs and files. The polarized
information in one direction is represented by 1, and vice versa. The direction is indicated
by 0.
Magnetic disks are less expensive than RAM and can store large amounts of data, but the
data access rate is slower than main memory because of secondary memory. Data can be
modified or can be deleted easily in the magnetic disk memory. It also allows random
access to data.
Floppy Disks
A floppy disk is a detachable, flexible magnetic storage device that may hold computer
files or other electronic data. It is composed of a flexible and thin magnetic storage disk
that is enclosed inside a rectangular plastic carrier that has a fabric lining for increased
sturdiness. Data can be read and written because the disk is magnetic. Writes and reads
floppy disks with a floppy disk drive.
Floppy disks also known as floppy diskettes, floppy disks or floppy disks, are a type of
storage medium that can read data storage information and are used to store electronic
data. Unlike CD-ROM, it was 8 inches in diameter when it was initially created, and users
could not write data to it.
Although later versions of this disk could hold up to 800KB of data, it could only hold
80KB at a time. These days, network file transfers and USB devices have taken the position
of floppy disks, however they are no longer in use.
• 8-inch Drive: The First ever floppy design to be adopted as a read-only format
before being able to read and write which was introduced in the early 1970s was
the 8-inch Drive. The physical characteristic that permitted the floppy drive series
its name was floppy.
• Zip Drive: The Zip drive was introduced by Iomega Corporation. Since zip drives
were peripheral, they were primarily able to enhance an alreadyexisting system.
Due to its high cost, this drive was not widely utilized and never really took off as
a storage device.
• 3.5" Drive: 3.5" Drive is another kind of floppy disk. Conversely, a 3.5′′ drive is
typically found in desktop computers and servers and has a bigger diameter of 3.5
inches.
• 5.25" Drive: A 5.25" is a floppy disk drive that was a common computer accessory.
During the early nineties, computers with capacities ranging from 360 kilobytes to
1.2 gigabytes were also armed with floppy disk drives.
• Flexibility and comparability: Floppy drives have several benefits, one of which
is their flexibility and comparably small size. 3.5" Drive floppy disks are smaller
in size than Compact Disks.
• Cost: Less costly than other storage devices portable and non-volatile which means
that data stored on them won't be lost when the system is powered down and
compatible with the majority of computers.
• Compose Protection: Floppy disks also include a little score that provides a
feature called Write Protection, even with plastic wrapping.
• Boot Disk: Floppy disk drives are often situated above the primary hard drives in
the boot order sequence.
• Capacity: The main disadvantage of these disks is their smaller capacity for
storing data when compared to more modern technologies like CDs, which have
an average capacity of 650–700 MB per disk.
• Dependability: A floppy disk drive and a variable capacity source were absent
from a vast majority of PCs.
• Easily Broke: The floppy disk, which is extremely flexible and fragile, was made
using a plastic shell. If someone handles it carelessly, it might break easily.
• Data Deletion: If the disk comes into touch with a magnetic field side, then data
may be deleted.
• Magnetic drums, magnetic tape, and magnetic disks are types of magnetic memory.
These memories use the property of magnetic memory. Here, we have explained
about magnetic tape in brief.
Disadvantages:
1. Sequential access is the disadvantage, means it does not allow access randomly or
directly.
2. It requires caring to store, i.e., vulnerable humidity, dust-free, and suitable
environment.
3. It stored data cannot be easily updated or modified, i.e., difficult to make updates
on data.
There are several Disk Several Algorithms. We will discuss in detail each one of them.
• SCAN
• C-SCAN
• LOOK
• C-LOOK
So, total overhead movement (total distance covered by the disk arm) =
(82-50)+(170-82)+(170-43)+(140-43)+(140-24)+(24-16)+(190-16) =642
Advantages of FCFS Here are some of the advantages of First Come First Serve.
• No indefinite postponement
Disadvantages of FCFS
are scheduled according to their calculated seek time. As a result, the request near the disk
arm will get executed first. SSTF is certainly an improvement over FCFS as it decreases
the average response time and increases the throughput of the system. Let us understand
this with the help of an example.
total overhead movement (total distance covered by the disk arm) = (50-43)+(43-
24)+(24-16)+(82-16)+(140-82)+(170-140)+(190-170) =208
• Can cause Starvation for a request if it has a higher seek time as compared to
incoming requests
• The high variance of response time as SSTF favors only some requests
3. SCAN
In the SCAN algorithm the disk arm moves in a particular direction and services the
requests coming in its path and after reaching the end of the disk, it reverses its
direction and again services the request arriving in its path. So, this algorithm works
as an elevator and is hence also known as an elevator algorithm. As a result, the
requests at the midrange are serviced more and those arriving behind the disk arm will
have to wait.
Example:
SCAN Algorithm
Therefore, the total overhead movement (total distance covered by the disk arm) is
calculated as
• High throughput
• Long waiting time for requests for locations just visited by disk arm
4. C-SCAN
In the SCAN algorithm, the disk arm again scans the path that has been scanned, after
reversing its direction. So, it may be possible that too many requests are waiting at the
other end or there may be zero or few requests pending at the scanned area.
These situations are avoided in the CSCAN algorithm in which the disk arm instead of
reversing its direction goes to the other end of the disk and starts servicing the requests
from there. So, the disk arm moves in a circular fashion and this algorithm is also similar
to the SCAN algorithm hence it is known as C-SCAN (Circular SCAN).
Example:
Circular SCAN
So, the total overhead movement (total distance covered by the disk arm) is calculated
as: =(199-50) + (199-0) + (43-0) = 391
Advantages of C-SCAN Algorithm
5. LOOK
LOOK Algorithm is similar to the SCAN disk scheduling algorithm except for the
difference that the disk arm in spite of going to the end of the disk goes only to the last
request to be serviced in front of the head and then reverses its direction from there
only. Thus it prevents the extra delay which occurred due to unnecessary traversal to
the end of the disk.
Example:
LOOK Algorithm
So, the total overhead movement (total distance covered by the disk arm) is calculated
as:
6. C-LOOK
Example:
6. C-LOOK
So, the total overhead movement (total distance covered by the disk arm) is calculated
as
= (190-50) + (190-16) + (43-16) = 341
Disk management is one of the critical operations carried out by the operating system.
It deals with organizing the data stored on the secondary storage devices which
includes the hard disk drives and the solid-state drives. It also carries out the function
of optimizing the data and making sure that the data is safe by implementing various
risk management techniques. We will learn more about disk management and its
related techniques found in operating systems.
system is responsible for configuring the file system, ensuring the safety and reliability
of reading and write operations to secondary storage, and maintains access times (the
time required to write data to or read data from secondary storage).
• Disk Format
Divides the disk into sectors before storing data so that the disk controller can read
and write Each sector can be:
The header retains information, data, and error correction code (ECC) sectors of data,
typically 512 bytes of data, but optional disks use the operating system’s own data
structures to preserve files using disks.
1. Divide the disc into multiple cylinder groups. Each is treated as a logical disk.
2. Logical format or “Create File System”. The OS stores the data structure of the
first file system on the disk. Contains free space and allocated space.
For efficiency, most file systems group blocks into clusters. Disk I / O runs in blocks.
File I / O runs in a cluster.
For example, the sizes can be 256,512, and 1,024 bytes. If the disk is formatted with a
larger sector size, fewer sectors can fit on each track.
As a result, fewer headers and trailers are written on each track and more space is
obtainable for user data. – Some operating systems can handle a sector size of 512
bytes.
The operating system keeps its data structures on disk before it uses the disk to store
the files. It performs this with the following two steps:
1. It partitions the disk into one or more groups of cylinders. Each partition is treated
by the OS as a separate disk.
In order to increase efficiency, the file system groups blocks in chunks called as
clusters.
Some operating systems give special programs the ability to use a disk partition as a
large sequential array of logical blocks, without any file-system data structures. This
array is sometimes called the raw disk, and I/O to this array is called as raw I/O.
Boot block:
• When the computer is turned on or restarted, the program stored in the initial
bootstrap ROM finds the location of the OS kernel from the disk, loads the kernel
into memory, and runs the OS. start.
• To change the bootstrap code, you need to change the ROM and hardware chip.
Only a small bootstrap loader program is stored in ROM instead.
• The full bootstrap code is stored in the “boot block” of the disk.
• The bootstrap program is required for a computer to initiate the booting after it is
powered up or rebooted.
• The bootstrap program then locates the OS kernel on disk, loads that kernel into
memory, and jumps to an initial address to start the operatingsystem execution.
• The Read Only Memory (ROM) does not require initialization and is at a fixed
location that the processor can begin executing when powered up or reset.
Therefore bootstrap is stored in ROM.
• Therefore, most systems store a small bootstrap loader program in the boot ROM
which invokes and bring full bootstrap program from disk into main memory.
• The modified version of full bootstrap program can be simply written onto the
disk.
• The fixed storage location of full bootstrap program is in the “boot blocks”.
• A disk that has a boot partition is called a boot disk or system disk.
Bad Blocks:
• Most disks are even stuffed from the factory with bad blocks and are handled in a
variety of ways.
• The controller can instruct each bad sector to be logically replaced with one of the
spare sectors. This scheme is known as sector sparing or transfer.
• However, unrecoverable hard errors may result in data loss and require manual
intervention.
1. Complete, means there is no way other than replacing the disk. Back up of content
must be taken on new disk.
3. After manufacturing, the bad blocks exist. Depending on the disk and controller in
use, these blocks are handled in a different ways.
Disk management in operating systems involves organizing and maintaining the data
on a storage device, such as a hard disk drive or solid-state drive. The main goal of
disk management is to efficiently utilize the available storage space and ensure data
integrity and security.
1. Partitioning: This involves dividing a single physical disk into multiple logical
partitions. Each partition can be treated as a separate storage device, allowing for
better organization and management of data.
2. Formatting: This involves preparing a disk for use by creating a file system on it.
This process typically erases all existing data on the disk.
3. File system management: This involves managing the file systems used by the
operating system to store and access data on the disk. Different file systems have
different features and performance characteristics.
4. Disk space allocation: This involves allocating space on the disk for storing files
and directories. Some common methods of allocation include contiguous
allocation, linked allocation, and indexed allocation.
5. Disk defragmentation: Over time, as files are created and deleted, the data on a disk
can become fragmented, meaning that it is scattered across the disk. Disk
defragmentation involves rearranging the data on the disk to improve performance.
3. Increased risk of data loss due to errors during disk management tasks.
Advantages:
2. Improved system performance: By using virtual memory, the operating system can
swap out less frequently used data from physical memory to disk, freeing up space
for more frequently used data and improving system performance.
3. Flexibility: Swap-space management allows the operating system to dynamically
allocate and deallocate memory as needed, depending on the demands of running
applications.
Disadvantages:
Slower access times: Accessing data from disk is slower than accessing data from physical
memory, which can result in slower system performance if too much swapping is required.
Increased disk usage: Swap-space management requires disk space to be reserved for use
as virtual memory, which can reduce the amount of available space for other data storage
purposes.
Risk of data loss: In some cases, if there is a problem with the swap file, such as a disk
error or corruption, data may be lost or corrupted.
Overall, swap-space management is a useful technique for optimizing memory usage and
improving system performance. However, it is important to carefully manage swap space
allocation and monitor system performance to ensure that excessive swapping does not
negatively impact system performance.
Swap-Space :
The area on the disk where the swapped-out processes are stored is called swap space.
Swap-space management :
Swap-Space management is another low-level task of the operating system. Disk space is
used as an extension of main memory by the virtual memory. As we know the fact that
disk access is much slower than memory access, In the swap space management we are
using disk space, so it will significantly decrease system performance. Basically, in all our
systems we require the best throughput, so the goal of this swap-space implementation is
to provide the virtual memory the best throughput. In this article, we are going to discuss
how swap space is used, where swap space is located on disk, and how swap space is
managed.
Swap-Space Use :
Swap space is used by the different operating systems in various ways. The systems which
are implementing swapping may use swap space to hold the entire process which may
include image, code, and data segments. Paging systems may simply store pages that have
been pushed out of the main memory. The need to swap space on a system can vary from
megabytes to gigabytes but it also depends on the amount of physical memory, the virtual
memory it is backing, and how it is using the virtual memory.
It is safer to overestimate than to underestimate the amount of swap space required because
if a system runs out of swap space it may be forced to abort the processes or may crash
entirely. Overestimation wastes disk space that could otherwise be used for files, but it
does not harm other.
The following table shows a different system using an amount of swap space:
Let, if the swap space is simply a large file within the file system. To create it, name it, and
allocate its space normal file-system routines can be used. This approach, though easy to
implement, is inefficient. Navigating the directory structures and the disk-allocation data
structures takes time and extra disk access. During reading or writing of a process image,
external fragmentation can greatly increase swapping times by forcing multiple seeks.
There is also an alternate to create the swap space which is in a separate raw partition.
There is no presence of any file system in this place. Rather, a swap space storage manager
is used to allocate and de-allocate the blocks. from the raw partition. It uses the algorithms
for speed rather than storage efficiency because we know the access time of swap space is
shorter than the file system. By this Internal fragmentation increases, but it is acceptable
because the life span of the swap space is shorter than the files in the file system. The raw
partition approach creates a fixed amount of swap space in case of disk partitioning.
Some operating systems are flexible and can swap both in raw partitions and in the file
system space, for example, Linux.
Swap-Space Management: An
Example –
The traditional UNIX kernel started with an implementation of swapping that copied the
entire process between contiguous disk regions and memory. UNIX later evolved to a
combination of swapping and paging as paging hardware became available. In Solaris, the
designers changed standard UNIX methods to improve efficiency. More changes were
made in later versions of Solaris, to improve the efficiency.
Linux is almost similar to the Solaris system. In both systems the swap space is used only
for anonymous memory, it is that kind of memory that is not backed by any file. In the
Linux system, one or more swap areas are allowed to be established. A swap area may be
in either in a swap file on a regular file system or a dedicated file partition.
Each swap area consists of 4 KB page slots, which are used to hold the swapped pages.
Associated with each swap area is a swap-map- an array of integers counters, each
corresponding to a page slot in the swap area. If the value of the counter is 0 it means the
page slot is occupied by a swapped page. The value of the counter indicates the number of
mappings to the swapped page. For example, a value 3 indicates that the swapped page is
mapped to the 3 different processes.
2. Goals of Protection
3. Domain Protection
4. Access Matrix
• Concept: An access matrix is a theoretical model that defines the rights of each
subject (user, process) to each object (file, directory) in the system.
• Structure:
o Rows represent subjects. o Columns represent objects. o Each cell in the
matrix specifies the types of access allowed (e.g., read, write, execute).
• Example: If a file A is in the column, and user U1 is in the row, the intersection
cell might contain "read" and "write" permissions, indicating that U1 can read and
write to A.
• Access Control Mechanisms:
o Access Control Lists (ACL): Lists the users and their permitted access
rights for each object.
o Capabilities: Tokens or keys associated with a subject, listing objects and
access rights for each.
5. Authentication
• Definition: A password that is valid for only a single transaction or login session,
offering enhanced security.
• Generation Methods:
o Time-based: Generated based on the current time and valid for a short
duration (e.g., 30 seconds).
o Event-based: Generated upon request and expires after a single use.
• Benefits: Reduces the risk of password interception and reuse.
• Examples: Google Authenticator, SMS-based OTP, email-based OTPs.
7. Program Threats
• Definition: Security risks that arise due to malicious programs that exploit software
vulnerabilities.
• Types of Program Threats:
o Virus: A self-replicating program that attaches itself to files and infects a
system when executed.
o Worm: A standalone program that replicates itself across networks, causing
system slowdowns.
o Trojan Horse: A program disguised as a legitimate application but
performs malicious actions in the background.
o Spyware: Software that secretly monitors user activity and collects
personal information.
o Ransomware: Malware that locks users out of their systems or files and
demands payment to unlock access.
• Mitigation: Regular software updates, using antivirus software, and practicing safe
browsing habits.
8. System Threats
• Definition: Larger threats that impact the entire system or network, often aiming
to disrupt or control the system.
• Types of System Threats:
• Denial of Service (DoS): Overloads the system resources, making it
unavailable to legitimate users.
• Distributed Denial of Service (DDoS): A coordinated DoS attack
using multiple systems to target a single system or network.
• Rootkits: Programs that modify the OS to hide malicious activity or
unauthorized access.
• Backdoors: Unauthorized access points created by attackers to bypass
normal authentication.
• Mitigation: Use of firewalls, intrusion detection systems, regular system
monitoring, and enforcing network security policies.
• Windows Security:
• User Account Control (UAC): Limits program access to system
resources based on the user's privileges.
• NTFS Permissions: File system permissions for fine-grained access
control.
• Windows Defender: Built-in antivirus and anti-malware software.
o Phishing and Social Engineering: Attacks that trick users into revealing
sensitive information.
o Zero-day Exploits: Vulnerabilities unknown to the software developer,
exploited before they can be patched.
• Solutions:
o Regular security audits and vulnerability assessments. o Educating
employees on security best practices. o Implementing robust access control
and monitoring policies. o Using encryption for data protection.
UNIT – IV
INTRODUCTION TO LINUX PROGRAM.
1. Linux System Architecture.
2. Linux Command format.
3. Linux Internal and External Commands.
4. Directory Commands.
5. File-related commands.
6. Disk-related commands and General Utilities.
The Linux System Architecture consists of several layers that work together to provide a
functioning operating system. Here's an overview:
1. Hardware Layer
• This is the physical hardware of the computer, including:
o CPU (Central Processing Unit)
o Memory (RAM)
o Input/Output Devices (e.g., keyboard, mouse, display, storage)
2. Kernel Layer
• The core of the Linux operating system that directly interacts with the hardware.
• Responsible for:
o Process Management: Scheduling and managing processes.
o Memory Management: Allocating and deallocating memory for processes.
o Device Drivers: Interface between the hardware and the OS.
o System Calls: Providing an interface for user-level processes to interact with
the kernel.
The kernel can be further divided into:
• Monolithic Kernel: Linux uses this, meaning most functions are handled in a single,
large kernel space.
• Modules: Dynamically loadable modules allow the kernel to extend its functionality
without rebooting.
3. System Libraries
• Libraries provide standard functions that developers and applications can use without
directly accessing kernel-level system calls.
• Example: glibc (GNU C Library) for basic system functions like I/O, memory
allocation, and string operations.
4. Shell
• Acts as an intermediary between the user and the operating system.
• Allows users to execute commands, scripts, and manage files.
• Types of shells:
o Bash (Bourne Again Shell): Most common.
o Other examples: Zsh, Ksh, Csh.
5. User Space
• Applications and Utilities: Programs that run in the user space, such as text editors
(e.g., Vim, Nano), browsers, and other tools.
• User Processes: Programs initiated by the user or scripts executed by the shell.
6. File System
• Linux organizes and stores data using a hierarchical file system.
• Common file systems: ext4, XFS, Btrfs, etc.
• Virtual file systems like /proc and /sys allow interaction with kernel data structures.
7. User Interface (Optional)
• Command-Line Interface (CLI): Primary interface through the shell.
• Graphical User Interface (GUI): Optional layer, including desktop environments like
GNOME, KDE, or XFCE.
Process Management
• Definition: A process is an instance of a running program.
• States of a Process:
1. New: Created but not yet ready.
2. Ready: Waiting for CPU allocation.
3. Running: Executing on the CPU.
4. Waiting: Waiting for resources or I/O.
5. Terminated: Finished execution.
Commands:
• ps: List active processes.
• top: Monitor running processes in real time.
• kill: Terminate a process.
4. Synchronization
• Purpose: Manage access to shared resources in a multi-process environment.
• Mechanisms:
o Mutex Locks: A synchronization primitive used to ensure mutual exclusion,
allowing only one thread or process to access a critical section at a time.
5. File Systems
• Structure: Hierarchical directory structure starting from root (/).
• Key Components:
Deadlocks
• Deadlock occurs when processes are stuck waiting for each other’s resources.
• Deadlock Prevention: Avoid one of the four necessary conditions (Mutual
Exclusion, Hold and Wait, No Preemption, Circular Wait).
• Use Mutex locks to prevent race conditions during database updates. Implement
dynamic priority adjustment to prioritize critical tasks.
Internal Commands:
These commands are built into the shell (like Bash) and do not require any external executable
files. They are processed directly by the shell itself.
Examples of Internal Commands:
• cd: Changes the current directory.
• echo: Displays a line of text or variable values.
• pwd: Prints the current working directory.
• exit: Exits the current shell.
• history: Displays the history of commands used.
• set: Sets shell options and environment variables.
• unset: Removes environment variables or functions.
Characteristics of Internal Commands:
• Execute faster as they are directly built into the shell.
• Do not require an external file or path.
2. External Commands:
These commands are not built into the shell and exist as standalone executable files in the
system. They are typically found in directories like /bin, /usr/bin, /sbin, and /usr/sbin.
Examples of External Commands:
• ls: Lists directory contents.
• cp: Copies files or directories.
• mv: Moves or renames files or directories.
• rm: Removes files or directories.
• find: Searches for files and directories.
• grep: Searches for patterns in files.
• man: Displays the manual pages of commands.
Characteristics of External Commands:
• They are stored as executable files.
• They often reside in system directories like /bin, /usr/bin, or /sbin.
• They may require more resources as they involve reading an external file to execute.
• Purpose: Lists the files and directories in the current or specified directory.
• Usage: ls [directory]
• To show detailed information, including file permissions, sizes, and modification dates
- ls -l
• To show hidden files (those starting with a dot) –
4. mkdir (Make Directory)