Chapter-1 Introduction To Operating Systems
Chapter-1 Introduction To Operating Systems
CHAPTER-1
The hardware consists of memory, central processing unit (CPU), and the input/output
(I/O) devices. It provides the basic computing resources for the system.
Operating systems can be explored from two viewpoints: that of the user and that of the
system
• The interrupt must transfer control to the appropriate interrupt service routine.
• The routine would call the interrupt-specific handler. The interrupt routine is called
indirectly through the table, with no intermediate routine needed. Generally, the table
of pointers is stored in low memory (the first 100 or so locations).
• These locations hold the addresses of the interrupt service routines for the various
devices.
• This array, or interrupt vector, of addresses is then indexed by a unique device
number, given with the interrupt request, to provide the address of the interrupt
service routine for the interrupting device.
Some systems go beyond graceful degradation and are called fault tolerant, because they
can suffer a failure of any single component and still continue operation.
Asymmetric multiprocessing:
Symmetric multiprocessing:
1. If one processor fails, then another processors should retrieve the interrupted
process state so that execution of the process can continue.
2. The processors should support efficient context switching operation.
3. Multiprocessor system supports large physical address space and large virtual
address space.
4. The IPC mechanism should be provided and implemented in hardware as it
becomes efficient and easy.
• Symmetric clustering:
➢ Two or more hosts are running applications, and are monitoring each other.
This mode is obviously more efficient, as it uses all of the available hardware. It
does require that more than one application be available to run.
Prepared by: Nirmala G, Asst. Prof, Dept. of CSE, S.S.I.T, Tumkur 5
CS4TH3: Operating System Chapter 1
• An operating system provides the environment within which programs are executed.
• One of the most important aspects of operating systems is the ability to multi-
program.
• When two or more programs are in memory at the same time, sharing the processor is
referred to the multiprogramming operating system.
➢ The operating system keeps several jobs in memory simultaneously (Figure 1.7).
➢ This set of jobs can be a subset of the jobs kept in the job pool which contains all
jobs that enter the system.
➢ Since the number of jobs that can be kept simultaneously in memory is usually
smaller than the number of jobs that can be kept in the job pool.
➢ The operating system picks and begins to execute one of the jobs in memory.
➢ The job may have to wait for some task, such as an I/O operation, to complete.
executes, another job. When that job needs to wait, the CPU is switched to
another job, and so on.
➢ Multiprogrammed systems provide an environment in which the various
system resources are utilized effectively.
Disadvantage:-
1. Multiprogrammed systems do not provide for user interaction with the
computer system.
2. To accommodate many jobs in memory, memory management is required.
CHAPTER 3
PROCESS MANAGEMENT
A program is a passive entity such as the contents of a file stored on disk where as
process is an active entity with a program counter specifying the next instruction to
execute and a set of associated resources.
Whenever a process is created, a process control block (PCB) is created for the process
by operating system. PCB also called as task control block. This is a data structure that is
used by an OS to keep track of all information concerning a process.
A PCB is shown in the following fig:
1. Process state: The state may be new, ready, running, waiting, halted and so on.
2. Process number: This is the unique number allocated by the OS to the process on
creation.
3. Program counter: The counter indicates the address of the next instruction to be
executed for this process.
4. CPU registers:
• Number of registers and type of registers totally depends upon the
computer architecture.
• It includes general-purpose registers, stack pointers, index registers and
accumulators, etc.
5. CPU scheduling information: This information includes a process priority,
pointers to scheduling queues and any other scheduling parameters.
Process scheduling:- Process scheduling refers to a set of policies and mechanisms built
into the OS that governs the order in which processes should be submitted to the CPU for
execution.
When the process enters into the system, they are put into a job queue. This queue
consists of all processes in the system.
• Ready queue: The processes that are residing in main memory and are ready and
waiting to execute are kept on a list called the ready queue. This queue is stored
as a linked list.
A ready queue header contains pointers to the first and final PCBs in the list. Each
PCB includes a pointer field that points to the next PCB in the ready queue. This
is shown in the following fig.(Ready queue).
• Device queue: The list of processes waiting for a particular I/O device is called a
device queue. Each device has its own device queue as shown below in fig. Ex:
The process makes an I/O request to a shared device such as a disk.
Fig 3.4 The ready queue and various I/O device queues.
Queuing diagram:
Process scheduling can be represented by queuing diagram as shown in the following
diagram.
3.2.2 Schedulers
The scheduler is responsible for deciding which program or process should be executed
on CPU.
Schedulers are of 3 types:
1. Long term scheduler
2. Short term scheduler
3. Medium term scheduler
Long-term scheduler:
• It is also called as job scheduler. Job scheduler selects the processes from this job
pool and loads them into memory for execution.
• The long-term scheduler must make a careful selection. Processes can be
described either I/O bound or CPU bound.
• I/O bound process: An I/O bound process is one that spends more of its time
doing I/O than it spends doing computations.
• CPU bound process: It generates I/O requests infrequently using more of its time
doing computations.
• Long-term scheduler should select a good process mix of I/O and CPU bound
processes. If all are I/O bound, the ready queue will always be empty.
• If all are CPU bound, I/O device queue will be empty and system will be
imbalanced.
• It also controls the degree of multiprogramming. If the degree of
multiprogramming is stable, then the average rate of process creation must be
equal to the average departure rate of processes leaving the system.
• On some systems, the long-term scheduler may be absent or minimal.
• Ex: Time sharing operating systems such as UNIX and Microsoft windows
systems have no long-term scheduler.
• When process changes the state from new to ready, then there is a long-term
scheduler.
Short-term scheduler:
• It is also called CPU scheduler. It selects from among the processes that are ready
to execute and allocates the CPU to one of them.
• Short-term scheduler executes atleast once every 100 milliseconds.
Medium-term scheduler:
Some os such as time-sharing systems may introduce an additional, intermediate level of
scheduling i.e medium-term scheduler.
Later, the process can be reintroduced into memory and its execution can be continued
where it left off. This scheme is called swapping.
The process is swapped out and swapped in by the medium-term scheduler.
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
pid_t pid;
/* fork another process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid == 0) { /* child process */
execlp("/bin/ls", "ls", NULL);
}
else { /* parent process */
/* parent will wait for the child to complete */
wait (NULL);
printf ("Child Complete");
}
return 0;
}
A process terminates when it finishes executing its final statement and asks the OS to
delete it by using the exit() system call. At that point process return a status value to its
parent process(via the wait() system call).
For ex:- Terminate process() in Win32 system call is used to terminate the process. The
system call can be invoked only by the parent of the process that is to be terminated.
A parent may terminate the execution of one of its child for a variety of reasons such as
1. The child has exceeded its usage of some of the resources that it has been
allocated.
2. The task assigned to the child is no longer required.
Some systems including VMS, do not allow a child to exist if its parent has terminated. In
such systems, if a process terminates then all its children must also be terminated. This
phenomenon is referred to as cascading termination.
1. The bounded buffer assumes a fixed buffer size. In this case, the consumer must
wait if the buffer is empty, and the producer must wait if the buffer is full.
The following variables reside in a region of memory shared by the producer and
consumer processes:
#define BUFFER_SIZE 10
typedef struct {
...
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
The shared buffer is implemented as a circular array with 2 logical pointers in and out.
The variable in points to the next free position in the buffer, out points to the first full
position in the buffer. The buffer is empty when in= =out; the buffer is full when
((in+1)%buffer_size)= = out.
The code for the producer and consumer processes is shown in fig 3.14 and 3.15
respectively
Fig 3.14 The producer process
item nextProduced
while(true)
{
/* Produce an item in nextProduced*/
while ((((in + 1) % buffer_size)= = out)
; /* do nothing*/
buffer[in] = nextProduced;
in = (in + 1) % buffer_size;
}
The producer process has a local variable nextProduced in which the new item to be
produced is stored.
In this model, communication takes place by means of messages exchanged between the
cooperating processes.
Message passing provides a mechanism to allow processes to communicate and to
synchronize their actions without sharing the same address space and is useful in a
distributed environment, where the communicating processes may reside on different
computer connected by a network.
For ex the chat program used on the www
A message passing facility provides at least two operations send(message) and
receive(message). Messages can be of fixed or variable size.
If processes P and Q want to communicate, they must send and receive messages from
each other, a communication link must exist between them.
The link can be implemented in a variety of ways.
1. Direct or Indirect communication
2. Synchronous or Asynchronous communication
3.4.2.1 Naming
The processes that want to communicate, can be either direct or indirect
communication.
In direct communication each process that wants to communicate must explicitly name
the recipient or sender of the communication.
In this scheme send() and receive() primitives are defined as:-
1. Send(P, message) – Send a message to process P
2. Receive(Q, message) – Receive a message from process Q
In indirect communication the messages are sent to and received from mailboxes or
ports. In this scheme a process can communicate with some other process via a number of
different mailboxes. Each mailbox has a unique identification.
Send() and receive() primitives are defined as follows:
Owner of the mailbox is either by process or by the OS. Each mailbox has a unique
owner so that no confusion about receiving message sent to this mailbox.
3.4.2.2 Synchronization
1. Zero capacity: Maximum length of the queue is zero. Communication link can
not have any messages waiting in it. In this case, the sender must block until the
recipient receives message.
2. Bounded capacity: The queue has finite length n thus at most n messages can
reside in it.
3. Unbounded capacity: The queue has potentially infinite length. Any number of
messages can wait in it. The sender never blocks.
Questions:
1. What is a process? With a state diagram, explain states of a process. Also write
the structure of PCB.
2. Explain the process state, with the suitable diagram.
3. What is PCB? Enumerate and explain various fields in PCB.
4. Differentiate between long-term scheduler, short-term scheduler and medium-
term scheduler.
5. Discuss the operations of process creation and process termination in UNIX.
6. Describe the implementation of IPC using shared memory and message passing.
7. Define IPC. What are the different methods used for logical implementation of a
message-passing system? Explain any one.
BACKGROUND
1
CS4TH3 OPERATING SYSTEMS
2
CS4TH3 OPERATING SYSTEMS
Consider a system consisting of n processes {P0, P1,..., Pn-1}. Each process has a
segment of code, called a critical section, in which the process may be changing common
variables, updating a table, writing a file, and so on. The important feature of the system is
that, when one process is executing in its critical section, no other process is to be allowed
to execute in its critical section.
3
CS4TH3 OPERATING SYSTEMS
That is, no two processes are executing in their critical sections at the same time.
The critical-section problem is to design a protocol that the processes can use to cooperate.
Each process must request permission to enter its critical section. The section of code
implementing this request is the entry section. The critical section may be followed by an
exit section. The remaining code is the remainder section. The general structure of a
typical process Pi, is shown in the following figure:
A solution to the critical-section problem must satisfy the following three requirements:
4
CS4TH3 OPERATING SYSTEMS
yields control of the CPU. Obviously, a nonpreemptive kernel is essentially free from race
conditions on kernel data structures, as only one process is active in the kernel at a time.
We cannot say the same about nonpreemptive kernels, so they must be carefully
designed to ensure that shared kernel data are free from race conditions. Preemptive kernels
are especially difficult to design for SMP architectures, since in these environments it is
possible for two kernel-mode processes to run simultaneously on different processors.
A preemptive kernel is more suitable for real-time programming, as it will allow a
real-time process to preempt a process currently running in the kernel. Furthermore, a
preemptive kernel may be more responsive, since there is less risk that a kernel-mode
process will run for an arbitrarily long period before relinquishing the processor to waiting
processes.
5
CS4TH3 OPERATING SYSTEMS
6
CS4TH3 OPERATING SYSTEMS
SYNCHRONIZATION HARDWARE
7
CS4TH3 OPERATING SYSTEMS
8
CS4TH3 OPERATING SYSTEMS
9
CS4TH3 OPERATING SYSTEMS
10
CS4TH3 OPERATING SYSTEMS
SEMAPHORES
11
CS4TH3 OPERATING SYSTEMS
wait(S) {
while S <= 0
; // no-op S--;
}
signal(S) {
S++;
}
All the modifications to 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 wait(S), the testing of the integer value of S (S < 0), and its
possible modification (S—), must also be executed without interruption.
Operating systems often distinguish between counting and binary semaphores. The
value of a counting semaphore can range over an unrestricted domain. The value of a binary
semaphore can range only between 0 and 1. On some systems, binary semaphores are
known as mutex locks, as they are locks that provide mutual exclusion.
We can use binary semaphores to deal with the critical-section problem for multiple
processes. The n processes share a semaphore, mutex, initialized to 1. Each process Pi is
organized as shown:
do {
waiting(mutex);
// critical section signal
(mutex) ,-
// remainder section
}while (TRUE);
12
CS4TH3 OPERATING SYSTEMS
We can also use semaphores to solve various synchronization problems. For example,
consider two concurrently running processes: P1 with a statement S1 and P2 with a
statement S2. Suppose we require that P2 be executed only after S1 has completed. We can
implement this scheme readily by letting P1 and P2 share a common semaphore synch,
initialized to 0, and by inserting the statements.
S1;
signal(synch);
wait(synch);
S2;
in process P2. Because synch is initialized to 0, P2 will execute S2 only after P1 has
invoked signal (synch), which is after statement S1 has been executed.
Semaphore Implementation:
The main disadvantage of the mutual-exclusion solutions and of the semaphore definition given
here, isthat they all require busy waiting. While a process is in its critical section, any other process
that tries to enter its critical section must loop continuously in the entry code. This continual
looping is clearly a problem in a real multiprogramming system, where a single CPU is shared
among many processes. Busy waiting wastes CPU cycles that some other process might be able to
use productively. This type of semaphore is also called a spinlock (because the process "spins"
while waiting for the lock). Spinlocks are useful in multiprocessor systems. The advantage of a
spinlock is that no context switch is required when a process must wait on a lock, and a context
switch may take considerable time. Thus, when locks are expected to be held for short times,
spinlocks are useful.
To overcome the need for busy waiting, we can modify the definition of the wait and signal
semaphore operations. When a process executes the wait operation and finds that the semaphore
value is not positive, it must wait. However, rather than busy waiting, the process can block itself.
The block operation places a process into a waiting queue associated with the semaphore, and the
state of the process is switched to the waiting state. Then, control is transferred to the CPU
scheduler, which selects another process to execute.
A process that is blocked, waiting on a semaphore S, should be restarted when some other
process executes a signal operation. The process is restarted by a wakeup operation, which changes
the process from the waiting state to the ready state. The process is then placed in the ready queue.
(The CPU may or may not be switched from the running process to the newly ready process,
depending on the CPU-scheduling algorithm.)
To implement semaphores under this definition, we define a semaphore
13
CS4TH3 OPERATING SYSTEMS
as a "C" struct:
typedef struct {
int value ;
struct process *L;
) semaphore;
Each semaphore has an integer value and a list of processes. When a process must wait on a
semaphore, it is added to the list of processes. A signal operation removes one process from the
list of waiting processes and awakens that process.
The wait semaphore operation can now be defined as
void
wait(semaphore
S) {S.value--;
if (S.value < 0) {
add this process
to S . L; block()
;
}
The signal semaphore operation can now be
defined as void signal(semaphore S) {
S.value++;
if (S.value <= 0) {
remove a process P
from S . L ; wakeup
(PI) ;
}
The block operation suspends the process that invokes it. The wakeup(P1) operation resumes the
execution of ablocked process P. These two operations are provided by the operating system as
basic system calls.
Binary Semaphores
The semaphore construct described in the previous sections is commonly known as a counting
semaphore, since its integer value can range over an unrestricted domain. A binary semaphore is
a semaphore with an integer value that can range only between 0 and 1. A binary semaphore can
be simpler to implement than a counting semaphore, depending on the underlying hardware
architecture. We will now show how a counting semaphore can be implemented using binary
semaphores. Let S be a counting semaphore.
To implement it in terms of binary semaphores we need the following data structures:
binary-semaphore S1, S2;
int C;
14
CS4TH3 OPERATING SYSTEMS
Initially S1 = 1, S2 = 0, and the value of integer C is set to the initial value of the counting
semaphore S.The wait operation on the counting semaphore S can be implemented as
follows:
wait (S1) ;
C--;
i f (C < 0) {
signal(S
1) ; wait
(S2) ;
}
signal(S1);
The signal operation on the counting semaphore S can be
implemented as follows: w a i t (S1) ;
C++ ;
i f (C <= 0)
signal (S2)
;e l s e
signal (S1)
;
Classic Problems of Synchronization
We present a number of different synchronization problems as examples for a large class of
concurrency-control problems. These problems are used for testing nearly every newly proposed
synchronization scheme. Semaphores are used for synchronization in our solutions.
signal (full);
}while (TRUE);
do {
wait (full);
wait(mutex);
...
//remove an item from buffer to nextc
...
signal(mutex);
signal(empty);
//consume the item in nextc
...
}while (TRUE);
16
CS4TH3 OPERATING SYSTEMS
variable keeps track of how many processes are currently reading the object. The
semaphore wrt functions as a mutual-exclusion semaphore for the writers.
The code for a writer process is shown:
Consider five philosophers who spend their lives thinking and eating. The
philosophers share a circular table surrounded by five chairs, each belonging to one
philosopher. In the center of the table is a bowl of rice, and the table is laid with five single
chopsticks.
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).
17
CS4TH3 OPERATING SYSTEMS
A philosopher may pick up only one chopstick at a time. Obviously, 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 her chopsticks. When
she is finished eating, she puts down both of her chopsticks and starts thinking again.
where all the elements of chopstick are initialized to 1. The structure of philosopher i is
shown
18
CS4TH3 OPERATING SYSTEMS
MONITORS
wait(mutex);
...
critical section
...
wait(mutex);
in this case, a deadlock will occur.
• suppose that a process omits the wait(mutex), or the signal(mutex), or both. in this case,
either mutual exclusion is violated or a deadlock will occur.
A type, or abstract data type, encapsulates private data with public methods to
operate on that data. A monitor type presents a set of programmer-defined operations that
are provided mutual exclusion within the monitor.
Monitor type also contains the declaration of variables whose values define the state
of an instance of that type, along with the bodies of procedures or functions that operate on
those variables. The syntax of a monitor is shown:
19
CS4TH3 OPERATING SYSTEMS
20
CS4TH3 OPERATING SYSTEMS
This solution imposes the restriction that a philosopher may pick up her chopsticks
only if both of them are available. To code this solution, we need to distinguish among
three states in which we may find a philosopher. For this purpose, we introduce the
following data structure:
21
CS4TH3 OPERATING SYSTEMS
Philosopher i can set the variable state [i] = eating only if her two neighbors are
not eating: (state [(i+4) % 5] != eating) and (state [(i+1) % 5] != eating). We also need
to declare
where philosopher i can delay herself when she is hungry but is unable to obtain
the chopsticks she needs.
The distribution of the chopsticks is controlled by the monitor dp, whose definition
is shown below.(next page)
Each philosopher, before starting to eat, must invoke the operation pickup (). This
may result in the suspension of the philosopher process. After the successful completion of
the operation, the philosopher may eat. Following this, the philosopher invokes the
putdown() operation. Thus, philosopher i must invoke the operations pickup() and
putdown() in the following sequence:
dp.pickup(i);
...
eat
...
dp.putdown(i);
22
CS4TH3 OPERATING SYSTEMS
23
CS4TH3 OPERATING SYSTEMS
wait(mutex) ;
...
body of F
...
if (next_count > 0)
signal(next);
else
signal(mutex);
if (x_count > 0) {
next_count++;
signal(x_sem);
wait(next) ;
next_count—;
}
24
Deadlocks
DEADLOCKS
Deadlock:- If a process has to get executed it needs some resources for completion of the task.
A process requests resources and 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
resource it has requested are held by other waiting processes. The situation is called a deadlock.
Example: Two processes P1 and P2 are running simultaneously P1 requests for resource R1 and
P2 requests for resource R2. Both requests are granted. Now P1 requests for R2 and P2 requests
for R1. Both processes cannot finish the execution because P1 is asking for R2 which is held by
P2 and P2 is asking for R1 which is held by P1.
R1
P1 P2
R2
System Model:
A system consists of a finite number of different types of resources (physical (for eg RAM,
CPU, printer,etc) or logical(for eg file)) for the competing processes. Eg: memory space, CPU
cycle files, I/O devices.
Each resource may have some number of identical instances. If a system has 2 CPUs, then the
resource type CPU has 2 instances.
Identical instances are those instances when allocation of any (resource type) instance will
satisfy the request.
A process must request a resource before using it and must release the resources after using it
Under the normal mode of operation, a process may utilize a resource in only the following
sequence.
1. Request: The process requests the resource. If the request can’t be granted immediately
(for example, if the resource is being used by another process) then the requesting
process must wait until it can acquire the resource.
2. Use: The process uses the resource if resource is available. Ex: if the resource is a printer
the process can print on the printer.
3. Release: The process releases the resource when work is over.
A process may request for any number of resources but it should not exceed the total number of
resources.
The request and release of resources are carried out through system calls. Eg: request/release
device, open\close files, allocate\free memory.
Deadlock Characterization
In a deadlock, processes never finish executing and system resources are tied up, preventing
other jobs from starting.
Necessary Conditions
A deadlock situation can arise if the following four conditions hold simultaneously in a system.
1. Mutual Exclusion: At least one resource must be held in a non-sharable mode i.e only
one process at a time can use the resource. If another process requests that resource, the
requesting process must be delayed until the resource has been released.
2. Hold and Wait: A process is holding a resource and waits for another resource that are
currently being held by other processes.
3. No preemption: Once the resource is allocated to a process it can’t be preempted i.e a
resource can be released only voluntarily by the process holding it.
4. Circular wait: A set {P0, P1, . . . Pn} of waiting processes must exist such that P0 is
waiting for a resource held by P1, P1 is waiting for a resource held by P2 and Pn is
waiting for a resource held by P0.
Note: All the above 4 conditions have to be satisfied if deadlock has to occur. If any one of the
conditions is not satisfied then deadlock will not occur.
Deadlocks can be described more precisely in terms of a directed graph called a system
resource allocation graph.
Resource allocation graph is a graph that consists of set of vertices ‘V’ and set of edges ‘E’.
1. P={P1, P2, … Pn} the set consisting of all the active processes in the system.
2. R={R1, R2…Rm} the set consisting of all resource types in the system.
The process vertex is pictorially represented as a circle and resource vertex is represented as a
rectangular box. The instances of the resource is represented as dots within the rectangle.
For ex: R1
P1 :
1. Request edge: The directed edge from a process to a resource is known as request
edge(shown in below fig a). Requesting of resource by process is denoted by request
egde.
For ex: P1 R1
2. Assignment edge: The directed edge from instance of a resource to process is known as
assignment edge(shown in below fig b). Holding a resource by process is denoted by
assignment edge.
For ex: R1 P1
Fig(a & b)
P1 P1
R1 : R1 :
The following fig shows the resource-allocation graph. System consists of 3 processes i.e P1, P2
and P3 and 4 resources i.e R1, R2, R3, and R4. Resources R1 and R3 have one instance, R2 has
two instances and R4 has 3 instances.
If the graph contains no cycles, then no process in the system is deadlock. If the graph does
contain a cycle, then a deadlock may exist.
Suppose that process P3 requests an instance of resource type R2. Since no resource instance is
currently available, a request edge P3→ R2 is added to the graph. This is shown in the below fig.
Resource allocation graph with deadlock. At this point, two minimal cycles exist in the system.
P1→ R1→P2→R3→P3→R2→P1
P2→R3→P3→R2→P2
Process P2 is waiting for the resource R3, which is held by process P3.
Resource allocation graph with a cycle but no deadlock as shown in the below fig:
Let us consider,
P1→ R1→P3→R2→P1
There is cycle but no deadlock. Because the process P4 may release its instance of resource type
R2. That resource can then be allocated to P3, breaking the cycle.
1. Protocol: Using protocol, we can prevent or avoid deadlocks. To take care that, system
will never enter a deadlock state.
2. Detect and recover: Allow the system to enter a deadlock state, detect it and recover
from deadlock.
3. Ignore the deadlock: To ensure the deadlock never occurs in the system.
The third solution is the one used by most operating systems including UNIX and Windows.
To ensure that deadlocks never occur, the system can use either a deadlock prevention or a
deadlock avoidance scheme.
Deadlock prevention is a set of methods for ensuring that at least one of the necessary
conditions cannot hold. These methods prevent deadlocks by constraining how requests for
resources can be made.
If a system does not employ either a deadlock prevention or a deadlock avoidance algorithm,
then a deadlock situation may occur.
If a system does not ensure that a deadlock will never occur and also does not provide a
mechanism for deadlock detection and recovery, then the system is in a deadlock state.
Deadlock Prevention
Deadlock prevention provides a set of methods for ensuring that at least one of the necessary
conditions cannot hold, that leads to deadlock.
Mutual Exclusion
The mutual exclusion means a non-sharable resource cannot be shared by multiple processes
simultaneously. A non-sharable resource like printer cannot be simultaneously shared by several
processes. Therefore, mutual exclusion cannot be prevented. Read-only-files are a good example
of a sharable resource. Several process can read the file at the same time.
In first algorithm, the execution of a process can begins only if it gets all the resource.
In second algorithm, the process can request a resource only if it is not holding anything.
For ex: There is a process that copies data from a DVD drive to a file on disk, sorts the file and
then prints the results to a printer. Therefore, it needs three resources for completion of task i.e
DVD drive, file and printer.
The first algorithm is used when OS has to allocate all 3 resources before execution begins. But
the disadvantage is resources will not be utilized to the maximum extent i.e the process would
need the printer only during the end. As printer is already held by P1 it will be unavailable for
other processes.
In second algorithm the process will initially request for DVD drive and file. Once copying and
sorting is complete it will release both these resource and then request for the printer and disk
file. After copying the disk file to the printer, it releases these two resources and terminates.
No preemption
Resources allocated to a process should be taken away forcibly from it whenever required. Two
implementations are possible.
1. If a process is holding some resources and requests another resource that can not be
immediately allocated to it, then all the resources currently being held are preempted.
2. When a process requests some resources
a. Check whether resources are available or not
b. If resources are available, allocate them
c. If not, check whether resources are allocated to other processes which are waiting for
additional resources.
d. If so, preempt the desired resources from waiting process and allocate them to the
requesting process.
This method is applicable only when state of resources can be saved and restored when
necessary. Ex CPU registers. But with printers and tape drives it is not possible.
Circular wait
One way to prevent circular wait condition is by linear ordering of different types of system
resources and to require that each process requests resources in an increasing order of
enumeration.
F(disk drive) = 5
F(printer) = 12
The request and grant of a resource works like this initially if a process is not having any
resource and request for a resource then OS will grant it.
Eg: P1 request for R1, R1 will be granted. Now if P2 requests R2, the resource will be allocated
only if F(R2) > F(R1) or F(R1) < F(R2). This can be proved by contradiction.
Analyzing the above equation, the condition F(R1) < F(R2) cannot be satisfied. Therefore there
can be no circular wait.
Deadlock Avoidance
In deadlock avoidance algorithm, the OS will be provided with extra information a priori to the
execution of the process.
For ex: How many processes will be there in the system? What are the different resources that
these processes might use? The order of request of these resources etc.
If this additional information is provided to the OS in advance, then deadlock can be avoided.
For eg P1, P2, …. Pn has a safe sequence if for each process (Pi) required resources can be
granted in some order.
A safe state is not a deadlock state. Conversely, a deadlock state is an unsafe state. Not all
unsafe states are deadlocks as shown in the below fig.
An unsafe state may lead to a deadlock. As long as the state is safe, an operating system can
avoid unsafe state. In an unsafe state, the operating system cannot prevent processes from
requesting resource such that a deadlock occurs.
For ex: A system consists of 3 processes P1, P2 and P3 and one resource R1. Number of units
(instances) for R1 is 12.
• The safe sequence for this is {P2, P1, P3}. This sequence satisfy the safety condition.
Safe sequence is calculated as follows.
• Process P2 can be allocated all its resources(R1) and then return to the system. [At
t1→P2→2, 3-2=1 instances remaining] Now available resource (R1) becomes 5 after
returning process P2.
• Now process P1 can get all its R1 and return them to the system. [ At t2→P1→5, 5-5=0]
Need of process P1 is 5 and in system R1 is available with 5. Now available resource
(R1) becomes 10 after returning process P1.
• Now process P3 could get all its R1 resource and return then to the system.
• Finally, the system will have 12 (R1) resources available.
Cycle detection algorithm is used for detecting cycle in the graph. If no cycle exists, then the
allocation of resources will leave the system in a safe state. If a cycle is found, then the allocation
puts the system is an unsafe state.
In the above fig 2, P1 is requesting for R1 therefore the claim edge is converted to request edge.
In the above fig 3, the request edge will be converted to assignment edge only if there are no
cycles in the graph. Therefore, allocating R1→P1 does not form any cycle. Request edge is
converted to assignment edge.
In the above fig 4, P2 will request for R1 then claim edge is converted to request edge. But this
request edge cannot be converted into assignment edge because already R1 is held by P1.
In the above fig 5, P2 will request for R2 then claim edge is converted to request edge.
In the above fig 6, OS will try to convert this request edge to assignment edge. By allocating R2
to P2 will lead to the formation of cycle in the resource allocation graph, creating a deadlock
between P1 and P2. Therefore R2 will not be assigned to P2 preventing from deadlock.
The name was chosen because the algorithm could be used in a banking system to ensure that the
bank never allocated its available cash in such a way that it could no longer satisfy the needs of
all its customers.
Several data structures must be maintained to implement the banker’s algorithm. Let n be the
number of processes in the system and m be the number of resource types. We need the
following data structures.
1. Available: This is a vector of length m that gives the information about the number of
resources currently available in the system of each type.
2. Max: This is a matrix of n*m that gives the information about the maximum demand of
each process w.r.t the resources.
3. Allocation: An n*m matrix that gives the information about the number of instances(or
resources) currently allocated to the process.
4. Need: This is a matrix of n*m that gives the information about the remaining number of
resources that the process will need to finish the task.
Need= Max - Allocation
1. Safety algorithm
2. Resource request algorithm
Safety Algorithm
It is used to find the state of the system, i.e system may be in safe state or unsafe state.
Resource-Request Algorithm
Let Requesti be the request vector for process Pi. If Requesti [j] = = k, then
process Pi wants k instances of resource type Rj.
When a request for resources is made by process Pi, the following actions are taken:
1. If Requesti ≤ Needi go to step 2. Otherwise, raise error condition, since process has
exceeded its maximum claim.
2. If Requesti ≤ Available, go to step 3. Otherwise, Pi must wait, since resources are not
available.
3. Pretend to allocate requested resources to Pi by modifying the state as follows:
Available = Available – Request;
Allocationi= Allocationi + Requesti;
Needi = Needi – Requesti;
Deadlock Detection
If the system is not using any deadlock avoidance and deadlock prevention algorithm, then a
deadlock situation may occur. In this the system must provide:
• An algorithm that examines the state of the system to determine whether a deadlock has
occurred.
• An algorithm to recover from the deadlock.
The below fig shows the resource allocation graph with corresponding wait for graph.
Fig: Resource Allocation graph (a) Corresponding wait for graph (b)
To detect deadlock, the system needs to maintain the wait for graph and periodically invoke an
algorithm that searches for a cycle in the graph.
For detecting a cycle in the graph, algorithm requires n2 operation where n is the number of
vertices in the graph.
1. Available: This is a vector of length m that gives the information about the number of
resources currently available in the system of each type.
2. Allocation: An n*m matrix that gives the information about the number of instances (or
resources) currently allocated to the process.
3. Request: An n*m matrix indicates the current request of each process.
Algorithm requires an order of (m x n2) operations to detect whether the system is in deadlocked
state.
Detection-Algorithm Usage
As we know:
Thus, if deadlock occurs frequently and many processes are getting affected, detection-algorithm
should be run frequently.
2. Algorithm can be invoked at less frequent intervals for ex once per hour, when CPU
utilization drops down 40%.
Advantages: Computation overhead will be less.
Disadvantages: Low resource utilization and less throughput will be caused. Partial
computation finished by processes will be lost.
Once a deadlock situation has been identified by the detection algorithm any of the following
actions can be taken:
1. Recover manually
2. System recovers automatically. There are 2 ways:
Nirmala G, Dept of CSE, SSIT 15
Deadlocks
a. Process termination
b. Resource preemption
Process Termination
The system reclaims all resources allocated to a process by aborting the process. Hence deadlock
is eliminated.
1. Abort all deadlocked processes: This method clearly will break the deadlock cycle, but
at great expense. Since processes may have computed for long time and all the partial
results are discarded.
2. Abort one process at a time: This process is repeated until the deadlock cycle is
eliminated. This is also very expensive, since after each process termination, a deadlock
detection algorithm must be invoked.
For a given set of deadlocked processes, we should find out which process should be terminated
to break the deadlock. There are various factors which governs the choice of process to be
aborted.
a. Priority of process.
b. How much computation is completed by process and how much remaining.
c. How many or what type resources are already used by process.
d. How many more resources are needed?
e. Nature of process – interactive or batch.
f. How many resources need to be terminated?
Resource Preemption
By preempting some resources from processes and reallocating these resources to other
processes, the deadlock can be eliminated.
Question Bank:
Memory Management
Basic Hardware
Program must be
→ brought (from disk) into memory and placed within a process for it to be run.
• Main-memory and registers are only storage CPU can access directly.
• Registers are accessible within in one CPU clock.
• Completing a memory acess can take many cycles of the CPU clock.
• Cache sits between main-memory and CPU registers to ensure fast memory access.
• Protection of memory is required to ensure correct operation.
• The base register holds the smallest legal physical address and the limit register specifies the range of
addresses. A pair of base and limit-registers define the logical (virtual) address space shown in the fig below:
Protection of memory space is accomplished by having the CPU hardware compare every address generated
in user mode with the registers. The base and limit registers can be loaded only by the operating system which
Nirmala G, Dept. of CSE, SSIT 1
CS4TH3 Operating Systems
uses a special privileged instruction that can be executed only in kernel mode. Only the operating system can
load the base and limit registers.
Address Binding
Address binding of instructions to memory-addresses can happen at 3 different stages (Figure 3.3). It is
mapping from one address space to another.
1) Compile Time
• If memory-location known a priori, absolute code can be generated.
• Must recompile code if starting location changes.
2) Load Time
• Must generate relocatable code if memory-location is not known at compile time. In this case, final
binding is delayed until load time.
3) Execution Time
• Binding delayed until run-time if the process can be moved during its execution from one memory-
segment to another.
Logical Address
Physical Address
The user program generates the logical address and thinks that the program is running in this logical address
but the program needs physical memory for its execution, therefore, the logical address must be mapped to the
physical address by MMU before they are used.
Comparison Chart:
User can view the logical User can never view physical
Dynamic Loading
All the programs are loaded in the main memory for execution. Sometimes complete program is loaded into
the memory, but sometimes a certain part or routine of the program is loaded into the main memory only when
it is called by the program, this mechanism is called Dynamic Loading.
Advantage is that a routine is loaded only when it is needed. This method is useful when large amounts of code
are needed to handle infrequently occurring cases such as error routines.
It does not require special support from OS. It is the responsibility of the users to design their programs to take
advantage of such method.
Dynamic linking
Establishing the linking between all the modules or all the functions of the program in order to continue the
program execution is called linking.
Linking intakes the object codes generated by the assembler and combines them to generate the executable
module.
1. The key difference between linking and loading is that the linking generates the executable file of a
program whereas, the loading loads the executable file obtained from the linking into main memory for
execution.
2. The linking intakes the object module of a program generated by the assembler. However, the loading
intakes the executable module generated by the linking.
3. The linking combines all object modules of a program to generate executable modules it also links the
library function in the object module to built-in libraries of the high-level programming language. On the
other hand, loading allocates space to an executable module in main memory.
Shared Libraries
Swapping
Swapping is one of the several methods of memory management. In swapping an idle or a blocked process
in the main memory is swapped out to the backing store (disk) and the process that is ready for execution in
the disk, is swapped in main memory for execution.
In simple Swapping is exchanging data between the hard disk and the RAM.
Working:
• A process must be in the main memory before it starts execution. So, a process that is ready for
execution is brought in the main memory. Now, if a running the process gets blocked.
• The memory manager temporarily swaps out that blocked process on to the disk. This makes the space
for another process in the main memory.
• So, the memory manager swaps in the process ready for execution, in the main memory, from the disk.
Nirmala G, Dept. of CSE, SSIT 5
CS4TH3 Operating Systems
• The swapped out process is also brought back into the main memory when it again gets ready for
execution.
• Ideally, the memory manager swaps the processes so fast, that the main memory always has processes
ready for execution.
Whenever a process with higher priority arrives the memory manager swaps out the process with the lowest
priority to the disk and swaps in the process with the highest priority in the main memory for execution. When
the highest priority process is finished, the lower priority process is swapped back in memory and continues to
execute. This Variant of swapping is termed as roll-out, roll-in or swap-out swap-in.
Note: Major part of swap-time is transfer-time; i.e. total transfer-time is directly proportional to the amount
of memory swapped.
Advantages of Swapping
Disadvantages of Swapping.
• In the case of heavy swapping activity, if the computer system loses power, the user might lose all the
information related to the program.
• If the swapping algorithm is not good, the overall method can increase the number of page faults and
decline the overall processing performance.
• Inefficiency may arise in the case where a resources or a variable is commonly used by the processes
which are participating in the swapping process
In contiguous memory allocation, all the available memory space remains together in one place. It means
freely available memory partitions are not scattered here and there across the whole memory space.
In the non-contiguous memory allocation the available free memory space are scattered here and there and
all the free memory space is not at one place. So this is time-consuming.
Memory Protection
Memory-protection means
→ protecting OS from user-process and
→ protecting user-processes from one another.
• Memory-protection is done using
Relocation-register: contains the value of the smallest physical-address.
Limit-register: contains the range of logical-addresses.
Memory Allocation
1) Fixed-sized Partitioning
• The memory is divided into fixed-sized partitions.
• Each partition may contain exactly one process.
• The degree of multiprogramming is bound by the number of partitions.
• When a partition is free, a process is
→ selected from the input queue and
→ loaded into the free partition.
• When the process terminates, the partition becomes available for another process.
Nirmala G, Dept. of CSE, SSIT 7
CS4TH3 Operating Systems
2) Variable-sized Partitioning
Disadvantage: Management is very difficult as memory is becoming purely fragmented after some time.
Three strategies used to select a free hole from the set of available holes.
1) First Fit
• Allocate the first hole that is big enough.
• Searching can start either
→ at the beginning of the set of holes or
→ at the location where the previous first-fit search ended.
Advantage: Fastest algorithm because it searches as little as possible.
Disadvantage: The remaining unused memory areas left after allocation become waste if it is too smaller. Thus
request for larger memory requirement cannot be accomplished.
2) Best Fit
Disadvantage: It is slower and may even tend to fill up memory with tiny useless holes.
3) Worst Fit
Disadvantage: If a process requiring larger memory arrives at a later stage then it cannot be accommodated as
the largest hole is already split and occupied.
First-fit and best fit are better than worst fit in terms of decreasing time and storage utilization.
Fragmentation
Fragmentation is a phenomenon in which storage space is used inefficiently, reducing capacity or performance
and often both. Whenever a process is loaded or removed from the physical memory block, it creates a small
hole in memory space which is called fragment
Both the internal and external classification affects data accessing speed of the system. Two types of memory
fragmentation:
1) Internal fragmentation: When a process is assigned to a memory block and if that process is smaller than the
memory requested, it creates a free space in the assigned memory block. Then the difference between assigned
and requested memory is called the internal fragmentation.
2) External fragmentation: When the process is loaded or removed from the memory, a free space is created.
This free space creates an empty space in the memory which is called external fragmentation.
In internal fragmentation
Internal fragmentation
2. memory. removed.
The above diagram clearly shows the internal fragmentation because the difference between memory
allocated and required space or memory is called Internal fragmentation.
In the above diagram, we can see that, there is enough space (55 KB) to run a process-07 (required 50 KB) but
the memory (fragment) is not contiguous. Here, we use compaction, paging or segmentation to use the free
space to run a process.
Paging
Paging is a memory management scheme. Paging allows a process to be stored in a memory in a non-
contiguous manner. Storing process in a non-contiguous manner solves the problem
of external fragmentation.
Characteristics:
• Each process is divided into parts where size of each part is same as page size.
• The size of the last part may be less than the page size.
• The pages of process are stored in the frames of main memory depending upon their availability.
Example-
Step-01:
CPU generates a logical address consisting of two parts-
1. Page Number
2. Page Offset
• Page Number specifies the specific page of the process from which CPU wants to read the data.
• Page Offset specifies the specific word on the page that CPU wants to read.
Step-02:
For the page number generated by the CPU,
• Page Table provides the corresponding frame number (base address of the frame) where that page is
stored in the main memory.
Step-03:
• The frame number combined with the page offset forms the required physical address.
• Frame number specifies the specific frame where the required page is stored.
• Page Offset specifies the specific word that has to be read from that page.
Diagram-
The following diagram illustrates the above steps of translating logical address into physical address-
Protection
• Memory-protection is achieved by protection-bits for each frame.
• The protection-bits are kept in the page-table.
• One protection-bit can define a page to be read-write or read-only.
• Every reference to memory goes through the page-table to find the correct frame-number.
• Firstly, the physical-address is computed. At the same time, the protection-bit is checked to verify that no
writes are being made to a read-only page.
• An attempt to write to a read-only page causes a hardware-trap to the OS (or memory-protection violation).
Nirmala G, Dept. of CSE, SSIT 14
CS4TH3 Operating Systems
Note: In other words, we can say that TLB is faster and smaller than the main memory but cheaper and bigger
than the register.
Working:
When a logical-address is generated by the CPU, its page-number is presented to the TLB.
If the page-number is found (TLB hit), its frame-number is
→ immediately available and
→ used to access memory.
If page-number is not in TLB (TLB miss), a memory-reference to page table must be made.
The obtained frame-number can be used to access memory (Figure 3.19).
In addition, we add the page-number and frame-number to the TLB, so that they will be
found quickly on the next reference.
• If the TLB is already full of entries, the OS must select one for replacement.
• Percentage of times that a particular page-number is found in the TLB is called hit ratio
Advantage:
• Search operation is fast.
• TLB reduces the effective access time.
• Only one memory access is required when TLB hit occurs.
Disadvantage:
• TLB can hold the data of only one process at a time.
• When context switches occur frequently, the performance of TLB degrades due to low hit ratio.
• As it is a special hardware, it involves additional cost.
Three types
1) Hierarchical Paging
2) Hashed Page-tables
3) Inverted Page-tables
Hierarchical Paging
• Problem: Most computers support a large logical-address space (232 to 264). In these systems, the page-
table itself becomes excessively large.
Solution: Divide the page-table into smaller pieces.
Segmentation
Types of Segmentation
Basic Method
Hardware Support
Advantages of Segmentation
• It allows to divide the program into modules which provides better visualization.
• Segment table consumes less space as compared to Page Table in paging.
• It solves the problem of internal fragmentation.
Disadvantage of Segmentation
• There is an overhead of maintaining a segment table for each process.
• The time taken to fetch the instruction increases since now two memory accesses are required.
• Segments of unequal size are not suited for swapping.
• It suffers from external fragmentation as the free space gets broken down into smaller pieces with the
processes being loaded and removed from the main memory.
BASIS FOR
PAGING SEGMENTATION
COMPARISON
fragmentation. fragmentation.
Address The user specified address is The user specifies each address by two
Size The hardware decides the page The segment size is specified by the
size. user.
Table Paging involves a page table that Segmentation involves the segment table
contains base address of each that contains segment number and offset
Fig: Diagram showing virtual memory that is larger than physical memory.
Page transfer Method: 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 be used anyway, decreasing the swap time and the amount of physical memory needed.
entry for a page that is not currently in memory is simply marked invalid, or contains the address
of the page on disk.
Nirmala G, Dept. of CSE, SSIT 2
CS4TH3 OPERATING SYSTEMS
When a page references an invalid page, then it is called Page Fault. It means that page is not in
main memory. The procedure for handling page fault is as follows:
1. We check an internal table for this process, to determine whether the reference was a valid
or invalid memory access.
2. If the reference was invalid, we terminate the process. If it was valid, but we have not yet
brought in that page in to memory.
3. We find a free frame (by taking one from the free-frame list).
4. We schedule a disk operation to read the desired page into the newly allocated frame.
5. When the disk read is complete, we modify the internal table kept with the process and the
page table to indicate that the page is now in memory.
6. We restart the instruction that was interrupted by the illegal address trap. The process can
now access the page as though it had always been in memory.
PAGE REPLACEMENT:
The page replacement is a mechanism that loads a page from disc to memory when a page of memory
needs to be allocated. Page replacement can be described as follows:
1. Find the location of the desired page on the disk.
2. Find a free frame:
a. If there is a free frame, use it.
b. If there is no free frame, use a page-replacement algorithm to select a victim frame.
c. Write the victim page to the disk; change the page and frame tables accordingly.
3. Read the desired page into the (newly) free frame; change the page and frame tables.
4. Restart the user process.
Page Replacement Algorithms: The page replacement algorithms decide which memory pages to
page out (swap out, write to disk) when a page of memory needs to be allocated. We evaluate an
algorithm by running it on a particular string of memory references and computing the number of
page faults. The string of memory references is called a reference string.
The different page replacement algorithms are described as follows:
1. First-In-First-Out (FIFO) Algorithm:
A FIFO replacement algorithm associates with each page the time when that page was brought
into memory. When a page must be replaced, the oldest page is chosen to swap out. We can
create a FIFO queue to hold all pages in memory. We replace the page at the head of the queue.
When a page is brought into memory, we insert it at the tail of the queue.
Example:
Note: For some page-replacement algorithms, the page fault rate may increase as the number of
allocated frames increases. This most unexpected result is known as Belady's anomaly.
The major problem is how to implement LRU replacement. An LRU page-replacement algorithm
may require substantial hardware assistance. The problem is to determine an order for the frames
defined by the time of last use. Two implementations are feasible:
Counters: We associate with each page-table entry a time-of-use field, and add to the CPU a
logical clock or counter. The clock is incremented for every memory reference. Whenever a
reference to a page is made, the contents of the clock register are copied to the time-of-use field
in the page-table entry for that page. We replace the page with the smallest time value. This
scheme requires a search of the page table to find the LRU page, and a write to memory (to the
time-of-use field in the page table) for each memory access. The times must also be maintained
when page tables are changed (due to CPU scheduling).
Stack: Another approach to implementing LRU replacement is to keep a stack of page numbers.
Whenever a page is referenced, it is removed from the stack and put on the top. In this way, the top of
the stack is always the most recently used page and the bottom is the LRU page. Because entries must
be removed from the middle of the stack, it is best implemented by a doubly linked list, with a head
and tail pointer. Each update is a little more expensive, but there is no search for a replacement; the
tail pointer points to the bottom of the stack which is the LRU page. This approach is particularly
appropriate for software or microcode implementations of LRU replacement. Example:
ALLOCATION OF FRAMES:
When a page fault occurs, there is a free frame available to store new page into a frame. While
the page swap is taking place, a replacement can be selected, which is written to the disk as the
user process continues to execute. The operating system allocate all its buffer and table space from
the free-frame list for new page.
Two major allocation Algorithm/schemes.
1. equal allocation
2. proportional allocation
1. Equal allocation: The easiest way to split m frames among n processes is to give everyone an
equal share, m/n frames. This scheme is called equal allocation.
2. proportional allocation: Here, it allocates available memory to each process according to its
size. Let the size of the virtual memory for process pi be si, and define S= ∑ Si
Then, if the total number of available frames is m, we allocate ai frames to process pi, where
ai is approximately ai = Si/ S x m.
STORAGE MANAGEMENT
FILE SYSTEM
The file system provides the mechanism for on-line storage of and access to both
data and programs of the operating system and all the users of the computer system. The
file system consists of two distinct parts: a collection of files, each storing related data, and
a directory structure, which organizes and provides information about all the files in the
system.
File Concept:
A file is a collection of related information that is recorded on secondary storage.
From a user's perspective, a file is the smallest allotment of logical secondary storage and
data cannot be written to secondary storage unless they are within a file.
Four terms are in common use when discussing files: Field, Record, File and Database
• A field is the basic element of data. An individual field contains a single value, such as an
employee’s last name, a date, or the value of a sensor reading. It is characterized by its
length and data type.
• A record is a collection of related fields that can be treated as a unit by some
application program. For example, an employee record would contain such fields as
name, social security number, job classification, date of hire, and so on.
• A file is a collection of similar records. The file is treated as a single entity by users
and applications and may be referenced by name.
• A database is a collection of related data. A database may contain all of the
information related to an organization or project, such as a business or a scientific
study. The database itself consists of one or more types of files.
File Attributes:
A file has the following attributes:
• Name: The symbolic file name is the only information kept in human readable form.
• Identifier: This unique tag, usually a number, identifies the file within the file system;
it is the non-human-readable name for the file.
• Type: This information is needed for those systems that support different types.
• Location: This information is a pointer to a device and to the location of the file on
that device.
• Size: The current size of the file (in bytes, words, or blocks), and possibly the
maximum allowed size are included in this attribute.
• Protection: Access-control information determines who can do reading, writing,
executing, and so on.
• Time, date, and user identification: This information may be kept for creation,
modification and last use. These data can be useful for protection, security, and usage
monitoring.
File Operations:
The operating system can provide system calls to create, write, read, reposition, delete,
and truncate files. The file operations are described as followed:
• Creating a file: Two steps are necessary to create a file. First, space in the file system
must be found for the file. Second, an entry for 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, and possibly other information.
• Writing a file: To write a file, we make a system call specifying both the name of the
file and the information to be written to the file. Given the name of the file, the system
searches the directory to find the location of the file. The system must keep a write
pointer to the location in the file where the next write is to take place. The write pointer
must be updated whenever a write occurs.
• Reading a file: To read from a file, we use a system call that specifies the name of
the file and where (in main memory) the next block of the file should be put. Again,
the directory is searched for the associated directory entry, and the system needs to keep
a read pointer to the location in the file where the next read is to take place. Once the
read has taken place, the read pointer is updated.
• Repositioning within a file: The directory is searched for the appropriate entry, and the
current-file-position is set to a given value. Repositioning within a file does not need to
involve any actual I/O. This file operation is also known as a file seeks.
• Deleting a file: To delete a file, we search the directory for the named file. Having
found the associated directory entry, we release all file space, so that it can be reused
by other files, and erase the directory entry.
• Truncating a file: The user may want to erase the contents of a file but keep its
attributes. Rather than forcing the user to delete the file and then recreate it, this
function allows all attributes to remain unchanged-except for file length-but lets the
file be reset to length zero and its file space released.
File Types: The files are classified into different categories as follows:
The name is split into two parts-a name and an extension, The system uses the extension
to indicate the type of the file and the type of operations that can be done on that file.
ACCESS METHODS
When a file is used, this information must be accessed and read into computer
memory. The information in the file can be accessed in several ways. There are two major
access methods as follows:
•
Sequential Access: Information in the file is processed in order, one record after the
other. A read operation reads the next portion of the file and automatically advances a
file pointer, which tracks the I/O location. Similarly, a write appends to the end of the
file and advances to the end of the newly written material (the new end of file).
Sequential access is based on a tape model of a file, and works as well on sequential-
access devices as it does on random-access ones.
• Direct Access: A file is made up of fixed length logical records that allow programs
to read and write records rapidly in no particular order. The direct-access method is
based on a disk model of a file, since disks allow random access to any file block.
For direct access, the file is viewed as a numbered sequence of blocks or records. A
direct-access file allows arbitrary blocks to be read or written. There are no restrictions
on the order of reading or writing for a direct-access file. For the direct-access
method, the file operations must be modified to include the block number as a
parameter. Thus, we have read n, where n is the block number, rather than read next,
and write n rather than write next.
DIRECTORY STRUCTURE
A directory is an object that contains the names of file system objects. File system
allows the users to organize files and other file system objects through the use of directories.
The structure created by placement of names in directories can take a number of forms: Single-
level tree, Two-level tree, multi-level tree or cyclic graph.
Single-Level Directory: The simplest directory structure is the single-level directory. All
files are contained in the same directory, which is easy to support and understand. A
single-level directory has significant limitations, when the number of files increases or when
the system has more than one user. Since all files are in the same directory, they must have
unique names.
Two-Level Directory: In the two-level directory structure, each user has its own user file
directory (UFD). Each UFD has a similar structure, but lists only the files of a single user.
When a user job starts or a user logs in, the system's master file directory (MFD) is
searched. The MFD is indexed by user name or account number, and each entry points
to the UFD for that user.
When a user refers to a particular file, only his own UFD is searched. Different
users may have files with the same name, as long as all the file names within each UFD
are unique.
To create a file for a user, the operating system searches only that user's UFD to
ascertain whether another file of that name exists. To delete a file, the operating system
confines its search to the local UFD; thus, it cannot accidentally delete another user's file
that has the same name.
Acyclic-Graph Directories:
An acyclic graph allows directories to have shared subdirectories and files. The
same file or subdirectory may be in two different directories. An acyclic graph is a natural
generalization of the tree structured directory scheme.
A shared file (or directory) is not the same as two copies of the file. With two copies,
each programmer can view the copy rather than the original, but if one programmer changes
the file, the changes will not appear in the other's copy.
Shared files and subdirectories can be implemented in several ways. A common
way is to create a new directory entry called a link. A link is a pointer to another file or
subdirectory.
When the disk is in use, a drive motor spins it at high speed. Most drives rotate 60
to 200 times per second. Disk speed has two parts. The transfer rate is the rate at which
data flow between the drive and the computer. The positioning time (or random-access
time) consists of seek time and rotational latency. The seek time is the time to move the
disk armto the desired cylinder. And the rotational latency is the time for the desired sector
to rotate to the disk head. Typical disks can transfer several megabytes of data per second
and they have seek times and rotational latencies of several milliseconds.
T = b / ( r x N)
SSTF Scheduling
It stands for shortest-seek-time-first (SSTF) algorithm. The SSTF algorithm selects the
request with the minimum seek time from the current head position. Since seek time increases
with the number of cylinders traversed by the head, SSTF chooses the pending request
closest to the current head position. We illustrate this with a request queue (0-199):
98, 183, 37, 122, 14, 124, 65, 67
Consider now the Head pointer is in cylinder 53.
SCAN Scheduling
In the SCAN algorithm, the disk arm starts at one end of the disk, and moves toward the
other end, servicing requests as it reaches each cylinder, until it gets to the other end of the
disk. At the other end, the direction of head movement is reversed, and servicing continues.
The head continuously scans back and forth across the disk. We illustrate this with a
request queue (0-199): 98, 183, 37, 122, 14, 124, 65, 67
Consider now the Head pointer is in cylinder 53.
C-SCAN Scheduling
Circular SCAN (C-SCAN) scheduling is a variant of SCAN designed to provide
a more uniform wait time. Like SCAN, C-SCAN moves the head from one end of the disk
to the other, servicing requests along the way. When the head reaches the other end, it
immediately returns to the beginning of the disk, without servicing any requests on the
return trip. The C- SCAN scheduling algorithm essentially treats the cylinders as a circular
list that wraps around from the final cylinder to the first one. We illustrate this with a request
queue (0-199): 98, 183, 37, 122, 14, 124, 65, 67. Consider now the Head pointer is in
cylinder 53.
LOOK Scheduling
Practically, both SCAN and C-SCAN algorithm is not implemented this way. More
commonly, the arm goes only as far as the final request in each direction. Then, it reverses
direction immediately, without going all the way to the end of the disk. These versions of
SCAN and C-SCAN are called LOOK and C-LOOK scheduling, because they look for a
request before continuing to move in a given direction. We illustrate this with a request
queue (0-199): 98, 183, 37, 122, 14, 124, 65, 67
Consider now the Head pointer is in cylinder 53.
DISK MANAGEMENT
The operating system is responsible for several aspects of disk management.
Disk Formatting:
A new magnetic disk is a blank slate. It is just platters of a magnetic recording material. Before
a disk can store data, it must be divided into sectors that the disk controller can read and
write. This process is called low-level formatting (or physical formatting).
Low-level formatting fills the disk with a special data structure for each sector. The data
structure for a sector consists of a header, a data area, and a trailer. The header and trailer
contain information used by the disk controller, such as a sector number and an error-
correcting code (ECC).
To use a disk to hold files, the operating system still needs to record its own data structures
on the disk. It does so in two steps. The first step is to partition the disk into one or more
groups of cylinders. The operating system can treat each partition as though it were a
separate disk. For instance, one partition can hold a copy of the operating system's
executable code, while another holds user files. After partitioning, the second step is logical
formatting (or creation of a file system). In this step, the operating system stores the initial
file-system data structures onto the disk.
Boot Block
When a computer is powered up or rebooted, it needs to have an initial program to run.
This initial program is called bootstrap program. It initializes all aspects of the system (i.e.
from CPU registers to device controllers and the contents of main memory) and then starts
the operating system.
To do its job, the bootstrap program finds the operating system kernel on disk, loads that
kernel into memory, and jumps to an initial address to begin the operating-system
execution.
For most computers, the bootstrap is stored in read-only memory (ROM). This location is
convenient, because ROM needs no initialization and is at a fixed location that the
processor can start executing when powered up or reset. And since ROM is read only, it
cannot be infected by a computer virus. The problem is that changing this bootstrap code
requires changing the ROM hardware chips.
For this reason, most systems store a tiny bootstrap loader program in the boot ROM, whose
only job is to bring in a full bootstrap program from disk. The full bootstrap program can
be changed easily: A new version is simply written onto the disk. The full bootstrap program
is stored in a partition (at a fixed location on the disk) is called the boot blocks. A disk
that has a boot partition is called a boot disk or system disk.
Bad Blocks
Since disks have moving parts and small tolerances, they are prone to failure. Sometimes
the failure is complete, and the disk needs to be replaced, and its contents restored from
backup media to the new disk.
More frequently, one or more sectors become defective. Most disks even come from the
factory with bad blocks. Depending on the disk and controller in use, these blocks are
handled in a variety of ways.
The controller maintains a list of bad blocks on the disk. The list is initialized during the low-
level format at the factory, and is updated over the life of the disk. The controller can be told to
replace each bad sector logically with one of the spare sectors. This scheme is known as sector
sparing or forwarding.
Protection
Goals of Protection
• Obviously to prevent malicious misuse of the system by users or programs. See chapter 15
for a more thorough coverage of this goal.
• To ensure that each shared resource is used only in accordance with system policies, which
may be set either by system designers or by system administrators.
• To ensure that errant programs cause the minimal amount of damage possible.
• Note that protection systems only provide the mechanisms for enforcing policies and
ensuring reliable systems. It is up to administrators and users to implement those
mechanisms effectively.
Principles of Protection
• The principle of least privilege dictates that programs, users, and systems be given just
enough privileges to perform their tasks.
• This ensures that failures do the least amount of harm and allow the least of harm to be done.
• For example, if a program needs special privileges to perform a task, it is better to make it a
SGID program with group ownership of "network" or "backup" or some other pseudo group,
rather than SUID with root ownership. This limits the amount of damage that can occur if
something goes wrong.
• Typically each user is given their own account, and has only enough privilege to modify
their own files.
• The root account should not be used for normal day to day activities - The System
Administrator should also have an ordinary account, and reserve use of the root account for
only those tasks which need the root privileges
Domain of Protection
• A computer can be viewed as a collection of processes and objects ( both h/w & s/w ).
• The need to know principle states that a process should only have access to those objects it
needs to accomplish its task, and furthermore only in the modes for which it needs access
and only during the time frame when it needs access.
• The modes available for a particular object may depend upon its type.
Domain Structure
• If the association is static, then the need-to-know principle requires a way of changing the
contents of the domain dynamically.
• If the association is dynamic, then there needs to be a mechanism for domain switching.
An Example: UNIX
An Example: MULTICS
• The MULTICS system uses a complex system of rings, each corresponding to a different
protection domain, as shown below:
• Rings are numbered from 0 to 7, with outer rings having a subset of the privileges of the
inner rings.
• Each file is a memory segment, and each segment description includes an entry that indicates
the ring number associated with that segment, as well as read, write, and execute privileges.
• Each process runs in a ring, according to the current-ring-number, a counter associated with
each process.
• A process operating in one ring can only access segments associated with higher ( farther
out ) rings, and then only according to the access bits. Processes cannot access segments
associated with lower rings.
• Domain switching is achieved by a process in one ring calling upon a process operating in
a lower ring, which is controlled by several factors stored with each segment descriptor:
• If a process operating in ring i calls a segment whose bracket is such that b1 <= i <= b2,
then the call succeeds and the process remains in ring i.
• Otherwise, a trap to the OS occurs, and is handled as follows:
➢ If i < b1, then the call is allowed, because we are transferring to a procedure with fewer
privileges. However if any of the parameters being passed are of segments below b1, then
they must be copied to an area accessible by the called procedure.
➢ If i > b2, then the call is allowed only if i <= b3 and the call is directed to one of the entries
on the list of gates.
• Overall, this approach is more complex and less efficient than other protection schemes.
Access Matrix
• The model of protection that we have been discussing can be viewed as an access matrix, in
which columns represent different system resources and rows represent different protection
domains. Entries within the matrix indicate what access that domain has to that resource.
• Domain switching can be easily supported under this model, simply by providing "switch"
access to other domains:
• The ability to copy rights is denoted by an asterisk, indicating that processes in that domain
have the right to copy that access within the same column, i.e. for the same object. There
are two important variations:
o If the asterisk is removed from the original access right, then the right
is transferred, rather than being copied. This may be termed a transfer right as
opposed to a copy right.
o If only the right and not the asterisk is copied, then the access right is added to the
new domain, but it may not be propagated further. That is the new domain does not
also receive the right to copy the access. This may be termed a limited copy right, as
shown in Figure 14.5 below:
• The owner right adds the privilege of adding new rights or removing existing ones:
• Copy and owner rights only allow the modification of rights within a column. The addition
of control rights, which only apply to domain objects, allow a process operating in one
domain to affect the rights available in other domains. For example in the table below, a
process operating in domain D2 has the right to control any of the rights in domain D4.