Operating System
Operating System
Functions of OS
1. Protection –
Protection refers to a mechanism which controls the access of programs,
processes, or users to the resources defined by a computer system. We can
take protection as a helper to multi programming operating system, so that
many users might safely share a common logical name space such as
directory or files.
3. Command interpretation –
A command interpreter is a program which reads the instructions given by
the user. It then translates these instructions into the context of the
operating system followed by the execution. Command interpreter is also
known as ‘shell’
4. Error detecting aids –
Operating system constantly monitors the system to detect errors and avoid
the malfunctioning of computer system.
6. Memory Management –
The operating system manages the Primary Memory or Main Memory. Main
memory is made up of a large array of bytes or words where each byte or
word is assigned a certain address. Main memory is a fast storage and it can
be accessed directly by the CPU. For a program to be executed, it should be
first loaded in the main memory. An Operating System performs the
following activities for memory management:
It keeps tracks of primary memory, i.e., which bytes of memory are used by
which user program. The memory addresses that have already been allocated
and the memory addresses of the memory that has not yet been used. In
multi programming, the OS decides the order in which process are granted
access to memory, and for how long. It Allocates the memory to a process
when the process requests it and deallocates the memory when the process
has terminated or is performing an I/O operation.
7. Processor Management –
In a multi programming environment, the OS decides the order in which
processes have access to the processor, and how much processing time each
process has. This function of OS is called process scheduling. An Operating
System performs the following activities for processor management.
Keeps tracks of the status of processes. The program which perform this
task is known as traffic controller. Allocates the CPU that is processor to a
process. De-allocates processor when a process is no more required.
8. Device Management –
An OS manages device communication via their respective drivers. It
performs the following activities for device management. Keeps tracks of all
devices connected to system. designates a program responsible for every
device known as the Input/Output controller. Decides which process gets
access to a certain device and for how long. Allocates devices in an effective
and efficient way. Deallocates devices when they are no longer required.
9. File Management –
A file system is organized into directories for efficient or easy navigation
and usage. These directories may contain other directories and other files.
An Operating System carries out the following file management activities. It
keeps track of where information is stored, user access settings and status
of every file and more… These facilities are collectively known as the file
system.
Examples of Batch
based Operating System: Payroll System, Bank Statements etc.
2. Time-Sharing Operating Systems –
Each task is given some time to execute, so that all the tasks work smoothly. Each
user gets time of CPU as they use single system. These systems are also known as
Multitasking Systems. The task can be from single user or from different users
also. The time that each task gets to execute is called quantum. After this time
interval is over OS switches over to next task.
Examples of Time-
Sharing OSs are: Multics, Unix etc.
Evolution of OS:
Process Management
Process 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.
Process Scheduling Queues
The OS maintains all PCBs in Process Scheduling Queues. The OS maintains a
separate queue for each of the process states and PCBs of all processes in the
same execution state are placed in the same queue. When the state of a process
is changed, its PCB is unlinked from its current queue and moved to its new state
queue.
The Operating System maintains the following important process scheduling
queues −
Job queue − This queue keeps all the processes in the system.
Ready queue − This queue keeps a set of all processes residing in main
memory, ready and waiting to execute. A new process is always put in this
queue.
Device queues − The processes which are blocked due to unavailability of an
I/O device constitute this queue.
The OS can use different policies to manage each queue (FIFO, Round Robin,
Priority, etc.). The OS scheduler determines how to move processes between the
ready and run queues which can only have one entry per processor core on the
system; in the above diagram, it has been merged with the CPU.
Schedulers
Schedulers are special system software which handle process scheduling in various
ways. 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.
The primary objective of the job scheduler is to provide a balanced mix of jobs,
such as I/O bound and processor bound. It also controls the degree of
multiprogramming. If the degree of multiprogramming is stable, then the average
rate of process creation must be equal to the average departure rate of
processes leaving the system.
On some systems, the long-term scheduler may not be available or minimal. Time-
sharing operating systems have no long term scheduler. When a process changes
the state from new to ready, then there is use of long-term scheduler.
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.
Short-term schedulers, also known as dispatchers, make the decision of which
process to execute next. Short-term schedulers are faster than long-term
schedulers.
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.
Context Switch
A context switch is the mechanism to store and restore the state or context of a
CPU in Process Control block so that a process execution can be resumed from the
same point at a later time. Using this technique, a context switcher enables
multiple processes to share a single CPU. Context switching is an essential part of
a multitasking operating system features.
When the scheduler switches the CPU from executing one process to execute
another, the state from the current running process is stored into the process
control block. After this, the state for the process to run next is loaded from its
own PCB and used to set the PC, registers, etc. At that point, the second process
can start executing.
Algorithm
A Process Scheduler schedules different processes to be assigned to the CPU
based on particular scheduling algorithms. There are six popular process
scheduling algorithms which we are going to discuss in this chapter −
First-Come, First-Served (FCFS) Scheduling
Shortest-Job-Next (SJN) Scheduling
Priority Scheduling
Shortest Remaining Time
Round Robin(RR) Scheduling
Multiple-Level Queues Scheduling
These algorithms are either non-preemptive or preemptive.
Non-preemptive algorithms are designed so that once a process enters the
running state, it cannot be preempted until it completes its allotted time.
P1 5-1=4
P2 8-2=6
P3 16 - 3 = 13
P0 0 5 0
P1 1 3 5
P2 2 8 14
P3 3 6 8
P0 0-0=0
P1 5-1=4
Average Wait Time: (0 + 4 + 12 + 5)/4 = 21 / 4 = 5.25
P2 14 - 2 = 12
P3 8-3=5
P0 0 5 1 0
P1 1 3 2 11
P2 2 8 1 14
P3 3 6 3 5
Waiting time of each process is as follows −
Process Waiting Time
P0 0-0=0
P1 11 - 1 = 10
P2 14 - 2 = 12
P3 5-3=2
P0 (0 - 0) + (12 - 3) = 9
P1 (3 - 1) = 2
P3 (9 - 3) + (17 - 12) = 11
Average Wait Time: (9+2+12+11) / 4 = 8.5
Multiple-Level Queues Scheduling
Multiple-level queues are not an independent scheduling algorithm. They make use
of other existing algorithms to group and schedule jobs with common
characteristics.
Multiple queues are maintained for processes with common characteristics.
Each queue can have its own scheduling algorithms.
Priorities are assigned to each queue.
For example, CPU-bound jobs can be scheduled in one queue and all I/O-bound
jobs in another queue. The Process Scheduler then alternately selects jobs from
each queue and assigns them to the CPU based on the algorithm assigned to the
queue.
Thread
A thread is a path of execution within a process. A process can contain multiple
threads.
Multithreading
A thread is also known as lightweight process. The idea is to achieve parallelism by
dividing a process into multiple threads. For example, in a browser, multiple tabs
can be different threads. MS Word uses multiple threads: one thread to format
the text, another thread to process inputs, etc.
A thread is a flow of execution through the process code, with its own program
counter that keeps track of which instruction to execute next, system registers
which hold its current working variables, and a stack which contains the execution
history.
A thread shares with its peer threads few information like code segment, data
segment and open files. When one thread alters a code segment memory item, all
other threads see that.
A thread is also called a lightweight process. 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. Threads have been
successfully used in implementing network servers and web server. They also
provide a suitable foundation for parallel execution of applications on shared
memory multiprocessors. The following figure shows the working of a single-
threaded and a multithreaded process.
Difference between Process and Thread
S.N. Process Thread
2 Process switching needs interaction Thread switching does not need to interact
with operating system. with operating system.
3 In multiple processing environments, All threads can share same set of open files,
each process executes the same child processes.
code but has its own memory and file
resources.
4 If one process is blocked, then no While one thread is blocked and waiting, a
other process can execute until the second thread in the same task can run.
first process is unblocked.
6 In multiple processes each process One thread can read, write or change another
operates independently of the thread's data.
others.
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.
Memory Management
Memory management is the functionality of an operating system which handles or
manages primary memory and moves processes back and forth between main
memory and disk during execution. Memory management keeps track of each and
every memory location, regardless of either it is allocated to some process or it is
free. It checks how much memory is to be allocated to processes. It decides
which process will get memory at what time. It tracks whenever some memory
gets freed or unallocated and correspondingly it updates the status.
This tutorial will teach you basic concepts related to Memory Management.
Process Address Space
The process address space is the set of logical addresses that a process
references in its code. For example, when 32-bit addressing is in use, addresses
can range from 0 to 0x7fffffff; that is, 2^31 possible numbers, for a total
theoretical size of 2 gigabytes.
The operating system takes care of mapping the logical addresses to physical
addresses at the time of memory allocation to the program. There are three types
of addresses used in a program before and after memory is allocated −
S.N. Memory Addresses & Description
1 Symbolic addresses
The addresses used in a source code. The variable names, constants, and instruction
labels are the basic elements of the symbolic address space.
2 Relative addresses
At the time of compilation, a compiler converts symbolic addresses into relative
addresses.
3 Physical addresses
The loader generates these addresses at the time when a program is loaded into main
memory.
Virtual and physical addresses are the same in compile-time and load-time
address-binding schemes. Virtual and physical addresses differ in execution-time
address-binding scheme.
The set of all logical addresses generated by a program is referred to as a logical
address space. The set of all physical addresses corresponding to these logical
addresses is referred to as a physical address space.
The runtime mapping from virtual to physical address is done by the memory
management unit (MMU) which is a hardware device. MMU uses following
mechanism to convert virtual address to physical address.
The value in the base register is added to every address generated by a
user process, which is treated as offset at the time it is sent to memory.
For example, if the base register value is 10000, then an attempt by the
user to use address location 100 will be dynamically reallocated to location
10100.
The user program deals with virtual addresses; it never sees the real
physical addresses.
Static vs Dynamic Loading
The choice between Static or Dynamic Loading is to be made at the time of
computer program being developed. If you have to load your program statically,
then at the time of compilation, the complete programs will be compiled and linked
without leaving any external program or module dependency. The linker combines
the object program with other necessary object modules into an absolute
program, which also includes logical addresses.
If you are writing a Dynamically loaded program, then your compiler will compile
the program and for all the modules which you want to include dynamically, only
references will be provided and rest of the work will be done at the time of
execution.
At the time of loading, with static loading, the absolute program (and data) is
loaded into memory in order for execution to start.
If you are using dynamic loading, dynamic routines of the library are stored on a
disk in relocatable form and are loaded into memory only when they are needed by
the program.
Static vs Dynamic Linking
As explained above, when static linking is used, the linker combines all other
modules needed by a program into a single executable program to avoid any
runtime dependency.
When dynamic linking is used, it is not required to link the actual module or library
with the program, rather a reference to the dynamic module is provided at the
time of compilation and linking. Dynamic Link Libraries (DLL) in Windows and
Shared Objects in Unix are good examples of dynamic libraries.
Swapping
Swapping is a mechanism in which a process can be swapped temporarily out of
main memory (or move) to secondary storage (disk) and make that memory
available to other processes. At some later time, the system swaps back the
process from the secondary storage to main memory.
Though performance is usually affected by swapping process but it helps in
running multiple and big processes in parallel and that's the reason Swapping is
also known as a technique for memory compaction.
The total time taken by swapping process includes the time it takes to move the
entire process to a secondary disk and then to copy the process back to memory,
as well as the time the process takes to regain main memory.
Let us assume that the user process is of size 2048KB and on a standard hard
disk where swapping will take place has a data transfer rate around 1 MB per
second. The actual transfer of the 1000K process to or from memory will take
2048KB / 1024KB per second
= 2 seconds
= 2000 milliseconds
Now considering in and out time, it will take complete 4000 milliseconds plus other
overhead where the process competes to regain main memory.
Memory Allocation
Main memory usually has two partitions −
Low Memory − Operating system resides in this memory.
High Memory − User processes are held in high memory.
Operating system uses the following memory allocation mechanism.
S.N. Memory Allocation & Description
1 Single-partition allocation
In this type of allocation, relocation-register scheme is used to protect user processes
from each other, and from changing operating-system code and data. Relocation register
contains value of smallest physical address whereas limit register contains range of logical
addresses. Each logical address must be less than the limit register.
2 Multiple-partition allocation
In this type of allocation, main memory is divided into a number of fixed-sized partitions
where each partition should contain only one process. When a partition is free, a process
is selected from the input queue and is loaded into the free partition. When the process
terminates, the partition becomes available for another process.
Fragmentation
As processes are loaded and removed from memory, the free memory space is
broken into little pieces. It happens after sometimes that processes cannot be
allocated to memory blocks considering their small size and memory blocks remains
unused. This problem is known as Fragmentation.
Fragmentation is of two types −
S.N. Fragmentation & Description
1 External fragmentation
Total memory space is enough to satisfy a request or to reside a process in it, but it is not
contiguous, so it cannot be used.
2 Internal fragmentation
Memory block assigned to process is bigger. Some portion of memory is left unused, as it
cannot be used by another process.
The following diagram shows how fragmentation can cause waste of memory and a
compaction technique can be used to create more free memory out of fragmented
memory −
External fragmentation can be reduced by compaction or shuffle memory contents
to place all free memory together in one large block. To make compaction feasible,
relocation should be dynamic.
The internal fragmentation can be reduced by effectively assigning the smallest
partition but large enough for the process.
Paging
A computer can address more memory than the amount physically installed on the
system. This extra memory is actually called virtual memory and it is a section of a
hard that's set up to emulate the computer's RAM. Paging technique plays an
important role in implementing virtual memory.
Paging is a memory management technique in which process address space is
broken into blocks of the same size called pages (size is power of 2, between 512
bytes and 8192 bytes). The size of the process is measured in the number of
pages.
Similarly, main memory is divided into small fixed-sized blocks of (physical)
memory called frames and the size of a frame is kept the same as that of a page
to have optimum utilization of the main memory and to avoid external
fragmentation.
Address Translation
Page address is called logical address and represented by page number and
the offset.
Logical Address = Page number + page offset
Frame address is called physical address and represented by a frame number and
the offset.
Physical Address = Frame number + page offset
A data structure called page map table is used to keep track of the relation
between a page of a process to a frame in physical memory.
1. If CPU try to refer a page that is currently not available in the main memory,
it generates an interrupt indicating memory access fault.
2. The OS puts the interrupted process in a blocking state. For the execution to
proceed the OS must bring the required page into the memory.
3. The OS will search for the required page in the logical address space.
4. The required page will be brought from logical address space to physical
address space. The page replacement algorithms are used for the decision
making of replacing the page in physical address space.
5. The page table will updated accordingly.
6. The signal will be sent to the CPU to continue the program execution and it will
place the process back into ready state.
Hence whenever a page fault occurs these steps are followed by the operating
system and the required page is brought into memory.
Advantages :
More processes may be maintained in the main memory: Because we are going
to load only some of the pages of any particular process, there is room for
more processes. This leads to more efficient utilization of the processor
because it is more likely that at least one of the more numerous processes will
be in the ready state at any particular time.
A process may be larger than all of main memory: One of the most
fundamental restrictions in programming is lifted. A process larger than the
main memory can be executed because of demand paging. The OS itself loads
pages of a process in main memory as required.
It allows greater multiprogramming levels by using less of the available
(primary) memory for each process.
Page Fault Service Time :
The time taken to service the page fault is called as page fault service time. The
page fault service time includes the time taken to perform all the above six steps.
Let Main memory access time is: m
Page fault service time is: s
Page fault rate is : p
Then, Effective memory access time = (p*s) + (1-p)*m
Swapping:
Swapping a process out means removing all of its pages from memory, or marking
them so that they will be removed by the normal page replacement process.
Suspending a process ensures that it is not runnable while it is swapped out. At
some later time, the system swaps back the process from the secondary storage to
main memory. When a process is busy swapping pages in and out then this situation
is called thrashing.
Thrashing :
At any given time, only few pages of any process are in main memory and therefore
more processes can be maintained in memory. Furthermore time is saved because
unused pages are not swapped in and out of memory. However, the OS must be
clever about how it manages this scheme. In the steady state practically, all of
main memory will be occupied with process’s pages, so that the processor and OS
has direct access to as many processes as possible. Thus when the OS brings one
page in, it must throw another out. If it throws out a page just before it is used,
then it will just have to get that page again almost immediately. Too much of this
leads to a condition called Thrashing. The system spends most of its time swapping
pages rather than executing instructions. So a good page replacement algorithm is
required.
In the given diagram, initial degree of multi programming upto some extent of
point(lamda), the CPU utilization is very high and the system resources are utilized
100%. But if we further increase the degree of multi programming the CPU
utilization will drastically fall down and the system will spent more time only in the
page replacement and the time taken to complete the execution of the process will
increase. This situation in the system is called as thrashing.
Causes of Thrashing :
1. High degree of multiprogramming : If the number of processes keeps on
increasing in the memory than number of frames allocated to each process will
be decreased. So, less number of frames will be available to each process. Due
to this, page fault will occur more frequently and more CPU time will be wasted
in just swapping in and out of pages and the utilization will keep on decreasing.
For example:
Let free frames = 400
Case 1: Number of process = 100
Then, each process will get 4 frames.
Case 2: Number of process = 400
Each process will get 1 frame.
Case 2 is a condition of thrashing, as the number of processes are
increased,frames per process are decreased. Hence CPU time will be consumed
in just swapping pages.
2. Lacks of Frames:If a process has less number of frames then less pages of
that process will be able to reside in memory and hence more frequent
swapping in and out will be required. This may lead to thrashing. Hence
sufficient amount of frames must be allocated to each process in order to
prevent thrashing.
Recovery of Thrashing :
Do not allow the system to go into thrashing by instructing the long term
scheduler not to bring the processes into memory after the threshold.
If the system is already in thrashing then instruct the mid term schedular to
suspend some of the processes so that we can recover the system from
thrashing.
Demand Paging
In computer operating systems, demand paging (as opposed to anticipatory paging)
is a method of virtual memory management. In a system that uses demand paging,
the operating system copies a disk page into physical memory only if an attempt is
made to access it and that page is not already in memory (i.e., if a page
fault occurs). It follows that a process begins execution with none of its pages in
physical memory, and many page faults will occur until most of a process's working
set of pages are located in physical memory. This is an example of a lazy
loading technique.
Demand paging follows that pages should only be brought into memory if the
executing process demands them. This is often referred to as lazy evaluation as
only those pages demanded by the process are swapped from secondary
storage to main memory. Contrast this to pure swapping, where all memory for a
process is swapped from secondary storage to main memory during the process
startup.
Commonly, to achieve this process a page table implementation is used. The page
table maps logical memory to physical memory. The page table uses
a bitwise operator to mark if a page is valid or invalid. A valid page is one that
currently resides in main memory. An invalid page is one that currently resides in
secondary memory. When a process tries to access a page, the following steps are
generally followed:
Attempt to access page.
If page is valid (in memory) then continue processing instruction as normal.
If page is invalid then a page-fault trap occurs.
Check if the memory reference is a valid reference to a location on secondary
memory. If not, the process is terminated (illegal memory access). Otherwise,
we have to page in the required page.
Schedule disk operation to read the desired page into main memory.
Restart the instruction that was interrupted by the operating system trap.
Advantages[edit]
Demand paging, as opposed to loading all pages immediately:
Only loads pages that are demanded by the executing process.
As there is more space in main memory, more processes can be loaded, reducing
the context switching time, which utilizes large amounts of resources.
Less loading latency occurs at program startup, as less information is accessed
from secondary storage and less information is brought into main memory.
As main memory is expensive compared to secondary memory, this technique
helps significantly reduce the bill of material (BOM) cost in smart phones for
example. Symbian OS had this feature.
Disadvantages[edit]
Individual programs face extra latency when they access a page for the first
time.
Low-cost, low-power embedded systems may not have a memory management
unit that supports page replacement.
Memory management with page replacement algorithms becomes slightly more
complex.
Possible security risks, including vulnerability to timing attacks; see Percival,
Colin (2005-05-13). "Cache missing for fun and profit" (PDF). BSDCan
2005. (specifically the virtual memory attack in section 2).
Thrashing which may occur due to repeated page faults.
Author C Append
Close
USUAL
FILE TYPE EXTENSION FUNCTION
Word
Processor wp, tex, rrf, doc Various word processor formats
FILE DIRECTORIES:
Collection of files is a file directory. The directory contains information about the
files, including attributes, location and ownership. Much of this information,
especially that is concerned with storage, is managed by the operating system. The
directory is itself a file, accessible by various file management routines.
TWO-LEVEL DIRECTORY
In this separate directories for each user is maintained.
Path name:Due to two levels there is a path name for every file to locate that
file.
Now,we can have same file name for different user.
Searching is efficient in this method.
TREE-STRUCTURED DIRECTORY :
Directory is maintained in the form of a tree. Searching is efficient and also there
is grouping capability. We have absolute or relative path name for a file.
FILE ALLOCATION METHODS
1. Continuous Allocation: A single continuous set of blocks is allocated to a file at
the time of file creation. Thus, this is a pre-allocation strategy, using variable size
portions. The file allocation table needs just a single entry for each file, showing
the starting block and the length of the file. This method is best from the point of
view of the individual sequential file. Multiple blocks can be read in at a time to
improve I/O performance for sequential processing. It is also easy to retrieve a
single block. For example, if a file starts at block b, and the ith block of the file is
wanted, its location on secondary storage is simply b+i-1.
Disadvantage
External fragmentation will occur, making it difficult to find contiguous blocks
of space of sufficient length. Compaction algorithm will be necessary to free
up additional space on disk.
Also, with pre-allocation, it is necessary to declare the size of the file at the
time of creation.
2. Linked Allocation(Non-contiguous allocation) : Allocation is on an individual block
basis. Each block contains a pointer to the next block in the chain. Again the file
table needs just a single entry for each file, showing the starting block and the
length of the file. Although pre-allocation is possible, it is more common simply to
allocate blocks as needed. Any free block can be added to the chain. The blocks
need not be continuous. Increase in file size is always possible if free disk block is
available. There is no external fragmentation because only one block at a time is
needed but there can be internal fragmentation but it exists only in the last disk
block of file.
Disadvantage:
Internal fragmentation exists in last disk block of file.
There is an overhead of maintaining the pointer in every disk block.
If the pointer of any disk block is lost, the file will be truncated.
It supports only the sequencial access of files.
3. Indexed Allocation:
It addresses many of the problems of contiguous and chained allocation. In this
case, the file allocation table contains a separate one-level index for each file: The
index has one entry for each block allocated to the file. Allocation may be on the
basis of fixed-size blocks or variable-sized blocks. Allocation by blocks eliminates
external fragmentation, whereas allocation by variable-size blocks improves
locality. This allocation technique supports both sequential and direct access to the
file and thus is the most popular form of file allocation.