Академический Документы
Профессиональный Документы
Культура Документы
Introduction
What is an Operating System?
An operating system (OS) is the interface between the user and the hardware. It
implements a virtual machine that is easier to program than bare hardware. An OS provides
standard services (an interface) which are implemented on the hardware, including: l Processes,
CPU scheduling, memory management, file system, networking. The OS coordinates multiple
applications and users (multiple processes) in a fair and efficient manner. The goal in OS
development is to make the machine convenient to use (a software engineering problem) and
efficient (a system and engineering problem)
(Ref Class Notes)
Definition:
An operating system is a program that manages the computer hardware. It also provides a basis for
application programs and acts as an intermediary between the computer user and the computer
hardware.
Computer System
A magician --provides each user with the illusion of a dedicated machine with infinite
memory and CPU time
A government--allocates resources efficiently and fairly, protects users from each other,
provides safe and secure communication.
A parent--always there when you need it, never breaks, always succeeds
A fast food restaurant--provides a service everyone needs, always works the same
everywhere (standardization)
A complex system--but keep it as simple as possible so that it will work
Why Study Operating Systems?
Abstraction how do you give the users the illusion of infinite resources (CPU time,
memory, file space)?
Operating systems are among the most complex pieces of software yet developed,
William Stallings, 1994
Loader
Job
sequencing
Monitor
Control card
interpreter
User program
area
Memory layout for a resident monitor
operator
computer
Much computer time was wasted in the interaction between operator and computer. To
speed up processing two points were developed magnetic tapes were used instead of cards, jobs
with similar needs were batched together and were run through the computer as a group. Second
generation computers were used mostly for scientific and engineering calculations, and
programmed in FORTRAN and ASSEMBLER languages.
The very simple batch operating system was always resident in memory, then normally
reads a steam of separate jobs, each with own control cards that predefine what the job does. For
the purpose of automatic job sequencing a small program was designed, called a resident monitor.
The resident monitor is always (resident) in memory.
Control-card interpreter is responsible for reading and carrying out the instructions on the
cards. Loader loads system programs and application programs into memory.
The Third generation (1965 1980)
This period was a real expansion of computers to commercial companies banks,
enterprises, insurance companies and etc. Most typical computer line was the System/360 as a
series of software compatible machines. OS/360 and the similar third generation operating systems
produced by other companies actually satisfied most of their customers.
New and most important feature was multi-programming.
In one-programming systems the CPU simply sat idle until I/O finished. For commercial
problem with the large data input CPU idle time was about 80 percent of the total time.
The multi-programming operating system keeps several jobs in memory at a time.
Job 3
Job 2
Job 1
The operating system picks and begins to execute one of the jobs in memory. Eventually, the job
may have to wait for I/O operation to complete; the operating system simply switches to and
executes another job. When that job needs to wait, the CPU is switched to another job, and so on.
As long as there is some job to execute, the CPU will never be idle. In addition, if several
jobs are ready to run at the same time, the system must choose among them. Making decision is
CPU scheduling, which will be covered later. To keep several programs in memory at the same time
requires some form of memory management.
Multi-programming and time-sharing are the central themes of modern operating system.
The fourth generation (1980-1990 and beyond)
With the development of LSI (Large Scale Integration) circuits, hardware costs have
decreased that made it possible for a single individual to have his own personal computer (PC). The
most powerful personal computers are usually called workstations. Two operating systems have
dominated on the personal computers market: Microsoft MS DOS and UNIX.
But really PCs operating systems were neither multi-user nor multi-tasking. The goals of these
operating systems have changed with time: instead of maximize CPU and peripheral utilization; the
systems were designed for maximizing user convenience and responsiveness.
An interesting development that began during the mid- 1980s is the growth of networks of personal
computers running network operating systems and distributed operating system.
In a network operating system each machine runs its own local operating system, and the users are
aware of the existence of multiple computers, and can log in to remote machines and copy files
from one machine to another. These operating systems are not fundamentally different from
single-processor operating systems. They obviously need a network interface controller and some
low-level software to drive it, and some programs to achieve remote file access.
1. Serial Processing
No operating system
Machines run from a console with display lights, toggle switches, input device, and printer.
Programs in machine code were loaded with the input device. If an error halts the program, then
the error indication was given by lights.
Main problems faced by early systems:
Schedule time - Most installations used a sign-up sheet to reserve machine time. On the other
hand, the user might run into problems, or not finish in the allotted time, and be forced to stop
before resolving the problem.
Setup time A single program called a job involves loading the compiler, source program, saving
compiled program, and loading and linking together the object program and common functions.
Privileged instructions
Certain instructions are designated as privileged and can be executed only by the monitor. If the
processor executes such an instruction while executing a user program, an error interrupt occurs.
Interrupts
This facility gives the operating system more flexibility in relinquishing control to and regaining
control from user programs.
3. Multi-programming
Even though the job sequencing is done automatically by simple batch processing system, the
processor is often idle. The problem is that I / O devices are slow compared to the processor. More
than 96% of the time is spend waiting for I / O devices and during this time the processor will be
idle. In order to overcome the inefficiency, the concept of multiprogramming evolved which
requires a large memory space. Suppose if there is a room for one operating system and two user
programs, when one job needs to wait for I / O, the processor can switch to the other job, which
likely is not waiting for I / O. It is known as Multiprogramming
4. Time-Sharing Systems
Characteristics:
Batch Multiprogramming
Time Sharing
Principal objective
Source of directives
to operating system
Commands entered at
the terminal
Time sharing is multiprogramming. The key differences between time-sharing systems and batch
multiprogramming systems are given in the table above.
Multi-processor systems
Multi-processing is a technique that involves operating several processors in parallel to obtain a
higher calculation power than that obtained using a high-end processor or to increase the
availability of the system (in the event of processor breakdown).
The term SMP (Symmetric Multiprocessing or Symmetric Multiprocessor) refers to an architecture
in which all processors access the same shared memory.
A multiprocessor system must be able to manage memory sharing between several processors but
also to distribute the work load.
Distributed Systems
Distributed system deals with hardware and software systems containing more than one
processing or storage element, concurrent processes, or multiple programs, running under a loosely
or tightly controlled regime. In distributed computing a program is split up into parts that run
simultaneously on multiple computers communicating over a network. Distributed programs often
must deal with heterogeneous environments, network links of varying latencies, and unpredictable
failures in the network or the computers.
The OS can support a distributed file system on a distributed system. There are many
different types of distributed computing systems and many challenges to overcome in successfully
designing one. The main goal of a distributed computing system is to connect users and resources in
a transparent, open, and scalable way. Ideally this arrangement is more fault tolerant and more
powerful than using stand-alone computer systems.
Examples of distributed systems include networked _le systems, many web applications such as
Facebook or twitter, or an instant messaging system. Distributed systems can be built using low
level socket communication or higher level abstractions such as remote procedure calls.
OS-9;
RTLinux (RealTime Linux);
QNX;
VxWorks.
Unit-1
Components of Operating System
Operating system Components
Even though, not all systems have the same structure many modern operating systems
share the same goal of supporting the following types of system components.
1. Process Management
The operating system manages many kinds of activities ranging from user programs to
system programs like printer spooler, name servers, file server etc. Each of these activities is
encapsulated in a process. A process includes the complete execution context (code, data, PC,
registers, OS resources in use etc.).
It is important to note that a process is not a program. A process is only ONE instant of a
program in execution. There are many processes can be running the same program. The five major
activities of an operating system in regard to process management are
2.
Main-Memory Management
Primary-Memory or Main-Memory is a large array of words or bytes. Each word or byte has
its own address. Main-memory provides storage that can be access directly by the CPU. That is to
say for a program to be executed, it must in the main memory.
The major activities of an operating in regard to memory-management are:
3.
Keep track of which part of memory are currently being used and by whom.
Decide which process are loaded into memory when memory space becomes available.
File Management
A file is a collected of related information defined by its creator. Computer can store files on the
disk (secondary storage), which provide long term storage. Some examples of storage media are
magnetic tape, magnetic disk and optical disk. Each of these media has its own properties like
speed, capacity, data transfer rate and access methods.
A file system normally organized into directories to ease their use. These directories may
contain files and other directions.
The five main major activities of an operating system in regard to file management are
1. The creation and deletion of files.
2. The creation and deletion of directions.
3. The support of primitives for manipulating files and directions.
4. The mapping of files onto secondary storage.
5. The back up of files on stable storage media.
4. I/O System Management
I/O subsystem hides the peculiarities of specific hardware devices from the user. Only the
device driver knows the peculiarities of the specific device to whom it is assigned.
5. Secondary-Storage Management
Generally speaking, systems have several levels of storage, including primary storage,
secondary storage and cache storage. Instructions and data must be placed in primary storage or
cache to be referenced by a running program. Because main memory is too small to accommodate
all data and programs, and its data are lost when power is lost, the computer system must provide
secondary storage to back up main memory. Secondary storage consists of tapes, disks, and other
media designed to hold information that will eventually be accessed in primary storage (primary,
secondary, cache) is ordinarily divided into bytes or words consisting of a fixed number of bytes.
Each location in storage has an address; the set of all addresses available to a program is called an
address space.
The three major activities of an operating system in regard to secondary storage management are:
1. Managing the free space available on the secondary-storage device.
2. Allocation of storage space when new files have to be written.
3. Scheduling the requests for memory access.
6.
Networking
called network. The communication-network design must consider routing and connection
strategies, and the problems of contention and security.
7.
Protection System
If a computer systems has multiple users and allows the concurrent execution of multiple
processes, then the various processes must be protected from one another's activities. Protection
refers to mechanism for controlling the access of programs, processes, or users to the resources
defined by a computer systems.
8. Command Interpreter System
A command interpreter is an interface of the operating system with the user. The user gives
commands with are executed by operating system (usually by turning them into system calls). The
main function of a command interpreter is to get and execute the next user specified command.
Command-Interpreter is usually not part of the kernel, since multiple command interpreters (shell,
in UNIX terminology) may be support by an operating system, and they do not really need to run in
kernel mode. There are two main advantages to separating the command interpreter from the
kernel.
If we want to change the way the command interpreter looks, i.e., I want to change the
interface of command interpreter, I am able to do that if the command interpreter is separate from
the kernel. I cannot change the code of the kernel so I cannot modify the interface.
If the command interpreter is a part of the kernel it is possible for a malicious process to gain access
to certain part of the kernel that it showed not have to avoid this ugly scenario it is advantageous to
have the command interpreter separate from kernel.
cannot control I/O devices directly. Therefore, the operating system must provide a
means to do I/O.
4. File-system manipulation:- The file system is of particular interest. Obviously, programs
need to read and write files and directories. They also need to create and delete them
by name, search for a given file, and list file information. Finally, some programs include
permissions management to allow or deny access to files or directories based on file
ownership.
5. Communications: - There are many circumstances in which one process needs to
exchange information with another process. Such communication may occur between
processes that are executing on the same computer or between processes that are
executing on different computer systems tied together by a computer network.
Communications may be implemented via shared memory or through message passing,
in which packets of information are moved between processes by the operating system.
6. Error detection: - The operating system needs to be constantly aware of possible errors.
Errors may occur in the CPU and memory hardware (such as a memory error or a power
failure), in I/O devices (such as a parity error on tape, a connection failure on a network,
or lack of paper in the printer), and in the user program (such as an arithmetic overflow,
an attempt to access an illegal memory location, or a too-great use of CPU time). For
each type of error, the operating system should take the appropriate action to ensure
correct and consistent computing. Debugging facilities can greatly enhance the user's
and programmer's abilities to use the system efficiently.
7. Resource allocation: - When there are multiple users or multiple jobs running at the
same time, resources must be allocated to each of them. Many different types of
resources are managed by the operating system. Some (such as CPU cycles, main
memory, and file storage) may have special allocation code, whereas others (such as I/O
devices) may have much more general request and release code. For instance, in
determining how best to use the CPU, operating systems have CPU-scheduling routines
that take into account the speed of the CPU, the jobs that must be executed, the
number of registers available, and other factors. There may also be routines to allocate
printers, modems, USB storage drives, and other peripheral devices.
8. Accounting: - We want to keep track of which users use how much and what kinds of
computer resources. This record keeping may be used for accounting (so that users can
be billed) or simply for accumulating usage statistics. Usage statistics may be a valuable
tool for researchers who wish to reconfigure the system to improve computing services.
9. Protection and security: - The owners of information stored in a multiuser or networked
computer system may want to control use of that information. When several separate
processes execute concurrently, it should not be possible for one process to interfere
with the others or with the operating system itself. Protection involves ensuring that all
access to system resources is controlled.
System Call
System calls provide an interface to the services made available by an operating system.
Process control
End, abort
Load, execute
Create process, terminate process
Get process attributes, set process
attributes
Wait for time
Wait event, signal event
Allocate, free memory
File management
Create / delete file
Open, close
Read, write, reposition/seek
Get / Set file attributes
Device Management
Request / Release device
Read, write, reposition
Get / Set device attributes
Attach / Detach devices
Information maintenance
Get / Set time or date
Get / Set system data
Get / Set process, file or device attributes
Communications
Create / Delete communication
connection
Send, receive messages
Transfer status information
Attach / Detach remote devices
System call changes mode to kernel, return from call resets it to user
Unit-1
System programs and Operating System Structure
Structure of operating Systems
As modern operating systems are large and complex careful engineering is required. There are four
different structures that have shown in this document in order to get some idea of the spectrum of
possibilities. These are by no mean s exhaustive, but they give an idea of some designs that have
been tried in practice.
Monolithic Systems:
This approach well known as The Big Mess.
The structure is that there is no structure.
A monolithic kernel is a kernel architecture where the entire kernel is run as a single
privileged entity running on the machine. Functionality in a monolithic kernel can be broken up into
modules; however, the code integration for each module is very tight.
Also, since all the modules run in the same address space, a bug in one module can bring
down the whole system. However, when the implementation is complete and trustworthy, the tight
internal integration of components allows the low-level features of the underlying system to be
effectively utilized, making a good monolithic kernel highly efficient.
In a monolithic kernel, all the systems such as the file system management run in an area
called the kernel mode. The main problem with this organization is maintainability. Examples - Unixlike kernels (Unix, Linux, MS-DOS, Mac OS).
Layered Approach
The operating system is divided into a number of layers (levels), each built on top of lower
layers. The bottom layer (layer 0), is the hardware; the highest (layer N) is the user
interface.
With modularity, layers are selected such that each uses functions (operations) and services
of only lower-level layers
An OS layer is an implementation of an abstract object that is the encapsulation of data and
operations that can manipulate those data. These operations (routines) can be invoked by
higher-level layers. The layer itself can invoke operations on lower-level layers.
Each layer is implemented by using only those operations that are provided lower level
layers.
The major difficulty is appropriate definition of various layers.
General OS Layers
A microkernel is a minimal computer operating system kernel which, in its purest form,
provides no operating-system services at all.
Assigns only a few essential functions to the kernel
Address spaces
Interprocess communication (IPC)
Basic scheduling
The microkernel is the only part of the system executing in a kernel mode. The actual
operating-system services are provided by "user-mode" servers.
Unit-1
Process Management
In this chapter we will study
How a process comes into existence?
How processes are managed?
A process in execution needs resources like processing resource, memory and IO resources.
Current machines allow several processes to share resources. In reality, one processor is shared
amongst many processes. A system like a file server even support processes from multiple users.
And yet the owner of every process gets an illusion that the server (read processor) is available to
their process without any interruption. This requires clever management and allocation of the
processor as a resource. We shall study the basic processor sharing mechanism amongst processes.
We introduce basic CPU-scheduling concepts and present several CPU-scheduling algorithms. We
also consider the problem of selecting an algorithm for a particular system.
WHAT IS A PROCESS?
A process is a program in execution.
To understand the importance of this definition, lets imagine that we have written a
program called my_prog.c in C. On execution, this program may read in some data and output some
data. Note that when a program is written and a file is prepared, it is still a script. It has no dynamics
of its own i.e., it cannot cause any input processing or output to happen. Once we compile, and still
later when we run this program, the intended operations take place. In other words, a program is a
text script with no dynamic behavior. When a program is in execution, the script is acted upon. It
can result in engaging a processor for some processing and it can also engage in I/O operations. It is
for this reason a process is differentiated from program.
>>Define what a process is and in which way a process differs from a program. <<
A process is an executable entity its a program in execution. When we compile a C
language program we get an a. out file which is an executable file. When we seek to run this file we
see the program in execution.
Processor Utilization:
Processor Utilization: A processor is a central and a key element of a computer system. This
is so because all information processing gets done in a processor. So a computer's throughput
depends upon the extent of utilization of its processor. The greater the utilization of the processor,
larger is the amount of information processed.
In a uni-programming system (figure a) we have one program engaging the processor. In
such a situation the processor is idling for very long periods of time. This is so because IO and
communication to devices (including memory) takes so much longer.
In figure above we see that during intervals when prog_A is not engaging the processor we
can utilize the processor to run another ready to run program. The processor now processes two
programs without significantly sacrificing the time required to process prog_A. Note that we may
have a small overhead in switching the context of use of a processor. However, multiprogramming
results in improving the utilization of computer's resources. In this example, with multiple programs
residing in the memory, we enhance the memory utilization also!!.
Response Time:
In time sharing system, The response time of a system is defined as the time interval which
spans the time from the last character input to the first character of output.
It is important that when we design a time sharing system we keep the response time at
some acceptable level. Otherwise the advantage of giving access to, and sharing, the resource
would be lost. A system which we use to access book information in a library is a time-shared
system. Clearly, the response time should be such that it should be acceptable, say a few seconds. A
library system is also an online system .In online system; devices (which can include
instrumentation in a plant) are continuously monitored (observed) by the computer system. If in an
online system the response time is also within some acceptable limits then we say it is a real-time
system.
1. Process state. The state may be new, ready, running, waiting, halted, and so on.
2. Program counter the counter indicates the address of the next instruction to be executed for
this process.
3. CPU registers. The registers vary in number and type, depending on the computer architecture.
They include accumulators, index registers, stack pointers, and general-purpose registers, plus
any condition-code information. Along with the program counter, this state information must be
saved when an interrupt occurs, to allow the process to be continued correctly afterward.
4. CPU-scheduling information. This information includes a process priority, pointers to scheduling
queues, and any other scheduling parameters.
5. Memory-management information. This information may include such information as the value
of the base and limit registers, the page tables, or the segment tables, depending on the
memory system used by the operating system.
6. Accounting information. This information includes the amount of CPU and real time used, time
limits, account members, job or process numbers, and so on.
7. I/O status information. This information includes the list of I/O devices allocated to the process,
a list of open files, and so on.
Context Switching
A context switch (also sometimes referred to as a process switch or a task switch) is the
switching of the CPU from one process or thread to another.
What Happens When Context Is Switched?
We will continue to assume that we have a uni-processor multi-programming
environment. We have earlier seen that only ready-to-run, main memory resident processes
can be scheduled for execution. For instance, each process in execution uses the program
counter, registers and other resources within the CPU. So, whenever a process is switched, the
OS moves out, and brings in, considerable amount of context switching information.
We have process P1 currently executing (note that the program counter is pointing in
executable code area of P1). Let us now switch the context to the process P2.
The following must happen:
o All the current context information about process P1 must be updated (save) in
its own context area.
o All context information about process P2 must be downloaded (restore) in its
own context area.
Process State
The operating systems principal responsibility is in controlling the execution of
processes. This includes determining the interleaving pattern for execution and allocation of
resources to processes. One part of designing an OS is to describe the behavior that we would
like each process to exhibit. The simplest model is based on the fact that a process is either
being executed by a processor or it is not.
Two State Model
Thus, a process may be considered to be in one of two states,
>>RUNNING
>>NOT RUNNING
When the operating system creates a new process, that process is initially labeled as
NOT RUNNING, and is placed into a queue in the system in the NOT RUNNING state. The
process (or some portion of it) then exists in main memory, and it waits in the queue for an
opportunity to be executed. After some period of time, the currently RUNNING process will be
interrupted, and moved from the RUNNING state to the NOT RUNNING state, making the
processor available for a different process. The dispatch portion of the OS will then select, from
the queue of NOT RUNNING processes, one of the waiting processes to transfer to the
processor. The chosen process is then relabeled from a NOT RUNNING state to a RUNNING
state, and its execution is either begun if it is a new process, or is resumed if it is a process
which was interrupted at an earlier time.
A Queuing Model:
Drawback of two state model:Although the two-state process management model is a perfectly valid design for an
operating system, the absence of a BLOCKED state means that the processor lies idle when the
active process changes from CPU cycles to I/O cycles. This design does not make efficient use of
the processor.
Five State Model
Null New
New Ready
Ready Running
Running Ready
Running Blocked
Blocked Ready
Ready Exit, Blocked Exit.
Operation on Thread
The processes in most systems can execute concurrently, and they may be created and deleted
dynamically. Thus, these systems must provide a mechanism for process creation and termination.
Process Creation
In general-purpose systems, some way is needed to create processes as needed during operation.
There are four principal events led to processes creation.
System initialization.
Execution of a process Creation System calls by a running process.
A user request to create a new process.
Initialization of a batch job.
Foreground processes interact with users. Background processes that stay in background sleeping
but suddenly springing to life to handle activity such as email, webpage, printing, and so on.
Background processes are called daemons. This call creates an exact clone of the calling process.
A process may create a new process by some create process such as 'fork'.
The creating process is called a parent process.
The new processes are called the children of that process.
Following are some reasons for creation of a process
Process Termination
A process terminates when it finishes executing its last statement. Its resources are returned to the
system, it is purged from any system lists or tables, and its process control block (PCB) is erased i.e.,
the PCB's memory space is returned to a free memory pool. The new process terminates the
existing process, usually due to following reasons:
Normal Exist Most processes terminates because they have done their job. This call is
exist in UNIX.
Error Exist When process discovers a fatal error. For example, a user tries to compile a
program that does not exist.
Fatal Error An error caused by process due to a bug in program for example, executing an
illegal instruction, referring non-existing memory or dividing by zero.
Killed by another Process A process executes a system call telling the Operating Systems
to terminate some other process. In UNIX, this call is kill. In some systems when a process
kills all processes it created are killed as well (UNIX does not work this way).
Unit-1
Interprocess Communication and light weight process
Threads
A thread is a single sequence stream within in a process.
Because threads have some of the properties of processes, they are sometimes called
lightweight processes. In a process, threads allow multiple executions of streams. In many
respect, 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.
Like a traditional process i.e., process with one thread, a thread can be in any of several states
Running
Blocked
Ready
Terminated
Each thread has its own stack. Since thread will generally call different procedures and thus a
different execution history. This is why thread needs its own stack.
An operating system that has thread facility, the basic unit of CPU utilization is a thread. A
thread has or consists of a program counter (PC), a register set, and a stack space.
Threads are not independent of one other like processes as a result threads shares with other
threads their code section, data section, OS resources also known as task, such as open files
and signals.
Processes Vs Threads
As we mentioned earlier that in many respect threads operate in the same way as that of
processes. Some of the similarities and differences are:
Similarities
Like processes threads share CPU and only one thread active (running) at a time.
Like processes, threads within processes, threads within a process execute sequentially.
Like processes, thread can create children.
And like process, if one thread is blocked, another thread can run.
Differences
Why Threads?
Following are some reasons why we use threads in designing operating systems.
1. A process with multiple threads makes a great server for example printer server.
2. Because threads can share common data, they do not need to use interprocess
communication.
3. Because of the very nature, threads can take advantage of multiprocessors.
Threads are cheap in the sense that
1. They only need a stack and storage for registers therefore, threads are cheap to create.
2. Threads use very little resources of an operating system in which they are working. That
is, threads do not need new address space, global data, program code or operating
system resources.
3. Context switching is fast when working with threads. The reason is that we only have to
save and/or restore PC, SP and registers.
But this cheapness does not come free - the biggest drawback is that there is no protection
between threads.
User-Level Threads
User-level threads implement in user-level libraries, rather than via systems calls, so thread
switching does not need to call operating system and to cause interrupt to the kernel. In fact,
the kernel knows nothing about user-level threads and manages them as if they were singlethreaded processes.
Advantages:
The most obvious advantage of this technique is that a user-level threads package can be
implemented on an Operating System that does not support threads. Some other advantages
are
Simple Management:
This simply means that creating a thread, switching between threads and
synchronization between threads can all be done without intervention of the kernel.
Disadvantages:
There is a lack of coordination between threads and operating system kernel. Therefore,
process as whole gets one time slice irrespective of whether process has one thread or 1000
threads within. It is up to each thread to relinquish control to other threads.
User-level threads require non-blocking systems call i.e., a multithreaded kernel. Otherwise,
entire process will blocked in the kernel, even if there are runnable threads left in the
processes. For example, if one thread causes a page fault, the process blocks.
Kernel-Level Threads
In this method, the kernel knows about and manages the threads. No runtime system is needed in
this case. Instead of thread table in each process, the kernel has a thread table that keeps track of
all threads in the system. In addition, the kernel also maintains the traditional process table to keep
track of processes. Operating Systems kernel provides system call to create and manage threads.
The implementation of general structure of kernel-level thread is
Advantages:
Because kernel has full knowledge of all threads, Scheduler may decide to give more time to
a process having large number of threads than process having small number of threads.
Kernel-level threads are especially good for applications that frequently block.
Disadvantages:
The kernel-level threads are slow and inefficient. For instance, threads operations are
hundreds of times slower than that of user-level threads.
Since kernel must manage and schedule threads as well as processes. It require a full thread
control block (TCB) for each thread to maintain information about threads. As a result there
is significant overhead and increased in kernel complexity.
Context Switching Threads are very inexpensive to create and destroy, and they are
inexpensive to represent. For example, they require space to store, the PC, the SP, and the
general-purpose registers, but they do not require space to share memory information,
Information about open files of I/O devices in use, etc. With so little context, it is much
faster to switch between threads. In other words, it is relatively easier for a context switch
using threads.
Sharing Treads allow the sharing of a lot resources that cannot be shared in process, for
example, sharing code section, data section, Operating System resources like open file etc.
Blocking The major disadvantage if that if the kernel is single threaded, a system call of
one thread will block the whole process and CPU may be idle during the blocking period.
Security Since there is, an extensive sharing among threads there is a potential problem of
security. It is quite possible that one thread over writes the stack of another thread (or
damaged shared data) although it is very unlikely since threads are meant to cooperate on a
single task.
Benefits of Multithreading
The benefits of multithreaded programming can be broken down into fourmajor categories:
1. Responsiveness. Multithreading an interactive application may allow a program to continue
running even if part of it is blocked or is performing a lengthy operation, thereby increasing
responsiveness to the user.
2.
Resource sharing. By default, threads share the memory and the resources of the process
to which they belong. The benefit of sharing code and data is that it allows an application to
have several different threads of activity within the same address space.
3. Economy. Allocating memory and resources for process creation is costly. Because threads
share resources of the process to which they belong, it is more economical to create and
context-switch threads.
4. Utilization of multiprocessor architectures. The benefits of multithreading can be greatly
increased in a multiprocessor architecture, where threads may be running in parallel on
different processors. A single threaded process can only run on one CPU, no matter how
many are available. Multithreading on a multi-CPU machine increases concurrency.
Multithreading Models
There must exist a relationship between user threads and kernel threads. We look at three common
ways of establishing this relationship.
Many-to-One Model
The many-to-one model maps many user-level threads to one kernel thread. Thread management is
done by the thread library in user space, so it is efficient; but the entire process will block if a thread
makes a blocking system call. Also, because only one thread can access the kernel at a time,
multiple threads are unable to run in parallel on multiprocessors.
Many-to-One Model
One-to-One Model
The one-to-one model maps each user thread to a kernel thread. It provides more concurrency than
the many-to-one model by allowing another thread to run when a thread makes a blocking system
call; it also allows multiple threads to run in parallel on multiprocessors.
The only drawback to this model is that creating a user thread requires creating the corresponding
kernel thread. Because the overhead of creating kernel threads can burden the performance of an
application, most implementations of this model restrict the number of threads supported by the
system.
One-to-One Model
Many-to-Many Model
The many-to-many model multiplexes many user-level threads to a smaller or equal number of
kernel threads. Whereas the many-to-one model allows the developer to create as many user
threads as she wishes, true concurrency is not gained because the kernel can schedule only one
thread at a time.
The one-to-one model allows for greater concurrency, but the developer has to be careful not to
create too many threads within an application (and in some instances may be limited in the number
of threads she can create).
Many-to-Many Model
The many-to-many model suffers from neither of these shortcomings:
Developers can create as many user threads as necessary, and the corresponding kernel threads
can run in parallel on a multiprocessor.
When a thread performs a blocking system call, the kernel can schedule another thread for
execution.
Threading Issues
Thread Creation
The fork() and exec() System Calls
If one thread in a program calls f ork ( ), does the new process duplicate all threads, or is the
new process single-threaded? Some UNIX systems have chosen to have two versions of fork ( )
one that duplicates all threads and another that duplicates only the thread that invoked the
fork ( ) system call.
If a thread invokes the exec ( ) system call, the program specified in the parameter to exec ( )
will replace the entire processincluding all threads.
Thread cancellation
Thread cancellation is the task of terminating a thread before it has completed. For example, if
multiple threads are concurrently searching through a database 1 and one thread returns the
result, the remaining threads might be canceled.
A thread that is to be canceled is often referred to as the target thread. Cancellation of a target
thread may occur in two different scenarios:
1. Asynchronous cancellation. One thread immediately terminates the target thread.
2. Deferred cancellation. The target thread periodically checks whether it should terminate,
allowing it an opportunity to terminate itself in an orderly fashion.
Signal Handling
A signal is used in UNIX systems to notify a process that a particular event has occurred. A
signal may be received either synchronously or asynchronously, depending on the source of
and the reason for the event being signaled. All signals, whether synchronous or asynchronous,
follow the same pattern:
1. A signal is generated by the occurrence of a particular event.
2. A generated signal is delivered to a process.
3. Once delivered, the signal must be handled.
Thread Pools
Unlimited threads could exhaust system resources, such as CPU time or memory. One solution
to this issue is to use a thread pool.
The general idea behind a thread pool is to create a number of threads at process startup and
place them into a pool, where they sit and wait for work. When a server receives a request, it
awakens a thread from this poolif one is availableand passes it the request to service. Once
the thread completes its service, it returns to the pool and awaits more work. If the pool
contains no available thread, the server waits until one becomes free.
Thread pools offer these benefits:
1. Servicing a request with an existing thread is usually faster than waiting to create
a thread.
2. A thread pool limits the number of threads that exist at any one point.
This is particularly important on systems that cannot support a large number of concurrent
threads.
Unit 1
CPU Scheduling and Process synchronization
CPU and I/O Burst Cycle
The execution of a process consists of a cycle of CPU execution and I/O wait.
A process begins with a CPU burst, followed by an I/O burst, followed by another
CPU burst and so on. The last CPU burst will end will a system request to
terminate the execution.
The CPU burst durations vary from process to process and computer to computer.
An I/O bound program has many very short CPU bursts.
A CPU bound program might have a few very long CPU bursts.
The key to the multiprogramming is scheduling. There are four types of scheduling that
an OS has to perform. These are:
o Long Term scheduling
The long term scheduling determines which programs are admitted to
the system for processing. Thus, it controls the level of
multiprogramming.
Once admitted, a job or a user program becomes a process and is added
to the queue for the short term scheduling (in some cases added to a
queue for medium term scheduling).
Long term scheduling is performed when a new process is created.
The criteria used for long-term scheduling may include first-come-first
serve, priority, expected execution time, and I/O requirements.
o Medium-Term Scheduling
The medium-term scheduling is a part of swapping function. This is a
decision to add a process to those that are at least partially in main
memory and therefore available for execution.
The swapping-in decision is made on the need to manage the degree of
multiprogramming and the memory requirements of the swapped-out
process.
o Short-Term Scheduling
A decision of which ready process to execute next is made in short-term
scheduling.
o I/O Scheduling
CPU Scheduler
Whenever, the CPU becomes idle, the OS must select one of the processes in the readyqueue to be executed.
The selection process is carried out the short-term scheduler or CPU scheduler. The CPU
scheduler selects a process from the ready queue and allocates the CPU to that process.
Currently running process may be interrupted and moved to the Ready state by
OS.
Allows for better service since any one process cannot monopolize the processor
for very long
Dispatcher
Dispatcher module gives control of the CPU to the process selected by the short-term
scheduler; this involves:
Switching context
Switching to user mode
Jumping to the proper location in the user program to restart that program
Dispatch latency time it takes for the dispatcher to stop one process and start another
running.
User oriented
Turnaround time: time interval from submission of job until its completion. It
includes actual processing time plus time spent waiting for resources, including
the processor. OR the amount of time between moment a process
first enters Ready State and the moment the process exits Running
State for the last time (Completed).
Waiting time: sum of periods spent waiting in ready queue.
Response time: time interval from submission of job to first response. Often a
process can begin producing some output to the user while continuing to
process the request. Thus this is a better measure than turnaround time from
the users point of view.
Normalized turnaround time: ratio of turnaround time to service time.
Service Time: The amount of time process needs to be in running state (Acquired
CPU) before it is completed.
System oriented
CPU utilization: percentage of time CPU is busy. CPU utilization may range from
0 to 100%. In a real system, it should range from 40% to 90%.
Throughput: number of jobs completed per time unit. This depends on the
average length of the processes.
Service time, wait time, turn around time, and throughput are some of the metrics used
to compare scheduling algorithms.
Any good scheduler should:
Maximize CPU utilization and throughput
Minimize turnaround time, waiting time, response time
All systems
Fairness Giving each process a fair share of the CPU
Policy enforcement seeing that stated policy is carried out.
Balance Keeping all parts of the system busy
Batch systems
Maximize throughput (job/hour)
Minimize turnaround time
Maximize CPU utilization
Interactive systems
Minimize response time (respond to request quickly)
Proportionality Meet users expectations
Real-time systems
Meeting deadlines
Predictability avoid quality degradation in multimedia systems.
Scheduling Algorithms
CPU scheduling deals with the problem of deciding which of the processes in the ready
queue is to be allocated the CPU. There are many different CPU scheduling algorithms,
which we will discuss now.
The process that requests the CPU first is allocated the CPU first. It is nonpreemptive
algorithm.
Can easily be implemented with a FIFO queue.
When a process enters the ready queue, its PCB is linked onto the tail of the
queue.
When CPU is free, it is allocated to the process at the head of the queue.
Advantages
Very simple
Disadvantages
Long average and worst-case waiting times
Poor dynamic behavior (convoy effect - short process behind long process)
Example 1:
Process
P1
P2
P3
Burst Time
24
3
3
Suppose that the processes arrive in the order: P1, P2,P3.The Gantt Chart for the
schedule is:
P
0
P
24
P
27
30
Suppose that the processes arrive in the order P2, P3, P1. The Gantt chart for the
schedule is:
P2
0
P3
3
P1
6
30
Example 2:
Process
Arrival Time
Service Time
P1
P2
P3
P4
P5
This algorithm associates with each process the length of its next CPU burst.
When the CPU is available, it is assigned the process that has the smallest next CPU
burst.
It is a non-preemptive policy.
Example:
Process
Arrival Time
Burst Time
P1
0.0
P2
2.0
P3
4.0
P4
5.0
SJF (non-preemptive)
P1
0
P3
7
P2
8
P4
12
16
P1
0
P2
2
P3
4
P2
5
P4
7
P1
11
n1 tn 1 n .
16
Priority Scheduling
Example:
Process
P1
CPU Burst
Priority
10
P2
P3
P4
P5
(Solve as a example)
Round-Robin Scheduling
Each process gets a small unit of CPU time (called time quantum), usually 10-100
milliseconds.
After this time has elapsed, the process is preempted and added to the end of the ready
queue.
Ready queue is treated as a circular queue.
CPU scheduler goes around the ready queue, allocating the CPU to each process for a
time interval of 1 time quantum.
Ready queue is a FIFO queue of processes.
New processes are added to the tail of the ready queue and the CPU scheduler
picks the first process from the ready queue, sets a timer to interrupt after 1
time quantum and dispatches the process.
If the process has a CPU burst of less than 1 time quantum, it releases the CPU
voluntarily. Otherwise, the timer will go off and will cause an interrupt to the OS. A
context switch will be executed and the process will be put at the tail of the ready
queue. The CPU scheduler then picks up the next process in the ready queue.
If there are n processes in the ready queue and the time quantum is q,
Each process gets 1/n of the CPU time in chunks of at most q time units at once.
No process waits more than (n-1)q time units until its next time quantum.
Example: If there are 5 processes, with a time quantum of 20 ms, then
each process will get up to 20 ms every 100 ms.
Typically, RR has higher average turnaround than SJF, but better response.
Example:
Process
Burst Time
P1
53
P2
17
P3
68
P4
24
Time Quantum: 20
P1
0
P2
20
37
P3
P4
57
P1
77
P3
97 117
P4
P3
P3
P1
The performance of the RR depends heavily on the size of the time quantum.
If the time quantum is very large, then RR policy is the same as FCFS policy.
If the time quantum is very small then most of the CPU time will be spent on context
switching.
Turnaround time also depends on the time quantum
A rule of thumb is that 80% of the CPU bursts should be shorter than the time quantum.
A process can move between the various queues; aging can be implemented this way.
Three queues:
Q0 time quantum 8 milliseconds
Q1 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.
Algorithm Evaluation
Process Synchronization
Concurrent processes (or threads) often need access to shared data and shared
resources.
Processes use and update shared data such as shared variables, files, and data
bases.
Example 1
Shared data
#define BUFFER_SIZE 10
typedef struct {
...
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
int counter = 0;
Producer process
Consumer process
item nextProduced; while (1)
{
while (counter == BUFFER_SIZE)
; /* do nothing */
buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
counter++;
}
The statements
counter++;
counter--;
must be performed atomically.
register1 = register1 + 1
counter = register1
If both the producer and consumer attempt to update the buffer concurrently, the
assembly language statements may get interleaved.
Interleaving depends upon how the producer and consumer processes are scheduled.
The value of count may be either 4 or 6, where the correct result should be 5.
Race Condition
The race condition is a situation where several processes access (read/write) shared
data concurrently and the final value of the shared data depends upon which process
finishes last
The actions performed by concurrent processes
will then depend on the order in which their
execution is interleaved.
To prevent race conditions, concurrent processes must
be coordinated or synchronized.
It means that neither process will proceed
beyond a certain point in the computation until
both have reached their respective synchronization point.
Critical Section/Region
1. Consider a system consisting of n processes all competing to use some shared data.
2. Each process has a code segment, called critical section, in which the shared data is
accessed.
Example: Race condition updating a variable
The problem stems from interruption of software-based process while executing critical
section (low-level).
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.
Implications:
Critical sections better be focused and short.
Better not get into an infinite loop in there.
If a process somehow halts/waits in its critical section, it must not
interfere with other processes.
2. Progress. If no process is executing in its critical section and there exist some
processes that wish to enter their critical section, then the selection of the
processes that will enter the critical section next cannot be postponed
indefinitely.
If only one process wants to enter, it should be able to.
If two or more want to enter, one of them should succeed.
3. Bounded Waiting. A bound must exist 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.
Assume that each process executes at a nonzero speed
No assumption concerning relative speed of the n processes.
Types of Solutions
Software solutions
Algorithms whose correctness does not rely on any other assumptions.
Hardware solutions
Rely on some special machine instructions.
Operating System solutions
Provide some functions and data structures to the programmer through
system/library calls.
Programming Language solutions
Linguistic constructs provided as part of a language.
Mutual Exclusion
Four Requirements of Mutual Exclusion
Dekker Algorithm
Peterson Algorithm
Unit 1
Tools for process synchronization
Semaphores
Synchronization tool (provided by the OS) that does not require busy waiting.
Logically, a semaphore S is an integer variable that, apart from initialization, can only be
accessed through 2 atomic and mutually exclusive operations:
wait(S)
signal(S)
wait(S)
signal(S)
{
while (S 0 do) ;
S--;
S++;
}
Modification to the value of semaphore (S) in the wait and signal is executed
individually.
In wait, the testing and possible modification of S must also be executed without
interruption.
Usage of Semaphore
The fundamental principle of semaphore is this: Two or more processes can cooperate
by means of simple signals such that a process can be forced to stop at a specified place
until it has received a specified signal.
/*critical section*/
signal(mutex);
remainder section
} while (1);
Suppose that we have two processes, P1 with statement s1 and P2 with statement s2. We
require that s2 be executed only after s1 has completed. This can be done with a shared
semaphore variable, mutex, initialized to 0, and by inserting the following statements in
P1
s1 ;
signal(mutex);
waite(mutex)
s2 ;
Semaphore Implementation
The semaphore discussed so far requires a busy waiting. That is if a process is in criticalsection, the other process that tries to enter its critical-section must loop continuously
in the entry code.
To overcome the busy waiting problem, the definition of the semaphore operations wait
and signal should be modified.
When a process executes the wait operation and finds that the semaphore value
is not positive, the process can block itself. The block operation places the
process into a waiting queue associated with the semaphore.
A process that is blocked waiting on a semaphore should be restarted when
some other process executes a signal operation. The blocked process should be
restarted by a wakeup operation which put that process into ready queue.
To implemented the semaphore, we define a semaphore as a record as:
typedef struct {
int value;
struct process *L;
} semaphore;
signal(S):
S.value++;
if (S.value <= 0)
{
remove a process P from S.L;
wakeup(P);
}
Types of Semaphores
Data structures:
binary-semaphore S1, S2;
int C:
Initialization:
S1 = 1
S2 = 0
C = initial value of semaphore S
Implementing S
wait operation
signal operation
wait(S1);
wait(S1);
C--;
C ++;
if (C < 0) {
if (C <= 0)
signal(S1);
wait(S2);
}
signal(S1);
signal(S2);
else
signal(S1);
A programmer who needs to write a tailor-made synchronization scheme can define one or more
variables of type condition:
condition x, y;
The only operations that can be invoked on a condition variable are wait () and signal(). The operation
x.waitO ;
means that the process invoking this operation is suspended until another process invokes
x.signal();
The x. signal () operation resumes exactly one suspended process. If no process is suspended, then the
signal () operation has no effect; that is, the state of x is the same as if the operation had never been
executed. Contrast this operation with the signal () operation associated with semaphores, which
always affects the state of the semaphore.
Unit 2
Classical Problems of Process synchronization
Classical Problems of Synchronization
Bounded-Buffer Problem
Readers and Writers Problem
Dining-Philosophers Problem
\*** Solution for all problems are given in the Class Note Book ****/
Unit 2
Concurrency and Deadlock
Resources
Preemptable resources
Can be taken away from a process with no ill effects
Nonpreemptable resources
Will cause the process to fail if taken away
Reusable resources
Used by one process at a time and not depleted by that use
Examples: Processors, I/O channels, main and secondary memory, files, databases, and
semaphores
Consumable resources
Created (produced) and destroyed (consumed) by a process
Examples: Interrupts, signals, messages, and information in I/O buffers
System Model
Deadlock
Formal Definition
A set of processes is deadlocked if each process in the set is waiting for an event that only
another process in the set can cause
Examples of Deadlock
Example 1
System has 2 tape drives. P1 and P2 each hold one tape drive and each needs another
one.
Example 2
Semaphores A and B, initialized to 1
P0
P1
wait (A);
wait (B);
wait(B)
wait(A)
Example 3
Space is available for allocation of 200K bytes, and the following sequence of events
occur
P0
P1
Request 80KB;
Request 70KB;
Request 60KB;
Request 80KB;
Resource-Allocation Graph
Deadlocks can be described more precisely in terms of a directed graph, called a system
resource-allocation graph.
This graph consists of a set of vertices V and a set of edges E.
V is partitioned into two types:
P = {P1,P2,,Pn}, the set consisting of all the processes in the system.
R = {R1, R2, , Rm}, the set consisting of all resource types in the system.
E is partitioned into two types as well:
Request edge directed edge P1 Rj
Assignment edgedirected edge Rj Pi
Different symbols are used to represent processes and resources as given below:
Process:
Pi
Rj
Pi
Rj
Deadlock Prevention
Preempted resources are added to the list of resources for which the process
is waiting.
Process will be restarted only when it can regain its old resources, as well as
the new ones that it is requesting.
(b)
The required resource(s) is/are taken back from the process(s) holding it/them and
given to the requesting process
Problems
Some resources (e.g. printer, tap drives) cannot be preempted without
detrimental implications.
May require the job to restart
Deadlock Avoidance
OS never allocates resources in a way that could lead to a deadlock
Processes must tell OS in advance how many resources they will request
Some Definitions
State of a system
An enumeration of which processes hold, are waiting for or might request which
resource
Safe state
2. No process is deadlocked and the current state will not lead to a dead lock state
3. Safe state is where there is at least one sequence that does not result in deadlock
Unsafe state
Is a state that is not safe
Basic Facts
If a system is in safe state no deadlocks.
If a system is in unsafe state possibility of deadlock.
Avoidance ensure that a system will never enter an unsafe state
Deadlock Avoidance with Resource-Allocation Graph
This algorithm can be used if we have only one instance of each resource type.
In addition to the request and assignment edges, a claim edge is also introduced.
Claim edge Pi Rj indicated that process Pj may request resource Rj in future; represented by a
dashed line.
Claim edge converts to request edge when a process requests a resource.
When a resource is released by a process, assignment edge reconverts to a claim edge.
Resources must be claimed a priori in the system. That is, before a process starts executing, all
of its claim edges must already appear in the resource-allocation graph.
Suppose that process Pi requests resource Rj. The request can be granted only if converting the
request edge if converting the request edge PiRj to an assignment edge does not result in a
cycle in the resource-allocation graph. That is we use a cycle detection algorithm is used. If no
cycle exits, the process Pi will have to wait.
Bankers Algorithm
Safety Algorithm
1. Let Work and Finish be vectors of length m and n, respectively.
Initialize: Work = Available
Finish [i]=false for i=1,3, , n.
2. Find and i such that both:
(a) Finish [i] = false
(b) Needi Work
If no such i exists, go to step 4.
3. Work = Work + Allocationi
Finish[i] = true
go to step 2.
4. If Finish [i] == true for all i, then the system is in a safe state.
Request = request vector for process Pi. If Requesti [j] = k then process Pi wants k instances of
resource type Rj.
1. If Requesti Needi go to step 2. Otherwise, raise error condition, since process has exceeded
its maximum claim.
2. If Requesti Available, go to step 3. Otherwise Pi must wait, since resources are not available.
3. Pretend to allocate requested resources to Pi by modifying the state as follows:
Available = Available = Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi Requesti
Available
ABC
ABC
ABC
P0
010
753
332
P1
200
322
P2
302
902
P3
211
222
P4
002
433
Allocation
Process
Need
ABC
P0
743
P1
122
P2
600
P3
011
P4
431
The system is in a safe state since the sequence <P1,P3,P4,P2,P0> satisfies safety criteria.
Example P1 Request (1,0,2)
Process
Allocation
Need
Available
ABC
ABC
ABC
P0
010
743
230
P1
302
020
P2
301
600
P3
211
011
P4
002
431
Executing safety algorithm shows that sequence <P1, P3, P4, P0, P2> satisfies safety
requirement.
Can request for (3,3,0) by P4 be granted?
Can request for (0,2,0) by P0 be granted?
If each resource type has exactly one instance and the graph has a cycle then a deadlock has
occurred. Or if the cycle involves only a set of resource types, each of which has only a single
instance, then the deadlock has occurred.
Therefore, a cycle in the graph is both a necessary and sufficient condition for the
existence of a deadlock.
Examples:
allowing the use of the optimal approach for each of resources in the system.
Unit-2
Memory management
Mono-programming
In uni-processor systems, main memory is divided into two parts as shown below:
Multi-programming
Memory Management
The Basics
Program must be brought into memory and placed within a process for it to be run.
Input queue collection of processes on the disk that are waiting to be brought into memory to
run the program.
Normally, a process is selected from the input queue and is brought into the memory for
execution. During execution of a process, it accesses instruction and data from the memory.
Most systems allow a user process to be loaded in any part of the memory. This affects the
addresses that a user program can access.
A loader is responsible to place a load module in main memory at some starting address.
There are three approaches, which can be used for loading.
Absolute loading A given module is always loaded into the same memory location. All
references in the load module must be absolute memory addresses. The address binding
can be done at programming time, compile time or assembly time.
Relocatable loading The loader places a module in any desired location of the main
memory. To make it possible, the compiler or assembler must generate relative
addresses.
Dynamic loading - Routine is not loaded until it is called resulting in better memoryspace utilization (unused routine is never loaded). It is useful when large amounts of
code are needed to handle infrequently occurring cases. No special support from the
operating system is required implemented through program design.
The function of a linker is to take as input a collection of object modules and produce a load
module consisting of an integrated set of programs and data modules to be passed to the
loader.
In each object module, there may be symbolic reference to location in other modules.
The linking can be done either statically or dynamically.
Static linking A linker generally creates a single load module that is the contiguous
joining of all the object modules with references properly changed. This is called static
Dynamic linking Linking is postponed until execution time. All external references are
not resolved until the CPU executes the external call.
Small piece of code, stub, used to locate the appropriate memory-resident library
routine.
Stub replaces itself with the address of the routine, and executes the routine.
Operating system needed to check if routine is in processes memory address.
Dynamic linking is particularly useful for libraries.
Address binding of instructions and data to memory addresses can happen at three different
stages.
Compile time: If memory location known a priori, absolute code can be generated; must
recompile code if starting location changes.
Load time: The compiler must generate relocatable code if memory location is not
known at compile time. The final binding is delayed until load time.
Execution time: Binding delayed until run time if the process can be moved during its
execution from one memory segment to another. Need hardware support for address
maps (e.g., base and limit registers).
The concept of a logical address space that is bound to a separate physical address space is
central to proper memory management.
Logical address generated by the CPU; also referred to as virtual address.
Physical address address seen by the memory unit.
Logical and physical addresses are the same in compile-time and load-time address-binding
schemes; logical (virtual) and physical addresses differ in execution-time address-binding
scheme.
Overlays
Overlay is one of the techniques to run a program that is bigger than the size of the physical
memory.
The idea of overlays is to keep in memory only those instructions and data that are needed at
any given time.
The basic idea is to divide the program into modules in such a way that not all modules need to
be in the memory at the same time.
Programmer specifies which modules can overlay each other
The linker inserts commands to invoke the loader when modules are referenced
Used in DOS as an alternative to Expanded memory
Advantages
Reduced memory requirements
Disadvantages
Overlap map must be specified by programmer
Programmer must know memory requirements
Overlapped modules must be completely disjoint
Swapping
Advantages:
Simplicity
No special hardware required
Disadvantages:
CPU wasted
Main memory not fully used
Limited job size
There might be a separate process queue (need absolute addressing) for each partition or a
single queue for all partitions (need dynamic addressing).
Merging of holes
Advantages
Efficient use of memory
Disadvantages
Partition management
Compaction or external fragmentation
Internal fragmentation
Fragmentation
xternal Fragmentation total memory space exists to satisfy a request, but it is not contiguous.
Internal Fragmentation allocated memory may be slightly larger than requested memory; this
size difference is memory internal to a partition, but not being used.
Reduce external fragmentation by compaction.
Shuffle memory contents to place all free memory together in one large block.
Compaction is possible only if relocation is dynamic, and is done at execution time.
I/O problem
Latch job in memory while it is involved in I/O.
Do I/O only into OS buffers.
High cost of compaction.
Paging
Address Translation
Associative memory
Note that TLB must be flushed every time a new page table is selected (during context switching,
for example).
Memory Protection
Memory protection implemented by associating protection bit with each frame. Normally these
bits are kept in the page table. One bit can define a page to be read and write or read-only. Since
every reference to a memory location is through the page table, and protection bits can be
checked at the address translation time. An illegal operation causes a trap.
One more bit, Valid-invalid bit, is also attached to each entry in the page table:
valid indicates that the associated page is in the process logical address space, and is
thus a legal page.
invalid indicates that the page is not in the process logical address space.
Hierarchical Paging
Hashed Page Tables
Inverted Page Tables
A logical address (on 32-bit machine with 4K page size) is divided into:
a page number consisting of 20 bits.
a page offset consisting of 12 bits.
Since the page table is paged, the page number is further divided into:
a 10-bit page number.
a 10-bit page offset.
Thus, a logical address is as follows:
Page offset
Page number
P1
10
P2
10
12
where pi is an index into the outer page table, and p2 is the displacement within the page of the outer
page table.
Virtual page numbers are compared in this chain searching for a match. If a match is found, the
corresponding physical frame is extracted.
Shared Pages
Shared code
One copy of read-only (reentrant) code shared among processes (i.e., text editors,
compilers, window systems).
Shared code must appear in same location in the logical address space of all processes.
Advantages:
Efficient memory use
Simple partition management due to discontinuous loading and fixed partition size
No compaction is necessary
Easy to share pages
Disadvantages
Job size <= memory size
Internal fragmentation
Need special hardware for address translation
Some main memory is used for page table
Address translation lengthens memory cycle times
Segmentation
1
4
1
2
3
4
2
3
user
space
physical memory
space
The user specifies each address by two quantities: segment name/number and offset.
<segment-number, offset>,
Mapping from logical address to physical address is done with the help of a segment table.
Segment table maps two-dimensional physical addresses; each table entry has:
base contains the starting physical address where the segments reside in memory.
limit specifies the length of the segment.
Segment-table base register (STBR) points to the segment tables location in memory.
Segment-table length register (STLR) indicates number of segments used by a program;
Segment number s is legal if s < STLR.
Segmentation Example
Virtual Memory
Background
Virtual memory is a technique that allows execution of processes that may not be completely in
the physical memory.
Virtual Memory gives the illusion of more physical memory than there really is (via demand
paging)
Virtual memory provides many benefits:
Only part of the program needs to be in memory for execution.
Logical address space can therefore be much larger than physical address space.
Allows address spaces to be shared by several processes.
Allows for more efficient process creation.
Use of virtual memory is also justified for many reasons such as:
There may be some code (e.g. error code) that may never be executed.
Arrays, list and tables are often allocated more memory than they actually use.
Certain options and features of a program may be used rarely.
Virtual memory can be implemented via:
Demand paging
Demand segmentation
Program Execution in Virtual memory
Operating system brings into main memory a few pieces of the program
Resident set - portion of process that is in main memory
An interrupt is generated when an address is needed that is not in main memory
Operating system places the process in a blocking state
Piece of process that contains the logical address is brought into main memory
Operating system issues a disk I/O Read request
Another process is dispatched to run while the disk I/O takes place
An interrupt is issued when disk I/O complete which causes the operating system to
place the affected process in the Ready state
Demand Paging
When we want to execute a process, it is swapped into the memory. However, a pager
(swapper) does not bring the whole process into the memory. Only those pages, which are
needed, are brought into the memory. That is, bring a page into memory only when it is needed.
Often page 0 is loaded initially when a job is scheduled
In Demand Paging a programs working set is kept in memory, reference outside WS causes
corresponding code to be retrieved from disk (page fault)
Provides the illusion of virtual memory
Demand paging has the many benefits such as
Less I/O needed
Less memory needed
Faster response
More users
Page replacement find some page in memory, but not really in use, swap it out.
Algorithm
Performance want an algorithm which will result in minimum number of page faults
Same page may be brought into memory several times
+ swap page in
+ restart overhead )
Example
Page Replacement
Read the desired page into the (newly) free frame. Update the page and frame tables.
Restart the process.
FIFO Algorithm
Beladys Anomaly
An interesting observation if FIFO algorithm is that increasing the page frames may not decrease
the number of page faults.
Optimal Algorithm
According to optimal (OPT or MIN), the page that is to be replaced is the one that will not be
used for the longest period of time.
Difficult to implement because it requires future knowledge of the reference string.
LRU chooses a page for replacement that has not been used for the longest period of time.
Uses the recent past as an approximation of the near future
Counting Algorithms
Keep a counter of the number of references that have been made to each page. The following
two schemes can be used:
LFU Algorithm: Replaces page with smallest count is to be replaced.
MFU Algorithm: replace page with the largest value of count. It is based on the argument
that the page with the smallest count was probably just brought in and has yet to be
used.
Not very commonly used algorithm.
Allocation of Frames
Each process needs minimum number of pages. Minimum number of frames is defined by the
computer architecture.
Two major allocation schemes:
Fixed allocation
Priority allocation
Fixed Allocation
Equal allocation e.g., if 100 frames and 5 processes, give each 20 pages.
Proportional allocation Allocate according to the size of process.
Priority Allocation
Global replacement process selects a replacement frame from the set of all frames; one
process can take a frame from another.
Local replacement each process selects from only its own set of allocated frames.
Process Creation
Copy on Write
Copy-on-Write (COW) allows both parent and child processes to initially share the same pages in
memory.
If either process modifies a shared page, only then is the page copied.
COW allows more efficient process creation as only modified pages are copied.
Memory-mapped file I/O allows file I/O to be treated as routine memory access by mapping a
disk block to a page in memory.
A file is initially read using demand paging. A page-sized portion of the file is read from the file
system into a physical page. Subsequent reads/writes to/from the file are treated as ordinary
memory accesses.
Simplifies file access by treating file I/O through memory rather than read() write() system calls.
Also allows several processes to map the same file allowing the pages in memory to be shared
Advantages:
Can run program larger than physical memory
Allows higher multiprogramming level than pure paging
Efficient memory usage
No compaction is required
Portions of process that are never called are never loaded
Simple partition management due to discontinuous loading and fixed partition size
Easy to share pages
Disadvantages:
Internal fragmentation
Program turnaround time increases each time a page is replaced, then reloaded
Need special address translation hardware
Unit-2
File
System
Unit-2
I/O Management and Disk Scheduling
Categories of I/O Devices
Human readable
Used to communicate with the user
Printers
Video display terminals
Display
Keyboard
Mouse
Machine readable
Used to communicate with electronic equipment
Disk and tap drives
Sensors
Controllers
Communication
Used to communicate with remote devices
Digital line drivers
Modems
Data rate
May be differences of several orders of magnitude between the data transfer rates
Application
Disk used to store files requires file-management software
Disk used to store virtual memory pages needs special hardware and software to support
it
Terminal used by system administrator may have a higher priority
Complexity of control
Unit of transfer
Data may be transferred as a stream of bytes for a terminal or in larger blocks for a disk
Data representation
Encoding schemes
Error conditions
Devices respond to errors differently
Programmed I/O
Process is busy-waiting for the operation to complete
Interrupt-driven I/O
I/O command is issued
Processor continues executing instructions
I/O module sends an interrupt when done
I/O Hardware
Disk Structure
Disk I/O
Whenever a process needs I/O to or from a disk, it issues a system call to the operating system.
If the desired disk drive and controller is available, the request can be serviced
immediately other wise the request is placed in a queue.
Once an I/O completes, the OS can choose a pending request to serve next.
The operating system is responsible for using hardware efficiently - for the disk drives, this
means having a fast access time and disk bandwidth.
Disk bandwidth is the total number of bytes transferred, divided by the total time between the
first request for service and the completion of the last transfer.
Disk Scheduling
The I/O requests are served in the order in which they reach. See below (total head
movement=640 cylinders)
FCFS is a fair scheduling algorithm but not an optimal one.
Shortest-Seek-Time-First (SSTF)
Selects the request with the minimum seek time from the current head position.
SSTF scheduling is a form of SJF CPU scheduling
May cause starvation of some requests
Is not optimal.
Illustration shows total head movement of 236 cylinders.
SCAN Scheduling
The disk arm starts at one end of the disk, and moves toward the other end, servicing requests
until it gets to the other end of the disk, where the head movement is reversed and servicing
continues.
Sometimes called the elevator algorithm.
Illustration shows total head movement of 208 cylinders (head is moving towards cylinder 0).
C-SCAN Scheduling
C-Look Scheduling
Version of C-SCAN
Arm only goes as far as the last request in each direction, then reverses direction immediately,
without first going all the way to the end of the disk.
Disk Management
Low-level formatting, or physical formatting - Dividing a disk into sectors that the disk controller
can read and write.
To use a disk to hold files, the operating system still needs to record its own data structures on
the disk.
Partition the disk into one or more groups of cylinders.
Logical formatting or making a file system.
Boot block initializes system.
The bootstrap is stored in ROM.
Bootstrap loader program.
Bad sectors may be managed manually. For example MS-DOS format command does a logical
format and if it finds any bad sector, it writes a special value into FAT.
Sector sparing method may also used to handle bad blocks (as used in SCSI disks). The controller
maintains a list of bad sectors which is updated regularly. Low level formatting also sets aside
some spare sectors. The controller can be asked to replace each bad sector logically with one of
the spare sectors.
Contacts
Saurabh Patil
Saurabh.terna@gmail.com
09773445201