0% found this document useful (0 votes)
6 views

OS Handout notes

Uploaded by

kannayogesh865
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

OS Handout notes

Uploaded by

kannayogesh865
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 71

KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY

(AUTONOMOUS)
NAMAKKAL- TRICHY MAIN ROAD, THOTTIAM,TRICHY
ARTIFICIAL INTELLIGENCE AND DATA SCIENCE
20IT401-OPERATING SYSTEMS
HANDOUT NOTES
UNIT 1
INTRODUCTION AND OPERATING SYSTEM STRUCTURES

What Operating Systems Do

 A computer system can be divided roughly into four components: the hardware,
the operating system, the application programs, and a user (Figure 1.1).
 The hardware—the central processing unit (CPU), the memory, and the
input/output (I/O) devices—provides the basic computing resources for the
system. The application programs—such as word processors, spreadsheets,
compilers, and web browsers—define the ways in which these resources are
used to solve users’ computing problems.
 The operating system controls the hardware and coordinates its use among the
various application programs for the various users.

Defining Operating Systems

 Operating systems exist because they offer a reasonable way to solve the
problem of creating a usable computing system.
 The fundamental goal of computer systems is to execute programs and to make
solving user problems easier.
Computer hardware is constructed toward this goal. Since bare hardware alone
is not particularly easy to use, application programs are developed.
 These programs require certain common operations, such as those controlling
the I/O devices.
MAINFRAME SYSTEMS

 It is the system where the first computer used to handle many commercial
scientific applications. The growth of mainframe systems traced from simple
batch system where the computer runs one and only one application to time
shared systems which allowed for user interaction with the computer system.

Batch /Early System:

 Early computers were physically large machine. The common input devices
were card readers, tape drivers. The common output devices were line printers,
tape drivers and card punches.
 In these systems the user did not interact directly with the computer system.
Instead the user preparing a job which consists of programming data and some
control information and then submitted it to the computer operator after some
time the output is appeared.

 The disadvantages of batch system are that in this execution environment the
CPU is often idle because the speed up of I/O devices is much slower than the
CPU.

Figure. 1.2.1 Batch Operating System.


The problems with Batch Systems are as follows −

 Lack of interaction between the user and the job.


 CPU is often idle, because the speed of the mechanical I/O devices is slower
than the CPU.
 Difficult to provide the desired priority.

Multi-programmed System:
 Multi-programming concept increases CPU utilization by organization jobs so
that the CPU always has one job to execute the idea behind multiprogramming
concept. The operating system keeps several jobs in memory simultaneously as
shown in below figure.

 This set of job is subset of the jobs kept in the job pool. The operating system
picks and beginning to execute one of the jobs in the memory. In this
environment the operating system simply switches and executes another job.
When a job needs to wait the CPU is simply switched to another job and so on.
 The disadvantages of the multi-programmed system are It does not provide user
interaction with the computer system during the program execution. The
introduction of disk technology solved these problems rather than reading the
cards from card reader into disk. This form of processing is known as spooling.

Time Sharing System:

 The time sharing system is also known as multi user systems. The CPU executes
multiple jobs by switching among them but the switches occurs so frequently
that the user can interact with each program while it is running.
 The time sharing system allows many users to share the computer
simultaneously. Since each action in this system is short, only a little CPU time
is needed for each user.
 The system switches rapidly from one user to the next so each user feels as if the
entire computer system is dedicated to his use, even though it is being shared by
many users.
PERSONAL COMPUTER SYSTEM / DESKTOP SYSTEM:
 Personal computer appeared in 1970‘s. They are microcomputers that are
smaller & less expensive than mainframe systems.
 Instead of maximizing CPU & peripheral utilization, the systems opt for
maximizing user convenience & responsiveness.
 At first file protection was not necessary on a personal machine. But when other
computers 2nd other users can access the files on a pc file protection becomes
necessary.
 The lack of protection made if easy for malicious programs to destroy data on
such systems. These programs may be self replicating& they spread via worm or
virus mechanisms.
MULTIPROCESSOR SYSTEMS

 Multiprocessor operating system allows the multiple processors, and these


processors are connected with physical memory, computer buses, clocks, and
peripheral devices. Main objective of using multiprocessor operating system is
to consume high computing power and increase the execution speed of system.

Symmetric Multiprocessor
 In this system, every processors have own identically copy of operating system,
and they can make communication in between each other. In which all
processors are connected each other with peer to peer relationship nature, it
means no master & slave relation.
 Examples – Windows NT, Solaris, Digital UNIX, OS/2 & Linux.
Asymmetric Multiprocessor
 In this system, every processor is allotted predefined tasks, and master processor
has power for controlling entire system. In which, It use the master- slave
relationship.
 Examples – SunOS Version 4, IOS
Shared Memory Multiprocessor
 In this system, each CPU contains sharable common memory.
Distributed Memory Multiprocessor
 In this system, all types of processors consists own private memory.
UMA Multiprocessor
 UMA Multiprocessor stands for “Uniform Memory Access Multiprocessor”. In
which, it allows to access all memory at the uniform speed rate for all
processors.
NUMA Multiprocessor
 NUMA Multiprocessor stands for “Non Uniform Memory Access
Multiprocessor”. In this system, it involves some areas of the memory for
accessing at the faster rate, and left parts of memory are utilized for other tasks.

Characteristics of Multiprocessor operating system:


 The Multi processor system allows making communication in between multiple
CPUs with their share memory and input/output devices.
 Multi processor system can use different types of processor as per own need,
such as central processing unit (CPU) or an input- output processor (IOP).
 Multiprocessors are split into multiple instruction stream multiple data stream
(MIMD) systems.

Advantages of Multiprocessor operating system


 Great Reliability
 Improve Throughput
 Cost Effective System
 Parallel Processing
Disadvantages of Multiprocessor Operating System:
 Multiprocessor has complicated nature in both form such as H/W and S/W.
 It is more expensive due to its large architecture.
 It has more time delay when processor receives message and take appropriate
action.
 It has big challenge related to skew and determinism.
 It needs context switching which can be impacted its performance.

DISTRIBUTED SYSTEMS

 In contrast to tightly coupled systems, the processors do not share memory or a


clock. Instead, each processor has its own local memory.
 The processors communicate with each other by various communication lines
such as high speed buses or telephone lines.
 Distributed systems depend on networking for their functionalities. By being
able to communicate distributed systems are able to share computational tasks
and provide a rich set of features to the users.
Types of Distributed Systems
The nodes in the distributed systems can be arranged in the form of client/server
systems or peer to peer systems. Details about these are as follows −
 Client/Server Systems
 Peer to Peer Systems
Advantages of Distributed Systems
 All the nodes in the distributed system are connected to each other. So nodes can
easily share data with other nodes.
 More nodes can easily be added to the distributed system i.e. it can be scaled as
required.

Disadvantages of Distributed Systems


 It is difficult to provide adequate security in distributed systems because the
nodes as well as the connections need to be secured.
 Some messages and data can be lost in the network while moving from one node
to another.
Uses of Cluster System
 Main goal of using of cluster system is to provide help in weather forecast,
scientific computing, and super computing system.
Clustered Systems in Operating System
 Cluster Operating systems are mixer of software and hardware clusters.
Hardware cluster provides help to share of ultra performance disks in between
all computer systems, and Software cluster offers better environment for all
system to work together.
 Cluster system has several nodes, and they contain individual cluster software.
This software helps to monitor entire cluster system. When, any one node of
cluster system gets halt, then other nodes manage of its storage system as well as
resources, and then you can try to restart this system.
Types of Clustered System
There are primarily three types of cluster system.
 Asymmetric Clustering System
 Symmetric Clustering System
 Parallel Cluster System
Advantages of Clustered System
 Clustered system provides much better performance for entire system because in
which multiple computers are merged with each other, and they perform their
tasks as parallel unit.
 Clustered system is better fault tolerance system, if anyone node gets fail then
entire system does not halt.
 Clustered system has great scalability because we can easily add new node with
this system.

Disadvantages of Clustered System


 It is more costly because it contains expensive hardware and its design.
 Required multiple servers and other hardware components for making one
 It is very hard to monitor and maintenance this system.

REAL TIME SYSTEMS

 Real time system is used when there are rigid time requirements on the
operation of a processor or flow of data. Sensors bring data to the computers.
 The computer analyzes data and adjusts controls to modify the sensors inputs.
System that controls scientific experiments, medical imaging systems and some
display systems are real time systems.
Real time system is of two types such as,
HANDHELD SYSTEMS
 Handheld systems include Personal Digital Assistants (PDAs), such as Palm-
Pilots or Cellular Telephones with connectivity to a network such as the
Internet.
 They are usually of limited size due to which most handheld devices have a
small amount of memory, include slow processors, and feature small display
screens.

HARDWARE PROTECTION
 If a computer system has multiple users and allows the concurrent execution of
multiple processes, then access to data must be regulated.
 For that purpose, mechanisms ensure that files, memory segments, CPU, and
other resources can be operated on by only those processes that have gained
proper authorization from the operating system.
SYSTEM COMPONENTS

BASIC ELEMENTS OF A COMPUTER:


A computer consists of processor, memory, I/O components and system bus.
 Processor
 Main memory
 I/O modules
 System bus
INSTRUCTION EXECUTION:

 A program to be executed by a processor consists of a set of instructions stored


in Memory. The instruction processing consists of two steps.
 Program execution consists of repeating the process of instruction fetch and
instruction execution
 The two steps are referred to as the fetch stage and the execute stage.
 The processing required for a single instruction is called an instruction cycle.
Instruction Fetch and Execute:

 Processor-memory
 Processor-I/O
 Data processing
 I/O Function
MEMORY HIERARCHY:
To achieve greatest performance, the memory must be able to keep up with the
processor.
 As the processor is executing instructions, we would not want it to have to pause
waiting for instructions or operands.
 Thus the cost of memory must be reasonable in relationship to other
components.
OPERATING SYSTEMS OVERVIEW:
 An OS is defined as a System program that controls the execution of application
programs and acts as an interface between applications and the computer
hardware.

OPERATING SYSTEM OBJECTIVES AND FUNCTIONS:


 An operating system is a program that manages a computer’s hardware. It also
provides a basis for application programs and acts as an intermediary between
the computer user and the computer hardware. It can be thought of as having
three objectives:
 Convenience
 Efficiency
 Ability to evolve

OPERATING-SYSTEM SERVICES
 An operating system provides an environment for the execution of programs. It
makes certain services available to programs and to the users of those programs.
 The specific services provided, of course, differ from one operating system to
another, but we can identify common classes. Figure 1.11 shows one view of the
various operating-system services and how they interrelate.

Types of Services
1) User- oriented service
2) System-oriented service
Types of System Calls
Systems calls are grouped into five categories such as
 Process control
 File management
 Device management
 Information maintenance
 Communications
SYSTEM PROGRAMS
 Another aspect of a modern system is its collection of system programs. In the
logical computer hierarchy, At the lowest level is hardware. Next is the
operating system, then the system programs, and finally the application
programs.
They can be divided into these categories:
1) File management
2) Status information
3) File modification
4) Programming language support
5) Program loading and execution
6) Communications
7) Application programs
OPERATING SYSTEM STRUCTURE
 A system as large and complex as a modern operating system must be
engineered carefully if it is to function properly and be modified easily.
 A common approach is to partition the task into small components, or modules,
rather than have one monolithic system. Each of these modules should be a well-
defined portion of the system, with carefully defined inputs, outputs, and
functions.
 Various ways to structure an operating system:
o Monolithic structure
 Simple structure – MS-DOS
 More complex – UNIX
 More complex – Linux
o Layered – An abstraction
o Microkernel – Mach
Microkernels
 We have already seen that as UNIX expanded, the kernel became large and
difficult to manage.
 In the mid-1980s, researchers at Carnegie Mellon University developed an
operating system called Mach that modularized the kernel using the microkernel
approach.

VIRTUAL MACHINES
 Traditionally, applications have run directly on an OS on a PC or a server. Each
PC or server would run only one OS at a time. Thus, the vendor had to rewrite
parts of its applications for each OS/platform they would run on. An effective
strategy for dealing with this problem is known as virtualization.
 Virtualization technology enables a single PC or server to simultaneously run
multiple operating systems or multiple sessions of a single OS. A machine with
SYSTEM DESIGN AND IMPLEMENTATION
In this section, we discuss problems we face in designing and implementing an
operating system. There are, of course, no complete solutions to such problems, but
there are approaches that have proved successful.
Design Goals
 The first problem in designing a system is to define goals and specifications. At
the highest level, the design of the system will be affected by the choice of
hardware and the type of system: batch, time sharing, single user, multiuser,
distributed, real time, or general purpose.
 Beyond this highest design level, the requirements may be much harder to
specify. The requirements can, however, be divided into two basic groups:
 User goals
 System goals.
Mechanisms and Policies
 One important principle is the separation of policy from mechanism.
Mechanisms determine how to do something; policies determine what will be
done.
 For example, the timer construct is a mechanism for ensuring CPU protection,
but deciding how long the timer is to be set for a particular user is a policy
decision.
 The separation of policy and mechanism is important for flexibility. Policies are
likely to change across places or over time. In the worst case, each change in
policy would require a change in the underlying mechanism.
 A general mechanism insensitive to changes in policy would be more desirable.
A change in policy would then require redefinition of only certain parameters of
the system. For instance, consider a mechanism for giving priority to certain
types of programs over others.
Implementation
 Once an operating system is designed, it must be implemented. Because
operating systems are collections of many programs, written by many people
over a long period of time, it is difficult to make general statements about how
they are implemented.
 Early operating systems were written in assembly language. Now, although
some operating systems are still written in assembly language, most are written
in a higher-level language such as C or an even higher-level language such as
C++. Actually, an operating system can be written in more than one language.
 The lowest levels of the kernel might be assembly language. Higher-level
routines might be in C, and system programs might be in C or C++, in
interpreted scripting languages like PERL or Python, or in shell scripts. In fact, a
given Linux distribution probably includes programs written in all of those
languages.
 The first system that was not written in assembly language was probably the
Master Control Program (MCP) for Burroughs computers. MCP was written in a
variant of ALGOL. MULTICS, developed at MIT, was written mainly in the
system programming language PL/1.
 The Linux and Windows operating system kernels are written mostly in C,
although there are some small sections of assembly code for device drivers and
for saving and restoring the state of registers.
 The advantages of using a higher-level language, or at least a systems
implementation language, for implementing operating systems are the same as
those gained when the language is used for application programs: the code can
be written faster, is more compact, and is easier to understand and debug.
UNIT 2
PROCESS MANAGEMENT

PROCESS CONCEPTS
 A process can be thought of as a program in execution.
 A process is the unit of work in a modern time-sharing system.
 A process generally includes the process stack, which contains temporary data
(such as method parameters, return addresses, and local variables), and a data
section, which contains global variables.
Process States:
1. New: The process is being created.
2. Running: Instructions are being executed.
3. Waiting: The process is waiting for some event to occur (such as an I/O
completion or reception of a signal).
4. Ready: The process is waiting to be assigned to a processor.
5. Terminated: The process has finished execution.

Process Control Block


1. Process state.
2. Program counter
3. CPU registers
4. CPU-scheduling information.
5. Memory-management information
6. Accounting information
7. I/O Status information
PROCESS SCHEDULING
 The objective of multiprogramming is to have some process running at all times,
so as to maximize CPU utilization.
Scheduling Queues
There are 3 types of scheduling queues. They are:
1. Job Queue
2. Ready Queue
3. Device Queue
OPERATIONS ON PROCESSES
1. Process Creation
 A process may create several new processes, during the course of execution.

 The creating process is called a parent process, whereas the new processes are
called the children of that process.
2. 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 data (output) to its parent process (via the
wait system call).
 A process can cause the termination of another process via an appropriate
system call.

COOPERATING PROCESSES
 The concurrent processes executing in the operating system may be either
independent processes or cooperating processes.
 A process is independent if it cannot affect or be affected by the other processes
executing in the system.
 A process is cooperating if it can affect or be affected by the other processes
executing in the system.
 Benefits of Cooperating Processes
1. Information sharing
2. Computation speedup
3. Modularity
4. Convenience
INTER-PROCESS COMMUNICATION
 Operating systems provide the means for cooperating processes to communicate
with each other via an inter process communication (PC) facility.
 IPC provides a mechanism to allow processes to communicate and to
synchronize their actions. IPC is best provided by a message passing system.
 Inter process communication is achieved through,
 Shared Memory
 Message Passing
Basic Structure:
 If processes P and Q want to communicate, they must send messages to and
receive messages from each other; a communication link must exist between
them.
 Physical implementation of the link is done through a hardware bus , network
etc,
 There are several methods for logically implementing a link and the operations:
1. Direct or indirect communication
2. Symmetric or asymmetric communication
3. Automatic or explicit buffering
4. Send by copy or send by reference
5. Fixed-sized or variable-sized messages
1. Direct Communication
 Each process that wants to communicate must explicitly name the recipient or
sender of the communication.
 A communication link in this scheme has the following properties:
i. A link is established automatically between every pair of processes that want
to communicate. The processes need to know only each other's identity to
communicate.
ii. A link is associated with exactly two processes.
iii. Exactly one link exists between each pair of processes.

 There are two ways of addressing namely


 Symmetry in addressing
 Asymmetry in addressing
2. Indirect Communication
 With indirect communication, the messages are sent to and received from
mailboxes, or ports.
 The send and receive primitives are defined as follows:
send (A, message) Send a message to mailbox A.
receive (A, message) Receive a message from mailbox A.
3. Buffering
 A link has some capacity that determines the number of message that can reside
in it temporarily. This property can be viewed as a queue of messages attached
to the link.
 There are three ways that such a queue can be implemented.
 Zero capacity
 Bounded capacity
 Unbounded capacity
4. Synchronization
 Message passing may be either blocking or non-blocking.
1. Blocking Send - The sender blocks itself till the message sent by it is received
by the receiver.
2. Non-blocking Send - The sender does not block itself after sending the
message but continues with its normal operation.
3. Blocking Receive - The receiver blocks itself until it receives the message.
4. Non-blocking Receive – The receiver does not block itself.

THREADS – OVERVIEW
 A thread is the basic unit of CPU utilization.
 It is sometimes called as a lightweight process.
 It consists of a thread ID, a program counter, a register set and a stack.

 It shares with other threads belonging to the same process its code section, data
section, and resources such as open files and signals.
Benefits of multithreaded programming
 Responsiveness
 Resource Sharing
 Economy
 Utilization of MP Architectures
MULTITHREADING MODELS
1. Many-to-One
2. One-to-One
3. Many-to-Many
1. Many-to-One:
 Many user-level threads mapped to single kernel thread.
 Used on systems that does not support kernel threads.
 One thread blocking causes all to block
 Multiple threads may not run in parallel on muticore system because only one
may be in kernel at a time. Few systems currently use this model
2.One-to-One:
 Each user-level thread maps to kernel thread
 Creating a user-level thread creates a kernel thread

 More concurrency than many-to-one


 Number of threads per process sometimes restricted due to overhead
3.Many-to-Many Model:
 Allows many user level threads to be mapped to many kernel threads
 Allows the operating system to create a sufficient number of kernel threads
 Solaris prior to version 9
 Windows with the Thread Fiber package
THREADING ISSUES
1. fork() and exec() system calls.
 A fork() system call may duplicate all threads or duplicate only the thread that
invoked fork().
 If a thread invoke exec() system call ,the program specified in the parameter to
exec will replace the entire process.
2. Thread cancellation.
 It is the task of terminating a thread before it has completed .
 A thread that is to be cancelled is called a target thread.
 There are two types of cancellation namely.
3. Signal handling
1. A signal is a used to notify a process that a particular event has occurred.
2. A generated signal is delivered to the process.
3. Once delivered the signal must be handled.
4. Thread pools
 Creation of unlimited threads exhausts system resources such as CPU time or
memory. Hence we use a thread pool.
 In a thread pool, a number of threads are created at process startup and placed in
the pool.
5. Thread specific data
 Threads belonging to a process share the data of the process. However each
thread might need its own copy of certain data known as thread-specific data.

CPU SCHEDULING
 CPU scheduling is the basis of multi-programmed operating systems.
 The objective of multiprogramming is to have some process running at all times,
in order to maximize CPU utilization.
 Scheduling is a fundamental operating-system function.
 Almost all computer resources are scheduled before use.
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 ready queue is not necessarily a first-in, first-out (FIFO) queue. It may be a
FIFO queue, a priority queue, a tree, or simply an unordered linked list.
Preemptive Scheduling
 CPU scheduling decisions may take place under the following four
circumstances:
Non-Preemptive Scheduling
 In non preemptive scheduling, once the CPU has been allocated a process, the
process keeps the CPU until it releases the CPU either by termination or by
switching to the waiting state.
 This scheduling method is used by the Microsoft windows environment.
Dispatcher
 The dispatcher is the module that gives control of the CPU to the process
selected by the short-term scheduler.
 This function involves:
1. Switching context
2. Switching to user mode
3. Jumping to the proper location in the user program to restart that program.

CPU SCHEDULING ALGORITHMS


 Scheduling algorithms are the algorithms used for scheduling tasks and
processes by process schedulers.
1. First-Come, First-Served Scheduling
2. Shortest Job First Scheduling
3. Priority Scheduling
MULTIPLE-PROCESSOR SCHEDULING
 CPU scheduling more complex when multiple CPUs are available
 Homogeneous processors within a multiprocessor
 Asymmetric multiprocessing – only one processor accesses the system data
structures, alleviating the need for data sharing

REAL-TIME CPU SCHEDULING

 CPU scheduling for real-time operating systems involves special issues. In


general, we can distinguish between soft real-time systems and hard real-time
systems.
 Soft real-time systems provide no guarantee as to when a critical real-time
process will be scheduled.
Minimizing Latency
 Consider the event-driven nature of a real-time system. The system is typically
waiting for an event in real time to occur.
 Events may arise either in software —as when a timer expires—or in
hardware—as when a remote-controlled vehicle detects that it is approaching an
obstruction.
Two types of latencies affect the performance of real-time systems:
1. Interrupt latency
2. Dispatch latency
 Interrupt latency refers to the period of time from the arrival of an interrupt at the
CPU to the start of the routine that services the interrupt.
 When an interrupt occurs, the operating system must first complete the instruction
it is executing and determine the type of interrupt that occurred.
 It must then save the state of the current process before servicing the interrupt using
the specific interrupt service routine (ISR).
 One important factor contributing to interrupt latency is the amount of time
interrupts may be disabled while kernel data structures are being updated.
 Real-time operating systems require that interrupts be disabled for only very short
periods of time.

Figure 2.12.2 Interrupt latency.


 The conflict phase of dispatch latency has two components:
1. Preemption of any process running in the kernel
2. Release by low-priority processes of resources needed by a high-priority
process
Priority-Based Scheduling
 The most important feature of a real-time operating system is to respond
immediately to a real-time process as soon as that process requires the CPU.
 The scheduler for a real-time operating system must support a priority-based
algorithm with preemption.
Rate-Monotonic Scheduling
 The rate-monotonic scheduling algorithm schedules periodic tasks using a static
priority policy with preemption.
 If a lower-priority process is running and a higher-priority process becomes
available to run, it will preempt the lower-priority process.
 Upon entering the system, each periodic task is assigned a priority inversely
based on its period.
 The shorter the period, the higher the priority; the longer the period, the lower
the priority.
 The rationale behind this policy is to assign a higher priority to tasks that require
the CPU more often.
Earliest-Deadline-First Scheduling
 Earliest-deadline-first (EDF) scheduling dynamically assigns priorities
according to deadline. The earlier the deadline, the higher the priority; the later
the deadline, the lower the priority.
 Under the EDF policy, when a process becomes runnable, it must announce its
deadline requirements to the system. Priorities may have to be adjusted to reflect
the deadline of the newly runnable process.

CASE STUDY: LINUX SCHEDULING


 Process scheduling in Linux has had an interesting history. Prior to Version 2.5,
the Linux kernel ran a variation of the traditional UNIX scheduling algorithm.
POSIX real-time scheduling API:
#include <pthread.h>
#include <stdio.h>
#define NUM THREADS 5
int main(int argc, char *argv[])
{
int i, policy;
pthread t tid[NUM THREADS];
pthread attr t attr;
/* get the default attributes */
pthread attr init(&attr);
/* get the current scheduling policy */
if (pthread attr getschedpolicy(&attr, &policy) != 0)
fprintf(stderr, "Unable to get policy.\n");
else {
if (policy == SCHED OTHER)
printf("SCHED OTHER\n");
else if (policy == SCHED RR)
printf("SCHED RR\n");
else if (policy == SCHED FIFO)
printf("SCHED FIFO\n");
}
/* set the scheduling policy - FIFO, RR, or OTHER */
if (pthread attr setschedpolicy(&attr, SCHED FIFO) != 0)
fprintf(stderr, "Unable to set policy.\n");
/* create the threads */
for (i = 0; i < NUM THREADS; i++)
pthread create(&tid[i],&attr,runner,NULL);
/* now join on each thread */
for (i = 0; i < NUM THREADS; i++)
pthread join(tid[i], NULL);
}
/* Each thread will begin control in this function */
void *runner(void *param)
{
/* do some work ... */
pthread exit(0);
}
 However, as this algorithm was not designed with SMP systems in mind, it did not adequately
support systems with multiple processors. In addition, it resulted in poor performance for
systems with a large number of runnable processes.
 With Version 2.5 of the kernel, the scheduler was overhauled to include a scheduling
algorithm—known as O(1)—that ran in constant time regardless of the number of tasks in the
system.
 The O(1) scheduler also provided increased support for SMP systems, including processor
affinity and load balancing between processors.
 However, in practice, although the O(1) scheduler delivered excellent performance on SMP
systems, it led to poor response times for the interactive processes that are common on many
desktop computer systems.
 During development of the 2.6 kernel, the scheduler was again revised; and in release 2.6.23
of the kernel, the Completely Fair Scheduler (CFS) became the default Linux scheduling
algorithm.
 Scheduling in the Linux system is based on scheduling classes. Each class is assigned a
specific priority. By using different scheduling classes, the kernel can accommodate different
scheduling algorithms based on the needs of the system and its processes.
 The scheduling criteria for a Linux server, for example, may be different from those for a
mobile device running Linux.
 To decide which task to run next, the scheduler selects the highest-priority task belonging to
the highest-priority scheduling class. Standard Linux kernels implement two scheduling
classes: (1) a default scheduling class using the CFS scheduling algorithm and (2) a real-time
scheduling class.
 Can discuss each of these classes here. New scheduling classes can, of course, be added.
Rather than using strict rules that associate a relative priority value with the length of a time
quantum, the CFS scheduler assigns a proportion of CPU processing time to each task.
CFS PERFORMANCE
 The Linux CFS scheduler provides an efficient algorithm for selecting which task to run
next.Each runnable task is placed in a red-black tree—a balanced binary search tree whose
key is based on the value of vruntime. This tree is shown below:

 When a task becomes runnable, it is added to the tree. If a task on the tree is not runnable
(for example, if it is blocked while waiting for I/O), it is removed.
 Generally speaking, tasks that have been given less processing time (smaller values of
vruntime) are toward the left side of the tree, and tasks that have been given more processing
time are on the right side.
 According to the properties of a binary search tree, the leftmost node has the smallest key
value, which for the sake of the CFS scheduler means that it is the task with the highest
priority.
 Because the red-black tree is balanced, navigating it to discover the leftmost node will
require O(logN) operations (where N is the number of nodes in the tree).
 However, for efficiency reasons, the Linux scheduler caches this value in the variable rb
leftmost, and thus determining which task to run next requires only retrieving the cached
value.
 Let’s examine the CFS scheduler in action: Assume that two tasks have the same nice
values. One task is I/O-bound and the other is CPU-bound.
 Typically, the I/O-bound task will run only for short periods before blocking for additional
I/O, and the CPU-bound task will exhaust its time period whenever it has an opportunity to
run on a processor.
 Therefore, the value of vruntime will eventually be lower for the I/O-bound task than for the
CPU-bound task, giving the I/O-bound task higher priority than the CPU-bound task.
 At that point, if the CPU-bound task is executing when the I/O-bound task becomes eligible
to run (for example, when I/O the task is waiting for becomes available), the I/O-bound task
will preempt the CPU-bound task.

Scheduling priorities on a Linux system.


 Any task scheduled using either the SCHED FIFO or the SCHED RR real-timepolicy runs
at a higher priority than normal (non-real time) tasks.
 Linux uses two separate priority ranges, one for real-time tasks and a second fornormal
tasks.
 Real-time tasks are assigned static priorities within the range of 0 to 99, andnormal (i.e.
non real-time) tasks are assigned priorities from 100 to 139.
 These two ranges map into a global priority scheme wherein numerically lowervalues
indicate higher relative priorities.
UNIT - 3
PROCESS SYNCHRONIZATION AND DEADLOCKS

ProcessesTypes
The two types of processes are
• Cooperating
• Processes
• Independent
• Processes

Cooperating process
• Can affect or be affected by the execution of another process
• A process that shares data with other processes or sharea logical address space(both
data and code)

Advantages
• Information sharing
• Computation speedup
• Modularity
• Convenience

Independentprocesses
• Cannot affect or be affected by the execution of another process
• Aprocess that does not share data

Atomicoperation
• Atomic operation means an operation that completes in its entirety without
interruption.
• The statements
counter++;
counter--;
must be performed atomically
Race Conditions
The situation where several processes access and manipulate shared data concurrently and
the out come of execution depends on the particular order in which the access takes place are
called race conditions

How can processes coordinate (orsynchronise) in order to guard again strace conditions?
The final value of the shared data depends upon which process finishes last. To prevent
race conditions, concurrent processes must be synchronized.

Types of Solutions
• Software solutions Algorithms whose correctness does not rely on any other assumptions.
• Hardware solutions Rely on some special machine instructions.

• Operating System solutions Provide some functions and data structures to the
programmer through system / library calls.
• Programming Language solutions Linguistic constructs provided as part of a language.

Two-ProcessSolutions
Consider an algorithm that is applicable to only two processes at a time with the following
notation.

Initial notation
• Only 2 processes, P0 and P1

• When usually just presenting process Pi, always denotes other process Pj(i != j)

MUTEX LOCKS
• Mutex is short for Mutual exclusion
• Mutex lockis used to solve the critical-section problem
◦ Used to ensure exclusive access to data shared between threads
◦ Used to protect critical regions and thus prevent race conditions

Illustration
• Aprocess must acquire the lock before entering a critical section using acquire () function
releases the lock when it exits the critical section using release() function
• Definition of acquire()
acquire()
{
while(!available) ; /* busywait*/
available= false;;
}
• Definition of release()
release()
{
available= true;
}
• Structure
SYNCHRONIZATION HARDWARE

SEMOPHORES
Semaphore is asynchronization tool (provided by the OS) that does not require busy
waiting .Semaphore is a variable that has an integer value
• May be initialized to a non negative number
• Wait operation decrements the semaphore value
• Signal operation increments semaphore value

Logical Definition
A semaphore S is an integer variable that ,apart from initialization ,can only be accessed
through 2 atomic and mutually exclusive operations:
• wait(S)-Originallycalled P(), from Dutch proberen―to test ‖
• signal(S) -Originally called V(), from Dutch verhogen ―to increment ‖

Waitand Signal Operations


• Busy-waiting implementation of these in divisible (atomic) operations:
wait(S)
{
while(S<=0 do)
;//no-op
S –– ;
}
signal(S)
{
S++;
}
• Modification to the value of semaphore (S) in the wait and signal is executed
individually.

• In wait,the testing and possible modification of S must also be executed without


interruption.

Usage of Semaphore
The fundamental principle of semaphore is this: Two or more processes can cooperate by
means of simple signals such that a process can be forced to stop at a specified place until it has
received a specified signal.

Usage1:Critical Section of n Processes Problem


Shared data
Semaphore mutex; //initially mutex=1
Structure of Process Pi
do {

wait(mutex);
critical section
signal(mutex);
remaindersection
}while(1);
Usage2:synchronization of 2 processes
Suppose that we have two processes, P1with statement s1 and P2with statement s2. We
require that s2 be executed only after s1 has completed. This can be done with a shared semaphore
variable,mutex, initialized to 0, and by inserting the following statements inP1
s1;signal(mutex);

and the following statements in P2


waite (mutex)s2;

Advantages
• Applicable to any number of processes on either a single processor or multiple
processors .It is simple and therefore easy to verify
• It can be used to support multiple critical sections

Disadvantages
• Busy-waiting consumes processor time
• Starvation is possible when a process leaves a critical section and more than one
process is waiting.

To avoid busy waiting, when a process has to wait, it will be put in a blocked queue of
processes waiting for the same event. Hence a semaphore is defined as a record
Type def struct
{
int value;
struct process *L;
}semaphore;

Assume two simple operations:


• Block suspends the process that in vokes it.
• Wakeup (P) resumes the execution of a blocked process P.

Semaphore Operations
Semaphore operations now defined as
When a process must wait for a semaphore S,it is blocked and put on the semaphore’s

S.value—;
if(S.value<0)
{
add this process to
S.L;block();
}

signal(S)
The signal operation removes (assume a fair policy like FIFO) one process from the queue
and puts it in the list of ready processes.
S.value++;
if(S.value <= 0)
{
remove a process P from
S.L;wakeup(P);
}
Implementing S

Wait operation
wait(S1);
C––;
if(C<0)
{
signal(S1);
wait(S2);
}
signal(S1);

signal operation
wait(S1);
C++;
if(C<=0)
signal(S2);
else
signal(S1);

Semaphoreas a General SynchronizationTool


• Execute B in Pj only after A executed in Pi
• Use semaphore flag initialized to 0

Classical problem of synchronization


Problems with Semaphores
• Semaphores provide a powerful tool for enforcing mutual exclusion and coordinate
processes.
• But wait (S) and signal (S) are scattered among several processes. Hence,difficult to
understand their effects.
• Usage must be correct in all the processes (correct order,correct variables, noo missions).

• One bad(or malicious) process can fail the entire collection of processes
CRITICAL REGION

MONITORS
A monitor is high-level synchronization construct that allows the safe sharing of an
abstract data type among concurrent processes.It can be implemented using semaphores.

Syntax
Key Idea:Only one process may be active with in the monitor at a time monitor
monitor-name
{
shared variable declarations
procedure body P1 (…)
{

Procedure body P2 (…)

Procedure body Pn (…) {


}
{

initialization code

}
}
Monitor is a software module containing : one or more procedures an initialization sequence
local data variables
Characteristics
• Local data variables are accessible only by the monitor process enters monitor by
invoking one of its procedures
• Only one process may be executing in the monitor at a time.

MonitorConditions
To allow a process to wait with in the monitor ,a condition variable must be declared, for
example:
condition x,y;

Condition Variables
Condition variables are local to the monitor (accessible only within the monitor).Condition
variables can only be used with the operations cwait and csignal executed on an associated queue.

Two operations on a condition variable:

x.wait() – process invoking the operation is suspended.


x.signal ()–resumes one of the processes (if any) that invoked x.wait()

Possible Monitor Dynamics


A waiting processes are either in the entrance queue or in a condition queue.A process
puts itself into condition queue cn by is suing cwait(cn). csignal(cn) brings into the monitor one
process in condition cn queue. Hence csignal(cn) blocks the calling process and puts it in the
urgent queue(unless csignal is the last operation of the monitor procedure).
Problem Definition
Illustrate the difficulty of allocating resources among process without deadlock and
starvation.
• The challenge is to grantre quests for chopsticks while avoiding dead lock and
starvation.
• Dead lock can occur if every one tries to get their chopsticks at once. Each gets a left
chopstick, and is stuck, because each right chopstick is someone else’s left chopstick.

• starvation.

• Dead lock can occur if every one tries to get their chopsticks at once. Each gets a left
chopstick, and is stuck, because each right chopstick is someone else’s left chopstick.
SYSTEM MODEL DEADLOCK
Deadlock is a situation that occurs when two processes are waiting for the other to
complete before proceeding. The result is that both processes hang.

Types of Deadlock
• Process Deadlock
◦ A process is deadlocked when it is waiting on an event which will never happen
• System Deadlock
◦ A system is deadlocked when one or more processes are deadlocked

DEAD LOCK CHARACTERIZATION

Condition
Deadlock can arise if four conditions hold simultaneously in a system

 Mutual exclusion condition


 Hold and wait condition
 No preemption condition
 Circular
METHODS FOR HANDLING DEADLOCKS
The three ways to deal with the deadlock problem are

Protocol
Ensure that the system will never enter a deadlock state

Detect and recover


Allow the system to enter a deadlock state and then recover

Ignore the deadlock


• Ignore the problem and pretend that dead locks never occur in the system Used by
most operating systems, including UNIX

Deadlock Management
• Deadlock prevention prevent deadlocks by constraining how requests resources can be
made for Deadlock avoidance
◦ Do not grant are source request if this allocation have the potential to lead to a
deadlock Deadlock detection
◦ Always grant resource request when possible. Periodically check for deadlocks
◦ If a deadlock exists, recover from it
• Recovery from deadlock
◦ After detection, allow processes to complete and resources to be reused
◦ May involve destroying and restarting processes

DEADLOCK PREVENTION
Deadlock prevention is to prevent the deadlocks occurrence by ensuring that atleast one of
the conditions cannot hold.

Types of Prevention Method


The deadlock prevention methods consists of the following two types

Indirect Method
• Prevent the occurrence of the three following conditions
◦ Mutual Exclusion
◦ Hold and Wait
◦ No-Preemption

DEADLOCK AVOIDANCE
A decision is made dynamically whether the current resource allocation request will ,if
granted, potentially lead to a deadlock.It requires knowledge of future process request.

Each process must require that the system has some addition alapriori information
available about its resources requirements:

Simple stand most use ful model requires that each process declare the maximum number
of resources of each type that it may need.

The deadlock-avoidance algorithm dynamically examines the resource-allocation state to


ensure that there can never be a circular-wait condition.

Resource allocation state


• The Resource allocation state is defined by the number of available and allocated
resources, and the maximum demands of the processes.

Two Approaches to Deadlock Avoidance


• Do not start a process if its demands might lead to deadlock
• Do not grant an incremental resource request to a process if this allocation might lead to
deadlock

Safe State
When a process requests an available resource, system must decide if immediate
allocation leaves the system in a safe state.
Example
Let’s assume a very simple model: each process declares its maximum needs. In this case,
algorithms exist that will ensure that no unsafe state is reached.

There exists a total of 12 tape drives. The current state looks like this:

Sequence1:
The sequence <P1, P0, P2> is a workable
sequence.At time t0,
Number of free tape drives=Total number of tape drives–Number of tape drives
allocated
12 –(5+2+2)
12 – 9
3.
ProcessP1
P1is allocated with all its tape drives and returned it.
ProcessP0
P0is allocated with all its tape drives and returned it.

ProcessP2
P2is allocated with all its tape drives and returned it.

Sequence2:
Suppose P2 requests and is given one more tape drive. What happens then?
Sequence1:
Is the sequence <P1, P0, P2> a safe sequence or not?
At time t0,
Number of free tape drives =Total number of tape drives–Number of tape drives
allocated
12 –(5+2+3)

12 – 10 =2.

ProcessP1
P1 is allocated with all its tape drives and return edit.

ProcessP0
P0is put in wait state since P0 requested resources (5tapes) are unavailable.
UNIT-4
STORAGE MANAGEMENT AND FILE SYSTEMS INTERFACE

STORAGE MANAGEMENT
Storage management is the process of
• Controlling and coordinating computer memory
• Assigningportions called blocks to various running programs too ptimizeoverall
system performance(Allocating Process)
• Freeing it forreuse whenno longerneeded (Deallocating process)
• Memory management residesinhardware,in the OS(operatingsystem) ,and in programs
and applications.

CPU hardware compare severy address generated in user mode with the registers
• A program executing in user mode at tempt to access OS memory or other users
memory results in a trap to the OS,which treats the attempt as a fatal error
• This prevents a user program from accidentally or deliberately modifying the code or
datastructures of either the OS or other users

Address Binding
• Address Binding is the process of converting the (relative or symbolic) address used in
a program to an actual physical (absolute) RAM address.
• Program must be brought into memory and placed within a process for it to be
executed.

Multistep Processing of a User Program


• User programs go through several steps before being run
• Program components do not necessarily know where in RAM they will be loaded
• RAM deals with absolute addresses
• Logical addresses need to be bound to physical addresses at some point.

Binding Steps
The stages involved in the binding of instructions and data to memory addresses is listed
below
Compile
Time
◦ If memory location is known a priori, absolute code can be generated; must recompile code
if starting location changes.
• Load time
◦ If memory location is not known at compile time then the relocatable code must be
generated.
• Execution time
Binding delayed until runtime if the process can be moved during its execution Logical Vs
Physical Address Space
The concept of a logical address space that is bound to a separate physical address space
is central to proper memory management.

Logical address
Logical address is the address generated by the CPU. It is also referred to as virtual
address.Physical address
Physical address is the address seen by the memory unit.

Virtual Address
Virtual Address is the logical address results in the execution-time address-binding
scheme.

Logical address Space


The set of all logical address generated by the program is called Logical address Space.

Physical address Space


The set of all physical address corresponding to the logical addresses is called Physical
address Space.

Note:
• Logical and physical addresses are the same incompile-time and load-time address-
binding schemes;
• Logical (virtual) and physical addresses differ in execution-time address-binding
scheme.

Memory-Management Unit (MMU)


• MMU is a hardware device that maps virtual to physical address at run-time.
• In a simple MMU scheme, the value in the relocation register is added to every
address generated by a user process at the time it is sent to memory.
• The user program deals with logical addresses; it never sees the real physical
addresses.
Dynamic relocation using are location register
Dynamic loading
Assume that the whole program and data for the program must be in memory in order for
a process to execute.
Problem
Even if you code is small,the memory needed will be large.Why?
• Each program must be linked with the whole standard library.
• The whole library must be in memory even though most functions in that library are
not used.

Solution (Dynamic Loading)


Dynamic loading means loading the library (or anyother binary for that matter) into the
memory during load or run-time

The steps involved in the dynamic loading are listed below


• Don’t load the whole program into memory before run a program
• Just load the main routine at the beginning and execute
• When a new routine is called, it is loaded into the memory
• The executable will still be large,but the memory used will be smaller
◦ Routines that are not used will not be in the memory
◦ Dynamic loading can introduce significant time penalty
◦ Space/ time trade-off

Advantage
• Better memory-space utilization
• Used routine is never loaded
• Useful when large amounts of code are needed to handle in frequently occurring cases
• No special support from the operating system is required implemented through
program design.

Program vs.Memorysizes
What to do when program size is larger than the amount of memory/partition (that exists
or can be) allocated to it?

There are two basic solutions within real memory management:


1) Dynamic Linking (Libraries –DLLs)
2) Overlays
Dynamic Linking and Shared Libraries
Static Linking Libraries
The basic idea of static linking is to resolve all addresses before running program. Here
Compiler compiles each file, but leaves external references unresolved. But Static linker resolves
all previously unresolved references.

In static linking, the code segments will be copy to each executables.

Advantages
• Changing libraries does not affect previously compiled programs.
• No address resolution necessary at run time

Disadvantages
• The executable size will be larger.
• Multiple copies of code might be in memory
◦ Wastes both disk space and main memory
• New version of library =>must recompile

Dynamic Linking Libraries


Dynamic linking is the process that links the external shared libraries at run time. It is also
called as late linking.

Stub
With dynamic linking ,a stub is included in the image for each library-routine reference.A
Stub is a small piece of code that indicates how to locate the appropriate memory-resident library
routine,or how to load the library if the routine is not already present.

How it works
• Use a stub to replace the whole library routine
• Enough information to locate the library routine
• When the stub is executed, it replace sit self with the address of the routine and
executes the routine.
◦ Only one copy of library code is needed for all programs.
◦ Such library is also known as a shared library.

Note: The executable is much smaller with such libraries.


Problem
One problem with dynamically linked executable is that it is less portable: assume the
shared library is available and the same

Statically linked executables are more self-contained.

SWAPPING
Swapping refers to moving entire processes in and out of main memory.

Purpose
A process to be executed should be available in the main memory for execution. All the
process are stored in the backing store temporarily. The purpose of swapping is to move the
process between main memory and backing store.

Schematic view of swapping

Swapping of two processes using disk as a balance store

RoundRobin CPU-Scheduling Algorithm


• Each process is provided a fix time to execute called time sliceor quantum
• CPU scheduler picks the process from the circular/ ready queue ,setatimerto interruptit
after the given time slice /quantumand dispatches it
• If process has burst timeless than defined time slice/quantum
◦ Process will leave the CPU after the completion
◦ CPU will proceed with the next process in the ready queue /circular queue
• Else If process has burst time longer than defined time slice/quantum(expires)
Operating System

◦ Timer will be stopped. It cause interruption to the OS


◦ Executed process is swapped out of the memory by the memory manager and it is
placed at the tail of the circular / ready queue using context switch
◦ CPU scheduler then proceed by selecting then ext process in there ady queue by
swapping into the mainmemory

CONTIGUOUS MEMORY AL LOCATION


The main memory must accommodate both the operating system and the various
userprocesses. We therefore need to allocate main memory in the most efficient way
possible.Thememory is usually divided into two partitions: one for the resident operating system
and one for the user processes. We usually want several user processes to reside in memory at the
same time.We therefore need to consider how to allocate available memory to the processes that
are in the input queue waiting to be brought into memory. In contiguous memory allocation, each
process iscontained in a single section of memory that is contiguous to the section containing the
next process.

Memory Allocation
Memory Allocation is the process of assigning blocks of memory on request for the
program execution.
• Single-partition allocation
• Multiple-partition allocation

Single-partition allocation
In this type of allocation, relocation-register scheme is used to protect user processes
fromeach other, and from changing operating-systemcode and data.

Mainmemory usually has two partitions


• Low Memory —Operating system resides in this memory
• High Memory — User processes then held in high memo

Memory Protection
• Memory protection is to prevent a process from accessing memory it does not
own.Memory protection can be achieved by combining relocation register with the
limit register. Relocation-register scheme provides an effective way to allow the
operating-systemsize to change dynamically.
• Relocation-register scheme used to provide the memory protection by
• Relocation register contains value of smallest physical address
• Limit register contains range of logical addresses
• The dispatcher loads the relocation and limit registers with the correct values as part
of the context switch
• Each logical address must be less than the limit register.
• Then ,MMU maps the logical address dynamically by adding the value in there
location register which is sent to memory.

Advantages
• Simple,easy to understand and use

Disadvantages
• Lead stop oorutilization of processor and memory
• Users job is limited to the size of available memory

Memory Allocation
Fixed Partitions Scheme
In this type of allocation, main memory is divided into a number of fixed-sized partitions
where each partition should contain only one process.When a partition is free ,a process is selected
from the input queue and is loaded in to the free partition. When the process terminates ,the
partition becomes available for another process.

Advantages
• Any process whose size is less than or equal to the partition size can be loaded into
any available partition.
• Supports multi programming.

Disadvantages
• If a program is too big to fit into a partition use overlayer technique.
• Memory use is in efficient ,i.e., block of data loaded into memory may be smaller than
the partition . It is known as internal fragmentation.

Variable Partition Scheme


• Partitions are of variable length and number .Process is allocated exactly as much
memory as required
◦ Initially ,all the memory is available and is considered as a single block(one big
hole).
• Operating system maintains a table for the information about:
◦ Allocated partitions
◦ Free partitions ( holes)

Methods to assign processes to partitions


There are two ways to assign processes to partitions
Use multiple queues
For each and every process one queue is present
◦ Assign each process to the smallest partition within which it will fit ,by using the
scheduling queues

• when a new process is to arrive it will put


inthe queue, it is able to fit without wasting
thememory space, irrespective of other
blocksqueues.
◦ Advantage
 Minimize wastage of memory.
◦ Disadvantage
 This scheme is optimum from the
system point of view. Because larger
partitions remains unused.
• Use single queue
◦ In this method, only one ready queue is
present for scheduling the jobs for all the
blocks irrespective of size.
◦ If any block is free even though it is
larger than the process,it will simply join
instead of waiting for the suitable block
size
◦ Advantage
 It is simple and minimum processing
over head.
◦ Disadvantage
 The number of partitions specified
atthe time of system generation
limits the number of active processes
 Small jobs do not use partition space
efficiently

Dynamic Allocation Placement Algorithms


The following algorithm isused to decide which free block to allocate to a program

First fit
• Allocate the first hole that is big enough;
• Process is placed in the first hole it can fit in.

Best fit
• Allocate the smallest hole that is big enough;must search entire list,unless ordered by
size.
• Produces the smallest left over hole
Example
Allocation of 16 MB block using three placement algorithms
Performance Issues
• Best Fit usually performs the worst,since it leaves behind the smallest possible hole
• First Fit is simplest to implementand usually fastest and best
• Next fit is usually a little worse than First Fit

Fragmentation
Fragmentation occurs when a system contains total free memory to satisfy any request, but
that can’t be utilized.

Internal f ragmentation
Internal fragmentation is that the allocated memory may be slightly larger than requested
memory ;this size difference is memory internal to a partition,but not being used.
Solution
• Blocks are usually split to prevent internal fragmentation.

External fragmentation
External fragmentation is done when the total memory space exists to satisfy a request,
but it is not contiguous.

• Many small holes.

Solution
• Compaction
• Noncontiguous logical-address space
◦ Paging
◦ Segmentation

Compaction
The goal is to shuffle memory content stop place all free memory to ge ther in one large
block.

Compactionis
• Possible only if relocation is dynamic,and is done at execution time.
• Determine its cost
• Impossible only if relocation is static,and is done at assembly or load tim.
Simple Compaction algorithm
• Move all processes toward one end of memory.
• All holes move in the other direction which produces large hole of memory.
• Drawback
◦ Very expensive scheme.

I/O problem
1) Latch job in memory while it is in I/O
2) Do I/O only in to OS buffers.

PAGING
Paging is a memory-management scheme that permits the logical address space of a
process can be non contiguous;process is allocated physical memory whenever the latter is
available.

Basic Method
Frames
Physical memory is divided into fixed-sized block scalled frames (size is power of 2,
between 512 bytes and 8192 bytes).

Illustration of Paging
The hard ware support for paging is illustrated below.

Address Translation Scheme


Address generated by CPU is divided into:
• Page number(p)
◦ Used as an index into page table which contain base address of each page in
physical memory
• Page offset(d)
◦ Combined with base addres stored fine the physical memory address that is sent
to the memory unit.
• Page Table
◦ The page table contains the base address of each page in physical memory.Setup a
page table (for each process) to translate logical to physical addresses.
paging Model of Memory
The paging model is logical and physical memory is given below

then

PagingModel of logical and physical memory


If size of logical-address space is 2m and page size is 2n addressing units (bytes or words),

• High-order m-n bits of a logical address –page number


• Low-order n bits of alogical address–page offset

Paging Example
Consider the memory of
Page Size =4 bytes
Physical Memory= 32 Bytes (32/4=8 Pages)

Mapping of Logical and Physical address

Paging is a form of dynamic relocation


• Every logical address is bound by the paging HW to some physical address

• Percentage of times that a page number is found in the TLB


• Ration related to number of associative registers
• Hit ratio =Effective Access Time (EAT)
EAT = (100 + 20)+ (200 + 20)(1–)
= 220–100

Hit ratio =80%


• EAT =0.80 120 +0.20 220= 140nano second
• So 40% slow down in memory access time.

Hit ratio =98%


• EAT =0.98 120 +0.02 220= 122 nano second
• So only 22% slow down in memory access time.

Protection
• Memory protection is implemented by associating protection bit with each frame.
Valid-invalid bit attached to each entry in the page table:
• ―Valid‖ indicates that the associated page is in the process’ logical address space, and
is thus a legal page
• ―Invalid ‖indicates that the page is not in the process’logical address space

Valid (v)or invalid (i)bit in a page table


Advantages of paging
• Efficient memory use

• Simple partition management due to discontinuous loading and fixed partition size

• No compaction is necessary

• Easy to share pages

Disadvantages of paging
• Job size <=memory size
• Internal fragmentation

• Need special hardware for address translation

• Somemain memory isused for page table

• Address translation lengthens memory cycle times

SEGMENTATION
Segmentation is the Memory Management Scheme that supports user view of memory.
Logical address space is the collection of segments

Basic Method
Program
A program is a collection of segments.

Segment
A segment is a logical unit of a program such as
• Main program, procedure,function

• Local variables,global variables,common block

• stack,symbol table,arrays

Each segment has a name and a length.The addresses specify both the segment name and
the offset within the segment.

Features
• Protect each entity independently
• Allow each segment to grow independently

• Share each segment independently

User’s View of a
Program

User’sView of a Program

Segmentation Architecture
• Segments are numbered and referred to by a segment number ,thus a logical address
consists of a tuple:
Logical Address

Segment Number Offset

• Segment table–maps two-dimensional physical addresses; each table entry has:

◦ base–contains the starting physical address where the segments reside in memory

◦ limit–specifies the length of the segment

• Segment-table base register (STBR) points to the segment table’s location in


memory
• Segment-table length register (STLR) indicates number of segments used by a
program;
◦ Segment number s is legal if s< STLR.
Segmentation Hardware

Example
Suppose a program is divided into 5 segments:
• Segment 0:Subroutine
• Segment1: sqrt function
• Segment2:main program
• Segment3:stack
• Segment 4: symbol table
The segment table is as follows:
Segment Limit Base
0 1000 1400
1 400 6300
2 400 4300
3 1100 3200
4 1000 4700

• Referenceto byte 53(53 <400 (limit_segment_2 ))in segment 2 is mapped to


◦ Base_Segment_2 + Byte= 4300 +53= 4353
• Reference to byte 1222 (1222<1000(limit_segment_1))in segment1result in a trap to
the OS.
Segmentation Examples

Segmentation with Paging


Pure segmentation is not very popular and not being used in many of the operating systems.
However, Segmentation can be combined with Paging to get the best features out of both the
techniques.

In Segmented Paging, the main memory is divided into variable size segments which are further
divided into fixed size pages.

1. Pages are smaller than segments.


2. Each Segment has a page table which means every program has multiple page tables.

3. The logical address is represented as Segment Number (base address), Page number and
page offset.

Segment Number → It points to the appropriate Segment Number.


Page Number → It Points to the exact page within the segment
Page Offset → Used as an offset within the page frame
Each Page table contains the various information about every page of the segment. The Segment
Table contains the information about every segment. Each segment table entry points to a page table
entry and every page table entry is mapped to one of the page within a segment.
UNIT – 5

FILE SYSTEM IMPLEMENTATION AND MASS STORAGE STRUCTURE

FILE SYSTEM STRUCTURE


Disks provide the bulk of secondary storage on which a file system is maintained.

Characteristics
1) A disk can be rewritten in place; it is possible to read a block from the disk, modify
the block, and write it back into the same place.
2) A disk can access directly any given block of information it contains.
Thus it is simple to access any file either sequentially or randomly, and switching from
one file to another.

Design problems
A file system poses two quite different design problems.
1) Defining how the file systems should look to the user.
2) Creating algorithms and data structures to map the logical file system onto the
physical secondary storage devices.

Layered file System


The file systems itself is generally composed of many different levels. Each level in the
design uses the features of lower levels to create new features for use by higher levels.

I/O control
• The lowest level, the I/O control, consists of device drivers and interrupts handlers to
transfer information between the main memory and the disk system.
Basic file system
• The basic file system needs only to issues generic commands to the appropriate device
driver to read and write physical blocks on the disk.
File organization module
• The file organization module knows about files and their logical blocks, as well as
physical blocks. By knowing the type of file allocation used and the location of the file,
the file organization module can translate logical block addresses to physicalblock
addresses for the basic file system to transfer.
Logical file system
• Finally the logical file system manages metadata information. Metadata includes all of
the file-system structure except the actual data. The logical file system manages the
directory structure to provide the file organization module with the information the latter
needs, given a symbolic file name.

File control block


A file control block contains information about the file, including the ownership,
permissions, and location of the file contents.

Layered File system

FILE SYSTEM IMPLEMENTATION


File-System Implementation are done by the following
• On-disk structure
• In-memory structure

Structure Overview
On-disk
On-disk is the file system may contain information about
• How to boot an operating system stored there
• The total number of blocks
• The number and location of free blocks
• The directory structure
• Individual files

On-disk structure

The On-disk structure include


• Boot control block
◦ Contain information needed by the system to boot an operating system from the
volume
◦ First block of a volume.
• Volume control block
◦ Contains volume details, such as the number of blocks in the partition, size of the
blocks, free block count and free-block pointers, and free FCB count and FCB
pointers.
• Directory structure
◦ Used to organize the files.
• FCB
◦ Contains the details about the file, including file permissions, ownership, size, and
location of the data blocks.

File data blocks


File permissions
File dates (create, access, write)
File owner, group, ACL
File Size
A typical file control block

In-memory
In-memory information is used for both file-system management and performance
improvement via caching.

In-memory Structure
The structures may include the ones described below
• In-memory mount table
◦ Contains information about each mounted volume
• In-memory directory-structure
◦ Holds the directory information of recently accessed directories
• System-wide open-file table
◦ Contains a copy of the FCB of each open file as well as other information
• Per-process open file table
◦ Contains a pointer to the appropriate entry in the system-wide open file table as
well as other information

File Creation
To create a new file,
• An application program calls the logical file system.
◦ The logical file system knows the format of the directory structures. Allocates a
new FCB.
• Reads the appropriate directory into the memory
• Updates it with the new file name and FCB and
• Writes it back to the disk.

File I/O Operation (Open & Read)


Now that a file has been created, it can be used for I/O.
• First, though it must be opened
◦ The open() call passes a file name to the file system.
◦ Search for the given file name in the directory structure.

In-memory file-system structures for File open.

◦ Once the file is found, the FCB is copied into a system-wide open-file table in
memory.

◦ Entry is made in the per-process open-file table pointing to the entry in the
existing system –wide open-file table
 other fields can include a pointer to the
 current location in the file and
 access mode (read or write) in which the file is open.

In-memory file-system structures for File read

◦ open() call returns a pointer to the appropriate entry in the per-process file-system
table. This pointer is named as
 File descriptor – UNIX
 File Handle – Windows 200

Partitions and Mounting


A disk can be sliced into multiple partitions, or a partition can span multiple disks. Each
partition can be either
• “raw”, containing no file system
• “Cooked” containing a file system.
Raw disk

• Raw disk is used where no file system is appropriate. It holds information needed bydisk

• RAID systems. It contains a miniature databases holding RAID configuration


information.

Boot information
• Boot information can be stored in a separate partition. It has its own format because at
boot time the system does not have file system device drivers loaded and therefore
cannot interpret the file system format.

Boot Loader

A boot loader contains multiple file systems and multiple operating systems occupy the boot space.
Once loaded, it can boot only one operating systems available on the disk. The disks can have
multiple partitions, each containing a different type of file system and a different operating system.

Root partition
• The root partition contains the operating system kernel and other system files. It is
mounted at boot time. Other partitions are automatically mounted at boot or manually
mounted later, depending on the operating system.
• The operating system verifies that the device contains a valid file system in the
successful mount operation.
◦ Validity checked by asking the device driver to read the device directory and
verifying that the directory has the expected format.
◦ Invalid Format – partition have consistency checked and corrected with or without
user intervention.
• Finally the operating system notes in its in-memory mount table structure that a file -
system is mounted along with the type of a file system.

DIRECTORY IMPLEMENTATION
The directory can be implemented in two ways
1) Linear List
2) Hash Table

Linear List
Linear list of file names with pointer to the data blocks is the simplest method of
implementing a directory which requires linear search to find a particular entry.
• Simple to program.

• Time-consuming to execute.
• File Creation

◦ Search the directory to be sure that no existing file has the same name

◦ Add to the end of the directory.

• File Deletion

◦ Search the directory for the named file

◦ Release the space allocated to it.

• File Reuse
◦ Mark the entry as unused by assigning a special name or a blank name
◦ Attach to the list of free directory entries.
◦ Copy the last entry in the directory into the freed location and to decrease the
length of the directory.

Advantages
• A linked list is used to decrease the time required to delete a file.

Disadvantages
• Finding the files in linear search
• Searching is slow so some operating system uses software cache to store more recently
used directory information.

Hash Table
A hash table is another data structure which is used for file directory. Hash Table is a
linear list with hash data structure.

The hash table takes a value computed from the file name and returns a pointer to the file
name in the linear list.

Collisions
A collision is a situation where two file names hash to the same location. Alternatively, a
chained-overflow hash table can be used to avoid collisions

Disadvantages
• Greatly decrease the directory search time.
• Hash table are generally fixed size and
• Dependence of the hash function on that size.

ALLOCATION METHODS
Allocation methods is a methods used to allocate space to the files for
• Utilizing the disk space effectively
• Accessing the files quickly

Methods
The three major methods for allocating disk space are
1) Contiguous allocation
2) Linked allocation
3) Indexed allocation

Contiguous Allocation
• Each file occupies a set of contiguous blocks on the disk.

• File location is defined by the disk address of the first block and its length

◦ Example:
 If the file is n blocks long and starts at location b, then it occupies blocks b,
b+1, b+2….b+n-1
• Both sequential access and direct access are supported by the contiguous allocation

• Directory entry for each file contains

◦ Address of the starting block


◦ Length of the area allocated for this file

Disadvantage

Contiguous allocation of disk space

• Difficult to find free space for a new file


• Suffer from external fragmentation

Linked Allocation
• Each file is a linked list of disk blocks, the disk blocks may be scattered on the disk.
• Directory contains
◦ A pointer to the first and (optionally the last) block of the file
◦ Pointer is initialized to null to signify an empty file
• Each block contains
◦ a pointer to the next block and the last block contains a NIL pointer

Linked allocation of Disk space

File Operations
• To create a new file,
◦ Create a new entry in the directory in the directory
• To write to a file,
◦ Removes the first free block and writes to that block
◦ New block is then linked to the end of the file
• To read a file,
◦ the pointers are just followed from block to block

File allocation table (FAT)


• An important variation on linked allocation
• Simple but efficient method of disk-space allocation
◦ Used by the MS-DOS operating system.
• FAT table has one entry for each disk block and is indexed by block number
◦ The directory entry
 contains the block number of the first block of the file
◦ The table entry
 indexed by that block number
 contains the block number of the next block in the file
◦ This chain continues until it reaches the last block
File Allocation Table

Advantages

• No external fragmentation

• No need to declare the size of a file when that file is created

• File can continue to grow as long as there are free blocks

Disadvantages

• Inefficient to support direct-access


◦ Effective only for sequential-access files

• Space required for the pointers

Indexed Allocation
• Solution to the problem of both contiguous and linked allocation
◦ By bringing all the pointers together into one location called the index block

• Each file has its own index block which is an array of disk block addresses. The ith
entry in the index block points to the ith block of the file.

Logical View

• The directory contains the address of the index block.


• When the file is created

◦ all pointers in the index block are set to NULL

• When the ith block is written

◦ a block from the free space manager and its address is put in the ith index block
entry

Indexed allocation of disk space

Mechanism for small Index Block


The Mechanisms to deal with the index block is too small are

1) Linked Scheme
◦ An index block is normally one disk block

◦ To allow large files, several index blocks are linked together Next
address is nil ora pointer to another index block
2) Multi Level index
◦ A variant of the linked representation use a first level index block to
point to a setof second-level index blocks
◦ This approach could be continued to a third or fourth level,
depending on thedesired maximum file size

3) Combined Scheme
◦ Another alternative, used in the UFS is to keep the first say 15
pointers of the index block in the file’s inode
◦ The first 12 of these pointers point to direct blocks that are they contain
addressesof blocks that contain data of the file.
◦ The next three pointers is an indirect blocks
 The first points to a single indirect block which is an index block
containing notdata but the addresses of blocks that do contain data.
 The second points to a double indirect block which contain the
address of a block that contains the address of blocks that contain
pointers to the actual datablocks.
 The last pointer contains the addresses of a triple indirect block.
The Unix inode

FREE SPACE MANAGEMENT

• Free Space Management is the mechanism of maintaining the free-space


list to keeptrack of free disk space.
• File Creation
◦ Search the free space list for the required amount of space
◦ Space is removed from the free space list and allocate it to the new file
• File Deletion
◦ Disk space is added to the free space list
Types of mechanism
The Free space list can be maintained in the following ways
• Bit Vector
• Linked List
• Grouping
• Counting

Bit Vector
• The free space list is maintained as bit map vector or bit vector
• Each block is represented by 1 bit. If the block is free the bit is 1 if
the block isallocated the bit is 0.
• Disadvantage is efficiency in finding the first free block or consecutive free
blocks onthe disk
• Indeed many computers supply bit-manipulation instructions that can
be usedeffectively for that purpose.
• The calculation of the block number
(number of bits per word) (number of 0-value words)+offset of first 1 bit
Bit vectors are inefficient unless the entire vector is kept in maim memory.
Keeping it inmain memory is possible for smaller disk but not necessarily for larger
ones.

Linked List
• Free space management is to link together all the free disk blocks, keeping a
pointer tothe first free block in a special location on the disk and caching it
in memory.
• The first block contains a pointer to the next free disk block and so on.
• We could keep a pointer to block 2 as the first free block. Block 2 would
contain a pointer to block 3, which would point to block 4, which would
point to block 5 and so on.

Grouping
• A modification of the free list approach is to store the addresses of n free
blocks in the first free block.
• The first n-1 of these blocks is actually free.
• The last block contains the addresses of another n free blocks, and so on.
• The addresses of a large number of free blocks can now be found quickly.

Counting
• Another approach is to take advantage of the fact that generally several
contiguous blocks may be allocated or freed simultaneously particularly
when space is allocated with the contiguous allocation algorithm or through
clustering.
• Thus rather than keeping a list of n free disk addresses, we can keep the
address of the first free block and the number n of free contiguous blocks
that follow the first block.
• Each entry in the free space list than consists of a disk address and count.

Linked free space list on disk

DISK STRUCTURE
The traditional head-sector-cylinder, HSC numbers are mapped to linear block
addresses by numbering the first sector on the first head on the outermost track as sector
0. Numbering proceedswith the rest of the sectors on that same track, and then the rest of
the tracks on the same cylinder before proceeding through the rest of the cylinders to the
center of the disk. In modern practice these linear block addresses are used in place
of the HSC numbers for a variety ofreasons:
The linear length of tracks near the outer edge of the disk is much longer than
for those tracks located near the center, and therefore it is possible to squeeze
many more sectors onto outer tracks than onto inner ones.
All disks have some bad sectors, and therefore disks maintain a few spare
sectors that can be used in place of the bad ones. The mapping of spare
sectors to bad sectors in managed internally to the disk controller.
Modern hard drives can have thousands of cylinders, and hundreds of sectors per
track on their outermost tracks. These numbers exceed the range of HSC numbers for
many ( older ) operating systems, and therefore disks can be configured for any
convenient combination of HSC values that falls within the total number of sectors
physically on the drive.
• There is a limit to how closely packed individual bits can be placed on a
physical media, but that limit is growing increasingly more packed as
technological advances are made.
• Modern disks pack many more sectors into outer cylinders than inner ones,
using one of two approaches:

◦ With Constant Linear Velocity, CLV, the density of bits is uniform from
cylinder to cylinder. Because there are more sectors in outer cylinders,
the disk spins slower when reading those cylinders, causing the rate of
bits passing under the read-write head to remain constant. This is the
approach used by modern CDs and DVDs.
◦ With Constant Angular Velocity, CAV, the disk rotates at a constant
angularspeed, with the bit density decreasing on outer cylinders. (These
disks would have a constant number of sectors per track on all
cylinders.)
DISK SCHEDULING
Disk Scheduling Algorithms is an algorithm used to schedule the servicing
of disk I/Orequests.

Types
FCFS
Scheduling
SSTF
Scheduling
SCAN
Scheduling
C-SCAN
Schedulin
LOOK
Schedulin
The disk scheduling algorithms are illustrated with a request queue (0-199)
• Queue = 98, 183, 37, 122, 14, 124, 65, 67
• Head pointer = 53
FCFS Scheduling
FCFS (First-Come First-Served) Scheduling is the simplest form of disk
scheduling. Itsimply servers the first request in the queue.

Illustration
Initially the disk head is at
cylinder 53.Head Movement
Current Next Total
53 98 45
98 183 85
183 37 146
37 122 85
122 14 108
14 124 110
124 65 59
65 67 2
Total 640
Total head movement = 640 cylinders.

FCFS Scheduling
Advantage
• Simple to write and understand
• Perform operations in order requested
• No reordering of work queue
• No starvation: every request is serviced

Disadvantage
• Large seek time as the disk head may have to jump from one extreme to another.
• Does not provide the fastest service.

SSTF Scheduling
SSTF (shortest-seek time first algorithm) scheduling is an algorithm that
selects therequest with the minimum seek time from the current head position.

Illustration
Initially the disk head is at
cylinder 53.Head Movement
Next
Current Total
(Closet)
53 65 12
65 67 2
67 37 30
37 14 23
14 98 84
98 122 24
122 124 2
124 183 59
Total 236
Total head movement = 236 cylinders.

SSTF Scheduling

You might also like