Operating System Concepts Overview
Operating System Concepts Overview
BSc IV SEMESTER
Prepared By,
Vinay Kumar V
Assistant Professor
Department of Computer Science
GFGC, Raibag
Operating System
UNIT I:
Introduction: Basics of Operating Systems: Definition, types of Operating
Systems, OS Service, System Calls, OS structure: Layered, Monolithic,
Microkernel Operating Systems – Concept of Virtual Machine.
UNIT II:
Process Management Process Definition , Process Relationship , Process
states , Process State transitions , Process Control Block , Context switching ,
Threads, Concept of multithreads , Benefits of threads, Types of threads.
Process Scheduling: Definition, Scheduling objectives, Types of Schedulers,
CPU scheduling algorithms, performance evaluation of the scheduling.
UNIT III:
Inter-process Communication, Race Conditions, Critical Section, Mutual
Exclusion, Hardware Solution, Strict Alternation, Peterson’s Solution, The
Producer Consumer Problem, Semaphores, Event Counters, Monitors,
Message Passing, and Classical IPC Problems.
Deadlocks: Definition, Deadlock characteristics, Deadlock Prevention,
Deadlock Avoidance (concepts only).
UNIT IV:
Memory Management: Logical and Physical address map, Memory
allocation, Internal and External fragmentation and Compaction, Paging.
Virtual Memory: Demand paging, Page Replacement policies.
UNIT V:
I/O Management Principles of I/O Hardware: Disk structure, Disk
scheduling algorithm
File Management: Access methods, File types, File operation, Directory
structure, File System structure, Allocation methods, Free-space
management, and directory implementation. Structure of Linux Operating
System, Exploring the Directory Structure, Naming Files and Directories,
Concept of shell, Types of shell, Editors for shell programming (e.g. vi),
basics of Shell programming.
UNIT I
Introduction: Basics of Operating Systems: Definition, types of Operating
Systems, OS Service, System Calls, OS structure: Layered, Monolithic,
Microkernel Operating Systems – Concept of Virtual Machine.
System Views:
We can view system as resource allocator i.e. a computer system has
many resources that may be used to solve a problem. The OS acts as a
manager of these resources. The OS must decide how to allocate these
resources to programs and the users so that it can operate the
Computer system efficiently and fairly.
A different view of an OS is that it need to control various I/O devices &
user programs i.e. an OS is a control program used to manage the
execution of user program to prevent errors and improper use of the
computer.
Resources can be either CPU Time, memory space, file storage space,
I/O devices and so on. The OS must support the following tasks
Drawback of First OS
The CPU may be idle for some time because the speed of the mechanical
devices slower compared to the electronic devices.
In order to eliminate this drawback, a batch OS was used to perform the task
of batching jobs.
Advantages:
Simple, Sequential job Scheduling.
Human interventions minimized.
Increased performance & System throughput due to batching of jobs.
Disadvantages:
Turn around time can be large from user point of view due to batching.
Difficult to debug the program.
A job can enter into infinite loop.
A job could corrupt the monitor.
Due to lack of protection scheme, one job may affect the pending jobs.
Example: Payroll System, Bank Statements etc
Note: Turn around time means time elapsed between the time of submission of a
process or job by a user and the time of completion of that process or job.
Spooling:
SPOOL(Simultaneous Peripheral Operation On-Line)
Spooling is a process in which data is temporarily held to be used and
executed by a device, program or the system.
Data is sent to and stored in memory of other volatile (Temporary
memory) storage until the program or computer requests it for
execution.
This set of job is subset of the jobs kept in the job pool. The operating
system picks and begins to execute one of the jobs in the memory.
When a job needs to wait the CPU is simply switched to another job
and so on.
Functions:
The multiprogramming operating system is sophisticated because the
operating system makes decisions for the user. This is known as Job
scheduling.
If several jobs are ready to run at the same time the system choose one
among them. This is known as CPU scheduling.
Other functions are Memory management, Device and File
Management.
Disadvantages of RTOS:
Limited Tasks: Very few tasks run at the same time and their
concentration is very less on few applications to avoid errors.
Use heavy system resources: Sometimes the system resources are
not so good and they are expensive as well.
Complex Algorithms: The algorithms are very complex and difficult
for the designer to write on.
Device driver and interrupt signals: It needs specific device drivers
and interrupts signals to response earliest to interrupts.
Thread Priority: It is not good to set thread priority as these systems
are very less pron to switching tasks.
I/O Operation:
Accomplish the task of Device allocation and control I/O devices.
Provide for notifying device errors, device status, etc.
Communication:
Accomplish the task of inter-process communication either on the
same computer system or between different computer systems on a
computer network.
Provide for Message passing and shared memory access in safe
mode.
Error detection:
The operating system should take the appropriate actions for the
occurrences of any type like arithmetic overflow; divide by zero
error, access to the illegal memory location and too large user CPU
time.
Accomplish the task of error detection and recovery if any. Ex: Paper
jam or out of paper in a printer.
Keep track status of CPU, Memory, I/O devices, Storage devices, File
system, networking, etc.
Abort execution in case of fatal errors such as RAM parity errors,
power fluctuations, if any.
Resource Allocation:
Accomplish the task of resource allocation to multiple jobs.
Reclaim the allocated resources after their use or as and when the
job terminates.
When multiple users are logged on to the system the resources must
be allocated to each of them.
For current distribution of the resource among the various
processes the operating system uses the CPU scheduling run times
which determine which process will be allocated with the resource.
Accounting:
The operating system keep track of which users use how many and
which kind of computer resources.
Maintain logs of system activities for performance analysis and error
recovery.
Protection:
Accomplish the task of protecting the system resources against
malicious use.
Provide for safe computing by employing security scheme against
unauthorized access/users.
Authenticate legitimate users with login passwords and
registrations.
The operating system is responsible for both hardware as well as
software protection.
The operating system protects the information stored in a multiuser
computer system.
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 and PERL etc.
System calls occur in different ways depending on the computer in use.
System calls can be roughly grouped into 5 major categories.
3. Device Management:
Request device, release device: If there are multiple users of the
system, we first request the device. After we finished with the device,
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/Management:
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.
5. Communication Management:
Create, Delete Connection.
Send, Receive Message.
Attach, Detach remote device(Mounting/Remote Login)
Transfer status information(byte)
1. Simple Structure
There are several commercial system that don’t have a well- defined
structure such operating systems begins as small, simple & limited
systems and then grow beyond their original scope.
MS-DOS is an example of such system. It was not divided into modules
carefully.
Another example of limited structuring is the UNIX operating system.
There was no CPU Execution Mode (user and kernel), so errors in
applications could cause the whole system to crash.
2. Monolithic Approach
A monolithic design of the operating system architecture makes no
special accommodation for the special nature of the operating system.
Although the design follows the separation of concerns, no attempt is
made to restrict the privileges granted to the individual parts of the
operating system.
The entire operating system executes with maximum privileges.
The communication overhead inside the monolithic operating system
is the same as the communication overhead inside any other software,
considered relatively low.
3. Layered approach:
In the layered approach, the OS is broken into a number of layers
(levels) each built on top of lower layers. The bottom layer (layer 0) is
the hardware & top most layer (layer N) is the user interface.
The main advantage of the layered approach is modularity.
The layers are selected such that each users functions (or operations)
& services of only lower layer.
This approach simplifies debugging & system verification, i.e. the first
layer can be debugged without concerning the rest of the system. Once
the first layer is debugged, its correct functioning is assumed while the
2nd layer is debugged & so on.
If an error is found during the debugging of a particular layer, the error
must be on that layer because the layers below it are already debugged.
Thus the design & implementation of the system are simplified when
the system is broken down into layers.
Each layer is implemented using only operations provided by lower
layers. A layer doesn’t need to know how these operations are
implemented; it only needs to know what these operations do.
The layer approach was first used in the operating system. It was
defined in six layers.
4. Microkernel approach:
This structures the operating system by removing all nonessential portions
of the kernel and implementing them as system and user level programs.
Drawback:
Virtual Machine includes the increased system overhead which is due
to simulation of virtual machine operation heavily.
The efficiency of VM OS depends upon the number of operations that
must be simulated by VM monitor.
UNIT II
Process Management Process Definition , Process Relationship ,
Process states , Process State transitions , Process Control Block ,
Context switching , Threads, Concept of multithreads , Benefits of
threads, Types of threads.
Process Scheduling: Definition, Scheduling objectives, Types of
Schedulers, CPU scheduling algorithms, performance evaluation of the
scheduling.
Process Management:
A program does nothing unless their instructions are executed by a
CPU.A process is a program in execution. A time shared user program
such as a complier is a process. A word processing program being run
by an individual user on a pc is a process.
A system task such as sending output to a printer is also a process. A
process needs certain resources including CPU time, memory files &
I/O devices to accomplish its task.
These resources are either given to the process when it is created or
allocated to it while it is running.
The OS is responsible for the following activities of process
management.
o Creating & deleting both user & system processes.
o Suspending & resuming processes.
o Providing mechanism for process synchronization.
o Providing mechanism for process communication.
o Providing mechanism for deadlock handling.
Process:
A process or task is an instance of a program in execution.
The execution of a process must programs in a sequential manner.
At any time at most one instruction is executed.
The process includes the current activity as represented by the value of
the program counter and the content of the processors registers. Also it
includes the process stack which contain temporary data (such as
method parameters return address and local variables) & a data
section which contain global variables. A process may also include a
heap, which is memory that is dynamically allocated during process
run time.
Difference between process & program:
A program by itself is not a process.
A program in execution is known as a process.
A program is a passive entity, such as the contents of a file stored on
disk whereas process is an active entity with a program counter
specifying the next instruction to execute and a set of associated
resources may be shared among several process with some scheduling
algorithm being used to determinate when the stop work on one
process and service a different one.
Process Relationship:
Process States
As a process executes, it changes state. The state of a process is defined by
the correct activity of that process. Each process may be in one of the
following states.
New: The process is being created.
Ready: The process is waiting to be assigned to a processor.
Running: Instructions are being executed.
Waiting: The process is waiting for some event to occur.
Terminated: The process has finished execution.
Threads
A thread is a flow of execution through the process code, with its own
program counter, system registers and stack.
Threads are a popular way to improve application performance
through parallelism.
A thread is sometimes called a light weight process.
Threads represent a software approach to improving performance of
operating system by reducing the over head thread is equivalent to a
classical process.
Each thread belongs to exactly one process and no thread can exist
outside a process.
Each thread represents a separate flow of control.
In this model, developers can create as many user threads as necessary and
the corresponding Kernel threads can run in parallels on a multiprocessor.
If the user level thread libraries are implemented in the operating system,
that system does not support Kernel threads use the many to one
relationship modes.
It allows another thread to run when a thread makes a blocking system call.
It supports multiple threads to execute in parallel on microprocessors.
Disadvantages:
Kernel threads are generally slower to create and manage than the
user threads.
Transfer of control from one thread to another within same process
requires a mode switch to the Kernel.
Benefits/Advantages of Thread
Thread minimizes context switching time.
Use of threads provides concurrency within a process.
Efficient communication.
Economy- It is more economical to create and context switch threads.
Utilization of multiprocessor architectures –
The benefits of multithreading can be greatly increased in a multiprocessor
architecture.
6 In multiple process each process One thread can read, write or even
operates independently of the completely wipe out another
others threads stack
Process Scheduling:
Scheduling is a fundamental function of OS. When a computer is
Multiprogrammed, it has multiple processes competing for the CPU at the
same time. If only one CPU is available, then a choice has to be made
regarding which process to execute next. This decision making process is
known as scheduling and the part of the OS that makes this choice is called
a scheduler. The algorithm it uses in making this choice is called
scheduling algorithm.
Scheduling queues: As processes enter the system, they are put into a job
queue. This queue consists of all process in the system. The process that are
residing in main memory and are ready & waiting to execute or kept on a list
called ready queue.
This queue is generally stored as a linked list. A ready queue header contains
pointers to the first & final PCB in the list. The PCB includes a pointer field
that points to the next PCB in the ready queue. The lists of processes waiting
for a particular I/O device are kept on a list called device queue. Each device
has its own device queue. A new process is initially put in the ready queue. It
waits in the ready queue until it is selected for execution & is given the CPU.
SCHEDULERS:
A process migrates between the various scheduling queues throughout
its life-time purposes. The OS must select for scheduling processes
from these queues in some fashion. This selection process is carried
out by the appropriate scheduler.
In a batch system, more processes are submitted and then executed
immediately. So these processes are spooled to a mass storage device
like disk, where they are kept for later execution.
Types of schedulers:
There are 3 types of schedulers mainly used:
1. Long term scheduler:
Long term scheduler selects process from the disk & loads them into
memory for execution.
It controls the degree of multi-programming i.e. no. of processes in
memory.
It executes less frequently than other schedulers.
If the degree of multiprogramming is stable than the average rate of
process creation is equal to the average departure rate of processes
leaving the system.
So, the long term scheduler is needed to be invoked only when a
process leaves the system.
Due to longer intervals between executions it can afford to take more
time to decide which process should be selected for execution.
Most processes in the CPU are either I/O bound or CPU bound.
An I/O bound process (an interactive ‘C’ program) is one that spends
most of its time in I/O operation than it spends in doing I/O operation.
A CPU bound process is one that spends more of its time in doing
computations than I/O operations (complex sorting program).
It is important that the long term scheduler should select a good mix of
I/O bound & CPU bound processes.
Scheduling objectives:
1. CPU utilization – keep the CPU as busy as possible
2. Throughput – Number of processes that complete their execution per
time unit
3. Turnaround time – amount of time to execute a particular process
4. Waiting time – amount of time a process has been waiting in the ready
queue
5. Response time – amount of time it takes from when a request was
submitted until the first response is produced, not output (for time-
sharing environment)
General Goals
Fairness
Fairness is important under all circumstances. A scheduler makes sure
that each process gets its fair share of the CPU and no process can
suffer indefinite postponement.
Note that giving equivalent or equal time is not fair. Think of safety
control and payroll at a nuclear plant.
Policy Enforcement
The scheduler has to make sure that system's policy is enforced. For
example, if the local policy is safety then the safety control processes
must be able to run whenever they want to, even if it means delay in
payroll processes.
Efficiency
Scheduler should keep the system (or in particular CPU) busy cent
percent of the time when possible. If the CPU and all the Input/Output
devices can be kept running all the time, more work gets done per
second than if some components are idle.
Response Time
A scheduler should minimize the response time for interactive user.
Turnaround
A scheduler should minimize the time batch users must wait for an
output.
Throughput
A scheduler should maximize the number of jobs processed per unit
time.
A little thought will show that some of these goals are contradictory. It
can be shown that any scheduling algorithm that favors some class of
jobs hurts another class of jobs. The amount of CPU time available is
finite, after all.
Non-preemptive Scheduling
A scheduling discipline is non-preemptive if, once a process has been given
the CPU, the CPU cannot be taken away from that process.
Preemptive Scheduling
A scheduling discipline is preemptive if, once a process has been given
the CPU can taken away.
The strategy of allowing processes that are logically runnable to be
temporarily suspended is called Preemptive Scheduling and it is
contrast to the "run to completion" method.
P1 P2 P3 P4
0 3 8 10 14
P3 P1 P2 P4
0 2 5 9 14
P1 P2 P4 P1 P3
0 1 5 10 17 26
P2 P5 P1 P3 P4
0 1 6 16 18 19
The Waiting time for process
P1 = 6
P2 = 0
P3 = 16
P4 = 18
P5 = 1
The Average Waiting time = (0 + 1 + 6 + 16 + 18)/5 = 41/5 = 8.2
Inter-process Communication:
Inter process communication (IPC) is a mechanism which allows
processes to communicate each other and synchronize their actions.
The communication between these processes can be seen as a method
of co-operation between them.
Processes executing concurrently 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. Any process that does not share
data with any other process is independent.
A process is cooperating if it can affect or be affected by the other
processes executing in the system. Clearly, any process that shares data
with other processes is a cooperating process.
Reasons for co-operating processes:
o 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.
Shared-Memory Systems
Inter-process communication using shared memory requires
communicating processes to establish a region of shared memory.
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.
Recall that, 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.
Race Conditions:
A situation like this, where several processes access and manipulate the
same data concurrently and the outcome of the execution depends on the
particular order in which the access takes place, is called a race
condition.
Suppose that two processes, P1 and P2, share the global variable a.
At some point in its execution, P1 updates a to the value 1, and at
some point in its execution, P2 updates a to the value 2. Thus, the
two tasks are in a race to write variable a.
In this example the "loser" of the race (the process that updates last)
determines the final value of a.
Example:
While (1)
{
Entry Section;
Critical Section;
Exit Section;
Remainder Section;
}
A solution to the critical section problem must satisfy the following three
conditions.
1. Mutual Exclusion: If process Pi is executing in its critical section
then no any other process can be executing in their critical section.
Mutual Exclusion:
Requirements for Mutual Exclusion:
1. Mutual exclusion must be enforced: Only one process at a time is
allowed into its critical section, among all processes that have critical
sections for the same resource or shared object.
2. A process that halts in its non critical section must do so without
interfering with other processes.
3. It must not be possible for a process requiring access to a critical
section to be delayed indefinitely: no deadlock or starvation.
4. When no process is in a critical section, any process that requests
entry to its critical section must be permitted to enter without delay.
5. No assumptions are made about relative process speeds or number of
processors.
6. A process remains inside its critical section for a finite time only.
Hardware Solution:
Hardware approaches to mutual exclusion.
Interrupt Disabling:
In a uniprocessor machine, concurrent processes cannot be overlapped; they
can only be interleaved. Furthermore, a process will continue to run until it
invokes an operating system service or until it is interrupted. Therefore, to
guarantee mutual exclusion, it is sufficient to prevent a process from being
interrupted. This capability can be provided in the form of primitives
defined by the system kernel for disabling and enabling interrupts.
Solution to Critical-section Problem Using Locks
do
{
acquire lock
critical section;
release lock
remainder section;
} while (TRUE);
Disadvantages
It works only in a single processor environment.
Interrupts can be lost if not serviced promptly.
A process waiting to enter its critical section could suffer from
starvation.
Definition:
boolean TestAndSet (boolean *target)
{
boolean rv = *target;
*target = TRUE;
return rv:
}
Solution:
do {
while ( TestAndSet (&lock ))
; // do nothing
// critical section
lock = FALSE;
// remainder section
} while (TRUE);
Advantages
1. It is simple and easy to verify.
2. It is applicable to any number of processes.
3. It can b used to support multiple critical section.
Disadvantages
1. Busy waiting is possible.
2. Starvation is also possible.
3. There may be deadlock.
Swap Instruction:
Definition:
void Swap (boolean *a, boolean *b)
{
boolean temp = *a;
*a = *b;
*b = temp:
}
Solution:
do {
key = TRUE;
while ( key == TRUE)
Swap (&lock, &key);
// critical section
lock = FALSE;
// remainder section
} while (TRUE);
Bounded-waiting Mutual Exclusion with TestandSet()
do {
waiting[i] = TRUE;
key = TRUE;
while (waiting[i] && key)
key = TestAndSet(&lock);
waiting[i] = FALSE;
// critical section
j = (i + 1) % n;
while ((j != i) && !waiting[j])
j = (j + 1) % n;
if (j == i)
lock = FALSE;
else
waiting[j] = FALSE;
// remainder section
} while (TRUE);
Peterson’s Solution:
Problem
When one process is updating shared modifiable data in its critical section,
no other process should allowed to enter in its critical section.
Each process disables all interrupts just after entering in its critical section
and re-enable all interrupts just before leaving critical section. With
interrupts turned off the CPU could not be switched to other process.
Hence, no other process will enter its critical and mutual exclusion achieved.
Conclusion
Disabling interrupts is sometimes a useful interrupts is sometimes a useful
technique within the kernel of an operating system, but it is not appropriate
as a general mutual exclusion mechanism for user process. The reason is
that it is unwise to give user process the power to turn off interrupts.
Conclusion
The flaw in this proposal can be best explained by example. Suppose process
A sees that the lock is 0. Before it can set the lock to 1 another process B is
scheduled, runs, and sets the lock to 1. When the process A runs again, it will
also set the lock to 1, and two processes will be in their critical section
simultaneously.
In this proposed solution, the integer variable 'turn' keeps track of whose
turn is to enter the critical section. Initially, process A inspects turn, finds it
to be 0, and enters in its critical section. Process B also finds it to be 0 and
sits in a loop continually testing 'turn' to see when it becomes 1.
Continuously testing a variable waiting for some value to appear is called the
Busy-Waiting.
Conclusion:
Taking turns is not a good idea when one of the processes is much slower
than the other. Suppose process 0 finishes its critical section quickly, so both
processes are now in their noncritical section. This situation violates above
mentioned condition 3.
ALGORITHM:
PROGRAM CODING
#include<stdio.h>
int mutex=1,full=0,empty=3,x=0; main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n [Link]\[Link]\[Link]\n"); while(1)
{
printf(" \nenter ur choice");
scanf("%d",&n);
switch(n)
{
case 1: if((mutex==1)&&(empty!=0))
producer();
else
printf("buffer is full\n");
break;
case 2: if((mutex==1)&&(full!=0))
consumer();
else
printf("buffer is empty");
break;
case 3: exit(0);
break;
}
}
}
int wait(int s)
{
return(--s);
}
int signal(int s)
{
return (++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\n producer produces the items %d",x);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\n consumer consumes the item %d",x);
x--;
mutex=signal(mutex);
}
Semaphores:
The integer value of the semaphore in the wait and signal operations must
be executed indivisibly. That is, when one process modifies the semaphore
value, no other process can simultaneously modify that same semaphore
value.
In addition, in the case of the wait(S), the testing of the integer value of S (S
0) and its possible modification (S := S – 1), must also be executed without
interruption.
Semaphore Implementation
Must guarantee that no two processes can execute wait () and signal ()
on the same semaphore at the same time
Thus, implementation becomes the critical section problem where the
wait and signal code are placed in the critical section.
Could now have busy waiting in critical section implementation But
implementation code is short Little busy waiting if critical section
rarely occupied
Note that applications may spend lots of time in critical sections and
therefore this is not a good solution.
Implementation of wait:
wait(semaphore *S) {
S->value--;
if (S->value < 0) {
add this process to S->list;
block();
}
}
Implementation of signal:
signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
remove a process P from S->list;
wakeup(P);
}
}
Semaphores are not provided by hardware. But they have several attractive
properties:
1. Semaphores are machine independent.
2. Semaphores are simple to implement.
3. Correctness is easy to determine.
4. Can have many different critical sections with different semaphores.
5. Semaphore acquires many resources simultaneously.
Drawback of Semaphore
1. They are essentially shared global variables.
2. Access to semaphores can come from anywhere in a program.
3. There is no control or guarantee of proper usage.
4. There is no linguistic connection between the semaphore and the data
to which the semaphore controls access.
5. They serve two purposes, mutual exclusion and scheduling constraints.
Monitors:
Although semaphores provide a convenient and effective mechanism for
process synchronization, using them incorrectly can result in timing errors
that are difficult to detect, since these errors happen only if particular
execution sequences take place and these sequences do not always occur.
monitor monitor_name
{
/* shared variable declarations */
function P1 ( . . . ) {
...
}
function P2 ( . . . ) {
...
}
.
.
.
function Pn ( . . . ) {
...
}
initialization code ( . . . ) {
...
}
}
Figure: Syntax of a monitor.
The monitor construct ensures that only one process at a time is active
within the monitor
However, the monitor construct, as defined so far, is not sufficiently
powerful for modeling some synchronization schemes.
For this purpose, we need to define additional synchronization
mechanisms.
condition x, y;
The only operations that can be invoked on a condition variable are
wait() and signal().
[Link]();
The operation means that the process invoking this operation is
suspended until another process invokes
[Link]();
The [Link]() operation resumes exactly one suspended process.
If no process is suspended, then the signal() operation has no effect;
On the one hand, since P was already executing in the monitor, the
signal and continue method seems more reasonable. On the other, if
we allow thread P to continue, then by the time Q is resumed, the
logical condition for which Q was waiting may no longer hold. When
thread P executes the signal operation, it immediately leaves the
monitor. Hence, Q is immediately resumed.
Message Passing:
Message passing provides a mechanism to allow processes to
communicate and to synchronize their actions without sharing the
same address space.
It is particularly useful in a distributed environment, where the
communicating processes may reside on different computers
connected by a network.
A message-passing facility provides at least two operations:
o send(message)
o receive(message)
If P and Q wish to communicate, they need to establish a
communication link between them exchange messages via
send/receive Implementation of communication link physical (e.g.,
shared memory, hardware bus) logical (e.g., logical properties)
Direct Communication
Processes must name each other explicitly:
o send (P, message) – send a message to process P
o receive(Q, message) – receive a message from process Q
Properties of communication link
Links are established automatically
A link is associated with exactly one pair of communicating processes
Between each pair there exists exactly one link
The link may be unidirectional, but is usually bi-directional
Indirect Communication
Messages are directed and received from mailboxes (also referred to as
ports)
Each mailbox has a unique id
Processes can communicate only if they share a mailbox
Properties of communication link
Link established only if processes share a common mailbox
A link may be associated with many processes
Each pair of processes may share several communication links
Link may be unidirectional or bi-directional
Operations
create a new mailbox
send and receive messages through mailbox
destroy a mailbox
Primitives are defined as:
o send(A, message) – send a message to mailbox A
o receive(A, message) – receive a message from mailbox A
Mailbox sharing
Problem: Now suppose that processes P1, P2, and P3 all share mailbox A.
Process P1 sends a message to A, while both P2 and P3 execute a receive()
from A. Which process will receive the message sent by P1?
Solutions
Allow a link to be associated with at most two processes
Allow only one process at a time to execute a receive operation
Allow the system to select arbitrarily the receiver. Sender is notified
who the receiver was.
Synchronization
Message passing may be either blocking or non-blocking
Blocking is considered synchronous
Blocking send has the sender block until the message is received
Blocking receive has the receiver block until a message is available
Non-blocking is considered asynchronous
Non-blocking send has the sender send the message and continue
Non-blocking receive has the receiver receive a valid message or null
Buffering
Queue of messages attached to the link; implemented in one of three ways
1. Zero capacity – 0 messages Sender must wait for receiver (rendezvous)
2. Bounded capacity – finite length of n messages Sender must wait if link
full
3. Unbounded capacity – infinite length Sender never waits
Classical IPC Problems:
These problems are used for testing nearly every newly proposed
synchronization scheme. In our solutions to the problems, we use
semaphores for synchronization, since that is the traditional way to present
such solutions. However, actual implementations of these solutions could
use mutex locks in place of binary semaphores.
1. The Bounded-Buffer Problem
2. The Readers–Writers Problem
3. The Dining-Philosophers Problem
When a philosopher thinks, she does not interact with her colleagues.
From time to time, a philosopher gets hungry and tries to pick up the
two chopsticks that are closest to her (the chopsticks that are between
her and her left and right neighbors).
A philosopher may pick up only one chopstick at a time.
She cannot pick up a chopstick that is already in the hand of a neighbor.
When a hungry philosopher has both her chopsticks at the same time,
she eats without releasing the chopsticks.
When she is finished eating, she puts down both chopsticks and starts
thinking again.
A philosopher tries to grab a chopstick by executing a wait() operation
on that semaphore.
She releases her chopsticks by executing the signal() operation on the
appropriate semaphores.
Thus, the shared data are where all the elements of chopstick are
initialized to 1.
semaphore chopstick[5];
do {
wait(chopstick[i]);
wait(chopstick[(i+1) % 5]);
...
/* eat for awhile */
...
signal(chopstick[i]);
signal(chopstick[(i+1) % 5]);
...
/* think for awhile */
...
} while (true);
Fig: The structure of philosopher i.
Although this solution guarantees that no two neighbors are eating
simultaneously, it nevertheless must be rejected because it could
create a deadlock.
Suppose that all five philosophers become hungry at the same time and
each grabs her left chopstick.
All the elements of chopstick will now be equal to 0.
When each philosopher tries to grab her right chopstick, she will be
delayed forever.
Several possible remedies to the deadlock problem are replaced by:
o Allow at most four philosophers to be sitting simultaneously at
the table.
o Allow a philosopher to pick up her chopsticks only if both
chopsticks are available (to do this, she must pick them up in a
critical section).
o Use an asymmetric solution—that is, an odd-numbered
philosopher picks up first her left chopstick and then her right
chopstick, whereas an even numbered philosopher picks up her
right chopstick and then her left chopstick.
Deadlocks
Definition: In a multiprogramming environment, several processes may
compete for a finite number of resources. A process requests resources; if
the resources are not available at that time, the process enters a waiting
state. Sometimes, a waiting process is never again able to change state,
because the resources it has requested are held by other waiting processes.
This situation is called a deadlock.
SYSTEM MODEL
• A system may consist of finite number of resources and is distributed
among number of processes. There resources are partitioned into several
instances each with identical instances.
• A process must request a resource before using it and it must release the
resource after using it. It can request any number of resources to carry out a
designated task. The amount of resource requested may not exceed the total
number of resources available.
Deadlock characteristics:
Necessary Conditions: A deadlock situation can occur if the following 4
conditions occur simultaneously in a system:-
1. Mutual Exclusion: Only one process must hold the resource at a time.
If any other process requests for the resource, the requesting process
must be delayed until the resource has been released.
2. Hold and Wait:- A process must be holding at least one resource and
waiting to acquire additional resources that are currently being held by
the other process.
3. No Preemption:- Resources can’t be preempted i.e., only the process
holding the resources must release it after the process has completed
its task.
4. Circular Wait:- A set {P0,P1……..Pn} of waiting process must exist such
that P0 is waiting for a resource i.e., held by P1, P1 is waiting for a
resource i.e., held by P2. Pn-1 is waiting for resource held by process
Pn and Pn is waiting for the resource i.e., held by P1. All the four
conditions must hold for a deadlock to occur.
Deadlock Prevention:
For a deadlock to occur each of the four necessary conditions must hold. If at
least one of the conditions does not hold then we can prevent occurrence of
deadlock.
4. Circular Wait: The fourth and the final condition for deadlock is the
circular wait condition. One way to ensure that this condition never, is to
impose ordering on all resource types and each process requests resource
in an increasing order.
Safe State:
A state is a safe state in which there exists at least one order in which
all the process will run completely without resulting in a deadlock.
A system is in safe state if there exists a safe sequence.
A sequence of processes <P1,P2,………..Pn> is a safe sequence for the
current allocation state if for each Pi the resources that Pi can request
can be satisfied by the currently available resources.
If the resources that Pi requests are not currently available then Pi can
obtain all of its needed resource to complete its designated task.
A safe state is not a deadlock state.
Whenever a process request a resource i.e., currently available, the
system must decide whether resources can be allocated immediately or
whether the process must wait. The request is granted only if the
allocation leaves the system in safe state.
In this, if a process requests a resource i.e., currently available it must
still have to wait. Thus resource utilization may be lower than it would
be without a deadlock avoidance algorithm.
Banker’s Algorithm:
This algorithm is applicable to the system with multiple instances of
each resource types, but this is less efficient then the resource
allocation graph algorithm.
When a new process enters the system it must declare the maximum
number of resources that it may need. This number may not exceed the
total number of resources in the system. The system must determine
that whether the allocation of the resources will leave the system in a
safe state or not. If it is so resources are allocated else it should wait
until the process release enough resources.
Several data structures are used to implement the banker’s algorithm.
Let ‘n’ be the number of processes in the system and ‘m’ be the number
of resources types.
Step 4: If Finish [i] = false, for some i, 1<=i<= n, then the system is in a
deadlock state.
Moreover, if Finish[i] = false, then process Pi is deadlocked.
Example 1:
5 processes P0 through P4;
3 resource types: A (10 instances), B (5instances), and C (7 instances).
Snapshot at time T0:
Allocation Max
ABC ABC
P0 010 753
P1 200 322
P2 302 902
P3 211 222
P4 002 433
Executing safety algorithm shows that sequence <P1, P3, P4, P0, P2>
satisfies safety requirement.
Can request for (3,3,0) by P4 be granted? –NO
Can request for (0,2,0) by P0 be granted? –NO (Results Unsafe)
Resource Preemption
Selecting a victim – minimize cost.
Rollback – return to some safe state, restart process for that state.
Starvation – same process may always be picked as victim, include number of
rollback in cost factor.
UNIT IV
UNIT IV:
Memory Management:
Memory Management: Logical and and Physical
Physical address
address map,
map,Memory
Memory
allocation,Internal
allocation, Internaland
andExternal
Externalfragmentation
fragmentationand
andCompaction,
Compaction,Paging.
Paging.
VirtualMemory:
Virtual Memory:Demand
Demandpaging,
paging,Page
PageReplacement
Replacementpolicies.
policies.
Background:
Memory management is concerned with managing the primary
memory.
Memory consists of array of bytes or words each with their own
address.
The instructions are fetched from the memory by the CPU based on the
value program counter. These instructions may cause additional
loading from and storing to specific memory addresses.
Memory unit sees only a stream of memory addresses. It does not
know how they are generated.
Program must be brought into memory and placed within a process for
it to be run.
Input queue – collection of processes on the disk that are waiting to be
brought into memory for execution.
User programs go through several steps before being run.
The figure below shows the dynamic relation. Dynamic relation implies
mapping from virtual address space to physical address space and is
performed at run time usually with some hardware assistance.
Dynamic Loading:-
For a process to be executed it should be loaded in to the physical
memory. The size of the process is limited to the size of the physical
memory.
Dynamic loading is used to obtain better memory utilization.
In dynamic loading the routine or procedure will not be loaded until it
is called.
Whenever a routine is called, the calling routine first checks whether
the called routine is already loaded or not. If it is not loaded it cause the
loader to load the desired program in to the memory and updates the
programs address table to indicate the change and control is passed to
newly called routine.
Advantage:-
Gives better memory utilization.
Unused routine is never loaded.
Do not need special operating system support.
This method is useful when large amount of codes are needed to
handle in frequently occurring cases.
Swapping:
Swapping is a technique of temporarily removing inactive programs from
the memory of the system. A process can be swapped temporarily out of the
memory to a backing store and then brought back in to the memory for
continuing the execution. This process is called swapping.
Example:-
In a multi-programming environment with a round robin CPU
scheduling whenever the time quantum expires then the process that
has just finished is swapped out and a new process swaps in to the
memory for execution.
A variation of swap is priority based scheduling. When a low priority is
executing and if a high priority process arrives then a low priority will
be swapped out and high priority is allowed for execution. This process
is also called as Roll out and Roll in.
Normally the process which is swapped out will be swapped back to
the same memory space that is occupied previously. This depends
upon address binding.
If the binding is done at load time, then the process is moved to same
memory location. If the binding is done at run time, then the process is
moved to different memory location.
This is because the physical address is computed during run time.
Swapping requires backing store and it should be large enough to
accommodate the copies of all memory images. The system maintains a
ready queue consisting of all the processes whose memory images are
on the backing store or in memory that are ready to run.
The logical address must be less than the limit register, the MMU maps
the logical address dynamically by adding the value in re-location
register. When the CPU scheduler selects a process for execution, the
dispatcher loads the re-location and limit register with correct values
as a part of context switch.
Since every address generated by the CPU is checked against these
register we can protect the OS and other users programs and data from
being modified.
Memory Allocation
Multiple-partition allocation:
Hole – block of available memory; holes of various size are scattered
throughout memory.
When a process arrives, it is allocated memory from a hole large
enough to accommodate it.
Operating system maintains information about:
1. Allocated partitions
2. Free partitions (hole)
A set of holes of various sizes, is scattered throughout memory at any
given time. When a process arrives and needs memory, the system
searches this set for a hole that is large enough for this process.
If the hole is too large, it is split into two:
o One part is allocated to the arriving process;
o The other is returned to the set of holes.
When a process terminates, it releases its block of memory, which is
then placed back in the set of holes.
If the new hold is adjacent to other holes, these adjacent holes are
merged to form one larger hole.
This procedure is a particular instance of the general dynamic storage
allocation problem, which is how to satisfy a request of size n from a
list of free holes.
There are many solutions to this problem. The set of holes is searched
to determine which hole is best to allocate. The first-fit, best-fit and
worst-fit strategies are the most common ones used to select a free
hole from the set of available holes.
First fit:- Allocates first hole that is big enough. This algorithm scans
memory from the beginning and selects the first available block that is
large enough to hold the process.
Best fit:- It chooses the hole i.e., closest in size to the request. It
allocates the smallest hole i.e., big enough to hold the process.
First fit and best fit are the most popular algorithms for dynamic
memory allocation.
First fit is generally faster.
Best fit searches for the entire list to find the smallest hole i.e., large
enough.
Worst fit reduces the rate of production of smallest holes.
As processes are loaded and removed from memory, the free memory
space is broken into little pieces. After some time that process which we
removed from memory cannot be allocated because of small pieces of
memory and memory block remains unused. This problem is called
Fragmentation.
(OR)
Internal Fragmentation:
There is wasted space internal to a portion due to the fact that block of
data loaded is smaller than the partition.
The scenario is where a free space is too small for any file to fit into.
The overhead to keep a track of that free space is too much for the operating
system. This is called internal fragmentation.
Example:- If there is a block of 50kb and if the process requests 40kb and if
the block is allocated to the process then there will be 10kb of memory left.
External Fragmentation
Exists when there is enough memory space exists to satisfy the request,
but it not contiguous i.e., storage is fragmented in to large number of small
holes. External Fragmentation may be either minor or a major problem.
As shown in the diagram, if we want a file to fit into the memory which
is equal to the sum of the freed space, we cannot as the space is not
contiguous. Therefore even though there is enough memory to hold the file
we cannot fit it due to the memory being scattered at different places. This
is external fragmentation.
Paging
Paging is a memory management scheme that permits the physical
address space of a process to be non-contiguous. Support for paging is
handled by hardware. It is used to avoid external fragmentation.
Paging avoids the considerable problem of fitting the varying sized
memory chunks on to the backing store. When some code or date residing in
main memory need to be swapped out, space must be found on backing
store.
Basic Method:-
Physical memory is broken in to fixed sized blocks called frames (f).
Logical memory is broken in to blocks of same size called pages (p).
When a process is to be executed its pages are loaded in to available
frames from backing store.
The blocking store is also divided in to fixed-sized blocks of same size
as memory frames.
Address Translation:
A data structure called page map table is used to keep track of the relation
between a page of a process to a frame in physical memory.
When the system allocates a frame to any page, it translates this logical
address into a physical address and create entry into the page table to be
used throughout execution of the program.
When a process is to be executed, its corresponding pages are loaded into
any available memory frames. Suppose you have a program of 8Kb but your
memory can accommodate only 5Kb at a given point in time, then the
paging concept will come into picture. When a computer runs out of RAM,
the operating system (OS) will move idle or unwanted pages of memory to
secondary memory to free up RAM for other processes and brings them
back when needed by the program.
This process continues during the whole execution of the program where
the OS keeps removing idle pages from the main memory and write them
onto the secondary memory and bring them back when required by the
program.
Advantages and Disadvantages of Paging
Paging reduces external fragmentation, but still suffer from internal
fragmentation.
Paging is simple to implement and assumed as an efficient memory
management technique.
Due to 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
It is a technique which allows execution of process that may not be compiled
within the primary memory.
It separates the user logical memory from the physical memory.
This separation allows an extremely large memory to be provided for
program when only a small physical memory is available.
Virtual memory makes the task of programming much easier because
the programmer no longer needs to working about the amount of the
physical memory is available or not.
The virtual memory allows files and memory to be shared by different
processes by page sharing.
It is most commonly implemented by demand paging.
The virtual address space of a process refers to the logical (or virtual) view
of how a process is stored in memory. Typically, this view is that a process
begins at a certain logical address—say, address 0—and exists in contiguous
memory
Demand paging
A demand paging system is similar to the paging system with swapping
feature.
When we want to execute a process we swap it into the memory. A
swapper manipulates entire process where as a pager is concerned
with the individual pages of a process.
The demand paging concept is using pager rather than swapper.
When a process is to be swapped in, the pager guesses which pages will
be used before the process is swapped out again.
Instead of swapping in a whole process, the pager brings only those
necessary pages into memory.
Thus it avoids reading into memory pages that will not used any way
decreasing the swap time and the amount of physical memory needed.
In this technique we need some hardware support to distinct between
the pages that are in memory and those that are on the disk.
A valid and invalid bit is used for this purpose.
When this bit is set to valid it indicates that the associate page is in
memory.
If the bit is set to invalid it indicates that the page is either not valid or
is valid but currently not in the disk.
Marking a page invalid will have no effect if the process never attempts
to access that page.
So while a process executes and access pages that are memory
resident, execution proceeds normally.
Access to a page marked invalid causes a page fault trap.
It is the result of the OS’s failure to bring the desired page into memory.
Page Replacement
Demand paging shares the I/O by not loading the pages that are never
used.
Demand paging also improves the degree of multiprogramming by
allowing more process to run at the some time.
Page replacement policy deals with the solution of pages in memory to
be replaced by a new page that must be brought in. When a user
process is executing a page fault occurs.
The hardware traps to the operating system, which checks the internal
table to see that this is a page fault and not an illegal memory access.
The operating system determines where the derived page is residing
on the disk, and this finds that there are no free frames on the list of
free frames.
When all the frames are in main memory, it is necessary to bring a new
page to satisfy the page fault, replacement policy is concerned with
selecting a page currently in memory to be replaced.
The page i,e to be removed should be the page i,e least likely to be
referenced in future.
Victim Page
The page that is supported out of physical memory is called victim page.
If no frames are free, the two page transforms come (out and one in)
are read. This will see the effective access time.
Each page or frame may have a dirty (modify) bit associated with the
hardware. The modify bit for a page is set by the hardware whenever
any word or byte in the page is written into, indicating that the page
has been modified.
When we select the page for replacement, we check its modify bit. If the
bit is set, then the page is modified since it was read from the disk.
If the bit was not set, the page has not been modified since it was read
into memory. Therefore, if the copy of the page has not been modified
we can avoid writing the memory page to the disk, if it is already there.
Some pages cannot be modified.
We must solve two major problems to implement demand paging:
We must develop a frame allocation algorithm and a page replacement
algorithm.
If we have multiple processors in memory, we must decide how many
frames to allocate and page replacement is needed.
Analysis:
Number of Page references = 20
Number of Page faults = 15
Number of Page Hits = 5
Page Hit Ratio = Number of Page Hits / Total Number of Page references
= (5 / 20)*100
= 25%
Page Fault Ratio = Number of Page Faults / Total Number of Page references
= (15 / 20)*100
= 75%
Belady’s Anamoly:
For some page replacement algorithm, the page fault may increase as the
number of allocated frames increases. FIFO replacement algorithm may face
this problem.
Optimal Algorithm:
Optimal page replacement algorithm is mainly to solve the problem of
Belady’s Anamoly.
Ideally we want to select an algorithm with the lowest page-fault rate
Such an algorithm exists, and is called (unsurprisingly) the optimal
algorithm:
Procedure: replace the page that will not be used for the longest time
(or at all) – i.e. replace the page with the greatest forward distance in
the reference string
Analysis:
Number of Page references = 20
Number of Page faults = 09
Number of Page Hits = 11
Page Hit Ratio = Number of Page Hits / Total Number of Page references
= (11 / 20)*100
= 55%
Page Fault Ratio = Number of Page Faults / Total Number of Page references
= (9 / 20)*100
= 45%
Analysis:
Number of Page references = 20
Number of Page faults = 12
Number of Page Hits = 08
Page Hit Ratio = Number of Page Hits / Total Number of Page references
= (8 / 20)*100
= 40%
Page Fault Ratio = Number of Page Faults / Total Number of Page references
= (12 / 20)*100
= 60%
UNIT V
I/O Management Principles of I/O Hardware: Disk structure, Disk
scheduling algorithm
File Management: Access methods, File types, File operation, Directory
structure, File System structure, Allocation methods, Free-space
management, and directory implementation.
Structure of Linux Operating System, Exploring the Directory
Structure, Naming Files and Directories, Concept of shell, Types of shell,
Editors for shell programming (e.g. vi), basics of Shell programming.
POLLING:
Incomplete protocol for interaction between the host and a controller
can be intricate, but the basic handshaking notion is simple.
The controller indicates its state through the busy bit in the status
register. (Recall that to set a bit means to write a 1 into the bit, and to
clear a bit mean to write a 0 into it.)
The controller sets the busy bit when it is busy working and clears the
busy bit when it is ready to accept the next command.
The host signals its wishes via the command-ready bit in the command
register.
The host sets the command-ready bit when a command is available for
the controller to execute.
For this example, the host writes output through a port, coordinating
with the controller by handshaking as follows.
1. The host repeatedly reads the busy bit until that bit becomes clear.
2. The host sets the write bit in the command register and writes a
byte into the data-out register.
3. The host sets the command-ready bit.
4. When the controller notices that the command-ready bit is set, it
sets the Busy.
5. The controller reads the command register and sees the write
command.
6. It reads the data-out register to get the byte, and does the I/O to the
device.
7. The controller clears the command-ready bit, clears the error bit in
the status register to indicate that the device I/O succeeded, and
clears the busy bit to indicate that it is finished.
The host is busy-waiting or polling: It is in a loop, reading the status register
over and over until the busy bit becomes clear. If the controller and device
are fast, this method is a reasonable one. But if the wait may be long, the
host should probably switch to another task
I/O Devices
Categories of I/O Devices
1. Human readable
2. Machine readable
3. Communication
Device Controllers
A computer system contains a multitude of I/O devices and their respective
controllers:
Network card
Graphics adapter
Disk controller
DVD-ROM controller
Serial port
USB
Sound card
Disk structure:
Seek Time:
o Seek time is the time required to move the disk arm to the
required track. It turns out that this is a difficult quantity to pin
down.
o The seek time consists of two key components:
The initial startup time.
The time taken to traverse the tracks that have to be crossed
once the access arm is up to speed.
Ts = m * n + s
Where,
Ts = seek time.
n = number of track traversed.
m = constant that depends on the disk drive
s = startup time.
Rotational Latency:
o Rotational latency is the additional addition time for waiting for
the disk to rotate to the desired sector to the disk head.
Rotational Delay:
o Disks, other than floppy disks, rotate at speeds ranging from 3600
rpm up to, as of this writing, 15,000 rpm; at this latter speed,
there is one revolution per 4 ms. Thus, on the average, the
rotational delay will be 2 ms.
o Floppy disks typically rotate at between 300 and 600 rpm. Thus
the average delay will be between 100 and 50 ms.
Disk Bandwidth:
o Disk bandwidth is the total number of bytes transferred divided
by total time between the first request for service and the
completion of last transfer.
Transfer Time:
o The transfer time to or from the disk depends on the rotation
speed of the disk in the following fashion:
T= b/rN
Where,
T = Transfer time
b = Number of bytes to be transferred
N = Number of bytes on a track
r = Rotation speed, in revolutions per second
Example: consider a disk queue with request for I/O to blocks on cylinders.
98, 183, 37, 122, 14, 124, 65, 67.
If the disk head is initially at 53, it will first move from 53 to 98 then to
183 and then to 37, 122, 14, 124, 65, 67 for a total head movement of
640 cylinders.
The wild swing from 122 to 14 and then back to 124 illustrates the
problem with this schedule.
If the requests for cylinders 37 and 14 could be serviced together
before or after 122 and 124 the total head movement could be
decreased substantially and performance could be improved.
Shortest Seek Time First (SSTF) algorithm
Shortest Seek Time First selects the request with the minimum seek
time from the current head position.
SSTF scheduling is a form of SJF scheduling; may cause starvation of
some requests.
Since seek time increases with the number of cylinders traversed by
head, SSTF chooses the pending request closest to the current head
position.
Illustration shows total head movement of 236 cylinders.
Example: consider a disk queue with request for I/O to blocks on cylinders.
98, 183, 37, 122, 14, 124, 65, 67.
If the disk head is initially at 53, the closest is at cylinder 65, then 67,
then 37 is closer than 98 to 67.
So it services 37, continuing we service 14, 98, 122, 124 and finally
183.
The total head movement is only 236 cylinders.
SSTF is essentially a form of SJF and it may cause starvation of some
requests. SSTF is a substantial improvement over FCFS, it is not
optimal.
SCAN algorithm
Example: consider a disk queue with request for I/O to blocks on cylinders.
98, 183, 37, 122, 14, 124, 65, 67.
Example: consider a disk queue with request for I/O to blocks on cylinders.
98, 183, 37, 122, 14, 124, 65, 67.
Look Scheduling Algorithm:
Both SCAN and C-SCAN move the disk arm across the full width of the
disk.
Start the head moving in one direction. Satisfy the request for the
closest track in that direction when there is no more request in the
direction, the head is traveling, reverse direction and repeat. This
algorithm is similar to innermost and outermost track on each circuit.
In practice neither of the algorithms is implemented in this way.
The arm goes only as far as the final request in each direction. Then it
reverses, without going all the way to the end of the disk.
These versions of SCAN and CSCAN are called Look and C-Look
scheduling because they look for a request before continuing to move
in a given direction.
Example: consider a disk queue with request for I/O to blocks on cylinders.
98, 183, 37, 122, 14, 124, 65, 67.
Selecting a Disk-Scheduling Algorithm
SSTF is common and it increases performance over FCFS.
SCAN and C-SCAN algorithm is better for systems that place a heavy
load on disk.
SCAN and C-SCAN have less starvation problem.
SSTF or Look is a reasonable choice for a default algorithm.
Performance depends on the number and types of requests.
Requests for disk service can be influenced by the file-allocation
method.
The disk-scheduling algorithm should be written as a separate module
of the operating system, allowing it to be replaced with a different
algorithm if necessary Either SSTF or LOOK is a reasonable choice for
the default algorithm.
File Management
A file is a collection of similar records.
The data can’t be written on to the secondary storage unless they are
within a file.
Files represent both the program and the data. Data can be numeric,
alphanumeric, alphabetic or binary.
Many different types of information can be stored on a file ---Source
program, object programs, executable programs, numeric data, payroll
recorder, graphic images, sound recordings and so on.
File Attributes
File attributes varies from one OS to other. The common file attributes are:
1. Name:- The symbolic file name is the only information kept in human
readable form.
2. Identifier:- The unique tag, usually a number, identifies the file within
the file system. It is the non-readable name for a file.
3. Type:- This information is needed for those systems that supports
different types.
4. Location:- This information is a pointer to a device and to the location
of the file on that device.
5. Size:- The current size of the file and possibly the maximum allowed
size are included in this attribute.
6. Protection:- Access control information determines who can do
reading, writing, execute and so on.
7. Time, data and User Identification:- This information must be kept
for creation, last modification and last use. These data are useful for
protection, security and usage monitoring.
File types:
A file has a certain defined structures according to its type:-
1. Text file:- Text file is a sequence of characters organized in to lines.
2. Object file:- Object file is a sequence of bytes organized in to blocks
understandable by the systems linker.
3. Executable file:- Executable file is a series of code section that the
loader can bring in to memory and execute.
4. Source File:- Source file is a sequence of subroutine and function, each
of which are further organized as declaration followed by executable
statements.
File operation:
File is an abstract data type. To define a file we need to consider the
operation that can be performed on the file.
Basic operations of files are:-
1. Creating a file:- Two steps are necessary to create a file. First space in
the file system for file is found. Second an entry for the new file must be
made in the directory. The directory entry records the name of the file
and the location in the file system.
2. Writing a file:- System call is mainly used for writing in to the file.
System call specify the name of the file and the information i.e., to be
written on to the file. Given the name the system search the entire
directory for the file. The system must keep a write pointer to the
location in the file where the next write to be taken place.
3. Reading a file:- To read a file system call is used. It requires the name
of the file and the memory address. Again the directory is searched for
the associated directory and system must maintain a read pointer to
the location in the file where next read is to take place.
4. Delete a file:- System will search for the directory for which file to be
deleted. If entry is found it releases all free space. That free space can
be reused by another file.
5. Truncating the file:- User may want to erase the contents of the file
but keep its attributes. Rather than forcing the user to delete a file and
then recreate it, truncation allows all attributes to remain unchanged
except for file length.
6. Repositioning within a file:- The directory is searched for
appropriate entry and the current file position is set to a given value.
Repositioning within a file does not need to involve actual i/o. The file
operation is also known as file seeks.
In addition to this basis 6 operations the other two operations include
appending new information to the end of the file and renaming the existing
file. These primitives can be combined to perform other two operations.
Most of the file operation involves searching the entire directory for the
entry associated with the file. To avoid this OS keeps a small table containing
information about an open file (the open table). When a file operation is
requested, the file is specified via index in to this table. So searching is not
required.
Semaphores are critical for preventing race conditions in concurrent processing by providing a mechanism for controlling access to shared resources. They ensure mutual exclusion, allowing only one process to access a critical section at a time. By managing synchronization between processes, semaphores prevent multiple processes from entering their critical sections simultaneously, which could lead to inconsistent data states and unexpected behavior . The use of semaphores thus helps maintain the integrity of processes running concurrently and enables safe and efficient sharing of resources .
A virtual-machine system facilitates operating-systems research and development by allowing system development on the virtual machine without disrupting normal system operation . However, it presents challenges, particularly the difficulty in implementing an exact duplicate of the underlying machine, which requires significant effort . Furthermore, virtual machines introduce increased system overhead due to the heavy simulation of operations . The efficiency of a virtual machine-based operating system is severely impacted by the number of operations simulated by the virtual machine monitor .
Logical address space refers to the set of addresses used by a program, representing abstract memory locations, while physical address space is the set of addresses used by the system's hardware in memory operations. Memory management schemes like paging translate logical addresses to physical ones, enabling efficient program execution without contiguous memory allocation . This separation allows for flexible and efficient use of RAM, avoiding fragmentation issues while maintaining program integrity and system performance .
Demand paging improves system performance by allowing processes to be loaded into memory only when needed, thus minimizing the number of pages in memory and ensuring efficient use of available RAM . By not loading pages that are never used, demand paging shares I/O load and enhances multiprogramming, allowing more processes to run simultaneously . This technique effectively manages limited memory resources by separating logical and physical memory using page replacement policies .
Deadlocks in process management can halt system operations, leading to inefficiencies and potential system failures as processes wait indefinitely for resources held by each other. To address deadlocks, strategies include prevention, avoidance, detection, and recovery . Prevention requires designing protocols to ensure hold-and-wait conditions are prevented, whereas avoidance involves resource allocation policies that ensure a circular wait cannot occur . Detection identifies deadlock occurrence, allowing for recovery methods such as forcibly releasing processes or resource preemption to resolve the deadlock .
The structure of an operating system, whether monolithic, layered, or microkernel, significantly impacts its resource management efficiency. In a monolithic system, fast communication between system components ensures efficient resource management but can lead to complex dependencies. Layered structures offer easier maintenance and enhanced security by isolating functions within layers. Microkernels improve modularity and security, isolating services in user space, albeit potentially introducing higher overhead due to increased inter-process communication . The chosen structure affects how resources like CPU, memory, and I/O are allocated and managed .
System calls impact the efficiency and security of an OS by serving as the interface between processes and the operating system, enabling vital operations such as process creation, management, and resource allocation . Efficiently implemented system calls allow for fast execution of these operations, directly influencing overall system performance. Furthermore, security is enhanced through system calls by providing controlled access to system resources, authentication, and protection mechanisms that prevent unauthorized access and malicious use . Thus, system calls ensure both the robustness and secure management of computing resources .
Different CPU scheduling algorithms can significantly affect process management by determining the sequence and allocation of CPU resources to processes, impacting system performance and efficiency. Scheduling algorithms like First-Come, First-Served (FCFS), Shortest Job Next (SJN), and Round Robin (RR) have varying performances based on factors such as average wait time and throughput. These algorithms strive to optimize scheduling objectives such as efficiency, fairness, and responsiveness . The choice of algorithm also impacts how processes transition through states and how resources are allocated, further influencing system responsiveness and user experience .
Multithreading in operating systems enhances performance by allowing concurrency within a single process, improving resource utilization and throughput. Benefits include responsiveness, as threads allow a program to remain responsive to user input while running other tasks in the background, and resource sharing, as threads within the same process share memory and resources efficiently . Challenges include complexity in design and debugging due to synchronization issues and potential for race conditions or deadlocks when multiple threads access shared resources concurrently without proper control mechanisms .
Page replacement algorithms significantly influence memory management strategies as they determine how pages are swapped in and out of memory when needed. Algorithms like FIFO (First-In-First-Out), Optimal, and LRU (Least Recently Used) guide which pages leave the memory to make space for new ones, directly impacting system efficiency and performance. For instance, while FIFO might replace the oldest page, LRU focuses on the least recently used page, affecting page fault rates and memory access times . Choosing the appropriate algorithm is crucial for optimizing memory usage, reducing page faults, and improving system speed .