0% found this document useful (0 votes)
5 views88 pages

Os Notes

This document discusses CPU scheduling in operating systems, highlighting the importance of maximizing CPU utilization through multiprogramming. It explains two main types of scheduling: non-preemptive and preemptive, detailing their characteristics and differences. Additionally, it covers various scheduling algorithms such as First Come First Serve, Shortest Job First, Priority Scheduling, and Round Robin, along with their advantages and disadvantages.

Uploaded by

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

Os Notes

This document discusses CPU scheduling in operating systems, highlighting the importance of maximizing CPU utilization through multiprogramming. It explains two main types of scheduling: non-preemptive and preemptive, detailing their characteristics and differences. Additionally, it covers various scheduling algorithms such as First Come First Serve, Shortest Job First, Priority Scheduling, and Round Robin, along with their advantages and disadvantages.

Uploaded by

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

UNIT 3

2
CPU SCHEDULING
4 Basic Concepts
 In a single-processor system, only one process can run at a time. Others
must wait until the CPU is free and can be rescheduled.
 The objective of multiprogramming is to have some process running at all
times, to maximize CPU utilization. The idea is relatively simple.
 Several processes are kept in memory at one time. When one process has
to wait, the operating system takes the CPU away from that process and
gives the CPU to another process. This pattern continues.
 Every time one process has to wait, another process can take over use of
the CPU.
 Scheduling of this kind is a fundamental operating-system function.
 The CPU is, of course, one of the primary computer resources. Thus, its
scheduling is central to operating-system design.
5
CPU Scheduler

 Whenever the CPU becomes idle, the operating system must


select one of the processes in the ready queue to be
executed. The selection process is carried out by the short-
term scheduler, or CPU scheduler.
 The scheduler selects a process from the processes in
memory that are ready to execute and allocates the CPU to
that process.
6
 CPU-scheduling decisions may take place under the
following four circumstances:
1. When a process switches from the running state to the waiting
state.
2. When a process switches from the running state to the ready state
(for example, when an interrupt occurs).
3. When a process switches from the waiting state to the ready state
(for example, at completion of I/O)
4. When a process terminates
When scheduling takes place only under circumstances 1 and 4, we
say that the scheduling scheme is nonpreemptive. Otherwise, it is
preemptive.
7 Non-Preemptive Scheduling
 Non-preemptive Scheduling is used when a process terminates, or a
process switches from running to waiting state.
 Under non-preemptive scheduling, once the CPU has been allocated
to a process, the process keeps the CPU until it releases the CPU either
by terminating or by switching to the waiting state. i.e a process
running by CPU is not interrupted in middle of the execution. Instead,
it waits till the process complete and then it can allocate the CPU to
another process.
 This scheduling method is used by the Microsoft Windows 3.1 and by
the Apple Macintosh OS X operating systems.
 It is the only method that can be used on certain hardware platforms,
because It does not require the special hardware(for example: a
timer) needed for preemptive scheduling.
8 Preemptive Scheduling
 Preemptive scheduling is used when a process switches from
running state to ready state or from waiting state to ready state.
 The resources (mainly CPU cycles) are allocated to the process
for the limited amount of time and then is taken away, and the
process is again placed back in the ready queue if that process
still has CPU burst time remaining. That process stays in ready
queue till it gets next chance to execute.
 In this type of Scheduling, the tasks are usually assigned with
priorities. At times it is necessary to run a certain task that has a
higher priority before another task although it is running.
 Therefore, the running task is interrupted for some time and
resumed later when the priority task has finished its execution.
9 Key Differences Between Preemptive and
Non-Preemptive Scheduling
 In preemptive scheduling the CPU is allocated to the processes for
the limited time whereas in Non-preemptive scheduling, the CPU is
allocated to the process till it terminates or switches to waiting state.
 The executing process in preemptive scheduling is interrupted in the
middle of execution when higher priority one comes whereas, the
executing process in non-preemptive scheduling is not interrupted in
the middle of execution and wait till its execution.

 In Preemptive Scheduling, there is the overhead of switching the


process from ready state to running state, vise-verse, and
maintaining the ready queue. Whereas in case of non-preemptive
scheduling has no overhead of switching the process from running
state to ready state.
10 Dispatcher

 The dispatcher is the module that gives control of the CPU to


the process selected by the short-term scheduler. This
function involves the following:
 Switching context
 Switching to user mode
 Jumping to the proper location in the user program to restart that
program
 The dispatcher should be as fast as possible, since it is invoked
during every process switch.
 The time it takes for the dispatcher to stop one process and
start another running is known as the dispatch latency.
11 Scheduling Criteria
 CPU utilization. We want to keep the CPU as busy as possible.
Conceptually, CPU utilization can range from 0 to 100 percent. In a real
system, it should range from 40 percent (for a lightly loaded system) to 90
percent (for a heavily loaded system).
 Throughput. The number of processes that are completed per time unit is
called throughput. For long processes, this rate may be one process per
hour; for short transactions, it may be ten processes per second.
 Turnaround time. From the point of view of a particular process, the
important criterion is how long it takes to execute that process. The
interval from the time of submission of a process to the time of completion
is the turnaround time. Turnaround time is the sum of the periods spent
waiting to get into memory, waiting in the ready queue, executing on the
CPU, and doing I/O.
12
 Waiting time. Waiting time is the sum of the periods spent
waiting in the ready queue.
 Response time. Another measure is the time from the
submission of a request until the first response is produced.
This measure, called response time, is the time it takes to
start responding, not the time it takes to output the
response.
 It is desirable to maximize CPU utilization and throughput
and to minimize turnaround time, waiting time, and
response time.
13 Scheduling Algorithms
To decide which process to execute first and which process to execute
last to achieve maximum CPU utilization, different algorithms are
proposed, they are:
 First Come First Serve(FCFS) Scheduling
 Shortest-Job-First(SJF) Scheduling
 Priority Scheduling
 Round Robin(RR) Scheduling
 Multilevel Queue Scheduling
 Multilevel Feedback Queue Scheduling
14 First Come First Serve Scheduling
 The simplest CPU-scheduling algorithm is the first-come, first-served (FCFS)
scheduling algorithm.
 In this scheduling algorithm, the process which arrives first, gets executed first,
or we can say that the process which requests the CPU first, gets the CPU
allocated first.
 First Come First Serve, is just like FIFO(First in First out) Queue data structure,
where the data element which is added to the queue first, is the one who
leaves the queue first.
 It's easy to understand and implement programmatically, using a Queue data
structure, where a new process enters through the tail of the queue, and the
scheduler selects process from the head of the queue.
 On the negative side, the average waiting time under the FCFS policy is often
quite long. Average waiting time is the average of the waiting times of the
processes in the queue, waiting for the scheduler to pick them for execution.
15  Consider the following set of processes that arrive at time 0,
with the length of the CPU burst given in milliseconds:
Process Burst Time
P1 24
P2 3
P3 3
 Suppose that the processes arrive in the order: P1 , P2 , P3 we
get the result shown in the following Gantt chart, which is a
bar chart that illustrates a particular schedule, including the
start and finish times of each of the participating processes

P1 P2 P3
0 24 27 30

 Waiting time for P1 = 0; P2 = 24; P3 = 27


 Average waiting time: (0 + 24 + 27)/3 = 17
16 Suppose that the processes arrive in the order:
P2 , P3 , P1
 The Gantt chart for the schedule is:

P2 P3 P1
0 3 6 30

 Waiting time for P1 = 6; P2 = 0; P3 = 3


 Average waiting time: (6 + 0 + 3)/3 = 3, much better than previous
case
 Convoy effect - short process behind long process
There is a convoy effect as the other processes wait for the big
process to get off the CPU. This effect results in lower CPU and
device utilization.
FCFS scheduling algorithm is non-preemptive. Once the CPU has
been allocated to a process, that process keeps the CPU until it
releases the CPU, either by terminating or by requesting I/O.
Shortest-Job-First(SJF) Scheduling
17  This algorithm associates with each process the length of the process’s next CPU
burst.
 When the CPU is available, it is assigned to the CPU Scheduling process that has
the smallest next CPU burst. If the next CPU bursts of two processes are the same,
FCFS scheduling is used to break the tie.
As an example of SJF scheduling, consider the following set of processes, with the
length of the CPU burst given in milliseconds:
ProcessArriva l Time Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
 SJF scheduling chart

P4 P1 P3 P2
0 3 9 16 24

 Average waiting time = (3 + 16 + 9 + 0) / 4 = 7


18  The SJF algorithm can be either preemptive or non-
preemptive.
 The choice arises when a new process arrives at the
ready queue while a previous process is still executing.
 The next CPU burst of the newly arrived process may be
shorter than what is left of the currently executing process.
 A preemptive SJF algorithm will preempt the currently
executing process, whereas a non-preemptive SJF
algorithm will allow the currently running process to finish
its CPU burst.
 SJF is optimal – gives minimum average waiting time for a
given set of processes
The difficulty is knowing the length of the next CPU
request
19 Pre-emptive SJF
 Now we add the concepts of varying arrival times and preemption to the analysis
ProcessA arri Arrival TimeT Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
 Preemptive SJF Gantt Chart
P1 P2 P4 P1 P3
0 1 5 10 17 26

 Process P1 is started at time 0, since it is the only process in the queue. Process P2
arrives at time 1. The remaining time for process P1 (7 milliseconds) is larger than
the time required by process P2 (4 milliseconds), so process P1 is preempted, and
process P2 is scheduled.
 Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4 = 26/4 = 6.5 msec
Priority Scheduling
20
 A priority is associated with each process, and the CPU is allocated to the
process with the highest priority. Equal-priority processes are scheduled in FCFS
order. Priorities are generally indicated by some fixed range of numbers
 As an example, consider the following set of processes, assumed to have arrived
at time 0 in the order P1, P2, ···, P5, with the length of the CPU burst given in
milliseconds:
ProcessA arri Burst TimeT Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
 Priority scheduling Gantt Chart

 Average waiting time = 6 + 16 + 18 + 1 = 8.2 msec


 Priority scheduling can be either preemptive or non-preemptive. When a process
arrives at the ready queue, its priority is compared with the priority of the currently
21 running process.
 Preemptive Priority Scheduling: If the new process arrived at the ready queue has
a higher priority than the currently running process, the CPU is preempted, which
means the processing of the current process is stopped and the incoming new
process with higher priority gets the CPU for its execution.
 Non-Preemptive Priority Scheduling: In case of non-preemptive priority scheduling
algorithm if a new process arrives with a higher priority than the current running
process, the incoming process is put at the head of the ready queue, which
means after the execution of the current process it will be processed.

Starvation
 A major problem with priority scheduling algorithms is indefinite blocking, or
starvation.
 A priority scheduling algorithm can leave some low priority processes waiting
indefinitely. In a heavily loaded computer system, a steady stream of higher-priority
processes can prevent a low-priority process from ever getting the CPU. This is
known as starvation.
 A solution to the problem of indefinite blockage of low-priority processes is aging.
Aging involves gradually increasing the priority of processes that wait in the system
for a long time.
22 Round Robin(RR) Scheduling
 The round-robin (RR) scheduling algorithm is designed especially for
timesharing systems. It is similar to FCFS scheduling, but preemption is added
to enable the system to switch between processes.
 A small unit of time, called a time quantum or time slice, is defined. A time
quantum is generally from 10 to 100 milliseconds in length. The CPU
scheduler goes around the ready queue, allocating the CPU to each
process for a time interval of up to 1 time quantum.
 To implement RR scheduling, new processes are added to the tail of the
ready queue. The CPU scheduler picks the first process from the ready
queue, sets a timer to interrupt after 1 time quantum, and dispatches the
process.
 After 1 time quantum, the timer will go off and will cause an interrupt to the
operating system. The process will be put at the tail of the ready queue. The
CPU scheduler will then select the next process in the ready queue.
23 Example of RR with Time Quantum = 4
The average waiting time under the RR policy is often long. Consider the
following set of processes that arrive at time 0, with the length of the CPU
burst given in milliseconds:
Process Burst Time
P1 24
P2 3
P3 3
• The Gantt chart is:

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

• Typically, higher average turnaround than SJF, but better response time.
• The average waiting time for this schedule - P1 waits for 6 milliseconds (10 - 4),
P2 waits for 4 milliseconds, and P3 waits for 7 milliseconds. Thus, the average
waiting time is 17/3 = 5.66 milliseconds.
Multilevel Queue Scheduling
24
 Another class of scheduling algorithms that has been created for
situations in which processes are easily classified into different groups.
 For example: A common division is made between foreground(or
interactive) processes and background (or batch) processes. These two
types of processes have different response-time requirements, and so
might have different scheduling needs. In addition, foreground
processes may have priority over background processes.
 A multi-level queue scheduling algorithm partitions the ready queue
into several separate queues. The processes are permanently assigned
to one queue, generally based on some property of the process, such
as memory size, process priority, or process type. Each queue has its
own scheduling algorithm.
 For example: separate queues might be used for foreground and
background processes. The foreground queue might be scheduled by
Round Robin algorithm, while the background queue is scheduled by
an FCFS algorithm. Scheduling must be done between the queues also.
Consider an example of a multilevel queue-scheduling algorithm with
25 five queues:
 System Processes
 Interactive Processes
 Interactive Editing Processes
 Batch Processes
 Student Processes
 Each queue has absolute priority over lower-priority queues. No
process in the batch queue, for example, could run unless the
queues for system processes, interactive processes, and interactive
editing processes were all empty. If an interactive editing process
entered the ready queue while a batch process was running, the
batch process will be preempted.
26 Multilevel Feedback Queue Scheduling
 When the multilevel queue scheduling algorithm is used,
processes are permanently assigned to a queue when they
enter the system. processes do not move from one queue to
the other, since processes do not change their foreground or
background nature.
 The multilevel feedback queue scheduling algorithm, in
contrast, allows a process to move 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 will be moved to a
lower-priority queue. Similarly, a process that waits too long in
a lower-priority queue may be moved to a higher-priority
queue. This form of aging prevents starvation.
Example of Multilevel Feedback
27 Queue
Three queues:
Q0 – RR with time quantum 8 milliseconds
Q1 – RR time quantum 16 milliseconds
Q2 – FCFS

Scheduling
A new job enters queue Q0 which is served
FCFS. When it gains CPU, job receives 8
milliseconds. If it does not finish in 8
milliseconds, job is moved to queue Q1.
At Q1 job is again served FCFS and
receives 16 additional milliseconds. If it still
does not complete, it is preempted and
moved to queue Q2.
PROCESS SYNCHRONIZATION
29
 Processes are categorized as one of the following two types:
 Independent Process: The execution of one process does not affect
the execution of other processes.
 Cooperating Process: A process that can affect or be affected by
other processes executing in the system.
 Cooperating processes can directly share a logical address space.
 Concurrent access to shared data may result in data
inconsistency.
 Processes Synchronization is the way by which processes that share
the same memory space are managed in an operating system.
 It helps maintain the consistency of data by using variables or
hardware so that only one process can make changes to the
shared memory at a time.
30 The Critical-Section Problem
 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.
 When one process is executing in its critical section, no other process is
allowed to execute in its critical section. 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.
31

Fig: General structure of a process


32
 A solution to the critical-section problem must satisfy the
following three requirements:
1. Mutual exclusion. If process Pi is executing in its critical section, then
no other processes can be executing in their critical sections.
2. Progress. If no process is executing in its critical section and some
processes wish to enter their critical sections, then only those
processes that are not executing in their remainder sections can
participate in deciding which will enter its critical section next.
3. Bounded waiting. There exists a bound, or limit, on the number of
times that other processes are allowed to enter their critical sections
after a process has made a request to enter its critical section and
before that request is granted.
33 Peterson’s Solution
 Peterson’s solution is a classic software-based solution to the
critical-section problem.
 Peterson’s solution is restricted to two processes that alternate
execution between their critical sections and remainder
sections. The processes are named Pi and Pj.
 Peterson’s Solution preserves all three conditions :
 Mutual Exclusion is assured as only one process can access the
critical section at any time.
 Progress is also assured, as a process outside the critical section
does not block other processes from entering the critical section.
 Bounded Waiting is preserved as every process gets a fair
chance.
34
 Peterson’s solution requires the two
processes to share two data items:
int turn; boolean flag[2];
 The variable turn indicates whose
turn it is to enter its critical section.
That is, if turn == i, then process Pi is
allowed to execute in its critical
section.
 The flag array is used to indicate if a
process is ready to enter its critical
section. For example, if flag[i] is
true, this value indicates that Pi is
Structure of a process in critical section
ready to enter its critical section.
35
• To enter the critical section, process Pi first sets flag[i] to be
true and then sets turn to the value j, thereby asserting that if
the other process wishes to enter the critical section, it can
do so.
• If both processes try to enter at the same time, turn will be
set to both i and j at roughly the same time.
• Only one of these assignments will last; the other will occur
but will be overwritten immediately.
• The eventual value of turn determines which of the two
processes is allowed to enter its critical section first.
36 Synchronization Hardware
 There are some simple hardware instructions that are available
on many systems and can be used effectively in solving the
critical-section problem.
 Hardware-based solutions to critical-section problem are based
on the premise of locking —that is, protecting critical regions
through the use of locks.
 Many modern computer systems therefore provide special
hardware instructions that allow us either to test and modify the
content of a word or to swap the contents of two words
atomically. The hardware solution is as follows:
1. Test and Set
2. Compare and Swap
37

Solution to critical-section problem through locks


38 Test and Set
 Test and set algorithm uses a Boolean variable 'lock' which is initialized to
false. This lock variable determines the entry of the process inside the
critical section of the code.
39  As one process is inside the critical section and lock value is now
'true', if any other process tries to enter the critical section then
the new process checks for while(TestAndSet(true)) which will
return true inside while loop and as a result the other process
keeps executing the while loop.
 As no queue is maintained for the processes stuck in the while
loop, bounded waiting is not ensured.
 If a process waits for a set amount of time before entering the
critical section, it is said to be a bounded waiting condition.
 In test and set algorithm the incoming process trying to enter the
critical section does not wait in a queue so any process may get
the chance to enter the critical section as soon as the process
finds the lock variable to be false. It may be possible that a
particular process never gets the chance to enter the critical
section and that process waits indefinitely.
40
Compare and Swap
 The compare and swap() operates on three operands: value,
expected, new_value.
 The operand value is set to new value only if the expression (*value
== expected) is true.
41

 Mutual exclusion can be provided as follows: a global variable


(lock) is declared and is initialized to 0.
 The first process that invokes compare and swap() will set lock to
1. It will then enter its critical section, because the original value
of lock was equal to the expected value of 0.
 Subsequent calls to compare and swap() will not succeed,
because lock now is not equal to the expected value of 0.
 When a process exits its critical section, it sets lock back to 0,
which allows another process to enter its critical section.
42
Classical Problems of Synchronization

 The Readers-Writers Problem


 The Dining-Philosophers Problem
43 The Readers-Writers Problem
 Suppose that a database is to be shared among several concurrent
processes. Some of these processes may want only to read the database,
whereas others may want to update (that is, to read and write) the
database.
 We distinguish between these two types of processes by referring to the
former as readers and to the latter as writers.
 If two readers access the shared data simultaneously, no adverse effects
will result.
 However, if a writer and some other process (either a reader or a writer)
access the database simultaneously, problems may occur.
 To ensure that these difficulties do not arise, we require that the writers have
exclusive access to the shared database while writing to the database. This
synchronization problem is referred to as the readers–writers problem.
44

 To solve this situation, a writer should get exclusive access


to an object i.e. when a writer is accessing the object, no
reader or writer may access it. However, multiple readers
can access the object at the same time.
 This can be implemented using semaphores.
 In the solution to the first readers–writers problem, the
reader processes share the following data structures:
 semaphore rw_mutex = 1; //writing mechanism common to reader
and writer
 semaphore mutex = 1; //to ensure mutual exclusion
 int read_count = 0; //no. of readers accessing the database
45

 The structure of a writer process

 If a writer wants to access the object, wait operation is performed


on rw_mutex. After that no other writer can access the object.
When a writer is done writing into the object, signal operation is
performed on rw_mutex.
46

 The structure of a reader process,


47
 In the above code, mutex and rw_mutex are semaphores that are
initialized to 1. Also, read_count is a variable that is initialized to 0.
 The mutex semaphore ensures mutual exclusion and rw_mutex
handles the writing mechanism and is common to the reader and
writer process code.
 The variable read_count denotes the number of readers
accessing the object.
 As soon as read_count becomes 1, wait operation is used on
rw_mutex. This means that a writer cannot access the object
anymore.
 After the read operation is done, read_count is decremented.
When read_count becomes 0, signal operation is used on
rw_mutex. So a writer can access the object now.
48 The Dining-Philosophers Problem
49
 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).
 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 the chopsticks.
 When she is finished eating, she puts down both chopsticks and starts thinking
again.
50
 The dining-philosophers problem is considered a classic
synchronization problem because it is an example of a large class
of concurrency-control problems.
 It is a simple representation of the need to allocate several
resources among several processes in a deadlock-free and
starvation-free manner.
 One simple solution is to represent each chopstick with a
semaphore.
 A philosopher tries to grab a chopstick by executing a wait()
operation on that semaphore.
 She releases her chopsticks by executing the signal() operation on
the appropriate semaphores.
51
 Thus, the shared data are,
 semaphore chopstick[5]; where all the elements of chopstick are
initialized to 1.
 The structure of a philosopher i,
 Although this solution guarantees that no two neighbors are eating
52 simultaneously, but it could create a deadlock.
 Suppose that all five philosophers become hungry at the same time
and each grabs her left chopstick.
 All the elements of chopstick will now be equal to 0. When each
philosopher tries to grab her right chopstick, she will be delayed
forever.
 Several possible remedies to the deadlock problem are replaced
by:
 Allow at most four philosophers to be sitting simultaneously at the
table.
 Allow a philosopher to pick up her chopsticks only if both chopsticks
are available.
 Use an asymmetric solution—that is, an odd-numbered philosopher
picks up first her left chopstick and then her right chopstick, whereas
an even numbered philosopher picks up her right chopstick and
then her left chopstick.
FILE AND DATABASE
SYSTEM
54
File Systems
 The file system is the most visible aspect of an operating
system.
 It provides the mechanism for 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.
55
 The file system permits users to create data collections, called
files, with desirable properties, such as:
 Long-term existence: Files are stored on disk or other secondary storage
and do not disappear when a user logs off.
 Sharable between processes: Files have names and can have
associated access permissions that permit controlled sharing.
 Structure: Depending on the file system, a file can have an internal
structure that is convenient for particular applications. In addition, files
can be organized into hierarchical or more complex structure to reflect
the relationships among files.
56 File Concept
 A file is a named collection of related information that is recorded on
secondary storage. Data cannot be written to the secondary storage
unless they are within a file.
 Commonly, files represent programs (both source and object forms) and
data. Data files may be numeric, alphabetic, alphanumeric, or binary.
 A file is a sequence of bits, bytes, lines, or records, the meaning of which
is defined by the file’s creator and user.
 A file has a certain structure, which depends on its type.
 A text file is a sequence of characters organized into lines. A source file is
a sequence of functions, each of which is further organized as
declarations followed by executable statements. An executable file is a
series of code sections that the loader can bring into memory and
execute.
File Attributes
57 A file’s attributes vary from one operating system to another but typically consist of
these:
 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 systems that support different types of files.
 Location. This information is a pointer to a device and to the location of the file
on that device.
 Size. The current size of the file (in bytes, words, or blocks) and possibly the
maximum allowed size 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,
last modification, and last use. These data can be useful for protection,
security, and usage monitoring.
58 File Types
 When we design a file system, the OS should be able to recognize and
support file types.
 A common technique for implementing file types is to include the type as
part of the file name.
 The system uses the extension to indicate the type of the file and the type
of operations that can be done on that file.
 Only a file with a .com, .exe, or .sh extension can be executed, for
instance. The .com and .exe files are two forms of binary executable files,
whereas the .sh file is a shell script containing commands to the operating
system.
 Application programs also use extensions to indicate file types in which
they are interested. For example, Java compilers expect source files to
have a .java extension, and the Microsoft Word word processor expects its
files to end with a .doc or .docx extension.
59

Common file types


60
File Operations
Typical operations include the following:
 Create: A new file is defined and positioned within the structure of files.
 Delete: A file is removed from the file structure and destroyed.
 Open: An existing file is declared to be “opened” by a process, allowing
the process to perform functions on the file.
 Close: The file is closed with respect to a process, so that the process no
longer may perform functions on the file, until the process opens the file
again.
 Read: A process reads all or a portion of the data in a file.
 Write: A process updates a file, either by adding new data that expands
the size of the file or by changing the values of existing data items in the
file.
61
File Structure
 Four terms are in common use when discussing files:
 Field
 Record
 File
 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 (e.g., ASCII string, decimal).
62
 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. Records may be of fixed
length or variable length.
 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.
Files have file names and may be created and deleted. Access
control restrictions usually apply at the file level.
 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.
63
File Access
 The information in the file can be accessed in several ways.
 Some systems provide only one access method for files while
others support many access methods, and choosing the right
one for a particular application is a major design problem.
 There are three ways to access a file into a computer system:
 Sequential-Access
 Direct Access
 Index sequential Method
64 Sequential Access
 It is the simplest access method.
 Information in the file is processed in order, one record after the
other.
 This mode of access is by far the most common; for example, editor
and compiler usually access the file in this fashion.
 Read and write make up the bulk of the operation on a file.
 A read operation- read_next()- read the next position of the file and
automatically advance a file pointer, which keeps track I/O location.
 Similarly, for the write- write_next() append to the end of the file and
advance to the newly written material.
65
Direct Access
 Another method is direct access (or relative access).
 Here, a file is made up of fixed-length logical records that allow
programs to read and write records rapidly in any 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. Thus, we may read block 14, then read block 53,
and then write block 7. There are no restrictions on the order of
reading or writing for a direct-access file.
 Direct-access files are of great use for immediate access to large
amounts of information.
66
 Databases are often of this type. When a query concerning a
particular subject arrives, we compute which block contains the
answer and then read that block directly to provide the desired
information.
 The block number provided by the user to the operating system
is normally a relative block number.
 A relative block number is an index relative to the beginning of
the file. Thus, the first relative block of the file is 0, the next is 1,
and so on, even though the absolute disk address may be 14703
for the first block and 3192 for the second.
67
Index Sequential Method
 This mechanism is built up on base of sequential access.
 This method construct an index for the file.
 The index, like an index in a book, contains the pointer to the
various blocks.
 To find a record in the file, we first search the index, and then by the
help of pointer we access the file directly.
 Index is searched sequentially and its pointer is used to access the
file directly.
68
File Allocation
 With direct-access nature of the disk, many files are stored on the
same disk.
 The main problem is how to allocate space to these files so that
disk space is utilized effectively and files can be accessed quickly.
 Three major methods of allocating disk space are in wide use:
 Contiguous allocation
 Linked allocation
 Indexed allocation
69
Contiguous Allocation
 In this scheme, each file occupies a contiguous set of blocks on the
disk.
 Contiguous allocation of a file is defined by the disk address and
length (in block units) of the first block.
 For example, if a file requires n blocks and is given a block b as the
starting location, then the blocks assigned to the file will be: b, b+1,
b+2,……b+n-1.
 This means that given the starting block address and the length of
the file (in terms of blocks required), we can determine the blocks
occupied by the file.
 The directory entry for each file indicates the address of the starting
block and the length of the area allocated for this file
70
71

Advantages:
 Accessing a file that has been allocated contiguously is easy.
 Both the Sequential and Direct Accesses are supported by
this. For sequential access, the file system remembers the disk
address of the last block referenced and, when necessary,
reads the next block. For direct access, the address of the ith
block of the file which starts at block b can easily be
obtained as (b+i).
 This is extremely fast since the number of seeks are minimal
because of contiguous allocation of file blocks.
72

Disadvantages:
 This method suffers from external fragmentation. This makes it
inefficient in terms of memory utilization.
 Another problem with contiguous allocation is determining how
much space is needed for a file. When the file is created, the
total amount of space it will need must be found and
allocated. Increasing file size is difficult because it depends on
the availability of contiguous memory at a particular instance.
73
Linked Allocation
 With linked allocation, each file is a
linked list of disk blocks; the disk blocks
may be scattered anywhere on the
disk.
 The directory contains a pointer to the
first and last blocks of the file.
 For example, a file of five blocks might
start at block 9 and continue at block
16, then block 1, then block 10, and
finally block 25. Each block contains a
pointer to the next block. These pointers
are not made available to the user.
74
Advantages:
 To create a new file, we simply create a new entry in the
directory. With linked allocation, each directory entry has a
pointer to the first disk block of the file. This pointer is initialized to
null to signify an empty file.
 A write to the file causes the free-space management system to
find a free block, and this new block is written to and is linked to
the end of the file. To read a file, we simply read blocks by
following the pointers from block to block.
 There is no external fragmentation with linked allocation, and any
free block on the free-space list can be used to satisfy a request.
 The size of a file need not be declared when the file is created. A
file can continue to grow as long as free blocks are available.
75

Disadvantages:
 Because the file blocks are distributed randomly on the disk, a
large number of seeks are needed to access every block
individually. This makes linked allocation slower.
 It can be used effectively only for sequential-access files. To
find the ith block of a file, we must start at the beginning of that
file and follow the pointers until we get to the ith block.
 Pointers required in the linked allocation incur some extra
overhead.
 The files are linked together by pointers scattered all over the
disk, and problem arises if a pointer were lost or damaged.
76
 An important variation on linked allocation is the use of a file-
allocation table (FAT).
 The table has one entry for each disk block and is indexed by block
number.
 The directory entry contains the block number of the first block of the
file.
 The table entry indexed by that block number contains the block
number of the next block in the file. This chain continues until it
reaches the last block, which has a special end-of-file value as the
table entry.
 An unused block is indicated by a table value of 0. Allocating a new
block to a file is a simple matter of finding the first 0-valued table
entry and replacing the previous end-of-file value with the address of
the new block. The 0 is then replaced with the end-of-file value.
77
 An illustrative example is the FAT structure for a file consisting of disk blocks 217,
618, and 339.
78
Indexed Allocation
 Linked allocation solves the external-fragmentation and size-
declaration problems of contiguous allocation.
 However, in the absence of a FAT, linked allocation cannot
support efficient direct access, since the pointers to the blocks
are scattered with the blocks themselves all over the disk and
must be retrieved in order.
 Indexed allocation solves this problem by bringing all the pointers
together into one location: the index block.
 In this scheme, a special block known as the Index
block contains the pointers to all the blocks occupied by a file.
79
 Each file has its own index block.
 The ith entry in the index block contains the disk address of the ith file
block. The directory entry contains the address of the index block. To find
and read the ith block, we use the pointer in the ith index-block entry.
80 Free Space Management
 Since disk space is limited, we need to reuse the space from
deleted files for new files,
 To keep track of free disk space, the system maintains a free-
space list. The free-space list records all free disk blocks.
 To create a file, we search the free-space list for the required
amount of space and allocate that space to the new file. This
space is then removed from the free-space list. When a file is
deleted, its disk space is added to the free-space list.
 The process of looking after and managing the free
blocks of the disk is called free space management.
81

 There are some methods or techniques to implement a


free space list. These are as follows:
 Bitmap
 Linked list
 Grouping
 Counting
82
Bit Map
 The free-space list can be implemented as a bit map or bit vector.
 Each block is represented by 1 bit. If the block is free, the bit is 1; if
the block is allocated, the bit is 0.
 For example, consider a disk where blocks 2, 3, 4, 5, 8, 9, 10, 11, 12,
13, 17, 18, 25, 26, and 27 are free and the rest of the blocks are
allocated.
 The free-space bit map would be,
001111001111110001100000011100000 ...
83

Advantages –
 Simple to understand.
 Finding the first free block is efficient. It requires scanning the words (a group
of 8 bits) in a bitmap for a non-zero word. (A 0-valued word has all bits 0). The
first free block is then found by scanning for the first 1 bit in the non-zero word.
 The block number can be calculated as:
(number of bits per word) *(number of 0-values words) + offset of bit first bit 1
in the non-zero word .
84 Linked List
 Another approach to free-space management is to link together
all the free disk blocks, keeping a pointer to the first free block in a
special location on the disk and caching it in memory.
 This first block contains a pointer to the next free disk block, and so
on.
 Consider the example in which blocks 2, 3, 4, 5, 8, 9, 10, 11, 12, 13,
17, 18, 25, 26, and 27 were free and the rest of the blocks were
allocated.
 In this situation, we would keep a pointer to block 2 as the first free
block. Block 2 would contain a pointer to block 3, which would
point to block 4, which would point to block 5, which would point
to block 8, and so on
 This scheme is not efficient; to traverse the list, we must read each
block, which requires substantial I/O time.
85
86
Grouping
 This approach stores the address of the free blocks in the first free
block.
 The first free block stores the address of some, say n free blocks.
Out of these n blocks, the first n-1 blocks are actually free and the
last block contains the address of next free n blocks.
 An advantage of this approach is that the addresses of a group of
free disk blocks can be found easily.
87
Counting

 This approach takes advantage of the fact that, generally,


several contiguous blocks may be allocated or freed
simultaneously.
 Thus, rather than keeping a list of n free disk addresses, we can
keep the address of the first free block and the number (n) of free
contiguous blocks that follow the first block.
 Each entry in the free-space list then consists of a disk address
and a count.
THANK YOU

You might also like