notes oscd

145
  Unit -1 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

Upload: sharma-sudhir

Post on 04-Feb-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 1/145

Unit -1

Introduction

What is an Operating System?

An operating system (OS) is the interface between the user and the hardware. Itimplements a virtual machine that is easier to program than bare hardware. An OS providesstandard services (an interface) which are implemented on the hardware, including: l Processes,CPU scheduling, memory management, file system, networking. The OS coordinates multipleapplications and users (multiple processes) in a fair and efficient manner. The goal in OSdevelopment is to make the machine convenient to use (a software engineering problem) andefficient (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 forapplication programs and acts as an intermediary between the computer user and the computerhardware.

Computer System

Page 2: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 2/145

(To Remember What OS)

A magician -- provides each user with the illusion of a dedicated machine with infinitememory and CPU timeA 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 succeedsA fast food restaurant-- provides a service everyone needs, always works the sameeverywhere (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)?

System design —tradeoffs between:o performance and convenience of these abstractions

o performance and simplicity of OSo functionality in hardware or software

Primary intersection point — OS is the point where hardware, software, programminglanguages, data structures, and algorithms all come together

“Operating systems are among the most complex pieces of software yet developed”,William Stallings, 1994

Requirement of Modern OS FunctionalityConcurrency:-

Multiple processes active at once. Processes can communicate. Processes may require mutually exclusive. access to some resource. CPU scheduling, resource management

Memory management:- Allocate memory to processes, move processes between disk and memory

File system:- allocate space for storage of programs and data on disk

Networks and distributed computing:- allow computers to work together

Security & protection

Page 3: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 3/145

Objectives of Operating System

Convenience – An operating system makes a computer more convenient to use.

Efficiency – An operating system allows the computer system resources to be used in an

efficient manner.

Ability to evolve – An operating system should permit the effective development, testing,and introduction of new system function without interfering with the service.

History of Operating System

The first and second generations (1945-1955, 1956-1965)

The only system program in early computers was a test program that had tried to locate trouble incomputer (that appeared the every hour because of law realibility).

Two p ersons are working to keep the “enormously large machines” in the working state. Hardware -engineer was responsible for electronical equipment, the main users were mathematicians withprogramming skills on machine language. There were not any economical value (price) related withthesecomputers.

“I think there is a world market for about five computers” Thomas J. Watson (1945)

Computes were still very expensive and only big corporations, major government agencies oruniversities could pay the multimillion-dollar price.

The number of users-programmers had increased and the problem appeared how to organize theefficient computer’s utilization.

Monitor

Memory layout for a resident monitor

Loader

Jobsequencing

Control cardinterpreter

User programarea

Page 4: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 4/145

The new group of personal was appeared: operators .

They were technicians on computer unit with the main task – to execute all operationsfaster than programmer could. A programmer first wrote the program on paper, then punch it oncards (for the second generation computers it existed special room and technicians). After this hebrought the cards to the input room and hands it to one of the operators. An operator executed alltechnical operations, went over to the printer and teared off the output and carried it to the outputroom, so that the programmer could collect it later. In this chain

Programmer operator computer

Much computer time was wasted in the interaction between operator and computer. Tospeed up processing two points were developed – magnetic tapes were used instead of cards, jobswith similar needs were batched together and were run through the computer as a group. Secondgeneration computers were used mostly for scientific and engineering calculations, andprogrammed in FORTRAN and ASSEMBLER languages.

The very simple “ batch operating system ” was always resident in memory, then normallyreads a steam of separate jobs, each with own control cards that predefine what the job does. Forthe 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 thecards. 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 aseries of software compatible machines. OS/360 and the similar third generation operating systemsproduced 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 commercialproblem 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.

Memory layout for a multi-programming system

Job 3

Job 2

Job 1

Page 5: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 5/145

The operating system picks and begins to execute one of the jobs in memory. Eventually, the jobmay have to wait for I/O operation to complete; the operating system simply switches to andexecutes 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 isCPU 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 havedecreased that made it possible for a single individual to have his own personal computer (PC). Themost powerful personal computers are usually called workstations. Two operating systems havedominated on the personal computers market: Microsoft MS DOS and UNIX .

But really PC’s operating systems were neither multi-user nor multi-tasking. The goals of theseoperating systems have changed with time: instead of maximize CPU and peripheral utilization; thesystems were designed for maximizing user convenience and responsiveness.

An interesting development that began during the mid- 1980s is the growth of networks of personalcomputers running network operating systems and distributed operating system.

In a network operating system each machine runs its own local operating system, and the users areaware of the existence of multiple computers, and can log in to remote machines and copy filesfrom one machine to another. These operating systems are not fundamentally different fromsingle-processor operating systems. They obviously need a network interface controller and somelow-level software to drive it, and some programs to achieve remote file access.

Evolution of Operating SystemsEase of Evolution of an Operating System

An operating system will evolve over time for a number of reasons:· Hardware upgrades plus new types of hardware – Early versions of UNIX did not employ pagingmechanism because they were run on machines without paging hardware. Most recent versionshave been modified to exploit paging capabilities.

· New Services – In response to user demand or in response to the needs of system managers, theoperating system will expand to offer new services.

· Fixes – An operating system may contain some faults which may be detected over time and fixeswill need to be made.

Page 6: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 6/145

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, thenthe 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 otherhand, the user might run into problems, or not finish in the allotted time, and be forced to stopbefore resolving the problem.· Setup time – A single program called a job involves loading the compiler, source program, savingcompiled program, and loading and linking together the object program and common functions.

2. Simple Batch System

Early computers were very expensive and therefore it was important to maximize the machine use.To improve use, the concept of batch processing was developed. Batch processing scheme use apiece of software known as the Monitor . With this type of operating system the user submits the job on cards or tape to a computer operator, who batches the jobs together sequentially and placesthe entire batch on an input device for use by the monitor.

The monitor controls the sequence of events. To do this much of the monitor always remains in themain memory referred as Resident monitor. The rest of the monitor consists of Utilities andcommon functions that are loaded as subroutines to the user program at the beginning of any jobthat require them. The monitor reads in jobs one at a time from the input device. As it read in, thecurrent job is placed in the user program area, and control is passed to this job. When the job iscompleted, it returns control to the monitor, which immediately reads in the next job.

The monitor handles the scheduling problem - a batch of job is queued up, and jobs are executed asrapidly as possible, with no intervening idle time. The monitor also handles the setup time – byhaving the instructions included in primitive form of Job Control Language (JCL), which is a specialtype of language used to provide instructions to the monitor regarding what compiler to use andwhat data to use.

Hardware Features

· Memory protectionWhile the user program is executing, it must not alter the memory area containing the monitor. Ifsuch an attempt is made, the processor hardware should detect an error and transfer the control tomonitor and job should be aborted by the monitor by displaying an error message.

· TimerA timer is used to prevent a single job from monopolizing the system. A timer is set at the beginningof the job. If the timer expires, an interrupt occurs and control returns to the monitor.

Page 7: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 7/145

· Privileged instructionsCertain instructions are designated as privileged and can be executed only by the monitor. If theprocessor 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 regainingcontrol from user programs.

3. Multi-programming

Multiprogramming needed for efficiency Single user cannot keep CPU and I/O devices busy at all times Multiprogramming organizes jobs (code and data) so CPU always has one to Execute A subset of total jobs in system is kept in memory One job selected and run via job scheduling When it has to wait (for I/O for example), OS switches to another job

Even though the job sequencing is done automatically by simple batch processing system, theprocessor is often idle. The problem is that I / O devices are slow compared to the processor. Morethan 96% of the time is spend waiting for I / O devices and during this time the processor will beidle. In order to overcome the inefficiency, the concept of multiprogramming evolved whichrequires a large memory space. Suppose if there is a room for one operating system and two userprograms, when one job needs to wait for I / O, the processor can switch to the other job, whichlikely is not waiting for I / O. It is known as Multiprogramming

Page 8: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 8/145

4. Time-Sharing Systems

Timesharing (multitasking) is logical extension in which CPU switches jobs sofrequently that users can interact with each job while it is running, creatinginteractive computing

Response time should be < 1 second

Each user has at least one program executing in memory [process If several jobs ready to run at the same time [ CPU scheduling If processes don’t fit in memory, swapping moves them in and out to run

Multiprogramming systems: several programs use the computer system

Time-sharing systems: several (human) users use the computer system interactively.

Characteristics:

Using multiprogramming to handle multiple interactive jobs Processor’s time is shared among multiple users Multiple users simultaneously access the system through terminals

Time sharing is multiprogramming. The key differences between time-sharing systems and batchmultiprogramming systems are given in the table above.

Multi-processor systems

Multi-processing is a technique that involves operating several processors in parallel to obtain ahigher calculation power than that obtained using a high-end processor or to increase theavailability of the system (in the event of processor breakdown).

The term SMP (Symmetric Multiprocessing or Symmetric Multiprocessor) refers to an architecturein which all processors access the same shared memory.

Commands entered atthe terminal

Job control languagecommands provided withthe job

Source of directivesto operating system

Minimize response time Maximize processor use Principal objective

Time Sharing Batch Multiprogramming

Page 9: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 9/145

A multiprocessor system must be able to manage memory sharing between several processors butalso to distribute the work load.

Distributed Systems

Distributed system deals with hardware and software systems containing more than oneprocessing or storage element, concurrent processes, or multiple programs, running under a looselyor tightly controlled regime. In distributed computing a program is split up into parts that runsimultaneously on multiple computers communicating over a network. Distributed programs oftenmust deal with heterogeneous environments, network links of varying latencies, and unpredictablefailures in the network or the computers.

The OS can support a distributed file system on a distributed system. There are manydifferent types of distributed computing systems and many challenges to overcome in successfullydesigning one. The main goal of a distributed computing system is to connect users and resources ina transparent, open, and scalable way. Ideally this arrangement is more fault tolerant and morepowerful than using stand-alone computer systems.

Examples of distributed systems include networked _le systems, many web applications such asFacebook or twitter, or an instant messaging system. Distributed systems can be built using lowlevel socket communication or higher level abstractions such as remote procedure calls.

Real time systems

Real time systems , used mainly in industry, are systems designed to operate in a time-constrainedenvironment. A real time system must also operate reliably according to specific time constraints; inother words, it must be able to properly process information received at clearly-defined intervals(regular or otherwise).

Here are some examples of real time operating systems:

OS-9; RTLinux (RealTime Linux); QNX; VxWorks .

Page 10: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 10/145

More Recent Developments in operating system

Parallel operating systems: - Shared memory, shared clock, Large number of tightly-coupledprocessors. Appearance of single operating system

Distributed operating systems: - No shared memory, no shared clock, Small number of loosely-

coupled processors. Appearance of single operating system is ideal goal, but not realized in practice.

Real-time operating systems: - Meet hard / soft real-time constraints on processing of data.

Page 11: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 11/145

Unit-1

Components of Operating System

Operating system Components

Even though, not all systems have the same structure many modern operating systemsshare 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 tosystem programs like printer spooler, name servers, file server etc. Each of these activities isencapsulated 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 aprogram in execution. There are many processes can be running the same program. The five majoractivities of an operating system in regard to process management are

Creation and deletion of user and system processes.

Suspension and resumption of processes.

A mechanism for process synchronization.

A mechanism for process communication.

A mechanism for deadlock handling.

2. Main-Memory Management

Primary-Memory or Main-Memory is a large array of words or bytes. Each word or byte hasits own address. Main-memory provides storage that can be access directly by the CPU. That is tosay for a program to be executed, it must in the main memory.

The major activities of an operating in regard to memory-management are: 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.

Allocate and deallocate memory space as needed.

3. File Management

Page 12: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 12/145

A file is a collected of related information defined by its creator. Computer can store files on thedisk (secondary storage), which provide long term storage. Some examples of storage media aremagnetic tape, magnetic disk and optical disk. Each of these media has its own properties likespeed, capacity, data transfer rate and access methods.

A file system normally organized into directories to ease their use. These directories maycontain 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 thedevice 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 accommodateall data and programs, and its data are lost when power is lost, the computer system must providesecondary storage to back up main memory. Secondary storage consists of tapes, disks, and othermedia 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 anaddress 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

A distributed system is a collection of processors that do not share memory, peripheraldevices, or a clock. The processors communicate with one another through communication lines

Page 13: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 13/145

called network. The communication-network design must consider routing and connectionstrategies, 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. Protectionrefers to mechanism for controlling the access of programs, processes, or users to the resourcesdefined by a computer systems.

8. Command Interpreter System

A command interpreter is an interface of the operating system with the user. The user givescommands with are executed by operating system (usually by turning them into system calls). Themain 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 inkernel mode. There are two main advantages to separating the command interpreter from thekernel.

If we want to change the way the command interpreter looks, i.e., I want to change theinterface of command interpreter, I am able to do that if the command interpreter is separate fromthe 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 accessto 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.

Services of Operating System

(Draw the diagram given in class note book)

Operating-system services provide functions that are helpful to the user.

1. User interface: - Almost all operating systems have a user interface (UI). This interfacecan take several forms. One is a command-line interface (CLI), which uses textcommands and a method for entering them (say, a program to allow entering and

editing of commands). Most commonly graphical user interface (GUI) is used.

2. Program execution: - The system must be able to load a program into memory and torun that program. The program must be able to end its execution, either normally orabnormally (indicating error).

3. I/O operations:- A running program may require I/O, which may involve a file or an I/Odevice. For specific devices, special functions may be desired (such as recording to a CDor DVD drive or blanking a CRT screen). For efficiency and protection, users usually

Page 14: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 14/145

cannot control I/O devices directly. Therefore, the operating system must provide ameans to do I/O.

4. File-system manipulation:- The file system is of particular interest. Obviously, programsneed to read and write files and directories. They also need to create and delete themby 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 fileownership.

5. Communications: - There are many circumstances in which one process needs toexchange information with another process. Such communication may occur betweenprocesses that are executing on the same computer or between processes that areexecuting 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 powerfailure), 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). Foreach type of error, the operating system should take the appropriate action to ensurecorrect and consistent computing. Debugging facilities can greatly enhance the user'sand programmer's abilities to use the system efficiently.

7. Resource allocation: - When there are multiple users or multiple jobs running at thesame time, resources must be allocated to each of them. Many different types ofresources are managed by the operating system. Some (such as CPU cycles, mainmemory, and file storage) may have special allocation code, whereas others (such as I/Odevices) may have much more general request and release code. For instance, indetermining how best to use the CPU, operating systems have CPU-scheduling routinesthat take into account the speed of the CPU, the jobs that must be executed, thenumber of registers available, and other factors. There may also be routines to allocateprinters, modems, USB storage drives, and other peripheral devices.

8. Accounting: - We want to keep track of which users use how much and what kinds ofcomputer resources. This record keeping may be used for accounting (so that users canbe billed) or simply for accumulating usage statistics. Usage statistics may be a valuabletool 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 networkedcomputer system may want to control use of that information. When several separateprocesses execute concurrently, it should not be possible for one process to interferewith the others or with the operating system itself. Protection involves ensuring that allaccess to system resources is controlled.

Page 15: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 15/145

System Call

Process traps to OS Interrupt Handler Supervisor/ Kernel mode is set Desired function executed Returns to application

System calls provide an interface to the services made available by an operating system.

Example to illustrate how system calls are used:

Writing a simple program to read data from one file and copy them to another file. The first inputthat the program will need is the names of the two files: the input file and the output file. Thesenames can be specified in many ways, depending on the operating-system design.

One approach is for the program to ask the user for the names of the two files. In an interactivesystem, this approach will require a sequence of system calls, first to write a prompting message onthe screen and then to read from the keyboard the characters that define the two files.

On mouse-based and icon-based systems, a menu of file names is usually displayed in a window.The user can then use the mouse to select the source name, and a window can be opened for thedestination name to be specified. This sequence requires many I/O system calls.

Types of System Calls

System calls can be grouped roughly into five major categories: process control, file manipulation,device manipulation, information maintenance, and communications.

Page 16: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 16/145

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

User mode and kernel mode

Dual-mode operation allows OS to protect itself and other system components Provides ability to distinguish when system is running user code or kernel code

Some instructions designated as privileged , only executable in kernel mode

System call changes mode to kernel, return from call resets it to user

Page 17: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 17/145

Unit-1

System programs and Operating System Structure

Structure of operating SystemsAs 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 ofpossibilities. These are by no mean s exhaustive, but they give an idea of some designs that havebeen tried in practice.

Monolithic Systems:

This approach well kno wn 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 singleprivileged entity running on the machine. Functionality in a monolithic kernel can be broken up intomodules; 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 bringdown the whole system. However, when the implementation is complete and trustworthy, the tightinternal integration of components allows the low-level features of the underlying system to beeffectively utilized, making a good monolithic kernel highly efficient.

In a monolithic kernel, all the systems such as the file system management run in an areacalled the kernel mode. The main problem with this organization is maintainability. Examples - Unix-like 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 lowerlayers. The bottom layer (layer 0), is the hardware; the highest (layer N) is the userinterface.

With modularity, layers are selected such that each uses functions (operations) and servicesof only lower-level layers

An OS layer is an implementation of an abstract object that is the encapsulation of data andoperations that can manipulate those data. These operations (routines) can be invoked byhigher-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 levellayers.

The major difficulty is appropriate definition of various layers.

Page 18: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 18/145

General OS Layers

Microkernel System Structure A microkernel is a minimal computer operating system kernel which, in its purest form, provides

no operating system services at all, only the mechanisms needed to implement such services, suchas low-level address space management, thread management, and inter-process communication(IPC). The actual operating system services are instead provided by \user-mode" servers. Theseinclude device drivers, protocol stacks, file systems and memory management code. A microkernelhas the benefit of allowing for very customizable and modular operating systems. This greatlysimplifies development of the OS since different modules is completely independent. However, notmany systems use microkernel due to performance concerns caused by the overhead of frequentcontext switches between user and kernel mode.

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 actualoperating-system services are provided by "user-mode " servers.

*****(Please Refer Class Note Book for university Question Paper)*****

Page 19: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 19/145

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 sharedamongst 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 totheir process without any interruption. This requires clever management and allocation of theprocessor 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. Wealso 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, let’s imagine that we have written aprogram called my_prog.c in C. On execution, this program may read in some data and output somedata. Note that when a program is written and a file is prepared, it is still a script. It has no dynamicsof its own i.e., it cannot cause any input processing or output to happen. Once we compile, and stilllater when we run this program, the intended operations take place. In other words, a program is atext script with no dynamic behavior. When a program is in execution, the script is acted upon. Itcan result in engaging a processor for some processing and it can also engage in I/O operations. It isfor 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 – it’s a program in execution. When we compile a Clanguage program we get an a. out file which is an executable file. When we seek to run this file wesee the program in execution.

Processor Utilization:Processor Utilization: A processor is a central and a key element of a computer system. Thisis so because all information processing gets done in a processor. So a computer's throughputdepends 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. Insuch a situation the processor is idling for very long periods of time. This is so because IO andcommunication to devices (including memory) takes so much longer.

Page 20: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 20/145

In figure above we see that during intervals when prog_A is not engaging the processor wecan utilize the processor to run another ready to run program. The processor now processes twoprograms without significantly sacrificing the time required to process prog_A. Note that we mayhave a small overhead in switching the context of use of a processor. However, multiprogrammingresults in improving the utilization of computer's resources. In this example, with multiple programsresiding 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 atsome acceptable level. Otherwise the advantage of giving access to, and sharing, the resourcewould be lost. A system which we use to access book information in a library is a time-sharedsystem. Clearly, the response time should be such that it should be acceptable, say a few seconds. Alibrary system is also an online system .In online system; devices (which can includeinstrumentation in a plant) are continuously monitored (observed) by the computer system. If in anonline system the response time is also within some acceptable limits then we say it is a real-timesystem.

Process Control Block

Each process is represented in the operatingsystem by a process control block

(PCB)—also called a task control block.

A PCB is shown in Figure. It contains manypieces of information associated with a specificprocess, including these:

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 forthis process.

Page 21: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 21/145

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, plusany condition-code information. Along with the program counter, this state information must besaved 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 valueof the base and limit registers, the page tables, or the segment tables, depending on thememory system used by the operating system.

6. Accounting information. This information includes the amount of CPU and real time used, timelimits, 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 theswitching 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-programmingenvironment. We have earlier seen that only ready-to-run, main memory resident processescan be scheduled for execution. For instance, each process in execution uses the programcounter, registers and other resources within the CPU. So, whenever a process is switched, theOS moves out, and brings in, considerable amount of context switching information.

We have process P1 currently executing (note that the program counter is pointing inexecutable 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) inits own context area.

o All context information about process P2 must be downloaded (restore) in itsown context area.

The program counter should have an address value to an instruction of processP2 and process P2 must be now marked as “running".

Information stored during context switching is:

1. Program counter2. Registers (like stack, index etc.) currently in use

Page 22: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 22/145

3. Changed state (changed from Running to ready-to-run)4. The base and limit register values5. IO status (files opened; IO blocked or completed etc.)6. Accounting7. Scheduling information8. Any other relevant information.

***Diagram for context switching is in class note book.***

Process State

The operating system ’s principal responsibility is in controlling the execution ofprocesses. This includes determining the interleaving pattern for execution and allocation ofresources to processes. One part of designing an OS is to describe the behavior that we wouldlike each process to exhibit. The simplest model is based on the fact that a process is eitherbeing 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 asNOT RUNNING, and is placed into a queue in the system in the NOT RUNNING state. Theprocess (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 beinterrupted, and moved from the RUNNING state to the NOT RUNNING state, making theprocessor available for a different process. The dispatch portion of the OS will then select, fromthe queue of NOT RUNNING processes, one of the waiting processes to transfer to theprocessor. The chosen process is then relabeled from a NOT RUNNING state to a RUNNINGstate, and its execution is either begun if it is a new process, or is resumed if it is a processwhich was interrupted at an earlier time.

A Queuing Model:

Page 23: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 23/145

A Queuing Model: Data structures play an important role in management of processes.In general an OS may use more than one data structure in the management of processes. It maymaintain a queue for all ready to run processes. It may maintain separate queues for blockedprocesses. It may even have a separate queue for each of the likely events (includingcompletion of IO). This formulation shown in the figure below is a very flexible model useful inmodeling computer system operations. This type of model helps in the study and analysis ofchosen OS policies.

Drawback of two state model:-

Although the two-state process management model is a perfectly valid design for anoperating system, the absence of a BLOCKED state means that the processor lies idle when theactive process changes from CPU cycles to I/O cycles. This design does not make efficient use ofthe processor.

Five State Model

Five State Model

The state of a process is defined in part by the current activity of that process. Each processmay be in one of the following states:

• New: The process is being created.

Page 24: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 24/145

• Running: Instructions are being executed.

• Blocked: The process is waiting for some event to occur (such as an I/O completion orreception of a signal).

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

• Terminated/Exit: The process has finished execution.

List down the possible transition

1. Null New2. New Ready3. Ready Running4. Running Ready

5.

Running Blocked6. Blocked Ready7. Ready Exit, Blocked Exit.

Explain each transition for 10 marks.

Queuing Model for Five State Model

Five state model with two suspend state:

Page 25: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 25/145

Five State Model with Two Suspend State.

Transitions are as follows:

BLOCKED → BLOCKED SUSPEND

If a process in the RUNNING state requires more memory, then at least

one BLOCKED process can be swapped out of memory onto disk. The transitioncan also be made for the BLOCKED process if there are READY processesavailable, and the OS determines that the READY process that it would like todispatch requires more main memory to maintain adequate performance.

BLOCKED SUSPEND → READY SUSPEND

A process in the BLOCKED SUSPEND state is moved to the READYSUSPEND state when the event for which it has been waiting occurs. Note thatthis requires that the state information concerning suspended processes beaccessible to the OS.

READY SUSPEND → READY

When there are no READY processes in main memory, the OS will need tobring one in to continue execution. In addition, it might be the case that aprocess in the READY SUSPEND state has higher priority than any of theprocesses in the READY state. In that case, the OS designer may dictate that it ismore important to get at the higher priority process than to minimize swapping.

Page 26: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 26/145

READY → READY SUSPEND

Normally, the OS would be designed so that the preference would be tosuspend a BLOCKED process rather than a READY one. This is because the READYprocess can be executed as soon as the CPU becomes available for it, whereasthe BLOCKED process is taking up main memory space and cannot be executedsince it is waiting on some other event to occur. However, it may be necessary tosuspend a READY process if that is the only way to free a sufficiently large blockof main memory. Finally, the OS may choose to suspend a lower-priority READYprocess rather than a higher-priority BLOCKED process if it believes that theBLOCKED process will be ready soon.

Operation on Thread

The processes in most systems can execute concurrently, and they may be created and deleteddynamically. 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 sleepingbut 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

User logs on. User starts a program. Operating systems creates process to provide service, e.g., to manage printer. Some program starts another process, e.g., Netscape calls xv to display a picture.

Page 27: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 27/145

Process Termination

A process terminates when it finishes executing its last statement. Its resources are returned to thesystem, 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 theexisting process, usually due to following reasons:

Normal Exist Most processes terminates because they have done their job. This call isexist in UNIX.

Error Exist When process discovers a fatal error. For example, a user tries to compile aprogram that does not exist.

Fatal Error An error caused by process due to a bug in program for example, executing anillegal instruction, referring non-existing memory or dividing by zero.

Killed by another Process A process executes a system call telling the Operating Systemsto terminate some other process. In UNIX, this call is kill. In some systems when a processkills all processes it created are killed as well (UNIX does not work this way).

Page 28: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 28/145

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 calledlightweight processes . In a process, threads allow multiple executions of streams. In manyrespect, threads are popular way to improve application through parallelism.

The CPU switches rapidly back and forth among the threads giving illusion that the threads arerunning 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 adifferent 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. Athread 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 otherthreads their code section, data section, OS resources also known as task, such as open filesand signals.

Processes Vs Threads

As we mentioned earlier that in many respect threads operate in the same way as that ofprocesses. 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.

Page 29: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 29/145

Differences

Unlike processes, threads are not independent of one another. Unlike processes, all threads can access every address in the task. Unlike processes, threads are design to assist one other. Note that processes might or

might not assist one another because processes may originate from different users.

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 operatingsystem resources.

3. Context switching is fast when working with threads. The reason is that we only have tosave and/or restore PC, SP and registers.

But this cheapness does not come free - the biggest drawback is that there is no protectionbetween threads.

User-Level Threads

User-level threads implement in user-level libraries, rather than via systems calls, so threadswitching 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 single-threaded processes.

Advantages:

The most obvious advantage of this technique is that a user-level threads package can beimplemented on an Operating System that does not support threads. Some other advantagesare

A user-level thread does not require modification to operating systems. Simple Representation:

Each thread is represented simply by a PC, registers, stack and a small control block, allstored in the user process address space.

Page 30: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 30/145

Simple Management:

This simply means that creating a thread, switching between threads andsynchronization between threads can all be done without intervention of the kernel.

Fast and Efficient:

Thread switching is not much more expensive than a procedure call.

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 1000threads 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 theprocesses. 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 inthis case. Instead of thread table in each process, the kernel has a thread table that keeps track ofall threads in the system. In addition, the kernel also maintains the traditional process table to keeptrack 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 toa 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 arehundreds of times slower than that of user-level threads.

Since kernel must manage and schedule threads as well as processes. It require a full threadcontrol block (TCB) for each thread to maintain information about threads. As a result thereis significant overhead and increased in kernel complexity.

Advantages of Threads over Multiple Processes

Context Switching Threads are very inexpensive to create and destroy, and they areinexpensive to represent. For example, they require space to store, the PC, the SP, and thegeneral-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

Page 31: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 31/145

faster to switch between threads. In other words, it is relatively easier for a context switchusing threads.

Sharing Treads allow the sharing of a lot resources that cannot be shared in process, forexample, sharing code section, data section, Operating System resources like open file etc.

Disadvantages of Threads over Multiprocesses

Blocking The major disadvantage if that if the kernel is single threaded, a system call ofone 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 ofsecurity. It is quite possible that one thread over writes the stack of another thread (ordamaged shared data) although it is very unlikely since threads are meant to cooperate on asingle task.

Application that Benefits from Threads

A proxy server satisfying the requests for a number of computers on a LAN would be benefited by a

multi-threaded process. In general, any program that has to do more than one task at a time couldbenefit from multitasking.

For example, a program that reads input, process it, and outputs could have three threads, one foreach task.

Application that cannot Benefit from Threads

Any sequential process that cannot be divided into parallel task will not benefit from thread, as theywould block until the previous one completes.

For example, a program that displays the time of the day would not benefit from multiple.

Page 32: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 32/145

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 increasingresponsiveness to the user.

2. Resource sharing. By default, threads share the memory and the resources of the processto which they belong. The benefit of sharing code and data is that it allows an application tohave several different threads of activity within the same address space.

3. Economy. Allocating memory and resources for process creation is costly. Because threadsshare resources of the process to which they belong, it is more economical to create andcontext-switch threads.

4. Utilization of multiprocessor architectures. The benefits of multithreading can be greatlyincreased in a multiprocessor architecture, where threads may be running in parallel ondifferent processors. A single threaded process can only run on one CPU, no matter howmany 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 commonways of establishing this relationship.

Page 33: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 33/145

Many-to-One Model

The many-to-one model maps many user-level threads to one kernel thread. Thread management isdone by the thread library in user space, so it is efficient; but the entire process will block if a threadmakes 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 thanthe many-to-one model by allowing another thread to run when a thread makes a blocking systemcall; 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 correspondingkernel thread. Because the overhead of creating kernel threads can burden the performance of anapplication, most implementations of this model restrict the number of threads supported by thesystem.

One-to-One Model

Many-to-Many Model

The many-to-many model multiplexes many user-level threads to a smaller or equal number ofkernel threads. Whereas the many-to-one model allows the developer to create as many user

Page 34: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 34/145

threads as she wishes, true concurrency is not gained because the kernel can schedule only onethread at a time.

The one-to-one model allows for greater concurrency, but the developer has to be careful not tocreate too many threads within an application (and in some instances may be limited in the numberof 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 threadscan run in parallel on a multiprocessor.

When a thread performs a blocking system call, the kernel can schedule another thread forexecution.

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 thenew 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 thefork ( ) system call.

If a thread invokes the exec ( ) system call, the program specified in the parameter to exec ( )will replace the entire process —including all threads.

Page 35: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 35/145

Thread cancellation

Thread cancellation is the task of terminating a thread before it has completed. For example, ifmultiple threads are concurrently searching through a database 1 and one thread returns theresult, the remaining threads might be canceled.

A thread that is to be canceled is often referred to as the target thread. Cancellation of a targetthread 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. Asignal may be received either synchronously or asynchronously, depending on the source ofand 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 solutionto 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 andplace them into a pool, where they sit and wait for work. When a server receives a request, itawakens a thread from this pool —if one is available —and passes it the request to service. Oncethe thread completes its service, it returns to the pool and awaits more work. If the poolcontains 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 createa 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 concurrentthreads.

Page 36: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 36/145

Unit 1CPU 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 toterminate 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.

Page 37: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 37/145

Histogram of CPU-burst Times

Types of Scheduling

The key to the multiprogramming is scheduling. There are four types of scheduling thatan 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 aqueue 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 adecision 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-outprocess.

o Short-Term Scheduling A decision of which ready process to execute next is made in short-term

scheduling.o I/O Scheduling

Page 38: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 38/145

The decision as to which process’s pending I/O requests shall be handledby the available I/O device is made in I/O scheduling.

CPU Scheduler

Whenever, the CPU becomes idle, the OS must select one of the processes in the ready-queue to be executed.

The selection process is carried out the short-term scheduler or CPU scheduler. The CPUscheduler selects a process from the ready queue and allocates the CPU to that process.

CPU scheduling decisions may take place when a process:

1. The running process changes from running to waiting state (current CPU burst ofthat process is over).

2. The running process terminates3. A waiting process becomes ready (new CPU burst of that process begins)4. The current process switches from running to ready stat (e.g. because of timer

interrupt).

Scheduling under 1 and 2 is nonpreemptive .

Once a process is in the running state, it will continue until it terminates orblocks itself.

Scheduling under 1 and 2 is preemptive .

Page 39: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 39/145

Currently running process may be interrupted and moved to the Ready state byOS.

Allows for better service since any one process cannot monopolize the processorfor very long

Dispatcher

Dispatcher module gives control of the CPU to the process selected by the short-termscheduler; 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 anotherrunning.

What is a Good Scheduler? Criteria

User oriented Turnaround time : time interval from submission of job until its completion. It

includes actual processing time plus time spent waiting for resources, includingthe processor. OR the amount of time between moment a processfirst enters Ready State and the moment the process exits RunningState for the last time (Completed).

Waiting time : sum of periods spent waiting in ready queue.

Page 40: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 40/145

Response time : time interval from submission of job to first response. Often aprocess can begin producing some output to the user while continuing toprocess the request. Thus this is a better measure than turnaround time fromthe user’s 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 from0 to 100%. In a real system, it should range from 40% to 90%.

Throughput : number of jobs completed per time unit. This depends on theaverage length of the processes.

Service time, wait time, turn around time, and throughput are some of the metrics usedto compare scheduling algorithms.

Any good scheduler should: Maximize CPU utilization and throughput Minimize turnaround time, waiting time, response time

Goals of Scheduling Algorithm for Different Systems

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.

Page 41: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 41/145

Scheduling Algorithms

CPU scheduling deals with the problem of deciding which of the processes in the readyqueue is to be allocated the CPU. There are many different CPU scheduling algorithms,which we will discuss now.

First-Come First-Served (FCFS) Scheduling

The process that requests the CPU first is allocated the CPU first. It is nonpreemptivealgorithm.

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)

Page 42: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 42/145

Example 1:

Process Burst Time

P124

P23

P33

Suppose that the processes arrive in the order: P1, P2,P3.The Gantt Chart for theschedule is:

Waiting time for P1 = 0; P2 = 24; P3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17

P1

P2

P3 24 27 300

Page 43: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 43/145

Suppose that the processes arrive in the order P2, P3, P1. The Gantt chart for theschedule is:

Waiting time for P1 = 6 ; P2 = 0 ; P3 = 3 Average waiting time: (6 + 0 + 3)/3 = 3

Example 2:

Consider the following set of processes:

Process Arrival Time Service Time

P1 0 3

P22 6

P34 4

P46 5

P58 2

Calculate waiting time, average waiting time, and turnaround time.(Solve as a practice example)

P 1 P 3 P 2 63 300

Page 44: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 44/145

Shortest-Job First Scheduling (SJF)

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.

Preemptive SJF – Shortest Remaining Time First

Preemptive version of SJF. If a new process arrives with CPU burst length less than remaining time of current

executing process, preempt the currently executing process and allocate the CPU to thenew process.

Advantages: Minimizes average waiting times.

Problems: How to determine length of next CPU burst? Problem: starvation of jobs with long CPU bursts.

Page 45: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 45/145

Example:

Process Arrival Time Burst Time

P1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4

SJF (non-preemptive)

Average waiting time = (0 + 6 + 3 + 7)/4 = 4

P 1 P 3 P 2

73 160

P 4

8 12

Page 46: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 46/145

SRT (preemptive SJB)

Average waiting time = (9 + 1 + 0 +2)/4 = 3

Determining Length of the Next CPU burst in SJF

Can only estimate the length. Can be done by using the length of previous CPU bursts, using exponential averaging.

P 1 P 3 P 2

42 110

P 4

5 7

P 2 P 1

16

:Define 4.10, 3.

burstCPUnextthefor valuepredicted 2.

burstCPUof lenghtactual 1.

1n

thn nt

.t nnn 11

Page 47: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 47/145

Priority Scheduling

In priority scheduling, a priority (an integer) is associated with each process. Priorities can be assigned either externally or internally. The CPU is allocated to the process with the highest priority (smallest integer means

highest priority). Priority scheduling can be:

Preemptive Nonpreemptive

Problem Starvation or indefinite blocking – low priority processes may never execute.

Solution Aging – as time progresses increase the priority of the process.

Page 48: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 48/145

Conceptual and Implementation view of Priority Scheduling

Example:

Process CPU Burst Priority

P110 3

P21 1

P32 3

P41 4

P55 2

(Solve as a example)

Round-Robin Scheduling

Page 49: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 49/145

Each process gets a small unit of CPU time (called time quantum ), usually 10-100milliseconds.

After this time has elapsed, the process is preempted and added to the end of the readyqueue.

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 schedulerpicks 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. Acontext switch will be executed and the process will be put at the tail of the readyqueue. 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, theneach 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

Page 50: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 50/145

P2 17

P3 68

P4 24

Time Quantum: 20

The Gantt chart is:

RR With Context Switching Overhead

In RR, context switching overhead should be considered.

P 1 P 2 P 3 P 4 P 1 P 3 P 4 P 1 P 3 P 3

0 20 37 57 77 97 117 121 134 154 162

Page 51: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 51/145

RR and Time Quantum

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 contextswitching.

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.

Turnaround time varies with the time quantum

Page 52: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 52/145

Multilevel Queue Scheduling

Ready queue is partitioned into separate queues. Each queue may have its own scheduling algorithm.

Page 53: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 53/145

A multilevel queue scheduling with absolute priory

Scheduling must be done between the queues. Fixed priority scheduling ; (i.e., serve all from foreground then from background).

Possibility of starvation. Time slice – each queue gets a certain amount of CPU time which it can schedule

amongst its processes; i.e., 80% to foreground in RR and 20% to background inFCFS.

Multilevel Feedback Queue Scheduling

A process can move between the various queues; aging can be implemented this way.

Page 54: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 54/145

Multilevel-feedback-queue scheduler defined by the following parameters: Number of queues Scheduling algorithms for each queue Method used to determine when to upgrade a process Method used to determine when to demote a process Method used to determine which queue a process will enter when that process

needs serviceExample

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

Deterministic modeling – takes a particular predetermined workload and defines theperformance of each algorithm for that workload.

Queueing models Implementation

Page 55: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 55/145

Process Synchronization

Currency arises in three different contexts:

Multiple applications – Multiple programs are allowed to dynamically shareprocessing time.

Structured applications – Some applications can be effectively programmed as aset of concurrent processes.

Operating system structure – The OS themselves are implemented as set ofprocesses.

Concurrent processes (or threads) often need access to shared data and sharedresources.

Processes use and update shared data such as shared variables, files, and data

bases.

Writing must be mutually exclusive to prevent a condition leading to inconsistent dataviews.

Maintaining data consistency requires mechanisms to ensure the orderly execution ofcooperating processes.

Example 1

Shared-memory solution to bounded-butter problem allows at most n – 1 items inbuffer at the same time. A solution, where all N buffers are used is not simple.

Suppose that we modify the producer-consumer code by dding a variable counter ,initialized to 0 and incremented each time a new item is added to the buffer.

Shared data

#define BUFFER_SIZE 10typedef struct {

. . .} item;

item buffer[BUFFER_SIZE];int in = 0;int out = 0;int counter = 0;

Page 56: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 56/145

Producer process

item nextProduced; while (1){

while (counter == BUFFER_SIZE); /* do nothing */

buffer[in] = nextProduced;in = (in + 1) % BUFFER_SIZE;counter++;

}

Consumer process

item nextConsumed; while (1){

while (counter == 0); /* do nothing */nextConsumed = buffer[out];out = (out + 1) % BUFFER_SIZE;counter--;

}

The statements

counter++;counter--;

must be performed atomically .

Atomic operation means an operation that completes in its entirety withoutinterruption.

The statement “ count++ ” may be implemented in machine language as:

register1 = counter

register1 = register1 + 1counter = register1

The statement “ count —” may be implemented as:

register2 = counterregister2 = register2 – 1

counter = register2

If both the producer and consumer attempt to update the buffer concurrently, theassembly language statements may get interleaved.

Interleaving depends upon how the producer and consumer processes are scheduled.

Page 57: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 57/145

Assume counter is initially 5. One interleaving of statements is:

producer: register1=counter (register1=5 )

producer: register1=register1+1 (register1=6 )

consumer: register2=counter (register2 = 5 )

consumer: register2=register2 –1 (register2 = 4 )

producer: counter=register1 (counter = 6 )

consumer: counter=register2 (counter = 4 )

The value of count may be either 4 or 6, where the correct result should be 5.

Example 2: Spooler Directory

Page 58: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 58/145

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 processfinishes last

The actions performed by concurrent processeswill then depend on the order in which theirexecution is interleaved.

To prevent race conditions , concurrent processes mustbe coordinated or synchronized .

It means that neither process will proceedbeyond a certain point in the computation untilboth 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

Page 59: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 59/145

Critical Section and Race Condition

Multiprogramming allows logical parallelism, uses devices efficiently - but we losecorrectness when there is a race condition. So we forbid logical parallelism inside critical section

– We lose some parallelism but we regain correctness.

Wherein Lies the Problem

The problem stems from interruption of software-based process while executing criticalsection (low-level).

The Critical-Section Problem

1. The critical-section problem is to design a protocol that the processes can cooperate.The protocol must ensure that when one process is executing in its critical section, noother process is allowed to execute in its critical section.

2. The critical section problem is to design a protocol that the processes can use so thattheir action will not depend on the order in which their execution is interleaved(possibly on many processors).

Page 60: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 60/145

Solution to Critical Section Problem – Requirements (

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 otherprocesses 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 someprocesses that wish to enter their critical section, then the selection of the

processes that will enter the critical section next cannot be postponedindefinitely. 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 otherprocesses are allowed to enter their critical sections after a process has made arequest 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 solutionsLinguistic constructs provided as part of a language.

Page 61: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 61/145

Mutual Exclusion

Four Requirements of Mutual Exclusion

:

Solution for Mutual Exclusion

Software approaches

Whether in a uniprocessor system or a multiprocessor one with shared main memory, it usuallyassumed that only one access to a memory location can be made at a time, so that the simplestoperation, an assignment to a location, is always performed without interference.

Page 62: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 62/145

Page 63: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 63/145

Page 64: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 64/145

Page 65: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 65/145

Dekker Algorithm

Page 66: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 66/145

Peterson Algorithm

Page 67: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 67/145

Unit 1Tools 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 beaccessed through 2 atomic and mutually exclusive operations:

– wait(S) – signal(S)

Wait and Signal Operations

wait(S)

{while (S 0 do) ;S--;

}

signal(S)

{S++;

}

Modification to the value of semaphore (S) in the wait and signal is executedindividually.

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 cooperateby means of simple signals such that a process can be forced to stop at a specified placeuntil it has received a specified signal.

Page 68: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 68/145

Usage 1: Critical Section of n Processes Problem

Shared data:

semaphore mutex; //initially mutex =1

Process Pi:

do {

wait(mutex);

/*critical section*/

signal(mutex);remainder section

} while (1);

Usage 2: synchronization of 2 processes

Suppose that we have two processes, P1 with statement s 1 and P2 with statement s 2. Werequire that s 2 be executed only after s 1 has completed. This can be done with a sharedsemaphore variable, mutex, initialized to 0, and by inserting the following statements in

P1

s1;

signal(mutex);

And the following statements in P 2

waite(mutex)

s2;

Page 69: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 69/145

Semaphore Implementation

The semaphore discussed so far requires a busy waiting. That is if a process is in critical-section, the other process that tries to enter its critical-section must loop continuouslyin the entry code.

To overcome the busy waiting problem, the definition of the semaphore operations waitand signal should be modified.

When a process executes the wait operation and finds that the semaphore valueis not positive, the process can block itself. The block operation places theprocess into a waiting queue associated with the semaphore.

A process that is blocked waiting on a semaphore should be restarted whensome other process executes a signal operation. The blocked process should berestarted 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;

Assume two simple operations: block suspends the process that invokes it. wakeup( P ) resumes the execution of a blocked process P.

Semaphore operations now defined as

wait (S):S.value--;if (S.value < 0){add this process to S.L;block;}

signal (S):S.value++;if (S.value <= 0){remove a process P from S.L;wakeup(P);}

Page 70: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 70/145

Types of Semaphores

Counting semaphore – integer value can range over an unrestricted domain. Binary semaphore – integer value can range only between 0 and 1; can be simplerto implement.

Can implement a counting semaphore S as a binary semaphore.

Implementing S as a Binary Semaphore

Data structures:

binary-semaphore S1, S2;int C:

Initialization:

S1 = 1

S2 = 0

C = initial value of semaphore S

Implementing S

wait operation

wait(S1);

C--;

if (C < 0) {signal(S1);

wait(S2);

}

signal(S1);

signal operation

wait(S1);

C ++;

if (C <= 0)signal(S2);

else

signal(S1);

Page 71: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 71/145

Page 72: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 72/145

Page 73: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 73/145

A programmer who needs to write a tailor-made synchronization scheme can define one or morevariables 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 . s i g n a l ( ) ;

The x. signal () operation resumes exactly one suspended process. If no process is suspended, then thesignal () 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, whichalways affects the state of the semaphore.

Page 74: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 74/145

Unit 2Classical 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 2Concurrency and Deadlock

Resources

Examples of computer resources Printers Tape drives Tables

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

Shared and exclusive resources Example of shared resource: FILE Example of exclusive resource: PRINTER

Consumable resources Created (produced) and destroyed (consumed) by a process Examples: Interrupts, signals, messages, and information in I/O buffers

Page 75: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 75/145

System Model

A system consists of a number of resources to be distributed among a number of competingprocesses.

There are different types of resources R1, R2,..., Rm. CPU cycles, memory space, I/O devices

Each resource type Ri has W i instances. For example, if two CPUs then resource type CPU hastwo instances.

Sequence of Events Required to Use a Resource

Each process utilizes a resource as follows: Request a resource :

Request is made through a system call Process must wait if request is denied

Requesting process may be blocked may fail with error code

Use the resource : The process can operate on the resource.

Release the resource : The process releases the resource. A resource is released through a system call.

Deadlock

Formal Definition

A set of processes is deadlocked if each process in the set is waiting for an event that onlyanother process in the set can cause

Usually the event is release of a currently held resource

None of the processes can … Run Release resources Be awakened

Involve conflicting needs for resources by two or more processes

Examples of Deadlock Example 1

System has 2 tape drives. P1 and P2 each hold one tape drive and each needs anotherone.

Example 2 Semaphores A and B, initialized to 1

P0 P1

Page 76: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 76/145

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

….

Request 80KB;

Request 60KB;

P1

Request 70KB;

Request 80KB;

Deadlock occurs if both processes progress to their second request

Four Conditions for Deadlock Deadlock can arise if four conditions hold simultaneously

Mutual exclusion condition: Only one process at a time can use a resource (non-shareable resource). Each resource is assigned to a process or is available

Hold and wait condition: A process holding at least one resource can request for additional resources

No preemption condition: A resource can be released only voluntarily by the process holding it. That is

previously granted resources cannot be forcibly taken away. Circular wait condition:

there exists a set { P0,P1,…,P0} of waiting processes such that P0 is waiting for a

resource that is held by P1, P1 is waiting for a resource that is held by P2,…,Pn –1 is waiting for a resource that is held by Pn, and P0 is waiting for a resource that isheld by P0.

Page 77: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 77/145

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 R j Assignment edge –directed edge R j Pi

Different symbols are used to represent processes and resources as given below:

Process:

Resource type of 4 instances:

Pi requests instance of R j :

Pi is holding an instance of R j : P i R j

P i

R j

Page 78: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 78/145

Method of Handling Deadlocks

Page 79: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 79/145

Just ignore the problem altogether Prevention

Ensure that the system will never enter a deadlock state Requires negating one of the four necessary conditions

Dynamic avoidance Require careful resource allocation

Detection and recovery Allow the system to enter a deadlock state and then recover We need some methods to determine whether or not the system has entered into

deadlock. We also need algorithms to recover from the deadlock.

The Ostrich Algorithm

Pretend there is no problem The system will eventually stop functioning Reasonable if

Deadlocks occur very rarely Cost of prevention is high

UNIX and Windows takes this approach It is a trade off between

Convenience Correctness

Deadlock Prevention

Prevent/deny Mutual Exclusion condition Use shareable resource.

Impossible for practical system. Prevent/Deny Hold and Wait condition

(a) Pre-allocation - Require processes to request resources before starting A process never has to wait for what it needs

(b) Process must give up all resources and then request all immediately needed Problems

May not know required resources at start of run Low resource utilization – many resources may be allocated but not used for long

time Starvation possible – a process may have to wait indefinitely for popular

resources.

Prevent/deny No Preemption condition(a) If a process that is holding some resources requests another resource that cannot be

immediately allocated to it, then all resources currently being held are released.

Page 80: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 80/145

Preempted resources are added to the list of resources for which the processis waiting.

Process will be restarted only when it can regain its old resources, as well asthe new ones that it is requesting.

(b) The required resource(s) is/are taken back from the process(s) holding it/them andgiven to the requesting process

Problems Some resources (e.g. printer, tap drives) cannot be preempted without

detrimental implications. May require the job to restart

Prevent/Deny Circular Wait Order resources (each resource type is assigned a unique integer) and allow process to

request for them only in increasing order If a process needs several instances of the same resource, it should issue a single request

for all of them. Alternatively, we can require that whenever a process requests an instance of a resource

type it has released all the resources which are assigned a smaller inter value. Problem:

Adding a new resource that upsets ordering requires all code ever written to bemodified

Resource numbering affects efficiency A process may have to request a resource well before it needs it, just

because of the requirement that it must request resources in ascendingorder

An example:

Page 81: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 81/145

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

1. No process is deadlocked, and there exits no possible sequence of future request inwhich deadlock could occur

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 R j indicated that process P j may request resource R j 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 R j . The request can be granted only if converting the

request edge if converting the request edge Pi R j to an assignment edge does not result in acycle in the resource-allocation graph. That is we use a cycle detection algorithm is used. If nocycle exits, the process Pi will have to wait.

Page 82: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 82/145

Resource-allocation graph for deadlock avoidance

An unsafe state in the resource-allocation graph

Page 83: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 83/145

Banker’s Algorithm

Applicable to system with multiple instances of resource types. Each process must a priori claim maximum use. When a process requests a resource it may have to wait. When a process gets all its resources it must return them in a finite amount of time. Banker’s algorithm runs each time:

A process requests resource – Is it sage? A process terminates – Can I allocate released resources to a suspended process waiting

for them? A new state is safe if and only if every process can complete after allocation is made

Make allocation and then check system state and deallocate if unsafe

Data Structures for Banker’s algorithm Let n = number of processes, and m = number of resources types. Available : Vector of length m. If available [ j ] = k , there are k instances of resource type R j

available. Max : n x m matrix. Max [i,j ] = k mean that process Pi may request at most k instances of R j . Allocation : n x m matrix. If Allocation[ i,j ] = k then Pi is currently allocated k instances of R j. Need : n x m matrix. If Need [i,j ] = k , then Pi may need k more instances of R j to complete its task.

Need [i,j] = Max [i,j ] – Allocation [i,j ].

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) Need i Work

If no such i exists, go to step 4.

3. Work = Work + Allocation iFinish [i ] = true go to step 2.

4. If Finish [i ] == true for all i , then the system is in a safe state.

Page 84: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 84/145

Resource-Request algorithm for Process P i

Request = request vector for process P i . If Request i [ j ] = k then process P i wants k instances of

resource type R j .

1. If Request i Need i go to step 2. Otherwise, raise error condition, since process has exceededits maximum claim.

2. If Request i Available , go to step 3. Otherwise P i must wait, since resources are not available.3. Pretend to allocate requested resources to P i by modifying the state as follows:

Available = Available = Request i ;

Allocation i = Allocation i + Request i ;

Need i = Need i – Request i

• If safe the resources are allocated to P i .

• If unsafe P i must wait, and the old resource-allocation state is restored

Example of Banker’s Algorithm

5 processes P0 through P4; 3 resource types

A (10 instances),B (5 instances), and

C (7 instances).

Page 85: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 85/145

Snapshot at time T 0:

AllocationMax Available

ABC ABC ABC

P0 0 1 0 7 5 3 3 3 2

P1 2 0 0 3 2 2

P2 3 0 2 9 0 2

P3 2 1 1 2 2 2

P4 0 0 2 4 3 3

The content of the matrix. Need is defined to be Max – Allocation.

Process Need

A B C

P0 7 4 3

P1 1 2 2

P2 6 0 0

P3 0 1 1

P4 4 3 1

The system is in a safe state since the sequence < P1 ,P3 ,P4 ,P2 ,P0> satisfies safety criteria.

Example P 1 Request (1,0,2)

Check that Request Availablethat is, (1,0,2) (3,3,2) true.

Page 86: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 86/145

Process Allocation Need Available

A B C A B C A B C

P0 0 1 0 7 4 3 2 3 0

P1 3 0 2 0 2 0

P2 3 0 1 6 0 0

P3 2 1 1 0 1 1

P4 0 0 2 4 3 1

Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2> satisfies safetyrequirement.

Can request for (3,3,0) by P4 be granted? Can request for (0,2,0) by P0 be granted?

Deadlock Detection Recovery

Allow system to enter deadlock state Need a detection algorithm Need a recovery algorithm

How to Detect a Deadlock Using a Resource-Graph?

If each resource type has exactly one instance and the graph has a cycle then a deadlock hasoccurred. Or if the cycle involves only a set of resource types, each of which has only a singleinstance, then the deadlock has occurred.

Therefore, a cycle in the graph is both a necessary and sufficient condition for theexistence of a deadlock.

Page 87: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 87/145

Examples:

Resource-allocation graph with a deadlock

Page 88: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 88/145

Recovery from Deadlocks – Process Termination

Abort all deadlocked processes. Abort one process at a time until the deadlock cycle is eliminated. In which order should we choose to abort?

Priority of the process. How long process has computed, and how much longer to completion. Resources the process has used. Resources process needs to complete. How many processes will need to be terminated? Is process interactive or batch?

Recovery from Deadlocks – Resource Preemption

Selecting a victim – minimize cost. Rollback – return to some safe state, restart process for that state. Starvation – same process may always be picked as victim, include number of rollback in cost

factor.

Combined Approach to Deadlock Handling

Combine the three basic approaches prevention avoidance detection

allowing the use of the optimal approach for each of resources in the system.

Partition resources into hierarchically ordered classes. Use most appropriate technique for handling deadlocks within each class.

Page 89: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 89/145

Unit-2Memory management

Mono-programming

In uni-processor systems, main memory is divided into two parts as shown below:

Multi-programming

In multiprogramming systems, user part of memory must be subdivided to accommodatemultiple processes.

Page 90: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 90/145

Memory Management

Main memory is a resource that must be allocated and deallocated

Memory Management Techniques determine: How the memory is to be (logically) subdivided? Where and how a process resides in memory? How addressing is performed? How process can be relocated? How memory is to be protection? How memory can be shared by processes? How to logical and physically organize memory

Addressing Requirements of a Process

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.

Page 91: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 91/145

Normally, a process is selected from the input queue and is brought into the memory forexecution. 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 theaddresses that a user program can access.

User programs go through several steps before being run.

Addresses may be represented during these steps. Addresses in the source program are generally symbolic (variable name). A compiler typically binds these addresses to relocatable addresses (in terms of offsets). The linkage editor or loader binds relocatable addresses to absolute addresses (physical

addresses).

Loading and Loader

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. Allreferences in the load module must be absolute memory addresses. The address bindingcan be done at programming time, compile time or assembly time.

Relocatable loading – The loader places a module in any desired location of the mainmemory. To make it possible, the compiler or assembler must generate relativeaddresses.

Dynamic loading - Routine is not loaded until it is called resulting in better memory-space utilization (unused routine is never loaded). It is useful when large amounts ofcode are needed to handle infrequently occurring cases. No special support from theoperating system is required implemented through program design.

Page 92: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 92/145

Page 93: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 93/145

Memory Management Unit - (MMU)

MMU is a hardware device that maps virtual to physical address at run-time. In a simple MMU scheme, the value in the relocation register is added to every address

generated by a user process at the time it is sent to memory. The user program deals with logical addresses; it never sees the real physical addresses.

Dynamic relocation using a relocation register

Overlays

Overlay is one of the techniques to run a program that is bigger than the size of the physicalmemory.

The idea of overlays is to keep in memory only those instructions and data that are needed atany given time.

The basic idea is to divide the program into modules in such a way that not all modules need tobe 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

Page 94: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 94/145

Overlays for a two-pass assembler

Advantages Reduced memory requirements

Disadvantages Overlap map must be specified by programmer Programmer must know memory requirements Overlapped modules must be completely disjoint

Swapping

A process needs to be in the memory to be executed. However, a process can be swapped temporarily out of memory to a backing store , and then brought back into memory forcontinued execution.

Swapping needs a backing store – fast disk large enough to accommodate copies of all memoryimages for all users; must provide direct access to these memory images.

Roll out, roll in – swapping variant used for priority-based scheduling algorithms; lower-priorityprocess is swapped out so higher-priority process can be loaded and executed.

Major part of swap time is transfer time; total transfer time is directly proportional to theamount of memory swapped. Note that context switching time in this scheme is quite high.

If we want to swap out a process, we must be sure that it is completely idle. Modified versions of swapping are found on many systems, i.e., UNIX, Linux, and Windows.

Page 95: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 95/145

Swapping of two processes using a disk as a backing store

Contiguous Allocation Techniques

Main memory usually divided into two partitions: Resident operating system, usually held in low memory with interrupt vector. User processes then held in high memory. Every process is loaded into a single contiguous partition of the memory.

Single-partition Allocation

A single processes in loaded into the memory at a time.

Page 96: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 96/145

Advantages: Simplicity No special hardware required

Disadvantages: CPU wasted Main memory not fully used Limited job size

Multiple-Partition Allocation – Fixed Partitions

Desirable in multiprogramming environment. A simple multi-partition scheme is to divide the memory into fixed size partitions. Each process is loaded into a single partition. In this scheme, the degree of multiprogramming is bounded by the number of partitions. This scheme was originally used in IBM OS/360. No longer in use now.

There might be a separate process queue (need absolute addressing) for each partition or asingle queue for all partitions (need dynamic addressing).

Page 97: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 97/145

Multiple-partition Allocation – Dynamic Partitions Partitions are of variable length and number Process is allocated exactly as much memory as required MVT , a dynamic partition scheme, works as follow:

Operating system maintains information about: Allocated partitions Free partitions (hole). Initially, all the memory is available and is considered as a

single block (one big hole ). When a process arrives, we search for a hole large enough for the process. If a hole is

found, it is allocated to the process. If we find a hole, which is too large, it is split intotwo: one part is allocated to the arriving process, the other is returned to the set ofholes.

When a process completes, the memory is freed and is placed in to the set of holes. If thenew hole is adjacent to other holes, they are packed in the set of holes. At this point, it ischecked whether there is a process waiting for memory and whether this newly freedhole and recombined memory could satisfy the demands of any of the waiting processes.

Page 98: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 98/145

Merging of holes

Page 99: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 99/145

Pros and Cons of Dynamic Allocation

Advantages Efficient use of memory

Disadvantages Partition management Compaction or external fragmentation Internal fragmentation

Dynamic Allocation Placement Algorithms

How to satisfy a request of size n bytes from a list of free holes? There are many solutions to this problem, but most common are as follows:

First-fit : Allocate the first hole that is big enough. The search can start either at thebeginning of the set of holes or where the previous first-fit search was ended.

Best-fit : Allocate the smallest hole that is big enough; must search entire list, unlessordered by size. Produces the smallest leftover hole.

Worst-fit : Allocate the largest hole; must also search entire list. Produces the largestleftover hole.

First-fit and best-fit better than worst-fit in terms of speed and storage utilization.Example

Page 100: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 100/145

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

Another possible solution to external fragmentation is paging. In paging, logical address space of a process can be noncontiguous; process is allocated physical

memory whenever the latter is available. Physical memory is divided into fixed-sized blocks called frames (size is power of 2, between 512

bytes and 8192 bytes). Logical memory is also divided into blocks of same size called pages . The page size is defined bythe hardware and is typically a power of 2 varying between 512 bytes and 16 megabytes perpage. The selection of a power of 2 as a page size makes the translation of logical address into apage number and offset easy.

To run a program of size n pages, need to find n free frames and load program. Set up a page table (for each process) to translate logical to physical addresses. Internal fragmentation is possible.

Page 101: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 101/145

Memory Allocation in Paging

When a new process arrives, its size, in pages, is determined. If the process has n pages then n frames must be available in the physical memory.

The first page of the process is then loaded into the first available frame, the next into nextavailable frame and so on.

Since OS is managing the physical memory, it must be aware of the allocation details of physicalmemory (allocated and free frames). This information is generally kept in a data structure knownas frame table which has one entry for each frame and indicates whether it is free or not and if

allocated to which page of which process.

Free frames – (a) before allocation (b) after allocation

Page 102: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 102/145

Page 103: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 103/145

Implementation of Page Table

Page table for each process is kept in main memory. Page-table base register ( PTBR) points to the page table. Page-table length register (PTLR) indicates size of the page table. In this scheme every data/instruction access requires two memory accesses. One for the page

table and one for the data/instruction. The two memory access problem can be solved by the use of a special fast-lookup hardware

cache called associative memory or translation look-aside buffers (TLBs).

Associative memory

Associative memory allows parallel search Contains page table entries that have been most recently used

Functions same way as a memory cache Given a virtual address, processor examines the TLB If page table entry is present (a hit), the frame number is retrieved and the real address is

formed If page table entry is not found in the TLB (a miss), the page number is used to index the process

page table First checks if page is already in main memory

if not in main memory a page fault is issued The TLB is updated to include the new page entry

Note that TLB must be flushed every time a new page table is selected (during context switching,for example).

Page 104: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 104/145

Effective Access Time

Associative Lookup = time unit Assume memory cycle time is 1 microsecond Hit ratio – percentage of times that a page number is found in the associative registers; ration

related to number of associative registers. Hit ratio = α Effective Access Time (EAT)

EAT = (1 + ) α + (2 + )(1 – α)

= 2 + – α

Memory Protection

Memory protection implemented by associating protection bit with each frame. Normally thesebits are kept in the page table. One bit can define a page to be read and write or read-only. Sinceevery reference to a memory location is through the page table, and protection bits can bechecked 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.

Valid and invalid bit in a page table

Page 105: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 105/145

Page Table Structure

Hierarchical Paging Hashed Page Tables Inverted Page Tables

Hierarchical Page Tables

Break up the logical address space into multiple page tables. A simple technique is a two-level page table.

Two Level Paging Example

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:

where p i is an index into the outer page table, and p2 is the displacement within the page of the outer

page table.

P2d

Page number

Page offset

10 10 12

Page 106: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 106/145

Address Translation Scheme

Address-translation scheme for a two-level 32-bit paging architecture

Hashed Page Tables

Common in address spaces > 32 bits. The virtual page number is hashed into a page table. This page table contains a chain of

elements hashing to the same location.

Page 107: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 107/145

Virtual page numbers are compared in this chain searching for a match. If a match is found, thecorresponding physical frame is extracted.

Hash page table

Inverted Page Table

One entry for each real page of memory. Entry consists of the virtual address of the page stored in that real memory location with

information about the process that owns that page. Decreases memory needed to store each page table, but increases time needed to search the

table when a page reference occurs. Use hash table to limit the search to one — or at most a few — page-table entries.

Inverted page table architecture

Page 108: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 108/145

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.

Private code and data Each process keeps a separate copy of the code and data. The pages for the private code and data can appear anywhere in the logical address

space.

Pros and Cons of Paging

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

Page 109: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 109/145

Page 110: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 110/145

Address Mapping in Segmentation

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 table’s location in memory.

Segment-table length register (STLR) indicates number of segments used by a program; Segment number s is legal if s < STLR.

Page 111: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 111/145

Segmentation Example

Comparison of Paging and Segmentation

Page 112: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 112/145

Virtual MemoryBackground

Virtual memory is a technique that allows execution of processes that may not be completely inthe 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 areneeded, 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 pr ogram’s “working set” is kept in memory, reference outside WS causescorresponding 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 113: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 113/145

Transfer of a Paged Memory to Contiguous Disk Space

Valid-Invalid Bit

For the above-mentioned scheme, we need some hardware support. With each page table entry a valid –invalid bit is associated (1 in-memory, 0 not-in-

memory) Initially valid –invalid but is set to 0 on all entries. During address translation, if valid –invalid bit in page table entry is 0 page fault .

Page Fault - Interrupt that arises upon a reference to a page that is not in main memory.

Page 114: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 114/145

Page Fault Handling

If there is ever a reference to a page, first reference will trap toOS page fault

OS looks at an internal table, usually kept in process’s PCB, to decide: Invalid reference abort.

Just not in memory. If the page is not in the memory, then

Get empty frame. Swap page into frame. Reset tables, validation bit = 1. Restart instruction

Note that when a page fault happens, the hardware traps to the OS. The OS reads the desirepage and restart the process from the point where it was interrupted.

Steps in Handling a Page Fault

What Happens If There is no Free Frame?

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

Page 115: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 115/145

Performance of Demand Paging

Page Fault Rate 0 p 1.0 if p = 0 no page faults if p = 1, every reference is a fault

Effective Access Time (EAT)

EAT = (1 – p) x memory access

+ p ( page fault overhead

+ [swap page out ] + swap page in

+ restart overhead )

Example

Memory access time = 1 microsecond 50% of the time the page that is being replaced has been modified and therefore needs to be

swapped out. Swap Page Time = 10 msec = 10,000 msec

EAT = (1 – p) x 1 + p (15000)1 + 15000P (in msec)

Page Replacement Prevent over-allocation of memory by modifying page-fault service routine to include page

replacement. Use modify (dirty ) bit to reduce overhead of page transfers – only modified pages are written to

disk. Page replacement completes separation between logical memory and physical memory – large

virtual memory can be provided on a smaller physical memory.

Basic Page Replacement Algorithm

Find the location of the desired page on disk.

Find a free frame:

- If there is a free frame, use it.

- If there is no free frame, use a page replacement algorithm to select a victim frame.

Page 116: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 116/145

Read the desired page into the (newly) free frame. Update the page and frame tables.

Restart the process.

Page Replacement Algorithms

Want lowest page-fault rate.

Evaluate algorithm by running it on a particular string of memory references ( reference string )and computing the number of page faults on that string.

FIFO Algorithm

When a page must be replaced, the oldest page is chosen. FIFO page replacement algorithm associates with each page the time when the page was

brought into the memory. A FIFO queue (instead of time) may also be used to implement this algorithm.

Example of FIFO Algorithm

Page 117: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 117/145

Belady’s Anomaly

An interesting observation if FIFO algorithm is that increasing the page frames may not decreasethe 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 beused for the longest period of time.

Difficult to implement because it requires future knowledge of the reference string.

Example of OPT Algorithm

Least Recently Used (LRU) Algorithm

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

Page 118: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 118/145

Two possible implantations of LRU:o Counter implementation

Every page entry has a counter; every time page is referenced through this entry,copy the clock into the counter.

When a page needs to be changed, look at the counters to determine which areto change.

o Stack implementation keep a stack of page numbers in a double link form:

Page referenced: – move it to the top – requires 6 pointers to be changed

No search for replacement

LRU Approximation Algorithms

Use a reference bit With each page associate a bit, initially = 0 When page is referenced bit set to 1.

Replace the one which is 0 (if one exists). We do not know the order, however. Addition reference-bits algorithm

Reference bits are recorded in regular intervals. For example, we can keep an 8-bit foreach page in a table in the memory.

After regular intervals, a timer interrupt transfers the control to the OS which shifts thereference bit for each page into the high-order bit of 8-bit shift registers containing thehistory of page.

The page with the lowest number is the LRU page.

Page 119: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 119/145

Counting Algorithms

Keep a counter of the number of references that have been made to each page. The followingtwo 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 beused.

Not very commonly used algorithm.

Allocation of Frames

Each process needs minimum number of pages. Minimum number of frames is defined by thecomputer 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

Use a proportional allocation scheme using priorities rather than size. If process Pi generates a page fault,

Select for replacement one of its frames. Select for replacement a frame from a process with lower priority number.

Global vs. Local Allocation

Global replacement – process selects a replacement frame from the set of all frames; oneprocess can take a frame from another.

Local replacement – each process selects from only its own set of allocated frames.

Process Creation

Virtual memory allows other benefits during process creation:

Copy-on-Write Memory-Mapped Files

Page 120: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 120/145

Copy on Write

Copy-on-Write (COW) allows both parent and child processes to initially share the same pages inmemory.

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 Files

Memory-mapped file I/O allows file I/O to be treated as routine memory access by mapping adisk 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 filesystem into a physical page. Subsequent reads/writes to/from the file are treated as ordinarymemory 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

Pros/Cons of Demand Paging

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

Page 121: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 121/145

Unit-2

Page 122: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 122/145

FileSystem

Page 123: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 123/145

Page 124: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 124/145

Page 125: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 125/145

Page 126: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 126/145

Page 127: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 127/145

Page 128: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 128/145

Page 129: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 129/145

Page 130: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 130/145

Page 131: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 131/145

Page 132: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 132/145

Page 133: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 133/145

Page 134: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 134/145

Page 135: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 135/145

Page 136: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 136/145

Page 137: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 137/145

Unit-2I/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

Differences in I/O Devices

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

Page 138: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 138/145

I/O Hardware

Incredible variety of I/O devices Common concepts

Port (a connection point) Bus (daisy chain or shared direct access) Controller (host adapter)

I/O instructions control devices Devices have addresses, used by

Direct I/O instructions Memory-mapped I/O

Page 139: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 139/145

Life Cycle of an I/O Request

Page 140: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 140/145

Disk Structure

Disks provide the bulk of secondary storage. Disk drives are addressed as large 1-dimensional arrays of logical blocks , where the logical block

is the smallest unit of transfer. The size of logical block is generally 512 bytes. The 1-dimensional array of logical blocks is mapped into the sectors of the disk sequentially.

Sector 0 is the first sector of the first track on the outermost cylinder. Mapping proceeds in order through that track, then the rest of the tracks in that cylinder,

and then through the rest of the cylinders from outermost to innermost. The number of sectors per tack is not a constant. Therefore, modern disks are organized in zones

of cylinders. The number of sectors per track is constant within a zone.

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.

Page 141: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 141/145

Disk performance Parameters

The operating system is responsible for using hardware efficiently - for the disk drives, thismeans 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.

Access time has two major components Seek time is the time for the disk are to move the heads to the cylinder containing the

desired sector. Rotational latency is the additional time waiting for the disk to rotate the desired sector

to the disk head.

Seek time is the reason for differences in performance Minimize seek time Seek time seek distance

Disk Scheduling

For a single disk there will be a number of I/O requests If requests are selected randomly, we will get the worst possible performance Several algorithms exist to schedule the servicing of disk I/O requests. We illustrate them with a request queue (0-199).

98, 183, 37, 122, 14, 124, 65, 67

Head pointer 53

Page 142: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 142/145

First Come First Serve (FCFS)

The I/O requests are served in the order in which they reach. See below (total headmovement=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.

Page 143: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 143/145

SCAN Scheduling

The disk arm starts at one end of the disk, and moves toward the other end, servicing requestsuntil it gets to the other end of the disk, where the head movement is reversed and servicingcontinues.

Sometimes called the elevator algorithm . Illustration shows total head movement of 208 cylinders (head is moving towards cylinder 0).

C-SCAN Scheduling Provides a more uniform wait time than SCAN. The head moves from one end of the disk to the other, servicing requests as it goes. When it

reaches the other end, however, it immediately returns to the beginning of the disk, withoutservicing any requests on the return trip.

Treats the cylinders as a circular list that wraps around from the last cylinder to the first one.

Page 144: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 144/145

Page 145: Notes Oscd

7/21/2019 Notes Oscd

http://slidepdf.com/reader/full/notes-oscd 145/145

Disk Management

Low-level formatting , or physical formatting - Dividing a disk into sectors that the disk controllercan read and write.

To use a disk to hold files, the operating system still needs to record its own data structures onthe 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 logicalformat 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 controllermaintains a list of bad sectors which is updated regularly. Low level formatting also sets asidesome spare sectors. The controller can be asked to replace each bad sector logically with one ofthe spare sectors.

Contacts