Sei sulla pagina 1di 89

What is operating system?

OS is the system software that acts as a interface between computer


hardware and users and provides easy interface to the users by hiding
underlying complexities of computer hardware. It acts as a platform between
device hardware and user interaction. It is initially loaded into the computer
by a boot program and manages all the other application programs in a
computer.
The purpose of an OS is to provide a environment in which the user can
execute the program in a convenient & efficient manner.
Examples of operating systems include Windows, MacOS, Unix and Linux.

Two views of an operating system

OS as Resource Manager: One view considers the operating system as a


resource manager. In this view the operating system is seen as a way of
providing the users with the resources they need at any given time. Modern
computers consist of processors, memories, timers, disks, mice, network
interfaces, printers, and a wide variety of other devices. So, the job of the
operating system is to provide for an orderly and controlled allocation of the
processors, memories, and I/O devices among the various programs
competing for them. OS manage all the pieces of a complex system.

OS as Extended Machine: In this view, the function of the operating system


is to present the user with the equivalent of an extended machine or virtual
machine that is easier to program than the underlying hardware i.e way of
not having to deal with the complexity of the hardware. Operating system
hides all these complexities for us, simple-minded users and provides
convenient interface. Operating system provides a variety of services that
programs can obtain using special instructions called system calls.

Types of OS
Types of Operating Systems: Some of the widely used operating systems
are as follows-

1. Batch Operating System –


This type of operating system does not interact with the computer directly.
There is an operator which takes similar jobs having the same requirement
and group them into batches. It is the responsibility of the operator to sort the
jobs with similar needs.

Advantages of Batch Operating System:

It is easy to manage large work repeatedly in batch systems


The idle time of batch system is very less

Disadvantages of Batch Operating System:


Batch systems are hard to debug
It is sometime costly

Examples of Batch based Operating System: Payroll System, Bank


Statements etc.

2. Time-Sharing Operating Systems(Multitasking Systems) –


Each task has given some time to execute, so that all the tasks work
smoothly. Each task gets time of CPU as they use single system. The time
that each task gets to execute is called quantum. After this time interval is
over OS switches over to next task.

Advantages of Time-Sharing OS:

Each task gets an equal opportunity


CPU idle time can be reduced

Disadvantages of Time-Sharing OS:

High priority jobs have to wait for a long time


Reliability problem

Examples of Time-Sharing OSs are : Multics, Unix etc.

3. Distributed Operating System –


The major benefit of working with these types of operating system is that it is
always possible that one user can access the files or software which are not
actually present on his system but on some other system connected within
same network. When computers work in a group in cooperation, they make a
distributed system.

Advantages of Distributed Operating System:

Failure of one will not affect the other network communication, as all systems
are independent of each other
Since resources are being shared, computation is highly fast and durable
Load on host computer reduces

Disadvantages of Distributed Operating System:

Failure of the main network will stop the entire communication


These types of systems are not readily available as they are very expensive,
highly complex and not understood well yet
Examples of Distributed Operating System are- LOCUS etc.

4. Network Operating System –


These systems runs on a server and provides the capability to manage data,
users, groups, security, applications, and other networking functions. These
type of operating systems allows shared access of files, printers, security,
applications, and other networking functions over a small private network.

Advantages of Network Operating System:

Highly stable centralized servers


Server access are possible remotely from different locations and types of
systems

Disadvantages of Network Operating System:

Servers are costly


Maintenance and updates are required regularly

Examples of Network Operating System are: Microsoft Windows Server


2003, Microsoft Windows Server 2008, UNIX, Linux, Mac OS X, Novell
NetWare, and BSD etc.

5. Real-Time Operating System –


These types of OSs serves the real-time systems or application. The time
interval required to process and respond to inputs is very small. This time
interval is called response time. Real-time operating systems often use
specialized scheduling algorithms so that they can achieve a deterministic
nature of behavior. The main objective of real-time operating systems is their
quick and predictable response to events.
Two types of Real-Time Operating System which are as follows:
Hard Real-Time Systems:
Soft Real-Time Systems:

Advantages of RTOS:

Maximum Consumption:
Task Shifting:
Focus on Application:

Disadvantages of RTOS:

Limited Tasks:
Use heavy system resources:
Complex Algorithms:

Examples of Real-Time Operating Systems are: Scientific experiments,


medical imaging systems, industrial control systems, weapon systems,
robots, air traffic control systems, etc.

OS SERVICES

1. Program Execution

The purpose of computer system is to allow the users to execute programs


in an efficient manner. The operating system provides an environment where
the user can conveniently run these programs.
To run a program, the program is required to be loaded into the RAM first
and then to assign CPU time for its execution. Operating system performs
this function for the convenience of the user.
2. I/O Operations

Each program requires an input and after processing the input submitted by
user it produces output. This involves the use of I/O devices. So the
operating system makes the users convenient to run programs by providing
I/O functions.
3. File System Manipulation

While working on the computer, generally a user is required to manipulate


various types of files like as opening a file, saving a file and deleting a file
from the storage disk. This is an important task that is also performed by the
operating system.

4. Communication

Operating system performs the communication among various types of


processes in the form of shared memory. In multitasking environment, the
processes need to communicate with each other and to exchange their
information. These processes are created under a hierarchical structure
where the main process is known as parent process and the sub processes
are known as child processes.

5. Error Detection

Operating system also deals with hardware problems. To avoid hardware


problems the operating system constantly monitors the system for detecting
the errors and fixing these errors (if found). The main function of operating
system is to detect the errors like bad sectors on hard disk, memory overflow
and errors related to I/O devices. After detecting the errors, operating system
takes an appropriate action for consistent computing.

6. Resource allocation

In the multitasking environment, when multiple jobs are running at a time, it


is the responsibility of an operating system to allocate the required resources
(like as CPU, main memory, tape drive or secondary storage etc.) to each
process for its better utilization. For this purpose various types of algorithms
are implemented such as process scheduling, CPU scheduling, disk
scheduling etc.

7. Accounting

Operating system keeps an account of all the resources accessed by each


process or user. In multitasking, accounting enhances the system
performance with the allocation of resources to each process ensuring the
satisfaction to each process.

8. Protection System

If a computer system has multiple users and allows the concurrent execution
of multiple processes, then the various processes must be protected from
one another's activities.

Operating System Structure


Here are the list of all those five designs or structures of the operating
systems, we will examine.

1. Monolithic Systems

2. Layered Systems

3. Virtual Machines

4. Exokernels

5. Client-Server Systems

Now let's describe briefly about all the above different-different


structures of operating systems.
Monolithic Systems
The operating system (OS) is written as a collection of procedures, each of
which can call any of the other ones, whenever it needs to.
When this technique is used, then each procedure in the system has a well-
defined interface in terms of parameter, and results, and each one is free to
call any of the other ones, if the latter provides some useful computation that
the former needs.
Monolithic systems provides a basic structure for the operating system.

● a main program that invokes the requested service procedure

● a set of service procedures that carry out the system calls

● a set of utility procedures that help the service procedure

In Monolithic system model, there is one service procedure for each


system call, that takes care of it. The utility procedures do jobs that are
needed by several service procedures, such as fetching the data from
the user programs.
This division of the procedures into the following three layers:

1. Main Procedure

2. Service Procedures

3. Utility Procedures

The figure given below shows all the layered provided above in
monolithic system model of operating system.
Layered Systems
This system has 6 layers as shown in the table given below.

Layer Function

5 The operator

4 User programs

3 I/O management

2 Operator-process communication

1 Memory and drum management

0 Processor allocation and multiprogramming

Here, are the description provided layer by layer.

● Layer 0 - This layer dealt with allocation of the processor,

switching between processes when interrupts occurred or timers


expired
● Layer 1 - This layer did the memory management

● Layer 2 - This layer handled the communication between each

process and the operator console


● Layer 3 - This layer took care of managing the Input/Output

devices and buffering the information streams to and from them


● Layer 4 - On this layer, user programs were found

● Layer 5 - On this layer, the system operator process was located

Virtual Machines
The system originally called CP/CMS, later renamed VM/370, was based
on an astute observation. That was a time sharing system, provides
multiprogramming and an extended machine with a more convenient
interface than the bare hardware.
The heart of the system known as virtual machine monitor that runs on
the bare hardware and does the multiprogramming, providing several
virtual machines to next layer up as shown in the given figure.

These virtual machines aren't extended machines, with files and other
nice features. They are the exact copies of the bare hardware, including
the kernel/user mode, Input/Output, interrupts, and everything else the
real machine has.
Exokernels
Exokernels is a program present at the bottom layer, running in the
kernel mode.
The work of exokernel is just to allocate the resources to the virtual
machines and check attempts to use them to make sure no machine is
trying to use some other's resources.
Exokernels saves a layer of mapping which is the advantage of the
exokernel scheme.
Client-Server Model
In the client-server model, as shown in the figure given below, all the
kernel does is handle the communication between the clients and the
servers.

By splitting the operating system (OS) up into parts, each of which only
handles one fact of the system, such as file service, terminal service,
process service, or memory service, each part becomes small and
manageable.
The adaptability of the client-server model, to use in distributed system
is the advantage of this model.

System Call
System calls are the interface between the operating system and the user

programs that allow user-level processes to request services of the operating system.
In general, system calls are available as assembly language instructions.

They are also included in the manuals used by the assembly level

programmers. System call provides the services of the operating system to the user

programs via Application Program Interface(API). System calls are usually made

when a process in user mode requires access to a resource. Then it

requests the kernel to provide the resource via a system call.

A figure representing the execution of the system call is given as follows:

As can be seen from this diagram, the processes execute normally in the

user mode until a system call interrupts this. Then the system call is

executed on a priority basis in the kernel mode. After the execution of the

system call, the control returns to the user mode and execution of user

processes can be resumed.

In general, system calls are required in the following situations:


● If a file system requires the creation or deletion of files. Reading and
writing from files also require a system call.
● Creation and management of new processes.
● Network connections also require system calls. This includes sending
and receiving packets.
● Access to a hardware devices such as a printer, scanner etc. requires
a system call.

Services Provided by System Calls :

1. Process creation and management

2. Main memory management

3. File Access, Directory and File system management

4. Device handling(I/O)

5. Protection

6. Networking, etc.

Types of System Calls

There are mainly five types of system calls. These are explained in detail as
follows:

Process Control
These system calls deal with processes such as process creation, process

termination etc.
File Management
These system calls are responsible for file manipulation such as creating a

file, reading a file, writing into a file etc.

Device Management
These system calls are responsible for device manipulation such as reading

from device buffers, writing into device buffers etc.

Information Maintenance
These system calls handle information and its transfer between the

operating system and the user program.

Communication
These system calls are useful for interprocess communication. They also

deal with creating and deleting a communication connection.

Examples of Windows and Unix System Calls –


CHAPTER 2 PROCESS
Process is an executing program including the current values of the program
counter, registers, and variables. A process is basically a program in
execution. The execution of a process must progress in a sequential
fashion.

A process is defined as an entity which represents the basic unit of work to


be implemented in the system.
To put it in simple terms, we write our computer programs in a text file and
when we execute this program, it becomes a process which performs all the
tasks mentioned in the program.

When a program is loaded into the memory and it becomes a process, it can
be divided into four sections ─ stack, heap, text and data.

Process Components

Stack: The process Stack contains the temporary data such as


method/function parameters, return address and local variables.

Heap: This is dynamically allocated memory to a process during its run time.

Text: This includes the current activity represented by the value of Program
Counter and the contents of the processor's registers.

Data: This section contains the global and static variables.

Process Scheduling: Below are different times with respect to a process.

Arrival Time – Time at which the process arrives in the ready queue.
Completion Time – Time at which process completes its execution.
Burst Time – Time required by a process for CPU execution.
Turn Around Time = Completion Time - Arrival Time
Waiting Time = Turn Around Time - Burst Time
Process Life Cycle
When a process executes, it passes through different states. These states
may differ in different operating systems, and the names of these states are
also not standardized.

In general, a process can have one of the following five states at a time.

Start: This is the initial state when a process is first started/created.

Ready: The process is waiting to be assigned to a processor. Ready


processes are waiting to have the processor allocated to them by the
operating system so that they can run. Process may come into this state
after Start state or while running it by but interrupted by the scheduler to
assign CPU to some other process.

Running: Once the process has been assigned to a processor by the OS


scheduler, the process state is set to running and the processor executes its
instructions.

Waiting: Process moves into the waiting state if it needs to wait for a
resource, such as waiting for user input, or waiting for a file to become
available.

Terminated or Exit: Once the process finishes its execution, or it is


terminated by the operating system, it is moved to the terminated state
where it waits to be removed from main memory.

Process Control Block (PCB)

A Process Control Block is a data structure maintained by the Operating


System for every process. The PCB is identified by an integer process ID
(PID). A PCB keeps all the information needed to keep track of a
process.The architecture of a PCB is completely dependent on Operating
System and may contain different information in different operating systems.
Here is a simplified diagram of a PCB −

Process State: The current state of the process i.e., whether it is ready,
running, waiting, or whatever.

Process privileges: This is required to allow/disallow access to system


resources.
Process ID: Unique identification for each of the process in the operating
system.

Pointer: A pointer to the parent process.

Program Counter: Program Counter is a pointer to the address of the next


instruction to be executed for this process.

CPU registers: Various CPU registers where process need to be stored for
execution for running state.

CPU Scheduling Information: Process priority and other scheduling


information which is required to schedule the process.

Memory management information: This includes the information of page


table, memory limits, Segment table depending on memory used by the
operating system.

Accounting information: This includes the amount of CPU used for process
execution, time limits, execution ID etc.

IO status information: This includes a list of I/O devices allocated to the


process.

THREAD

Thread is an execution unit which consists of its own program counter, a


stack, and a set of registers. Threads are also known as Lightweight
processes. Threads are popular way to improve application through
parallelism. The CPU switches rapidly back and forth among the threads
giving illusion that the threads are running in parallel.
As each thread has its own independent resource for process execution,
multiple processes can be executed parallely by increasing number of
threads.
Threads provide a way to improve application performance through
parallelism. Threads represent a software approach to improving
performance of operating system by reducing the overhead 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.

Advantages of Thread
Threads minimize the context switching time.
Use of threads provides concurrency within a process.
Efficient communication.
It is more economical to create and context switch threads.
Threads allow utilization of multiprocessor architectures to a greater scale
and efficiency.
Types of Thread
User Level Threads − User managed threads.

Kernel Level Threads − Operating System managed threads acting on a


kernel, an operating system core.

User Level Threads


In this case, the thread management kernel is not aware of the existence of
threads. The thread library contains code for creating and destroying
threads, for passing message and data between threads, for scheduling
thread execution and for saving and restoring thread contexts. The
application starts with a single thread.

Advantages
Thread switching does not require Kernel mode privileges.
User level thread can run on any operating system.
Scheduling can be application specific in the user level thread.
User level threads are fast to create and manage.

Disadvantages
In a typical operating system, most system calls are blocking.
Multithreaded application cannot take advantage of multiprocessing.

Kernel Level Threads


In this case, thread management is done by the Kernel. There is no thread
management code in the application area. Kernel threads are supported
directly by the operating system. The Kernel maintains context information
for the process as a whole and for individuals threads within the process.
Scheduling by the Kernel is done on a thread basis. The Kernel performs
thread creation, scheduling and management in Kernel space. Kernel
threads are generally slower to create and manage than the user threads.

Advantages
Kernel can simultaneously schedule multiple threads from the same process
on multiple processes.
If one thread in a process is blocked, the Kernel can schedule another
thread of the same process.
Kernel routines themselves can be multithreaded.

Disadvantages
Kernel threads are generally slower to create and manage than the user
threads.
Transfer of control from one thread to another within the same process
requires a mode switch to the Kernel.
Schedulers

Schedulers are special system software which handle process scheduling in


various ways. The scheduler is to identify the process whose selection will
results the best possible system performance. Their main task is to select
the jobs to be submitted into the system and to decide which process to run.
Schedulers are of three types −

● Long-Term Scheduler
● Short-Term Scheduler

● Medium-Term Scheduler

Long Term Scheduler :

It is also called a job scheduler. A long-term scheduler determines which


programs are admitted to the system for processing. It selects processes
from the queue and loads them into memory for execution. Process loads
into the memory for CPU scheduling.
Short Term Scheduler :

It is also called as CPU scheduler. Its main objective is to increase system


performance in accordance with the chosen set of criteria. It is the change of
ready state to running state of the process. CPU scheduler selects a process
among the processes that are ready to execute and allocates CPU to one of
them.

Medium Term Scheduler :

Medium-term scheduling is a part of swapping. It removes the processes


from the memory. It reduces the degree of multiprogramming. The medium-
term scheduler is in-charge of handling the swapped out-processes.

A running process may become suspended if it makes an I/O request. A


suspended processes cannot make any progress towards completion. In this
condition, to remove the process from memory and make space for other
processes, the suspended process is moved to the secondary storage. This
process is called swapping, and the process is said to be swapped out or
rolled out. Swapping may be necessary to improve the process mix.

Scheduling

The process scheduling is the activity of the process manager that handles
the removal of the running process from the CPU and the selection of another
process on the basis of a particular strategy.

Process scheduling is an essential part of a Multiprogramming operating


systems. Such operating systems allow more than one process to be loaded
into the executable memory at a time and the loaded process shares the CPU
using time multiplexing.
Preemptive vs. Nonpreemptive Scheduling

Nonpreemptive:
• Once a process has been given the CPU, it runs until blocks for I/O or
termination.
• Treatment of all processes is fair.
• Response times are more predictable.
• Useful in real-time system.
• Shorts jobs are made to wait by longer jobs - no priority

Preemptive:
• Processes are allowed to run for a maximum of some fixed time.
• Useful in systems in which high-priority processes requires rapid
attention.
• In time-sharing systems, preemptive scheduling is important in
guaranteeing acceptable response times.
• High overhead.

Criteria for comparing scheduling algorithms:


CPU Utilization
Balance Utilization
Throughput
Turnaround Time
Waiting Time
Response Time
Predictability
Fairness
Priorities

Different Scheduling Algorithms:

First Come First Serve (FCFS)


Simplest scheduling algorithm that schedules according to arrival times of
processes. Once the process has the CPU, it runs to completion. It ensures
fair to all processes.
Problems:
No guarantee of good response time.
Large average waiting time.
Not applicable for an interactive system.

Shortest Job First (SJF) –


Process which have the shortest burst time are scheduled first. In
case of the tie FCFS scheduling is used. The decision policies are based
bcon the CPU burst time.
Problems:
Estimation of run time to completion. Accuracy?
Not applicable in timesharing system.
Shortest Remaining Time First (SRTF)–
It is a preemptive mode of SJF algorithm in which jobs are scheduled
according to the shortest remaining time. Any time a new process enters the
pool of processes to be scheduled, the scheduler compares the expected
value for its remaining processing time with that of the process currently
scheduled. If the new process’s time is less, the currently scheduled process
is preempted.
Demerits:
Very high overhead than SJF.
Requires additional computation.
Favors short jobs, longs jobs can be victims of starvation.

Round Robin (RR) Scheduling –


Each process is assigned a fixed time, in cyclic way.
Each process is assigned a time interval (quantum), after the specified
quantum, the running process is preempted and a new process is allowed to
run. Preempted process is placed at the back of the ready list.

Priority Based scheduling (Non Preemptive) –


In this scheduling, processes are scheduled according to their priorities, i.e.,
highest priority process is schedule first. If priorities of two processes match,
then scheduling is according to the arrival time.

Highest Response Ratio Next (HRRN) –


In this scheduling, processes with highest response ratio is scheduled. This
algorithm avoids starvation.
Response Ratio = (Waiting Time + Burst time) / Burst time
Multilevel Queue Scheduling (MLQ) –
According to the priority of process, processes are placed in the different
queues. Generally high priority process are placed in the top level queue.
Only after completion of processes from top level queue, lower level queued
processes are scheduled.

Multi level Feedback Queue (MLFQ) Scheduling –


It allows the process to move in between queues. The idea is to separate
processes according to the characteristics of their CPU bursts. If a process
uses too much CPU time, it is moved to a lower-priority queue.

CHAPTER 3 Interprocess Communication

IPC is a mechanism which allows processes to communicate each other and


synchronize their actions. IPC allows passing of information from one
process to another process. Processes frequently need to communicate with
other processes. The communication between these processes can be seen
as a method of co-operation between them.For example, in a shell pipeline,
the output of the first process must be passed to the second process, and so
on down the line. Thus there is a need for communication between
processes, preferably in a well-structured way not using interrupts.
Processes can communicate with each other using these two ways:

1. Shared Memory

2. Message passing

Race Condition
A race condition occurs when two or more process can access shared data
and they try to change it at the same time. Because the process scheduling
algorithm can swap between process at any time, you don't know the order
in which the process will attempt to access the shared data. Therefore, the
result of the change in data is dependent on the process scheduling
algorithm, i.e. both threads are "racing" to access/change the data.

Critical Section
One way to avoid race conditions is not to allow two processes to be in their
critical sections at the same time. Critical section is the part of the process
that accesses a shared variable. The regions of a program that try to access
shared resources and may cause race conditions are called critical section.
To avoid race condition among the processes, we need to assure that only
one process at a time can execute within the critical section.
Mutual Exclusion
A way of making sure that if one process is using a shared modifiable data,
the other processes will be excluded from doing the same thing. Formally,
while one process executes the shared variable, all other processes desiring
to do so at the same time moment should be kept waiting; when that process
has finished executing the shared variable, one of the processes waiting;
while that process has finished executing the shared variable, one of the
processes waiting to do so should be allowed to proceed. In this fashion,
each process executing the shared data (variables) excludes all others from
doing so simultaneously. This is called Mutual Exclusion.

Note that mutual exclusion needs to be enforced only when processes


access shared modifiable data - when processes are performing operations
that do not conflict with one another they should be allowed to proceed
concurrently.

Mutual Exclusion Conditions


If we could arrange matters such that no two processes were ever in their
critical sections simultaneously, we could avoid race conditions. We need
four conditions to hold to have a good solution for the critical section problem
(mutual exclusion).
• No two processes may at the same moment inside their critical sections.
• No assumptions are made about relative speeds of processes or number of
CPUs.
• No process should outside its critical section should block other processes.
• No process should wait forever to enter its critical section.

Proposal 1 -Disabling Interrupts (Hardware Solution)


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 technique within the kernel of an
operating system, but it is not appropriate as a general mutual exclusion
mechanism for users process. The reason is that it is unwise to give user
process the power to turn off interrupts.

Proposal 2 - Lock Variable (Software Solution)


In this solution, we consider a single, shared, (lock) variable, initially 0. When
a process wants to enter in its critical section, it first test the lock. If lock is 0,
the process first sets it to 1 and then enters the critical section. If the lock is
already 1, the process just waits until (lock) variable becomes 0. Thus, a 0
means that no process in its critical section, and 1 means hold your horses -
some process is in its critical section.

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.

Proposal 3 - Strict Alteration


In this proposed solution, the integer variable 'turn' keeps track of whose turn
is to enter the critical section. Initially, process A inspect 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 non-critical section. This situation violates above
mentioned condition

3. Using Systems calls 'sleep' and 'wakeup'


Basically, what above mentioned solution do is this: when a processes wants
to enter in its critical section , it checks to see if then entry is allowed. If it is
not, the process goes into tight loop and waits (i.e., start busy waiting) until it
is allowed to enter. This approach waste CPU-time.
Now look at some interprocess communication primitives is the pair of steep-
wakeup.
• Sleep -It is a system call that causes the caller to block, that is, be
suspended until some other process wakes it up.
• Wake up -It is a system call that wakes up the process.

Both 'sleep' and 'wakeup' system calls have one parameter that represents a
memory address used to match up 'sleeps' and 'wake ups'.

Peterson’s Solution:
It is a solution to the mutual exclusion problem that does not require strict
alternation, but still uses the idea of lock variables together with the concept
of taking turns.
Peterson's algorithm is used for mutual exclusion and allows two processes to share a
single-use resource without conflict. It uses only shared memory for communication.
Peterson's formula originally worked only with two processes, but has since been
generalized for more than two.
This is a software mechanism implemented at user mode. It is a busy waiting
solution which can be implemented for only two processes. It uses two
variables that are turn variable and interested variable.
The Code of the solution is given below

# define N 2
# define TRUE 1
# define FALSE 0
int interested[N] = FALSE;
int turn;
voidEntry_Section (int process)
{
int other;
other = 1-process;
interested[process] = TRUE;
turn = process;
while (interested [other] =True && TURN=process);
}
voidExit_Section (int process)
{
interested [process] = FALSE;
}

However, the Peterson solution provides you all the necessary requirements
such as Mutual Exclusion, Progress, Bounded Waiting and Portability.

Semaphore
In 1965, Dijkstra proposed a new and very significant technique for
managing concurrent processes by using the value of a simple integer
variable to synchronize the progress of interacting processes. This integer
variable is called semaphore. So it is basically a synchronizing tool and is
accessed only through two low standard atomic operations, wait and signal
designated by P(S) and V(S)respectively.
In very simple words, semaphore is a variable which can hold only a non-
negative Integer value, shared between all the threads, with operations wait
and signal,
Types of Semaphores
Semaphores are mainly of two types:
1. Binary Semaphore: It is a special form of semaphore used for

implementing mutual exclusion, hence it is often called a Mutex. A

binary semaphore is initialized to 1 and only takes the values 0 and

1 during execution of a program.

2. Counting Semaphores:These are used to implement bounded

concurrency.

The Bounded Buffer Producers and Consumers(Producers and


Consumers Problem)
The bounded buffer producers and consumers assumes that there is a fixed
buffer size i.e., a finite numbers of slots are available.

Statement
To suspend the producers when the buffer is full, to suspend the consumers
when the buffer is empty, and to make sure that only one process at a time
manipulates a buffer so there are no race conditions or lost updates. As an
example how sleep-wakeup system calls are used, consider the producer-
consumer problem also known as bounded buffer problem. Two processes
share a common, fixed-size (bounded) buffer. The producer puts information
into the buffer and the consumer takes information out. Trouble arises when
1. The producer wants to put a new data in the buffer, but buffer is already
full. Solution: Producer goes to sleep and to be awakened when the
consumer has removed data.

2. The consumer wants to remove data the buffer but buffer is already
empty. Solution: Consumer goes to sleep until the producer puts some data
in buffer and wakes consumer up.
Conclusion
This approaches also leads to same race conditions we have seen in earlier
approaches. Race condition can occur due to the fact that access to 'count'
is unconstrained. The essence of the problem is that a wakeup call, sent to a
process that is not sleeping, is lost.

Chapter 4 Deadlock

Operating System | Process Management | Deadlock Introduction

A process in operating systems uses different resources and uses resources


in following way.

1) Requests a resource

2) Use the resource

3) Releases the resource

Deadlock is a situation where a set of processes are blocked because each


process is holding a resource and waiting for another resource acquired by
some other process.

Consider an example when two trains are coming toward each other on
same track and there is only one track, none of the trains can move once
they are in front of each other. Similar situation occurs in operating systems
when there are two or more processes hold some resources and wait for
resources held by other(s). For example, in the below diagram, Process 1 is
holding Resource 1 and waiting for resource 2 which is acquired by process
2, and process 2 is waiting for resource 1.
Deadlock can arise if following four conditions hold simultaneously
(Necessary Conditions)
Necessary conditions for Deadlocks

1. Mutual Exclusion

A resource can only be shared in mutually exclusive manner. It implies,


two process cannot use the same resource at the same time.

2. Hold and Wait

A process waits for some resources while holding another resource at


the same time.

3. No preemption
The process which once scheduled will be executed till the completion.
No other process can be scheduled by the scheduler meanwhile.

4. Circular Wait

All the processes must be waiting for the resources in a cyclic manner
so that the last process is waiting for the resource which is being held
by the first process.

Strategies for handling Deadlock

1. Deadlock Ignorance
Deadlock Ignorance is the most widely used approach among all the
mechanism. This is being used by many operating systems mainly for end
user uses. In this approach, the Operating system assumes that deadlock
never occurs. It simply ignores deadlock. This approach is best suitable for a
single end user system where User uses the system only for browsing and
all other normal stuff.

There is always a tradeoff between Correctness and performance. The


operating systems like Windows and Linux mainly focus upon performance.
However, the performance of the system decreases if it uses deadlock
handling mechanism all the time. If deadlock happens 1 out of 100 times
then it is completely unnecessary to use the deadlock handling mechanism
all the time.
In these types of systems, the user has to simply restart the computer in the
case of deadlock. Windows and Linux are mainly using this approach.

2. Deadlock prevention
Deadlock happens only when Mutual Exclusion, hold and wait, No
preemption and circular wait holds simultaneously. If it is possible to violate
one of the four conditions at any time then the deadlock can never occur in
the system.

The idea behind the approach is very simple that we have to fail one of the
four conditions but there can be a big argument on its physical
implementation in the system.

We will discuss it later in detail.

3. Deadlock avoidance
In deadlock avoidance, the operating system checks whether the system is
in safe state or in unsafe state at every step which the operating system
performs. The process continues until the system is in safe state. Once the
system moves to unsafe state, the OS has to backtrack one step.

In simple words, The OS reviews each allocation so that the allocation


doesn't cause the deadlock in the system.

We will discuss Deadlock avoidance later in detail.

4. Deadlock detection and recovery


This approach let the processes fall in deadlock and then periodically check
whether deadlock occur in the system or not. If it occurs then it applies some
of the recovery methods to the system to get rid of deadlock.

We will discuss deadlock detection and recovery later in more detail since it
is a matter of discussion.

Deadlock Prevention
Let's see how we can prevent each of the conditions.

1. Mutual Exclusion
Mutual section from the resource point of view is the fact that a resource can
never be used by more than one process simultaneously which is fair
enough but that is the main reason behind the deadlock. If a resource could
have been used by more than one process at the same time then the
process would have never been waiting for any resource.

However, if we can be able to violate resources behaving in the mutually


exclusive manner then the deadlock can be prevented.

Spooling
For a device like printer, spooling can work. There is a memory associated
with the printer which stores jobs from each of the process into it. Later,
Printer collects all the jobs and print each one of them according to FCFS.
By using this mechanism, the process doesn't have to wait for the printer and
it can continue whatever it was doing. Later, it collects the output when it is
produced.
Although, Spooling can be an effective approach to violate mutual exclusion
but it suffers from two kinds of problems.

1. This cannot be applied to every resource.

2. After some point of time, there may arise a race condition between the
processes to get space in that spool.

We cannot force a resource to be used by more than one process at the


same time since it will not be fair enough and some serious problems may
arise in the performance. Therefore, we cannot violate mutual exclusion for a
process practically.

2. Hold and Wait


Hold and wait condition lies when a process holds a resource and waiting for
some other resource to complete its task. Deadlock occurs because there
can be more than one process which are holding one resource and waiting
for other in the cyclic order.
However, we have to find out some mechanism by which a process either
doesn't hold any resource or doesn't wait. That means, a process must be
assigned all the necessary resources before the execution starts. A process
must not wait for any resource once the execution has been started.

!(Hold and wait) = !hold or !wait (negation of hold and wait is, either you don't
hold or you don't wait)

This can be implemented practically if a process declares all the resources


initially. However, this sounds very practical but can't be done in the
computer system because a process can't determine necessary resources
initially.

Process is the set of instructions which are executed by the CPU. Each of
the instruction may demand multiple resources at the multiple times. The
need cannot be fixed by the OS.

The problem with the approach is:

1. Practically not possible.

2. Possibility of getting starved will be increases due to the fact that some
process may hold a resource for a very long time.

3. No Preemption
Deadlock arises due to the fact that a process can't be stopped once it
starts. However, if we take the resource away from the process which is
causing deadlock then we can prevent deadlock.
This is not a good approach at all since if we take a resource away which is
being used by the process then all the work which it has done till now can
become inconsistent.

Consider a printer is being used by any process. If we take the printer away
from that process and assign it to some other process then all the data which
has been printed can become inconsistent and ineffective and also the fact
that the process can't start printing again from where it has left which causes
performance inefficiency.

4. Circular Wait
To violate circular wait, we can assign a priority number to each of the
resource. A process can't request for a lesser priority resource. This ensures
that not a single process can request a resource which is being utilized by
some other process and no cycle will be formed.
Among all the methods, violating Circular wait is the only approach that can
be implemented practically.

Deadlock Avoidance

Deadlock can be avoided if certain information about processes are


available to the operating system before allocation of resources, such as
which resources a process will consume in its lifetime. For every resource
request, the system sees whether granting the request will cause the system
to enter an unsafe state. The system then only grants requests that will lead
to safe states. In order for the system to be able to determine whether the
next state will be safe or unsafe, it must know in advance at any time:

Resources currently available

Resources currently allocated to each process


Resources that will be required and released by these processes in the
future

It is possible for a process to be in an unsafe state but for this not to result in
a deadlock. The notion of safe/unsafe states only refers to the ability of the
system to enter a deadlock state or not. For example, if a process requests
A which would result in an unsafe state, but releases B which would prevent
circular wait, then the state is unsafe but the system is not in deadlock. One
known algorithm that is used for deadlock avoidance is the Banker's
algorithm, which requires resource usage limit to be known in advance.
However, for many systems it is impossible to know in advance what every
process will request. This means that deadlock avoidance is impossible
practically.

Banker’s Algorithm: The Banker's algorithm is a resource allocation and


deadlock avoidance algorithm developed by Dijkstra. The Banker's algorithm
is run by the operating system whenever a process requests resources. The
algorithm avoids deadlock by denying or postponing the request if it
determines that accepting the request could put the system in an unsafe
state. When a new process enters a system, it must declare the maximum
number of instances of each resource type that it may claim during its life.
Also, when a process gets all its requested resources it must return them in
a finite amount of time.

Deadlock Detection and Recovery


In this approach, The OS doesn't apply any mechanism to avoid or prevent
the deadlocks. Therefore the system considers that the deadlock will
definitely occur. In order to get rid of deadlocks, The OS periodically checks
the system for any deadlock. In case, it finds any of the deadlock then the
OS will recover the system using some recovery techniques.
The main task of the OS is detecting the deadlocks. The OS can detect the
deadlocks with the help of Resource allocation graph.

In single instanced resource types, if a cycle is being formed in the system


then there will definitely be a deadlock. On the other hand, in multiple
instanced resource type graph, detecting a cycle is not just enough. We
have to apply the safety algorithm on the system by converting the resource
allocation graph into the allocation matrix and request matrix.
In order to recover the system from deadlocks, either OS considers
resources or processes.

For Resource

Preempt the resource


We can snatch one of the resources from the owner of the resource
(process) and give it to the other process with the expectation that it will
complete the execution and will release this resource sooner. Well, choosing
a resource which will be snatched is going to be a bit difficult.

Rollback to a safe state


System passes through various states to get into the deadlock state. The
operating system can rollback the system to the previous safe state. For this
purpose, OS needs to implement check pointing at every state.
The moment, we get into deadlock, we will rollback all the allocations to get
into the previous safe state.

For Process

Kill a process
Killing a process can solve our problem but the bigger concern is to decide
which process to kill. Generally, Operating system kills a process which has
done least amount of work until now.

Kill all process


This is not a suggestible approach but can be implemented if the problem
becomes very serious. Killing all process will lead to inefficiency in the
system because all the processes will execute again from starting.
CHAPTER 7 FILE SYSTEM

What is a File ?
A file can be defined as a data structure which stores the sequence of
records. Files are stored in a file system, which may exist on a disk or in the
main memory. Files can be simple (plain text) or complex (specially-
formatted).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.

The collection of files is known as Directory. The collection of directories at


the different levels, is known as File System.

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 into blocks
understandable by the systems linker.
3. Executable file:- Executable file is a series of code section that the loader
can bring into 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.

Attributes of the File


1.Name
Every file carries a name by which the file is recognized in the file system.
One directory cannot have two files with the same name.
2.Identifier
Along with the name, Each File has its own extension which identifies the
type of the file. For example, a text file has the extension .txt, A video file
can have the extension .mp4.
3.Type
In a File System, the Files are classified in different types such as video files,
audio files, text files, executable files, etc.
4.Location
In the File System, there are several locations on which, the files can be
stored. Each file carries its location as its attribute.
5.Size
The Size of the File is one of its most important attribute. By size of the file,
we mean the number of bytes acquired by the file in the memory.
6.Protection
The Admin of the computer may want the different protections for the
different files. Therefore each file carries its own set of permissions to the
different group of Users.
7.Time and Date
Every file carries a time stamp which contains the time and date on which
the file is last modified.

Operations on the File


There are various operations which can be implemented on a file. We will
see all of them in detail.
1.Create
Creation of the file is the most important operation on the file. Different types
of files are created by different methods for example text editors are used to
create a text file, word processors are used to create a word file and Image
editors are used to create the image files.
2.Write
Writing the file is different from creating the file. The OS maintains a write
pointer for every file which points to the position in the file from which, the
data needs to be written.
3.Read
Every file is opened in three different modes : Read, Write and append. A
Read pointer is maintained by the OS, pointing to the position up to which,
the data has been read.
4.Re-position
Re-positioning is simply moving the file pointers forward or backward
depending upon the user's requirement. It is also called as seeking.
5.Delete
Deleting the file will not only delete all the data stored inside the file, It also
deletes all the attributes of the file. The space which is allocated to the file
will now become available and can be allocated to the other files.
6.Truncate
Truncating is simply deleting the file except deleting attributes. The file is not
completely deleted although the information stored inside the file get
replaced.

File Access Methods


Let's look at various ways to access files stored in secondary memory.

Sequential Access
Most of the operating systems access the file sequentially. In other words,
we can say that most of the files need to be accessed sequentially by the
operating system.
In sequential access, the OS read the file word by word. A pointer is
maintained which initially points to the base address of the file. If the user
wants to read first word of the file then the pointer provides that word to the
user and increases its value by 1 word. This process continues till the end of
the file.
Modern word systems do provide the concept of direct access and indexed
access but the most used method is sequential access due to the fact that
most of the files such as text files, audio files, video files, etc need to be
sequentially accessed.

Direct Access
The Direct Access is mostly required in the case of database systems. In
most of the cases, we need filtered information from the database. The
sequential access can be very slow and inefficient in such cases.
Suppose every block of the storage stores 4 records and we know that the
record we needed is stored in 10th block. In that case, the sequential access
will not be implemented because it will traverse all the blocks in order to
access the needed record.
Direct access will give the required result despite of the fact that the
operating system has to perform some complex tasks such as determining
the desired block number. However, that is generally implemented in
database applications.
Indexed Access
If a file can be sorted on any of the filed then an index can be assigned to a
group of certain records. However, A particular record can be accessed by
its index. The index is nothing but the address of a record in the file.
In index accessing, searching in a large database became very quick and
easy but we need to have some extra space in the memory to store the
index value.

Directory Structure
What is a directory?
Directory can be defined as the listing of the related files on the disk. The
directory may store some or the entire file attributes.
To get the benefit of different file systems on the different operating systems,
A hard disk can be divided into the number of partitions of different sizes.
The partitions are also called volumes or mini disks.
Each partition must have at least one directory in which, all the files of the
partition can be listed. A directory entry is maintained for each file in the
directory which stores all the information related to that file.

A directory can be viewed as a file which contains the Meta data of the
bunch of files.
Every Directory supports a number of common operations on the file:

1. File Creation

2. Search for the file

3. File deletion

4. Renaming the file

5. Traversing Files

6. Listing of files

Single Level Directory


The simplest method is to have one big list of all the files on the disk. The
entire system will contain only one directory which is supposed to mention all
the files present in the file system. The directory contains one entry per each
file present on the file system.

This type of directories can be used for a simple system.


Advantages

1. Implementation is very simple.

2. If the sizes of the files are very small then the searching becomes
faster.

3. File creation, searching, deletion is very simple since we have only one
directory.

Disadvantages

1. We cannot have two files with the same name.

2. The directory may be very big therefore searching for a file may take
so much time.

3. Protection cannot be implemented for multiple users.

4. There are no ways to group same kind of files.


5. Choosing the unique name for every file is a bit complex and limits the
number of files in the system because most of the Operating System
limits the number of characters used to construct the file name.

Two Level Directory


In two level directory systems, we can create a separate directory for each
user. There is one master directory which contains separate directories
dedicated to each user. For each user, there is a different directory present
at the second level, containing group of user's file. The system doesn't let a
user to enter in the other user's directory without permission.

Characteristics of two level directory system

1. Each files has a path name as /User-name/directory-name/

2. Different users can have the same file name.

3. Searching becomes more efficient as only one user's list needs to be


traversed.

4. The same kind of files cannot be grouped into a single directory for a
particular user.
Every Operating System maintains a variable as PWD which contains the
present directory name (present username) so that the searching can be
done appropriately.

Tree Structured Directory


In Tree structured directory system, any directory entry can either be a file or
sub directory. Tree structured directory system overcomes the drawbacks of
two level directory system. The similar kind of files can now be grouped in
one directory.
Each user has its own directory and it cannot enter in the other user's
directory. However, the user has the permission to read the root's data but
he cannot write or modify this. Only administrator of the system has the
complete access of root directory.
Searching is more efficient in this directory structure. The concept of current
working directory is used. A file can be accessed by two types of path, either
relative or absolute.
Absolute path is the path of the file with respect to the root directory of the
system while relative path is the path with respect to the current working
directory of the system. In tree structured directory systems, the user is given
the privilege to create the files as well as directories.

Acyclic-Graph Structured Directories


The tree structured directory system doesn't allow the same file to exist in
multiple directories therefore sharing is major concern in tree structured
directory system. We can provide sharing by making the directory an acyclic
graph. In this system, two or more directory entry can point to the same file
or sub directory. That file or sub directory is shared between the two
directory entries.
These kinds of directory graphs can be made using links or aliases. We can
have multiple paths for a same file. Links can either be symbolic (logical) or
hard link (physical).
If a file gets deleted in acyclic graph structured directory system, then
1. In the case of soft link, the file just gets deleted and we are left with a
dangling pointer.
2. In the case of hard link, the actual file will be deleted only if all the
references to it gets deleted.

Directory Implementation
There is the number of algorithms by using which, the directories can be
implemented. However, the selection of an appropriate directory
implementation algorithm may significantly affect the performance of the
system.
The directory implementation algorithms are classified according to the data
structure they are using. There are mainly two algorithms which are used in
these days.

1. Linear List
In this algorithm, all the files in a directory are maintained as singly linked list.
Each file contains the pointers to the data blocks which are assigned to it
and the next file in the directory.
Characteristics

1. When a new file is created, then the entire list is checked whether the
new file name is matching to a existing file name or not. In case, it
doesn't exist, the file can be created at the beginning or at the end.
Therefore, searching for a unique name is a big concern because
traversing the whole list takes time.

2. The list needs to be traversed in case of every operation (creation,


deletion, updating, etc) on the files therefore the systems become
inefficient.

2. Hash Table
To overcome the drawbacks of singly linked list implementation of
directories, there is an alternative approach that is hash table. This approach
suggests to use hash table along with the linked lists.
A key-value pair for each file in the directory gets generated and stored in
the hash table. The key can be determined by applying the hash function on
the file name while the key points to the corresponding file stored in the
directory.
Now, searching becomes efficient due to the fact that now, entire list will not
be searched on every operating. Only hash table entries are checked using
the key and if an entry found then the corresponding file will be fetched using
the value.

File Systems
File system is the part of the operating system which is responsible for file
management. It provides a mechanism to store the data and access to the
file contents including data and programs. Some Operating systems treats
everything as a file for example Ubuntu.
The File system takes care of the following issues

● File Structure
We have seen various data structures in which the file can be stored.
The task of the file system is to maintain an optimal file structure.

● Recovering Free space

Whenever a file gets deleted from the hard disk, there is a free space
created in the disk. There can be many such spaces which need to be
recovered in order to re-allocate them to other files.

● disk space assignment to the files

The major concern about the file is deciding where to store the files on
the hard disk. There are various disks scheduling algorithm which will
be covered later in this tutorial.

● tracking data location

A File may or may not be stored within only one block. It can be stored
in the non contiguous blocks on the disk. We need to keep track of all
the blocks on which the part of the files reside.

File System Structure


File System provide efficient access to the disk by allowing data to be stored,
located and retrieved in a convenient way. A file System must be able to
store the file, locate the file and retrieve the file.
Most of the Operating Systems use layering approach for every task
including file systems. Every layer of the file system is responsible for some
activities.
The image shown below, elaborates how the file system is divided in
different layers, and also the functionality of each layer.
● When an application program asks for a file, the first request is
directed to the logical file system. The logical file system contains the
Meta data of the file and directory structure. If the application program
doesn't have the required permissions of the file then this layer will
throw an error. Logical file systems also verify the path to the file.
● Generally, files are divided into various logical blocks. Files are to be
stored in the hard disk and to be retrieved from the hard disk. Hard
disk is divided into various tracks and sectors. Therefore, in order to
store and retrieve the files, the logical blocks need to be mapped to
physical blocks. This mapping is done by File organization module. It is
also responsible for free space management.

● Once File organization module decided which physical block the


application program needs, it passes this information to basic file
system. The basic file system is responsible for issuing the commands
to I/O control in order to fetch those blocks.

● I/O controls contain the codes by using which it can access hard disk.
These codes are known as device drivers. I/O controls are also
responsible for handling interrupts.

Allocation Methods
There are various methods which can be used to allocate disk space to the
files. Selection of an appropriate allocation method will significantly affect the
performance and efficiency of the system. Allocation method provides a way
in which the disk will be utilized and the files will be accessed.
There are following methods which can be used for allocation.

1. Contiguous Allocation.

2. Extents

3. Linked Allocation

4. Clustering

5. FAT

6. Indexed Allocation
7. Linked Indexed Allocation

8. Multilevel Indexed Allocation

9. Inode

Contiguous Allocation
If the blocks are allocated to the file in such a way that all the logical blocks
of the file get the contiguous physical block in the hard disk then such
allocation scheme is known as contiguous allocation.
In the image shown below, there are three files in the directory. The starting
block and the length of each file are mentioned in the table. We can check in
the table that the contiguous blocks are assigned to each file as per its need.

Advantages

1. It is simple to implement.

2. We will get Excellent read performance.

3. Supports Random Access into files.

Disadvantages
1. The disk will become fragmented.

2. It may be difficult to have a file grow.

Linked List Allocation

Linked List allocation solves all problems of contiguous allocation. In


linked list allocation, each file is considered as the linked list of disk
blocks. However, the disks blocks allocated to a particular file need not
to be contiguous on the disk. Each disk block allocated to a file
contains a pointer which points to the next disk block allocated to the
same file.

Advantages

1. There is no external fragmentation with linked allocation.

2. Any free block can be utilized in order to satisfy the file block requests.

3. File can continue to grow as long as the free blocks are available.

4. Directory entry will only contain the starting block address.


Disadvantages

1. Random Access is not provided.

2. Pointers require some space in the disk blocks.

3. Any of the pointers in the linked list must not be broken otherwise the
file will get corrupted.

4. Need to traverse each block.

Linked Index Allocation


Single level linked Index Allocation

In index allocation, the file size depends on the size of a disk block. To allow
large files, we have to link several index blocks together. In linked index
allocation,

● Small header giving the name of the file

● Set of the first 100 block addresses

● Pointer to another index block

For the larger files, the last entry of the index block is a pointer which points
to another index block. This is also called as linked schema.
Advantage: It removes file size limitations

Disadvantage: Random Access becomes a bit harder

Multilevel Index Allocation

In Multilevel index allocation, we have various levels of indices. There are


outer level index blocks which contain the pointers to the inner level index
blocks and the inner level index blocks contain the pointers to the file data.

● The outer level index is used to find the inner level index.

● The inner level index is used to find the desired data block.

Advantage: Random Access becomes better and efficient.

Disadvantage: Access time for a file will be higher.


Inode

In UNIX based operating systems, each file is indexed by an Inode. Inode


are the special disk block which is created with the creation of the file
system. The number of files or directories in a file system depends on the
number of Inodes in the file system.

An Inode includes the following information

1. Attributes (permissions, time stamp, ownership details, etc) of the file


2. A number of direct blocks which contains the pointers to first 12 blocks
of the file.

3. A single indirect pointer which points to an index block. If the file


cannot be indexed entirely by the direct blocks then the single indirect
pointer is used.

4. A double indirect pointer which points to a disk block that is a collection


of the pointers to the disk blocks which are index blocks. Double index
pointer is used if the file is too big to be indexed entirely by the direct
blocks as well as the single indirect pointer.

5. A triple index pointer that points to a disk block that is a collection of


pointers. Each of the pointers is separately pointing to a disk block
which also contains a collection of pointers which are separately
pointing to an index block that contains the pointers to the file blocks.

Free Space Management


A file system is responsible to allocate the free blocks to the file therefore it
has to keep track of all the free blocks present in the disk. There are mainly
two approaches by using which, the free blocks in the disk are managed.

1. Bit Vector

In this approach, the free space list is implemented as a bit-map vector. It


contains the number of bits where each bit represents each block.

If the block is empty then the bit is 1 otherwise it is 0. Initially all the blocks
are empty therefore each bit in the bit-map vector contains 1.

LAs the space allocation proceeds, the file system starts allocating blocks to
the files and setting the respective bit to 0.

2. Linked List

It is another approach for free space management. This approach suggests


linking together all the free blocks and keeping a pointer in the cache which
points to the first free block.

Therefore, all the free blocks on the disks will be linked together with a
pointer. Whenever a block gets allocated, its previous free block will be
linked to its next free block.

CHAPTER 8 SECURITY

Security refers to providing a protection system to computer system


resources such as CPU, memory, disk, software programs and most
importantly data/information stored in the computer system. If a computer
program is run by an unauthorized user, then he/she may cause severe
damage to computer or data stored in it. So a computer system must be
protected against unauthorized access, malicious access to system memory,
viruses, worms etc.

The Security Problem

● Protection dealt with protecting files and other resources from


accidental misuse by cooperating users sharing a system, generally
using the computer for normal purposes.
● Security deals with protecting systems from deliberate attacks, either
internal or external, from individuals intentionally attempting to steal
information, damage information, or otherwise deliberately wreak
havoc in some manner.
● Some of the most common types of violations include:
○ Breach of Confidentiality - Theft of private or confidential
information, such as credit-card numbers, trade secrets, patents,
secret formulas, manufacturing procedures, medical information,
financial information, etc.
○ Breach of Integrity - Unauthorized modification of data, which
may have serious indirect consequences. For example a popular
game or other program's source code could be modified to open
up security holes on users systems before being released to the
public.
○ Breach of Availability - Unauthorized destruction of data, often
just for the "fun" of causing havoc and for bragging rites.
Vandalism of web sites is a common form of this violation.
○ Theft of Service - Unauthorized use of resources, such as theft
of CPU cycles, installation of daemons running an unauthorized
file server, or tapping into the target's telephone or networking
services.
○ Denial of Service, DOS - Preventing legitimate users from using
the system, often by overloading and overwhelming the system
with an excess of requests for service.
Cryptography

Cryptography is a method of protecting information and communications


through the use of codes so that only those for whom the information is
intended can read and process it. The pre-fix "crypt" means "hidden" or
"vault" and the suffix "graphy" stands for "writing."

Modern cryptography concerns itself with the following four objectives:

1. Confidentiality: the information cannot be understood by anyone


for whom it was unintended
2. Integrity: the information cannot be altered in storage or transit
between sender and intended receiver without the alteration being
detected
3. Non-repudiation: the creator/sender of the information cannot deny
at a later stage his or her intentions in the creation or transmission
of the information
4. Authentication: the sender and receiver can confirm each other's
identity and the origin/destination of the information

Symmetric Encryption

There are various types of algorithms for encryption, some common algorithms include:

● Secret Key Cryptography (SKC): Here only one key is used for both encryption and
decryption. This type of encryption is also referred to as symmetric encryption.
● Public Key Cryptography (PKC): Here two keys are used. This type of encryption is
also called asymmetric encryption. One key is the public key that anyone can
access. The other key is the private key, and only the owner can access it. The
sender encrypts the information using the receiver’s public key. The receiver
decrypts the message using his/her private key. For nonrepudiation, the sender
encrypts plain text using a private key, while the receiver uses the sender’s public
key to decrypt it. Thus, the receiver knows who sent it.
● Hash Functions: These are different from SKC and PKC. They use no key and are
also called one-way encryption. Hash functions are mainly used to ensure that a
file has remained unchanged.

● Symmetric cipher models also called as secret key cryptography or


symmetric key cryptography.
● In symmetric key cryptography a single key is used for encryption as
well as decryption.
● As shown in image sender encrypt plain text using shared secret
key and the resultant cipher text is transmitted through
communication medium such as Internet, at the receiver side the
cipher text is decrypted using same decryption key to obtain original
plain text.
● Note that encryption and decryption process uses well known
symmetric key algorithm called as Data Encryption Standard (DES).

● Mathematically it is represented as P = D (K, E(P)).Where E (P) =


Encryption of plain text, D (K, E(P)) = Decryption of Plain text using
shared key K.
● For Example : Stream and block cipher, Data Encryption Standard
(DES), Advance Encryption Standard (AES) and BLOFISH.
● Here the possibility is that if an attacker/opponent got cipher text ??
he/she may apply different permutations and combinations to
decrypt and obtain the original plain text. Here the main aim of
cryptography is come into picture. Always sender has to think on
applying different encoding technique on plain text message and
convert it into cipher text message so that attacker cannot read the
actual plain text easily.

Advantages of Symmetric Encryption

● Symmetric key is faster than asymmetric key cryptography


● Because of single key data cannot decrypt easily at receiver side
even if it is intercepted by attacker
● As the same key is used for encryption and decryption, receiver
must have the senders otherwise he cannot decrypt (without sender
permission).
● Symmetric key achieves the authentication principle because it
checks receiver’s identity.
● DES and AES techniques are implemented using symmetric key
cryptography.
● System resources are less utilized in symmetric key cryptography

Disadvantages of Symmetric Encryption


● Once the key is stolen while transmitting data between sender and
receiver it is very easy to decrypt the message as same key is used
for encryption and decryption
● symmetric key cryptography, key is transmitted first and then
message is transfer to the receiver. If attacker intercept the
communication between sender and receiver, then he can decrypt
the message before it reaches to intended recipients

Asymmetric Encryption

● Asymmetric key cryptography is also called as public key


cryptography.
● In asymmetric key cryptography two keys are used, one for
encryption and other for decryption.
● As mentioned asymmetric key cryptography involves use of two
keys one is public key that may know to everyone and can be used
to encrypt messages, and verify signatures. Other is private key
known only to the receiver of the message or verifier, used to
decrypt messages, and sign (create) signatures.
● It is also called as asymmetric key cryptography because one key is
used for encryption only its corresponding key must be used for
decryption. No other key can decrypt the message.The sender and
receiver can encrypt messages using encryption key (public) or
verify signatures, he cannot decrypt messages or create signatures
because he required decryption key (private) which is known only to
the receiver of the message. Public key cryptosystem /asymmetric
key cryptography is shown in Image.

● Mathematically it is represented as P = D(Kd, E (Ke,P))


● For example, sender Ramesh wants to communicate with the
receiver Suresh then they must have each one of this i.e. private key
and public key then and then communication will be successful.

Advantages of Asymmetric Encryption

● Asymmetric key cryptography, key cannot be distributing among


sender and receiver as both have their own key, so there is no
problem of key distribution while transmitting the data over insecure
channel.
● The main advantage of asymmetric key cryptography is that two
separate keys are used for used encryption and decryption; even if
encryption key is stolen by attacker he/ she cannot decrypt the
message as decryption key is only available with receiver only.
● RSA algorithm and Diffie Hellman key exchange are implemented
using asymmetric key cryptography.
● Easy to use for user and scalable does not require much
administrative work.

Disadvantages of Asymmetric Encryption

● Because of different key used between sender and receiver require


more time to get the transmission done as compare to symmetric
key cryptography. (Slower that symmetric key cryptography very few
asymmetric encryption methods achieve the fast transmission of
data).
● Asymmetric key cryptography utilizes more resource as compare to
symmetric key cryptography.

Conclusion:
Being a complex and slow encryption technique the asymmetric encryption
is generally used for exchanging the keys and the symmetric encryption
being a faster technique is used for bulk data transmission.

OS User Authentication
User authentication process is used just to identify who the owner is or who
the identified person is.

In personal computer, generally, user authentication can be perform using


password.

When a computer user wants to log into a computer system, then the
installed operating system (OS) on that computer system generally wants to
determine or check who the user is. This process is called as user
authentication.

Sometime it is too important to authenticate the user because the computer


system may have some important documents of the owner.

Most methods of authenticating the computer users when they attempt or try
to log into the system are based on one of the following three principles:
● Something, the user knowns

● Something, the user has

● Something, the user is

That computer users who want to cause some trouble on any specific
computer system, have to first log into that computer systems, means getting
past whichever the authentication method or procedure is used. Those
computer users are called as hackers.

Basically, hacker is a term of honour that is reserved for or given to a great


computer programmer as normal computer user or programmer can't get
access into anyone's system without permission.

User can be authenticated through one of the following way:

● User authentication using password

● User authentication using physical object

● User authentication using biometric

● User authentication using countermeasures

Now let's describe briefly about all the above authentication process one by
one.

User Authentication using Password


User authentication using password is the most widely used form of
authenticating the user.

In this method of authenticating the user with password, it is to require that


the user who is going to authenticate has to type their login name or id and
login password.

Authenticating the user using their password is an easy method and also
easy to implement.

Keeping a central list of pairs is the simplest implementation of user


authentication using password method.

Here, in this method, the login name typed in is looked up in the list and
typed password is then compared to stored password.

Now, if both login and password match, then the login is allowed or the user
is successfully authenticated and approved to log into that system. And in
case if now match occurred, then the login error is detected.

How to Improve Password Security ?


Here are the list of four basic and common way to secure the password:

● Password should be minimum of eight characters

● Password should contain both uppercase and lowercase letters

● Password should contain at least one digit and one special characters

● Don't use dictionary words and known name such as stick, mouth, sun,

albert etc.

What is One Time Password (OTP) ?


One Time Password (OTP) is the most extreme form of changing the
password all the time.

One time password is a very safe way to implement.

When OTPs are used, the user get a book containing a list of many
passwords. Each login uses the next password in the list.

Therefore, if an intruder ever discover the password, then it will n't do any
good for him as the next time, a different password must be used.

User Authentication using Physical Object


User authentication using a physical object is a second way to authenticate
the user here.
Here, physical object may refer to Bank's Automated Teller Machine (ATM)
card or any other plastic card that is used to authenticate.

To authenticate the user, plastic card is inserted by the user into a reader
associated with the terminal or computer system.

Generally, the user must not only insert the card that is used as physical
object to authenticate him/her, but also type in a password just to prevent
someone from using a lost or stolen card.

User Authentication using Biometric


User authentication using biometric is the third authentication method here.

This method measures the physical characteristics of the user that are very
hard to forge. These are called as biometrics.

User authentication using biometric's example is a fingerprint, voiceprint, or


retina scan reader in the terminal could verify the identity of the user.

Basically, the typical biometric system has the following two parts:
● Enrolment

● Identification

Now, let's describe briefly about the above two parts of the biometric system.

Enrolment
In biometric system, during enrolment, characteristics of the user are
measured and the results digitized.

Then, significant features are extracted and stored in the record associated
with the user.

The record can be kept or stored in a central or main database or stored on


a smart card that the user carrier around and inserts into a remote reader,
for example, at an ATM machine.

Identification
In identification, the user shows up and provides a login name or id. Now,
again, the system makes the measurement.

Now, if the new values match the ones sampled at enrolment time, then the
login is accepted, otherwise the login attempt is rejected.
User Authentication using Countermeasure
User authentication using countermeasure method is used to make the
unauthorized access much harder.

For example, a company could have their policy that the employee working
in the Computer Science (CS) department are only allowed to log in from 10
A.M. to 4 P.M., Monday to Saturday, and then only from a machine in the CS
department connected to company's Local Area Network (LAN).

Now, any attempt to log in by a CS department employee at any wrong time


or from any wrong place would be treated or handled as an attempted break
in and log in failure.

9.4.8 Design Principles for Security

By now it should be clear that designing a secure operating system is not a


trivial matter. People have been working on this problem for decades without
much success. As far back as 1975, researchers have identified some
general principles that should be used as a guide to designing secure
systems (Saltzer and Schroeder, 1975). A brief summary of their ideas
(based on experience with MULTICS) is given below. These ideas are as
valid now as when they were first stated.
First, the system design should be public. Assuming that the intruders do not
know how the system works serves only to delude the designers. The
intruders will find out sooner or later, and if the protection is compromised by
this knowledge, the system is sunk.

Second, the default should be no access. Errors in which legitimate access


is refused will be reported much faster than errors in which unauthorized
access is allowed. When in doubt, say “No.”

Third, check for current authority. The system should not check for
permission, determine that access is permitted, and then squirrel away this
information for subsequent use. Many systems check for permission when a
file is opened, and not afterward. This means that a user who opens a file,
and keeps it open for weeks, will continue to have access, even if the owner
has long since changed the file protection or maybe even tried to delete the
file.

Fourth, give each process the least privilege possible. If an editor has only
the authority to access the file to be edited (specified when the editor is
invoked), editors with Trojan horses will not be able to do much damage.
This principle implies a fine-grained protection scheme. We will discuss such
schemes later in this chapter.
Fifth, the protection mechanism should be simple, uniform, and built into the
lowest layers of the system. Trying to retrofit security to an existing insecure
system is nearly impossible. Security, like correctness, is not an add-on
feature.

Sixth, the scheme chosen must be psychologically acceptable. If users feel


that protecting their files is too much work, they just will not do it.
Nevertheless, they will complain loudly if something goes wrong. Replies of
the form “It is your own fault” will generally not be well received.

To this list, we would like to add one other principle that has been gained by
decades of hard-won experience:

Keep the design simple

If the system is elegant and simple, was designed by a single architect, and
has a few guiding principles that determine the rest, it has a chance of being
secure. If the design is a mess, with no coherence and many fundamental
concessions to ancient insecure systems in the name of backward
compatibility, it is going to be a security nightmare. You can design a system
with many features (options, user-friendliness, etc.) but a system with many
features is a big system. And a big system is potentially an insecure system.
The more code there is, the more security holes and bugs there will be. From
a security perspective, the simplest design is the best design.
A file descriptor is a number that uniquely identifies an open
file in a computer's operating system. It describes a data
resource, and how that resource may be accessed.
1. Multiprogramming – A computer running more than one program at a time
(like running Excel and Firefox simultaneously).
2. Multiprocessing – A computer using more than one CPU at a time.
3. Multitasking – Tasks sharing a common resource (like 1 CPU).
4. Multithreading is an extension of multitasking.
5. Concurrent processing is a computing model in which multiple
processors execute instructions simultaneously for better performance.
6. Parallel processing- a mode of operation in which a process is split into parts,
which are executed simultaneously on different processors attached to the
same computer.

The kernel is the central module of an operating system (OS). It is the part of the operating system that
loads first, and it remains in main memory.

Direct memory access (DMA) is a method that allows an input/output (I/O)


device to send or receive data directly to or from the main memory,
bypassing the CPU to speed up memory operations. The process is
managed by a chip known as a DMA controller (DMAC).

Potrebbero piacerti anche