unit 4

65
Unit –IV Introduction to Operating System, Process and threads and Deadlocks threads and Deadlocks

Upload: pmghate

Post on 19-May-2015

277 views

Category:

Education


0 download

DESCRIPTION

Introduction to Operating System, Process and threads and Deadlocks

TRANSCRIPT

Page 1: Unit  4

Unit –IV

Introduction to Operating System, Process and

threads and Deadlocks threads and Deadlocks

Page 2: Unit  4

Syllabus

• Evolution of O.S.

• Function of OS.

• Types various OS.

• OS concepts. • OS concepts.

• OS structure

• Processes, threads, Inter process communication, IPC problems.

• Scheduling Resources,

• introduction to deadlock, ostrich algorithm,

• Deadlock detection and recovery,

• avoidance, prevention, other aspects

Page 3: Unit  4

Concept of operating system

• It is an interface between user & hardware of a computer system

• It is system software which may be viewed as an organized

collection of software consisting of procedures for operating a

computer & providing environment for execution of program. computer & providing environment for execution of program.

• But what does it do?

to provide an orderly and controlled allocation of the

processors, memories and I/O devices among the various

programs competing for them

Real Life Example:

• Government

Page 4: Unit  4

FIRST GENERATION (1945-55)

– Computer is made up of vacuum tubes.

– Programs are written in assembly language.

– Punched cards are used to upload programs and store a data.

Page 5: Unit  4

FIRST GENERATION (1945-55)

Early batch system

– bring cards to 1401

– read cards to tape

– put tape on 7094 which does computing

– put tape on 1401 which prints output

Page 6: Unit  4

Computer System

Page 7: Unit  4

SECOND GENERATION

(1955-65)

– Transistors are used.

– Main frame was developed.

– Programs written in assembly language and in Fortran.

– Punched cards are used.

– Batch processing used.

Page 8: Unit  4

Evolution of O.S. Function

• First generation 1945 – 1955

– vacuum tubes, plug boards (no OS)

• Second generation 1955 – 1965

– transistors, batch systems

• Third generation 1965 – 1980

– ICs and multiprogramming

• Fourth generation 1980 – present

– personal computers, hand-held devices, sensors

Page 9: Unit  4

Hardware and software combine to solve specific problems

– Software: Application logic providing a service or solves some

problem: entertainment, information management, scientific problem

solving, system control, forwarding packets in a router, etc.

– Hardware: provides the basic computing resources (Processor(s),

Memory, System Bus and I/O modules).

• Software is divided into two categories:• Software is divided into two categories:

– System software: simplifies application programming by creating

convenient to use programming and run-time environments.

• Resource Management: time and space multiplexing of system

resources

• Extended virtual machine: standardized interfaces and abstractions

simplify programming and system use

– Application software: used directly by user (person, another program

or computer system) to solve some problem

Page 10: Unit  4

Cont ..

– Functions of an Operating System

• Resource management

• User environment

– Design Approaches

• Layered, Kernel and Virtual

– Types of Advanced Operating Systems

• Distributed, Multiprocessor, Database and Real-time

– Overview of UNIX (why do we care)

Page 11: Unit  4

Functions of an OS

– Resource Management

• Time management - temporal properties

– CPU and disk transfer scheduling

• Space management

– main and secondary storage allocation

• Synchronization and deadlock handling

• Accounting and status information

Page 12: Unit  4

Functions of an OS (cont)

– User Environment - OS layer transforms bare hardware machine into

higher level abstractions

• Execution environment - process management, file manipulation,

interrupt handling, I/O operations, language.

• Error detection and handling

• Protection and security• Protection and security

• Fault tolerance and failure recovery

Page 13: Unit  4

Design Approaches

– Deal with complexities of modern systems

– Separation of Policies and Mechanisms

• Policies - What should be done

• Mechanisms - How it should be done

– Three common approaches:

• Layered Approach• Layered Approach

• Kernel Approach

• Virtual Machine Approach

Page 14: Unit  4

Layered Approach

Level Name Objects Example

13 Shell User programming env. Bash statements

12 User process User process Quit,kill,suspend,resume

11 Directories Directories Create,destroy,attach,list

� Simplifies design, implementation and testing

� Modular by dividing OS into functional layers.

11 Directories Directories Create,destroy,attach,list10 Devices External: printer,display Create,open,close

9 File system Files Create,open,close

8 Communications Pipes Crreate,open,close

7 Virtual memory Segments,pages Read,write,fetch

6 Local secondary store Blocks,channel Read,write,fetch

5 Primitive process Process,semaphore Suspend,resume,wait

4 Interrupts Interrupt-handlers Invoke,mask,retry

3 Procedures Procedure,stack,display Mark stack,call,return2 Instruction set Evaluation stack Load,store,add

1 Electronic circuit Registers,gates,buses Clear,transfer,activate

Page 15: Unit  4

Kernel Based Approach

� Kernel contains a collection of primitives which are used to build the OS

� OS implements policy

� Kernel implements mechanisms

Hardware

kernel

Operating system

Page 16: Unit  4

Virtual Machine Approach

� Virtual software layer over hardware

� Illusion of multiple instances of hardware

� Supports multiple instances of OSs

Hardware

Virtual machine software

VM1 VM2 VM3 VM4

Page 17: Unit  4

Types of Advanced OSs

� Distributed Operating Systems

� Multiprocessor Operating Systems

� Database Operating Systems

� Real-time Operating Systems

Page 18: Unit  4

Distributed Operating Systems

– Controls and manages resources for a network of autonomous computers

• manage both hardware and software resources

• behaves as a single monolithic system.

– User not aware of program or resource location

– Design issues same as traditional systems– Design issues same as traditional systems

– Practical issues:

• lack of shared memory

• lack of global clock

• unpredictable communication delays.

Page 19: Unit  4

Multiprocessor Operating Systems

– Consists of a set of processors that

• share a set of physical memory blocks

• share a common clock

• "share" over an interconnection network.

– Control and manage resources

• hardware and software resources• hardware and software resources

• viewed as a uniprocessor system.

– Design issues same as traditional system.

– Practical issues:

• increased complexity of synchronization, scheduling, memory management, protection and security

Page 20: Unit  4

Database Operating Systems

– Database systems place increased demands on an operating system to

efficiently support:

• concept of a transactions

• manage large volumes of data

• concurrency control

• system failure control

Page 21: Unit  4

Real-time Operating Systems

– Place application specific special requirements on an operating system.

– Policies and mechanisms are geared to ensuring jobs meet their

deadlines.

– Problem is one of resource scheduling and overall system utilization.

Page 22: Unit  4

OS Concepts

– Processes

– Files

– System Calls

– The command interpreter

– Signals

Page 23: Unit  4

OS Concepts - Processes

– A process is program in execution

– It consists of following:

�Executable program

�Program’s data

�Stack and stack pointer

�Program counter and other CPU registers

�Details of opened files

Page 24: Unit  4

OS Concepts – Files

– Every OS provides system calls for file management which include:

�File creation

�File deletion

�Read & write operations

Files are stored in directory with system calls:

�To put a file in directory

�To remove a file from a directory

Page 25: Unit  4

OS Concepts - Files

Page 26: Unit  4

OS Concepts – System Calls

– Provide an interface to the services made available by an operating

system.

– User program interact with operating system through system calls.

– Normally available as library functions in a high level language

– System calls are available for:

�Process management

�Memory management

�File operations

�Input/output operations

Page 27: Unit  4

OS Concepts – Command Interpreter

– Provides command-line interface

– Allows user to enter a command on command lone prompt

– The command to be executed are implemented in two ways:

• Command interpreter itself contains the necessary code to be

executed.

• Command is implemented through a system file. To execute a • Command is implemented through a system file. To execute a

command the necessary system file is loaded into memory and

executed.

Page 28: Unit  4

OS Concepts – Signals

– Are to notify a process that a particular event has occurred.

– Can be synchronous and asynchronous.

Page 29: Unit  4

OS Structure

– Monolithic system

– Layered system

– Virtual machines

– Client-server model

Page 30: Unit  4

OS Structure – Monolithic system

– Written as collection of procedures

– Services provided through system calls.

Page 31: Unit  4

OS Structure – Layered systems

• Based on layered approach consists of number of layers, each built on top

of lower layer.

Page 32: Unit  4

OS Structure- Virtual Machine

– A single real machine gives an illusion of several virtual machines,

each having its own:

• Virtual processor

• Virtual storage and input/output devices

Page 33: Unit  4

OS Structure – Client server model

• Kernel part is very small.

• Most of the OS functions are implemented using user processes.

Page 34: Unit  4

Processes

– A process is program in execution

– It consists of following:

�Executable program

�Program’s data

�Stack and stack pointer

�Program counter and other CPU registers

�Details of opened files

Page 35: Unit  4

Processes States

1.New 2. Ready 3.Running 4.Blocked 5.Terminated

Page 36: Unit  4

Processes

Control Block• A data structure used to

keep track of all

information concerning

a process

• Includes:

• Process numberProcess number

• Priority

• Process state

• Program counter

• Registers

Page 37: Unit  4

Threads

• Low cost alternative to processes for certain kind of concurrent

applications.

Page 38: Unit  4

Benefits of Threads

– Improves responsiveness

– Takes less time terminate a thread than a process

– Takes less time to switch between two threads within the same process.

– Enhance efficiency in communication.

– Allows an application to have several different threads of activity

within the same address space.

Page 39: Unit  4

Inter Process Communication

• Includes thread synchronization and data exchange between threads beyond the process boundaries.

• Two methods of IPC:

• Through shared memory

• Through message passing.

Page 40: Unit  4

IPC Problems

The Bounded Buffer (Producer-Consumer) Problem

– Assume there are n slots capable of holding one item. Process producer

will produce items to fill slots and process consumer will consume the

items in these slots.

– There is no information on the relative speeds of processes.

– Devise a protocol which will allow these processes to run concurrently. – Devise a protocol which will allow these processes to run concurrently.

– A common buffer whose elements (slots) will be filled/emptied by the

producer/consumer is needed.

– The consumer should not try to consume items which have not been

produced yet (i.e. the consumer can not consume empty slots).

– The producer should not try to put item into filled slots.

Page 41: Unit  4

IPC Problems

The Readers and Writers Problem

– Imagine a big database, such as an airline reservation system, with

many competing processes wishing to read and write.

– It is acceptable to have multiple processes reading the database at the

same time, if one process is writing to the database, no other processes

may have access to the database, not even readers.may have access to the database, not even readers.

– The solution is that the readers have priority over writers.

– If a writer appears while several readers are in the database, the writer

must wait.

Page 42: Unit  4

IPC Problems

The Dining Philosophers Problem

– There are N philosophers spending their lives thinking and eating in a

room. In their round table there is a plate of infinite rice and N

chopsticks.

– From time to time, a philosopher gets hungry.

– He tries to pick up the two chopsticks that are on his right and his left. – He tries to pick up the two chopsticks that are on his right and his left.

– A philosopher that picks both chopsticks successfully (one at a time)

starts eating.

– A philosopher may pick one chopstick at a time.

– When a philosopher finishes eating, he puts down both of his

chopsticks to their original position and he starts thinking again.

– The question is to write a program which does not let any philosopher

to die due to hunger (i.e. no deadlocks).

Page 43: Unit  4

Process Scheduling

– Fundamental operating system function.

– Scheduling is a set of policies and mechanisms supported by operating

systems that controls the order in which the work to be done is

completed.

– Scheduler manages the running processes in the system

– There are various criteria/goals by which a scheduling algorithm can be – There are various criteria/goals by which a scheduling algorithm can be

evaluated:

1. FAIRNESS: Make sure each process gets its fair share of the CPU

2. EFFICIENCY: Keep the CPU busy 100% of the time

3. RESPONSE TIME: Minimize response time for interactive users

4. TURNAROUND: Minimize the time batch users must wait for output

5. THROUGHPUT:Maximize the number of jobs processed in a given

timeframe

Page 44: Unit  4

Process Scheduling Algorithms

• Pre-emptive Scheduling

– Scheduler suspends a running process

– Allows other processes to run without each process having to complete

– Must be careful with real-time

– Must be careful to avoid race conditions

– Multiuser friendly– Multiuser friendly

• Non pre-emptive Scheduling

– Run each process to completion

– Not efficient for I/O bound applications

– Easy to understand and implement

– No race condition issues

– Must prevent starvation

– No way to guarantee turnaround

Page 45: Unit  4

Optimization Criteria

• Each scheduling algorithm must be evaluated from how it optimizes the

following variables:

– 1. Max CPU utilization

2. Max throughput

3. Min turnaround time

4. Min waiting time

5. Min response time

Page 46: Unit  4

First-Come, First Served

– FCFS) same as FIFO

– Simple, fair, but poor performance.

– Average queuing time may be long.

Page 47: Unit  4
Page 48: Unit  4

Round Robin Scheduling

– Quantum: A time interval that a process is allowed to run

– – A process may not use its entire quantum if it needs to block for I/O

• With Round Robin scheduling each process is given a quantum of time

• When quantum expires or process blocks the scheduler picks the next

process to run

• Round robin scheduling requires a queue

– queue to get next process to run

– After process has run for its quantum, Enquire it.

Page 49: Unit  4
Page 50: Unit  4

Priority Scheduling

• Used when all processes are not equally important

• Priority Scheduling

– Each process is assigned a priority

– The process with the highest priority is chosen to run by the scheduler

• Scheduler must ensure fairness

– May lower or increase priorities of some processes to ensure that:– May lower or increase priorities of some processes to ensure that:

• Processes are not getting too much CPU time

• Process are not getting too little CPU time

• Often priorities are grouped into classes

• Each process within a priority class must also be scheduled

– Can use round robin scheduling

• Starvation occurs if a low priority process never runs. Solution: build aging into a variable priority

Page 51: Unit  4

Shortest Job First

– When multiple batch jobs are sitting in a queue with the same priority,

the scheduler runs the shortest job first.

– Optimal for minimizing queuing time, but impossible to implement.

– Predicting the time the process will use on its next schedule:

t( n+1 ) = w * t( n ) + ( 1 - w ) * T( n )

– Here: t(n+1) is time of next burst.

t(n) is time of current burst.

T(n) is average of all previous bursts .

W is a weighting factor emphasizing current or

previous bursts.

Page 52: Unit  4
Page 53: Unit  4

• Traffic only in one direction.

DEADLOCKS Bridge Crossing

Example

• Traffic only in one direction.

• Each section of a bridge can be viewed as a resource.

• If a deadlock occurs, it can be resolved if one car backs up (preempt

resources and rollback).

• Several cars may have to be backed up if a deadlock occurs.

• Starvation is possible.

Page 54: Unit  4

DEADLOCKS

NECESSARY CONDITIONS

ALL of these four must happen simultaneously for a deadlock to occur:

Mutual exclusion

One or more than one resource must be held by a process in a non-sharable(exclusive) mode.

Hold and Wait

A process holds a resource while waiting for another resource.

No Preemption

There is only voluntary release of a resource - nobody else can make a processgive up a resource.

Circular Wait

Process A waits for Process B waits for Process C .... waits for Process A.

Page 55: Unit  4

• If the graph contains no cycles, then no process is deadlocked.

• If there is a cycle, then:

a) If resource types have multiple instances, then deadlock MAY exist.

b) If each resource type has 1 instance, then deadlock has occurred.

DEADLOCKS RESOURCE

ALLOCATION GRAPH

R3 Assigned to P3

Resource allocation graph

P2 Requests P3

Page 56: Unit  4

DEADLOCKS RESOURCE

ALLOCATION GRAPH

Resource allocation graph

with a deadlock.

Resource allocation graph

with a cycle but no deadlock.

Page 57: Unit  4

HOW TO HANDLE DEADLOCKS – GENERAL STRATEGIES

There are three methods:

Ignore Deadlocks:

Ensure deadlock never occurs using either

Prevention Prevent any one of the 4 conditions from happening.

DEADLOCKS Strategy

Most Operating systems do this!!

Prevention Prevent any one of the 4 conditions from happening.

Avoidance Allow all deadlock conditions, but calculate cycles about to happenand stop dangerous operations..

Allow deadlock to happen. This requires using both:

Detection Know a deadlock has occurred.

Recovery Regain the resources.

Page 58: Unit  4

Do not allow one of the four conditions to occur.

Mutual exclusion:

a) Automatically holds for printers and other non-sharables.

b) Shared entities (read only files) don't need mutual exclusion (and aren’tsusceptible to deadlock.)

DEADLOCKS Deadlock

Prevention

susceptible to deadlock.)

c) Prevention not possible, since some devices are intrinsically non-sharable.

Hold and wait:

a) Collect all resources before execution.

b) A particular resource can only be requested when no others are being held. Asequence of resources is always collected beginning with the same one.

c) Utilization is low, starvation possible.

Page 59: Unit  4

Do not allow one of the four conditions to occur.

No preemption:

a) Release any resource already being held if the process can't get an additionalresource.

b) Allow preemption - if a needed resource is held by another process, which is alsowaiting on some resource, steal it. Otherwise wait.

DEADLOCKS Deadlock

Prevention

Circular wait:

a) Number resources and only request in ascending order.

EACH of these prevention techniques may cause a decrease in utilizationand/or resources. For this reason, prevention isn't necessarily the besttechnique.

Prevention is generally the easiest to implement.

Page 60: Unit  4

If we have prior knowledge of how resources will be requested, it's possible to determine if weare entering an "unsafe" state.

Possible states are:

Deadlock No forward progress can be made.

Unsafe state A state that may allow deadlock.

DEADLOCKS Deadlock

Avoidance

Unsafe state A state that may allow deadlock.

Safe state A state is safe if a sequence of processes exist such that there are enoughresources for the first to finish, and as each finishes and releases itsresources there are enough for the next to finish.

The rule is simple: If a request allocation would cause an unsafe state, do not honor thatrequest.

NOTE: All deadlocks are unsafe, but all unsafes are NOT deadlocks.

Page 61: Unit  4

NOTE: All deadlocks are unsafe, but all unsafes are NOT deadlocks.

DEADLOCKS Deadlock

Avoidance

SAFEDEADLOCK

UNSAFE

Only with luck will processes avoid

deadlock.

O.S. can avoid

deadlock.

Page 62: Unit  4

A method used to determine if a particular state is safe. It's safe if there exists a sequenceof processes such that for all the processes, there’s a way to avoid deadlock:

The algorithm uses these variables:

Need[I] – the remaining resource needs of each process.

DEADLOCKS

Safety Algorithm

Deadlock

Avoidance

Need[I] – the remaining resource needs of each process.

Work - Temporary variable – how many of the resource are currently available.

Finish[I] – flag for each process showing we’ve analyzed that process or not.

need <= available + allocated[0] + .. + allocated[I-1] ���� Sign of success

Let work and finish be vectors of length m and n respectively.

Page 63: Unit  4

Need an algorithm that determinesif deadlock occurred.

Also need a means of recoveringfrom that deadlock.

DEADLOCKS Deadlock Detection

SINGLE INSTANCE OF A RESOURCE TYPE

• Wait-for graph == remove the resourcesfrom the usual graph and collapse edges.

• An edge from p(j) to p(i) implies that p(j) iswaiting for p(i) to release.

Page 64: Unit  4

PROCESS TERMINATION:

• Could delete all the processes in the deadlock -- this is expensive.

• Delete one at a time until deadlock is broken ( time consuming ).

• Select who to terminate based on priority, time executed, time tocompletion, needs for completion, or depth of rollback

DEADLOCKS Deadlock Recovery

completion, needs for completion, or depth of rollback

• In general, it's easier to preempt the resource, than to terminate theprocess.

RESOURCE PREEMPTION:

• Select a victim - which process and which resource to preempt.

• Rollback to previously defined "safe" state.

• Prevent one process from always being the one preempted ( starvation ).

Page 65: Unit  4

COMBINED APPROACH TO DEADLOCK HANDLING:

� Type of resource may dictate best deadlock handling. Look at ease ofimplementation, and effect on performance.

� In other words, there is no one best technique.

DEADLOCKS

Deadlock Recovery

� In other words, there is no one best technique.

� Cases include:

Preemption for memory,

Pre allocation for swap space,

Avoidance for devices ( can extract Needs from process. )