51
1 1 Operating Systems ITNP023 ITNP23 1.2 Operating systems 1. Basic concepts 2. History 3. Modern OS as virtual machines 4. File manager and peripherals manager 5. Protection 6. Resource management: 1. Memory manager 2. Processes and multitasking [CPU - and other resources] 3. Scheduling [CPU - and other resources] 4. Deadlocks [Computation in general]

OS 2016 v1 - Computing Science and Mathematics, … · memory and the processor starts to execute it. ... that manages the computer and provide an high-level access to the ... The

Embed Size (px)

Citation preview

1

1

Operating Systems

ITNP023

ITNP23 1.2

Operating systems

1. Basic concepts

2. History

3. Modern OS as virtual machines

4. File manager and peripherals manager

5. Protection

6. Resource management:

1. Memory manager

2. Processes and multitasking [CPU - and other resources]

3. Scheduling [CPU - and other resources]

4. Deadlocks [Computation in general]

2

ITNP23 1.3

Operating Systems: Introduction

Running an application (informal description):

� Via an interface, typically a windows/icon interface, you can select and launch an application by double-clicking its icon with the mouse.

You double click on the icon of your voip preferred program.

� The executable code of the application is loaded from an hard disk into the RAM memory and the processor starts to execute it.

The disk spins, after a while the windows of the program opens.

� Possibly, the application interacts with one or more users and other devices, and uses available resources.

You select one of your contact and start a call, the built-in camera starts to record images that the app processes and broadcasts together with voice. At a certain point you decide to send a file to the contact, who in turn sends back a picture.

� It may happen that several application run concurrently sharing data and computational power, i.e. the processor [although you may not realise it].

You decide to save the received picture, open it, and print it. In doing so, a photo application is launched, and it accesses the printer.

How does all this happen ?

ITNP23 1.4

Operating Systems: Introduction

Running an application (informal description):

� Video/mouse/keyboard interface

� File system

You double click on the icon of your voip preferred program.

� Device management: Disk, Drivers, Bus, Memory, Processor.

The disk spins, after a while the windows of the program opens.

� Running the code

� Running several processes (i.e. programs, for the time being), safely

� Managing resources

� Reacting to several events

You select one of your contact and start a call, the built-in camera starts to record images that the app processes and broadcasts together with voice. At a certain point you decide to send a file to the contact, who in turn sends back a picture.

� Managing processes, processor(s) and memory allocation, inter-process and external communications

You decide to save the received picture, open it, and print it. In doing so, a photo application is launched, and it accesses the printer.

3

ITNP23 1.5

Operating Systems: Introduction

An Operating System (OS) is in charge of most of the mentioned tasks!

It is a large and complex application itself, perhaps with special capabilities, that manages the computer and provide an high-level access to the computer itself.

It manages the internal functionalities of a computer, such as processor allocation to processes, as well as interface functionalities with local and remote structures and devices, such as file systems, disks or network communications.

The OS is continuously executing (concurrently with other other applications) whenever the computer is on. Generally, it may be composed of several, possibly layered, components.

Well known OS are� Unix,

� MS Dos

� Linux,

� MacOS,

� MS Windows

� Android

� iOS

ITNP23 1.6

A bit of history

4

ITNP23 1.7

Managing programs on early computers

� On early computers, programs were all in machine code and ran directly on the computer, with no interface.

� Input was from switches, paper tape, cards, tape, etc and output was in lights, print, paper tape, cards, etc.

� High level languages (like ForTran) made programming easier, but running programs became a complicated task.

� To run a ForTran program, each programmer had to:

� load the ForTran compiler (perhaps from tape)

� load the program (perhaps from punched cards)

� compile the program and save it (on another tape)

� load the compiled program

� load the program’s data (perhaps from punched cards)

� run the program and save or print its output

ITNP23 1.8

Console of the Ferranti Mark 1

5

ITNP23 1.9

Managing programs on early computers

� This long list of tasks increased setup time and reduced utilisation (the proportion of time the computer is actually being used) and programmer productivity.

� Initial solution: employ a (human) computer operator to perform the tasks of loading and running programs, thus freeing the programmers to get on with writing and debugging their programs.

� The computer operator’s job was mindless and mechanical…

� …so, a small program was developed to do automatic job sequencing, replacing the human operator.

� This program was called the monitor.

ITNP23 1.10

Managing programs with a monitor

� The monitor program was loaded when the computer was switched on, and then kept permanently in main memory (unlike user programs).

� The sequence of events was now as follows:

� Turn on the computer

� Load the monitor

� Run the monitor:

� The monitor reads and interprets a punched card describing the next job (a user program, plus its associated data) to be run.

� It loads this job from punched cards into memory.

� It then passes control to the job.

� The job executes, and (ideally) then returns control to the monitor, and the cycle begins again.

6

ITNP23 1.11

Problems with the monitor system

There was no protection from errors in user programs:

� the running program had access to all of main memory, and could corrupt the monitor

� a program could accidentally read too many data cards, affecting the next job

� a program could enter an infinite loop, making the computer unusable. (There was no way of interacting with, or interrupting the currently running program, other than switching off the computer.)

� only one user job could be run at a time:

resources were used inefficiently. For example, an I/O bound job (e.g., printing a file) would prevent other jobs from using the CPU even though its own CPU use was minimal.

ITNP23 1.12

Modern OS

7

ITNP23 1.13

Modern operating systems

� In modern computers, these problems are solved by a collection of systems programs, which are part of the operating system.

� The OS, provides, in effect, a virtual machine which is more powerful/efficient/accessible/... than the machine provided by the hardware

� The OS virtual machine can

� provide protection

� run several programs at the same time (multitasking)

� provide virtual memory

� provide programmers with a high-level interface

� manage access to the machine’s resources

� ...

ITNP23 1.14

Components of an operating system

� Many modern operating systems are constructed in a layered fashion.

� The outer layer is the user interface (or shell). This is often presented as a graphical user interface, managed by a program called a window manager. It also may contain a set of utility programs (which may modularly be embedded into the OS).

� The inner layer (or kernel) consists of several components which provide the basic functions of the operating system. These generally regard the managing of the computer architecture and may include:

� the file manager

� device drivers

� the scheduler and dispatcher

� the memory manager

� We shall learn about the roles of these components.

8

ITNP23 1.15

Evolution of Computer Systems

Operating SystemHardware

Application Software

User

Operating SystemHardware

...

User 1

...

User n...

ITNP23 1.16

Bootstrap programs

How does all start ?

� Hardware alone (the “raw machine” or “naked iron”) can do very little. Even the most basic computer needs a built-in program to organise the machine’s activity when it is turned on (e.g. to load the first user program and start it running).

� This program is called the bootstrap (as the machine “pulls itself up by its own boot straps”)

� The bootstrap lives in ROM so that it is present when the computer is switched on.

� The CPU runs the bootstrap which reads in the Operating System from mass storage (see later).

� It is relatively small (a few Kb on a PC).

� It is an example of a system program.

9

ITNP23 1.17

OS: The interface

� Different OSs use different means as an interface.

� For instance, the first versions of UNIX operating system were mainly text-oriented.

� The system displays a prompt character on the screen to indicate it is waiting for a command. The instructions entered by the user are in the form of a command language.

An example command would be:

$ ls

$ is the prompt char

ls provides a list of all files and directories in the current directory

ITNP23 1.18

� The interface provided by the Windows OS is a graphical user interface (GUI)

� To communicate with the user, a GUI supports visual aids, point and click operations entered via a mouse, icons, pull-down menus, scrolling windows and many other visually interactive techniques.

OS: The interface

10

ITNP23 1.19

File manager and device manager

ITNP23 1.20

File management

� The file manager coordinates the use of the machine’s mass storage facilities (e.g. disk, tape, etc)� for each file it records the location where it is stored and which users are

allowed to access it

� it keeps track of which areas of mass storage are currently free

� The OS usually provides the user with a hierarchical view of files organised into directories or folders.

� Directories may be nested; a chain of directories within directories is called a path� eg: C:\IT21\lectures\OS.ppt

11

ITNP23 1.21

File management: file protection

File protection is provided on an individual file-by-file basis

� A file may be read or not readable (read protection)

� A file may be written to or not accessible for writing (write protection)

� A file may be executed or not be executable (execution protection).

So for example, you may have permission to read a file but not write or execute it.

ITNP23 1.22

Example of the Unix OS

User base system divided into 3 categories: owner (creator of the file)

group (group of users associated with a file)

and a universe (rest of the world)

Under this protection system, each file provide nine protection flags: read, write, execute for each of the 3 categories.

File management: file protection

12

ITNP23 1.23

Management of peripheral devices

� As we have seen, communication with peripheral devices (keyboards, displays, etc) is done by means of dedicated hardware controllers.

� The operating system contains a collection of programs called device drivers. These are the software routines which communicate with the various I/O controllers.

� The device drivers translate general requests into the technical steps required by a specific device.

� This allows the design of other programs to be independent of the unique details of particular devices.

ITNP23 1.24

Systems security and protection: users

What the users are allowed to do (by the OS) ?

� The OS controls access to the computer and its resources.

� It has to ensure only authorised users can access the system and ensure that such authorised users are only allowed to do what they are authorised to do!

� In most OS, access to the computer is via an access control whereby users must enter login id and password before gaining access to the computer. Information about users and passwords is stored in a special file called the password file. Users have hence an identity.

� File permissions, for instance, allow the OS to dictate what authorised users can do once they are logged on. For example, IT students may have access to particular files on the server to read and write, but they may be prevented to write/update the information in some configuration files. System managers may have access to these files both in read and write mode.

13

ITNP23 1.25

What the programs are allowed to do (by the OS) ?

� If application programs were allowed access to all the facilities provided by the

(physical) machine, they could cause the computer to crash or corrupt data on its

disk(s), including damaging the OS itself.

� The solution is to prevent such problems by ensuring that access to machine facilities

can only be obtained via the OS, that guarantees the proper usage of resources, e.g. a

program in execution has a (virtual) area of memory and can not read/write outside it.

� For example, application programs cannot write data on disk directly. They need to call

the specific OS program (in this case, a device driver) which does this job. The OS

makes available a set of so-called system calls, by means of which programs can access

the computer resources in a controlled manner.

Systems security and protection: programs

ITNP23 1.26

� How can we ensure that user programs do not circumvent the OS and access the machine facilities directly?

� A solution: the CPU is built in such a way that it can run programs in at least two modes: supervisor and user modes

� The supervisor mode has more privileges than user mode, and is used by the OS programs only: application programs are run in the less privileged user mode

� In each mode the CPU operates by fetching and executing machine instructions (as before)

� but, for example, access to certain parts of memory is not allowed to instructions executed in user mode. Access is unrestricted to instructions run in supervisor mode

Systems security and protection: programs

14

ITNP23 1.27

How the OS provides protection

� If a user program attempts to perform an instruction which it is not authorised to do (e.g., access a restricted area of memory), an exception is generated.

� An exception is a kind of interrupt (we will see later on what an interrupt is. By time being, the interrupt is a message that can stop the "normal" execution of a program).

� The CPU then stops execution of the user program, and an interrupt handling mechanism is invoked. The interrupt handler in turn invokes the appropriate operating system program to deal with the interrupt.

ITNP23 1.28

� EXAMPLE:

When a user program wishes to write data to disk, the following sequence of actions takes place:

� The program executes a special instruction, i.e. a system call.

� This instruction causes a software interrupt to be generated.

� The execution of the program is suspended (and relevant data about the current state of the computation saved), the CPU is switched to supervisor mode, and the interrupt handler takes over.

� The interrupt handler invokes the appropriate OS routine for writing to disk.

� The data is written to disk by an OS routine.

� The CPU is switched back to user mode and the user program resumes execution.

How the OS provides protection

15

ITNP23 1.29

Operating systems

1. Basic concepts

2. History

3. Modern OS as virtual machines

4. File manager and peripherals manager

5. Protection

6. Resource management:

1. Processes and multitasking [CPU - and other resources]

2. Scheduling [CPU - and other resources]

3. Deadlocks [Computation in general]

4. Memory manager

ITNP23 1.30

Resource manager:

Processes, multitasking and deadlocks

16

ITNP23 1.31

Processes

� The notion of a process is central to operating systems.

� A process can be thought of as a program in execution, comprising code, data, PC, registers, an allocated memory space ...

(though sometimes an executing program can spawn other processes, so this is not entirely accurate).

� Number of processes >> number of CPUs => Some processes cannot run ...

� ... furthermore, some "running" processes might be waiting for something to occur, wasting CPU time, i.e. they might temporarily not need the CPU, which can be assigned to another process.

ITNP23 1.32

Processes

That is:

� Some processes are actually running (as many as the number of CPUs in the computer) ...

� ... others, ready to run, can be waiting for the CPU ...

� ... other processes may be blocked (or suspended) waiting for other resources or events, e.g. the swapping of a page from disk to RAM, as seen.

17

ITNP23 1.33

� The OS virtual machine can (in effect) run several programs at the "same" time.

� Efficient usage of CPU:

I/O bound process (editor):

CPU bound process (sci calc.):

Efficient sharing of CPU:

Multitasking

key pressed

start

overhead

ITNP23 1.34

Multitasking

� Several application programs (as well as the OS programs) will be stored in memory, but only one program, i.e. a process, is running at any time (on a given CPU). Such a process is called the running process.

� When certain conditions arise, the currently running process can be temporarily suspended and another started. We say that the CPU is being switched between processes.

� The CPU can be switched hundreds of times per second, and give the illusion that many programs are being executed at the same time.

Other applications/motivations:

� processes that share information

� real-time

� interactivity

18

ITNP23 1.35

� When a process is suspended, its relevant information has to be saved.

� The operating system stores information about each process (unique id number, resource allocations, etc.) in a process control block (PCB).

Process Structure

Image

Text

Data

Process Control Block

Hardware Context

Software Context

Process state

Program counter

CPU scheduling information

Memory management inf.

Accounting information

I/O status information

Multitasking

ITNP23 1.36

� new process are created (a program is launched) and put in the ready queue (list)

� then the use of the CPU is assigned to a ready process, which becomes running

� a running process can either terminate, suspend while waiting for some event to occur or be forced to release the CPU (interrupted), for instance because it is being running for too long or because the OS has to load a new page in memory, and hence be put back to the ready queue

� a waiting process can eventually become ready again, when the event it was waiting for occurs.

Multitasking

19

ITNP23 1.37

The OS switches CPU between processes according to the occurrence of certain events.

These events are signalled by interrupts, i.e. special signals that are sent to the CPU. Modern computers are interrupt driven.

There different kinds of interrupt signals:

� Hardware interrupts are sent by hardware devices such as the I/O device controllers, the system clock or the memory management unit (as seen).

� Software interrupts are sent by the currently running program (ie, the program can interrupt itself!). These can be triggered by errors (eg division by zero, invalid memory access) or requests for a service provided by the OS (more later).

Multitasking

ITNP23 1.38

At any point in time, each interrupt can be disabled (meaning that the CPU has been instructed to ignore it) or enabled.

When an enabled interrupt signal is received, the CPU stops execution of the current program and starts executing an operating system program called the interrupt handler. What this does depends on the nature of the interrupt that was received.

Modern operating systems have specific handlers, i.e. pieces of code, for different kinds of interrupts, and use an interrupt vector table to quickly look up the appropriate handler when an interrupt is received.

Multitasking

20

ITNP23 1.39

When an appropriate interrupt signal is received by the CPU, the OS makes a context switch:

� the current values of all the CPU registers (in particular the PC) is saved in the PCB

� the PC and other relevant registers are given different values, hence executing a different piece of code.

In other words, the current process is suspended and another process started. This may happen because the process is forced to release the CPU, for instance by a timer interrupt, or because the process suspends itself waiting for some event to occur.

Since the interrupted process's PC (and other registers), i.e. the process state was saved, it can be continued again at a later time.

Multitasking

ITNP23 1.40

Example:

� The running process A makes an I/O request, by calling the right OS program (!), eg. A is waiting for some data input from the user

� the OS then passes on the request to the appropriate I/O controller, and enablesan interrupt from the device involved (the keyboard controller, in our example)

� the OS saves A's state

� the OS switches the CPU to B

� B is now running

� when the keyboard controller generates an interrupt, B will be interrupted and control passed back to the OS

� the OS saves B's state, restores the state of A, disables further interrupts from A's peripheral, makes A running again (by restoring the saved PC).

Note that after the second step the I/O controller is handling the I/O operation request issued by A, so that the CPU can be assigned to other tasks, i.e., in our example, running B.

Problem: who is B?

Multitasking

21

ITNP23 1.41

Operating systems

1. Basic concepts

2. History

3. Modern OS as virtual machines

4. File manager and peripherals manager

5. Protection

6. Resource management:

1. Processes and multitasking [CPU - and other resources]

2. Scheduling [CPU - and other resources]

3. Deadlocks [Computation in general]

4. Memory manager

ITNP23 1.42

Scheduling

22

ITNP23 1.43

Scheduling

� In order to maximise CPU usage, avoid busy wait and support multi-tasking the CPU is switched between processes. Processes are orgainised in ready, running, waiting, and others queues:

� The scheduler is the component of the OS responsible to select the next ready program to run.

� The dispatcher is the component of the OS responsible to manage the context switch.

ITNP23 1.44

Scheduler

• Selects the new ready process to execute.

• Consists of system calls executed in protected [monitor,supervisor] mode,

• these are invoked within the context of the the running process.

• The schedule maintains data in a suitable structure, a queue data structure typically containing process PCBs, e.g:

23

ITNP23 1.45

Scheduler

• When is the scheduler activated?

1. One process switches from running to waiting state2. One process switches from running to ready state3. One process switches from waiting to ready4. One process terminates

NOTE: 1,4 is non-preemptive, i.e. the process "decides" to release the CPU, while 2,3 is preemptive, i.e. the running process is "forced" to release the CPU.

ITNP23 1.46

Scheduler

Why (for what purpose) is the scheduling mechanism activated?

� CPU use: maximise CPU usage, this is one of the most important motivations

� Throughput: maximise the number of completed processes per time unit

� Turnaround time: (of a process) minimise the time due for completion[waiting+executing+I/O]

� Waiting time: (of a process) minimise the time spent in the ready queue

� Response time: (of a process) minimise the time to the first output,e.g., for time-sharing environments.

Different goals, e.g.:

� Minimise the maximum response time for good service to all users

� Minimise the waiting time for interactive applications

24

ITNP23 1.47

Scheduler

Different scheduling algorithms exist, exhibiting different features:

� First Come First Served

� Shortest Job First

� Highest Priority First

� Round Robin (Time Slicing)

ITNP23 1.48

First come-First served (FCFS)Processes are executed in the same order as they become ready.

The ready queue is a FIFO queue: an incoming process is inserted in the queue tail, a next-to-execute process is selected from the queue head.

Non-preemptive: CPU released only on termination or I/O!

Example: Ready queue [ P3[3], P2[3], P1 [24]]:

Average waiting time: 0+24+27 / 3 = 17

Ready queue [ P1[24], P3[3], P2[3]]:

Average waiting time: 0+3+6 / 3 = 3 Convoy effects: all waiting for the "slowest"

FCFS: + simple/efficient implementation

- poor control over process scheduling [sensitive to arrival order]

- bad for interactive [real-time] applications

P1 P2 P3

24 27 300

P1P3P2

63 300

25

ITNP23 1.49

Shortest Job First (SJF)

- Each ready process has associated the next CPU time requirement.

- The process with the shortest next time process is selected.

- The ready queue is a priority queue with predicted next time as a priority.

Example: (each line represents the arrival in the

ready queue -initial point- and the CPU

time requirement - length)

Non-preemptive:

(A running process releases

the CPU only on termination)

Average waiting time: 0+6+3+7 / 4 = 4

Preemptive:

Shortest-Remaining-Time-First (SRTF)

(A running process releases the CPU on

termination or after a given time interval. Note P4 that has become the shortest one).

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

P1 P3 P2

73 160

P4

8 12

P1 P3P2

42 110

P4

5 7

P2 P1

16

0 1 2 3 4 5 6 7 8 9 0 1

P4P3P2P1

ITNP23 1.50

Shortest Job First (SJF)

SJF: + minimizes average waiting time

- next CPU time has to be estimated

[e.g., weighted average on the most recent running time]

26

ITNP23 1.51

Priority scheduling

Scheduling can be based on other priorities associated to processes, such as time limits,

resource usage, price paid, ... . The process with the highest priority is selected.

The ready queue is a priority queue. Can be either preemptive or non-preemptive.

Same general functioning as SJF (which is an example of priority scheduling).

Problem: starvation (common to priority scheduling algorithms)

lowest-priority processes indefinitely delayed by incoming highest-priority ones

Solution: aging (as seen in page replacement algorithms for memory management)

priority of ready processes increases in time so that those "starving",

ie., the lowest priority processes indefinitely delayed,

age more rapidly than those more frequently running.

Priority, then, also depends on age: "too old" starving processes acquire

highest priority.

ITNP23 1.52

Round Robin (RR)Based on time slicing: equally shares CPU amongst ready processes: each ready process gets r time units, a time quantum (milliseconds). Different policies are possible for the ready queue (e.g. priority based on process relevance or elapsed CPU time), let us assume FIFO.

Preemptive: when quantum expires, the running process is preempted by a clock interrupt.

Example: Process in the ready queue are [ P4[24], P3[68], P2[17], P1[53] ] and r = 20.

With a ready queue with n processes, the CPU time is equally shared ( 1/n of the total amount) amongst processes and each process waits at most (n-1) r in the queue before getting the CPU (plus context switch overhead!).

Relevance of r: r large approximates FCFS,

r too small makes context switch overhead predominant

RR: + better response time

- higher average turnaround time than SJF, typically (depends on time quantum)

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162

27

ITNP23 1.53

Dispatcher

Once that a new process to run has been selected by the scheduler, the dispatcher,

an OS component, is responsible for managing the context switch.

� It gives control of the CPU to the process selected by the scheduler

� First, it saves the state of the old process,

� then loads the state of the new process and

� jumps to the proper location to resume the new process by suitably setting the PC.

Time spent for context switching is critical (dispatch latency) !

ITNP23 1.54

Context Switching

28

ITNP23 1.55

Operating systems

1. Basic concepts

2. History

3. Modern OS as virtual machines

4. File manager and peripherals manager

5. Protection

6. Resource management:

1. Processes and multitasking [CPU - and other resources]

2. Scheduling [CPU - and other resources]

3. Deadlocks [Computation in general]

4. Memory manager

ITNP23 1.56

Deadlock

29

ITNP23 1.57

The OS must prevent programs or users from attempting operations that could cause the computer system to enter a state where it is incapable of doing any further work.

When resources are involved and required by different programs, it is easy to see how a situation may arise where the computation cannot proceed, and the computer freezes up. This is known as deadlock.

The control of access to resources is one of the most important jobs of the OS.

By resources we mean here things like: memory space, peripheral devices (disks, printers, display, etc), CPU time,

We have discussed some aspects of how an OS manages resources (CPU scheduling and multi-tasking, virtual memory management). We shall now look at issues regarding resource management for deadlock avoidance.

OS: Safe use of resources

ITNP23 1.58

Deadlock

� Deadlock, informally speaking, occurs when one or more processes are suspended waiting for a set of events that cannot occur, given the current state of the computer, and will not occur in the future.

� Deadlock typically involves two or more processes, each of which is waiting for a resource held by one of the other processes.

� Example:

� process A has exclusive access to the printer, but requires access to file_1 in order to proceed

� process B has exclusive access to file_1, but requires access to the printer in order to proceed

� neither of the two is programmed so as to release any resource until it has accomplished its task.

In this example, both processes are blocked from progressing and will remain so indefinitely.

30

ITNP23 1.59

Deadlock

� Deadlock may potentially arise in a system which has the following four characteristics:

Mutual exclusion: processes compete for resources which cannot be shared.

Hold and wait: processes hold resources already allocated to them while waiting for additional resources.

No Preemption: once a resource has been allocated to a process, it cannot be forcibly taken away.

Circular Wait: must be a circular chain of 2 or more processes. Each is waiting for resources held by the other members of the chain.

� All the above 4 conditions need to be meet for deadlocks to occur.

ITNP23 1.60

Graphical representation of Deadlock

� Graphs have two kind of nodes: processes and resources

� An arc from a resource to a process means the resource has previously been assigned (a)

� And arc from a process to a resource means the process is requesting this resource (and it is currently blocked on this wait) (b)

� A circle means deadlock (c)

31

ITNP23 1.61

Deadlock

The problem of deadlock can be dealt with by several different techniques:

1. Ostrich algorithm: ignore the problem. Maybe if you ignore it, it will ignore you.

2. Prevention: by negating one of the four conditions necessary for deadlocks to occur"BEFORE".

3. Dynamic avoidance: by careful resource allocation"MEANWHILE".

4. Detection and recovery: allow deadlocks to occur, detect them, and take some action"AFTER".

ITNP23 1.62

Deadlock handling: summaryDeadlock handling:

1. Ostrich algorithm: do nothing.

2. Prevention:

1. Mutual Exclusion

2. Hold and Wait

3. No Preemption

4. Circular Wait

3. Dynamic avoidance: safe/unsafe states Banker algorithm.

4. Detection and recovery:

1. Detection

1. Preemption

2. Rollback

3. Killing processes

32

ITNP23 1.63

1. Ostrich Algorithm

� Pretend there is no problem

� Mathematicians vs engineers

� Reasonable if � deadlocks occur very rarely � cost of prevention is high� non-critical systems

� This approach has been traditionally followed by several OSs, some deadlock management components are being added more recently.

� It is a trade-off between � convenience� correctness.

ITNP23 1.64

2. Deadlock prevention

� Prevention considers the four conditions for deadlock and attacks them

� Attacking the Mutual Exclusion condition

� Attacking the Hold and Wait condition

� Attacking the No Preemption condition

� Attacking the Circular Wait condition.

33

ITNP23 1.65

2.1. Attacking Mutual Exclusion

� The first condition necessary for deadlock is mutual exclusion: resources are not shareable.

� Sometimes resources have to be assigned to process in a mutually exclusive way.

E.g., consider two processes writing to the same printer at the same time.

� Some devices (such as printer) can be spooled:

� only the printer server uses the printer resource

� when a process requests the printer, it is instead connected to a print

server process which stores the file to be printed on disk

� it does this simultaneously for many processes

� when the (actual) printer becomes available, the OS, via the print server,

will take charge of printing the stored files sequentially.

� This eliminates deadlock for printer usage, because only a process accesses the

resource, which hence does not have to be mutually shared anymore.

ITNP23 1.66

2.2. Attacking Hold and Wait

� A process can hold some resources while waiting for others to be allocated to them.

� Hold and Wait can be attacked by requiring processes to request all the resources before starting execution

� All resources are assigned at the beginning� A process never has to wait for what it needs� Processes always run to completion.

� Problems:� Processes may not know required resources at start of run� Resources that other processes could be using are tied up� Not optimal use of resources!

� Variation: � Processes must give up all resources before acquiring any new one� Then, they can request all the immediately-needed resources again.

34

ITNP23 1.67

2.3. Attacking No Preemption

� Not very promising!

� Some resources can not be easily preempted.

Imagine a process accessing a CD writer and starting burning the CD, halfway through its job the process is forced to release the CD writer.

ITNP23 1.68

2.4. Attacking Circular Wait

Several ways of attacking possible:

� A process is only entitled to a single resource:

� If a second resource is needed, the first resource has to be freed

� Imagine a process copying a big file from tape to printer – unacceptable!

� Global numbering of all resources

� Processes can request resources whenever they want

� Processes may hold as many resources as needed

� BUT requests must be made in numerical order.

35

ITNP23 1.69

2.4. Attacking Circular Wait

� Each process acquires resources in ascending order

� The process holding the resource with the highest number associated cannot be waiting for any other resource (before freeing the ones currently held),

� hence that process can not be part of a circular wait.

� More in general, there cannot be cycles in the resource graphs, hence no deadlock can occur.

� Processes will eventually finish, hence freeing their resources.

ITNP23 1.70

3. Deadlock Dynamic Avoidance

It is based on dynamically managing resource allocation.

� Resources are requested one at a time

� To avoid deadlocks, the OS must decide whether it is safe or not to allocate a resource and only allocate if its safe

� Is there a algorithm that can avoid deadlock by making the right choice?

� Yes – but certain information needs to be available

� The algorithm is based on safe and un-safe states.

36

ITNP23 1.71

Safe and unsafe states

� The algorithm relies on the following hypothesis:

� The maximum number of resources each process needs is known.

� At any instant the resource availability in the system is known.

� After a process has obtained all the needed resources it terminates, releasing all the obtained resources.

� A state is safe if

� it is not deadlocked, and

� there exists a possible sequence of resource allocations that allows each process to acquire all the needed resources, according to their maximum limits, and hence to terminate.

� An unsafe state is not a deadlocked state!

� but it is a state that will eventually lead to a deadlock if no resources are freed!

ITNP23 1.72

Example (sketch):

1 2 3

1. Processes D and C need resources U and T

2. T is granted to D, the state is safe (as we will see in a minute)

3. U cannot be granted to C, as the resulting state would not be safe, actually it is deadlocked ...

3. Deadlock Dynamic Avoidance (cntd.)

37

ITNP23 1.73

Example (sketch):

1 2 3 4

1. Processes D and C need resources U and T

2. T is granted to D, the state is safe (as we will see in a minute)

3. U cannot be granted to C, as the resulting state would not be safe, actually it is deadlocked ...

... and indeed U is also granted to D4. D has acquired all the needed resources and it will eventually release them, which then

become available to C.

3. Deadlock Dynamic Avoidance (cntd.)

ITNP23 1.74

3. Deadlock Dynamic Avoidance (cntd.)

� Safe state: it is guaranteed that all processes will terminate� Unsafe state: it cannot be guaranteed that all processes will terminate.

� One deadlock avoidance algorithm is the Banker’s algorithm [Dijkstra] :

� it relies on safe and unsafe states

� it denies or delays any resource request that leads the system to an unsafe state

� when a process has obtained all the resources it needs, it must eventually free them

Disadvantages:

� the maximum amount of resources needed by processes can hardly be known in advance, making the algorithm non feasible

� the number of processes may vary dynamically, causing new and unforeseen resource needs

� there are no guarantees on when resources will be released.

38

ITNP23 1.75

4. Deadlock Detection and Recovery

Detection:

� Construct a resource graph as discussed before

� If the graph contains any cycles → deadlock

� Any process part in a cycle is deadlocked

� Algorithms for cycle detection in directed graph have a polynomial complexity.

� Simplest case: a system has only one resource of each type (1 printer, 1 plotter), but resources with multiplicity are treated analogously.

ITNP23 1.76

� Example with 7 processes and 6 resources

� Simple to detect deadlocks in a graph

� However, formal algorithm required for actual implementation → much more complex

4. Deadlock Detection and Recovery

39

ITNP23 1.77

When to check for deadlocks?

� Every time a resource request has been issued in the system → detects deadlocks as early as possible ...

� ... but very costly.

� Check every n minutes.

� Check only when the CPU utilisation drops below a certain threshold (only few processes are running).

4. Deadlock Detection and Recovery

ITNP23 1.78

Recovery:

1. Preemption

2. Rollback

3. Killing processes

4. Deadlock Detection and Recovery

40

ITNP23 1.79

Recovery through Preemption.

� Successively take a resource away from a process

� Assign the resource to another process

� Depends on resource type, manual intervention may be required

� Issues:

� Selection of a victim� Order preemption to minimise cost� Cost includes: number of resources held, time the process has already run

� Rollback� What to do with the victim? Kill it or ‘rollback’ to a saved state?

� Starvation� Resources may be preempted from always the same process� Processes may be picked only a finite number of times

4. Deadlock Detection and Recovery

ITNP23 1.80

Recovery through Rollback

� Checkpoint a process periodically, save state.

� State should contain memory image and status of resources.

� Restart the process if it is found deadlocked with the state of the last checkpoint.

� Work since the used checkpoint is lost.

� Process has to wait to re-acquire the resources.

4. Deadlock Detection and Recovery

41

ITNP23 1.81

Recovery through Killing Processes

� Crudest but simplest way to break a deadlock.

� State of some resources may be incorrect� E.g., updating a file

� Kill all processes in the deadlock

� Certainly resolves the deadlock� Very expensive, all processes need to re-run

� Kill one process at a time ...

� choose process that can be re-run from the beginning� incurs overhead, after a process is aborted the deadlock detection

algorithm needs to be run again.

� ... the other processes get the resources of the killed process.

4. Deadlock Detection and Recovery

ITNP23 1.82

Recovery through Killing Processes: Select a process for termination.

Several possible criteria!

� What is the priority of the process?

� How long has the process computed, and how much longer will the process compute to finish its task?

� How many and what resources do the processes hold (simple to preempt?) ?

� How many more resources does a process need to complete computation?

� How many processes will need to be terminated?

� Is the process interactive or batch?

4. Deadlock Detection and Recovery

42

ITNP23 1.83

Deadlock handling: summaryDeadlock handling:

1. Ostrich algorithm: do nothing.

2. Prevention:

1. Mutual Exclusion

2. Hold and Wait

3. No Preemption

4. Circular Wait

3. Dynamic avoidance: safe/unsafe states Banker algorithm.

4. Detection and recovery:

1. Detection

1. Preemption

2. Rollback

3. Killing processes

ITNP23 1.84

Operating systems

1. Basic concepts

2. History

3. Modern OS as virtual machines

4. File manager and peripherals manager

5. Protection

6. Resource management:

1. Processes and multitasking [CPU - and other resources]

2. Scheduling [CPU - and other resources]

3. Deadlocks [Computation in general]

4. Memory manager

43

ITNP23 1.85

Memory manager

ITNP23 1.86

Memory management

� The memory manager coordinates the machine’s use of main memory.

The memory manager programs

� allocate memory space for programs and data,

� manage allocated memory,

� and retrieve the allocated space when it is no longer needed.

� If a machine does only one task at a time, memory management is very simple.

44

ITNP23 1.87

Memory management

� Multi-user: computer used by more users at the "same" time

� Multitasking: several programs/processes running at the "same" time

[see later on]

� Multi-user or multitasking environments make memory management more complicate:

Many programs and blocks of data are in memory concurrently, and the memory manager must keep track of all this.

� In most modern machines, memory management is made even more complex by the provision of virtual memory...

ITNP23 1.88

Virtual memory

� A running program needs to have its code and data stored in memory, from where they can be accessed. Typically this is the main memory, i.e. the RAM.

� A program running on a practical computer can usually ‘think about’ (address) much more memory than what is actually installed on the computer (so programs + data can be larger than available RAM)

� For example, a program running on a CPU with registers capable of holding 32-bit addresses can access a memory space of 4Gb, but the computer might only have 512Mb, 1Gb or 2Gb of physical memory.

45

ITNP23 1.89

Virtual memory

� So apparently CPUs have the addressing capacity

� to run large programs, and/or

� run several programs ‘at the same time’,

but not enough physical memory to do so.

� We use OS programs and special memory management hardware to remove this discrepancy

� The (program in execution in the) CPU sees virtual memory instead of physical memory…. How?

ITNP23 1.90

Virtual memory

� Only a part of a program is maintained in the RAM memory,

i.e. the part "in use" (RAM fast but expensive and limited).

� The rest of the program is kept on disk (slower but larger and cheaper).

+ virtually infinite memory

(limited by the disk capacity and addressing capability)

+ multi-tasking

- overhead to manage virtual memory

46

ITNP23 1.91

Virtual memory

� Idea: distinguish between a logical view and a physical view of the memory allocated to a program.

� The logical view consists of a virtual representation of the program memory, e.g. a contiguous block of memory, possibly larger than the currently available RAM memory.

� The physical view consists of the actual allocation of the program in memory, e.g. a set of memory blocks, possibly non-contiguous, possibly part in RAM and part in disk.

NB: Clearly, this also makes the program "location independent".

ITNP23 1.92

Virtual memory

� Logical addresses: represent the locations of bits of code within (the logical view of) a program, but have no real meaning as addresses in the main (physical) memory.

� Physical addresses: are the actual addresses where programs are located at in main memory.

� A special hardware/software maps the (logical) addresses used by the CPU to the actual (physical) locations in memory.

� Whenever the (program running on the) CPU tries to access a part not currently held in memory, the OS transfers the required part from disk to memory, so that the CPU can access it (you can hear this happening).

47

ITNP23 1.93

Paging

� Paging is a method by which virtual memory can be implemented,

so that the logical addresses used by a program can be conceptually separated from the address where a program is physically stored in memory.

� The virtual address space is divided (conceptually) as a number of equal-sized blocks: pages

� The available physical memory is also divided up into sections of the same size: page frames

� The dimension of the page is chosen so as to optimise performances: a trade-off between speed/availability (a single large page in RAM, if possible) and the advantages of virtual memory (needs to spread programs between RAM and disk).

ITNP23 1.94

� Paging maintains a correspondence between the logical and physical addresses, such that each logical address is transformed into a physical address when the program is being executed. This transformation is known as mapping (or memory mapping).

� These operations are handled by the OS and are transparent to the (program running on the) CPU.

Paging

48

ITNP23 1.95

Address calculation (mapping)

� Memory management hardware (a memory management unit or MMU)converts the virtual addresses issued by the CPU into the physical addresses used to access the (physical) memory.

� The MMU maintains a page table for each process/program, which for each virtual page, contains

� a pointer to a page frame (in main memory), or

� an indication that it is not in main memory.

� Given a virtual address (by the CPU), the MMU

� checks if the page is the main memory

� if it is, computes the corresponding physical address, and handles access to the physical cell of memory.

ITNP23 1.96

Page Table Entry Format

vPage frame

number

Valid bit Read/Write protection field

020263031

Valid bit set to 1 if page is resident in physical memory

Address calculation (mapping)

49

ITNP23 1.97

0

1

.

.

.

100 2F357

Page Frame

Virtual Address:

093031

Virtual page: 100 Page offset

Page Table:

(simplified)

Physical Address:

093031

Address calculation (mapping)

Physical page: 2F357 Page offset

ITNP23 1.98

Page swapping

� If the page is not in memory, a page-fault exception occurs, and an I/O operation is required to transfer pages between physical memory and disk. The current process is blocked until the transfer has been completed

[see later on].

� The page containing the address in question is swapped in

� and another page is swapped out to make room.

What problem?

50

ITNP23 1.99

Page swapping

� An algorithm chooses the page to be swapped out.

� Examples of used algorithms are:

� present for the longest time (First in, First out).

� least recently used (L.R.U.)

� least frequently used (L.F.U.)

� Swapping out only involves actually copying back on to the disk if the page has had its contents changed (because the pages are there on the disk all the time).

ITNP23 1.100

Page replacement policies

� First in, First out (FIFO): "pages removed and inserted in the same order"

� Easy to implement (via a queue)

� Performs badly in practice

� Least Recently Used (LRU): "if not used recently will not be needed in the near future"

� Potentially efficient

� Difficult to implement

� Needs to keep track of when the page was last referenced

� Each page has a counter (kept in page table)

� Each time a new page is referenced, page counters on other resident pages are incremented (aging)

� Page with largest counter is swapped out

� May use hardware counter to assist in this

� Least Frequently Used (LFU): "if not frequently used not very interesting"

� Possibly optimal choice

� Again difficult to implement (needs hardware counter to assist in this)

51

ITNP23 1.101

Operating systems

1. Basic concepts

2. History

3. Modern OS as virtual machines

4. File manager and peripherals manager

5. Protection

6. Resource management:

1. Processes and multitasking [CPU - and other resources]

2. Scheduling [CPU - and other resources]

3. Deadlocks [Computation in general]

4. Memory manager