Unit –IVIntroduction to Operating System, Process andthreads and Deadlocksthreads and Deadlocks
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
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 organizedcollection of software consisting of procedures for operating acomputer & 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 theprocessors, memories and I/O devices among the variousprograms competing for themReal Life Example:• Government
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.
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
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.
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
Hardware and software combine to solve specific problems– Software: Application logic providing a service or solves someproblem: entertainment, information management, scientific problemsolving, 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 creatingconvenient to use programming and run-time environments.• Resource Management: time and space multiplexing of systemresources• Extended virtual machine: standardized interfaces and abstractionssimplify programming and system use– Application software: used directly by user (person, another programor computer system) to solve some problem
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)
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
Functions of an OS (cont)– User Environment - OS layer transforms bare hardware machine intohigher 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
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
Layered ApproachLevel Name Objects Example13 Shell User programming env. Bash statements12 User process User process Quit,kill,suspend,resume11 Directories Directories Create,destroy,attach,listSimplifies design, implementation and testingModular by dividing OS into functional layers.11 Directories Directories Create,destroy,attach,list10 Devices External: printer,display Create,open,close9 File system Files Create,open,close8 Communications Pipes Crreate,open,close7 Virtual memory Segments,pages Read,write,fetch6 Local secondary store Blocks,channel Read,write,fetch5 Primitive process Process,semaphore Suspend,resume,wait4 Interrupts Interrupt-handlers Invoke,mask,retry3 Procedures Procedure,stack,display Mark stack,call,return2 Instruction set Evaluation stack Load,store,add1 Electronic circuit Registers,gates,buses Clear,transfer,activate
Kernel Based ApproachKernel contains a collection of primitives which are used to build theOSOS implements policyKernel implements mechanismsHardwarekernelOperating system
Virtual Machine ApproachVirtual software layer over hardwareIllusion of multiple instances of hardwareSupports multiple instances of OSsHardwareVirtual machine softwareVM1 VM2 VM3 VM4
Types of Advanced OSsDistributed Operating SystemsMultiprocessor Operating SystemsDatabase Operating SystemsReal-time Operating Systems
Distributed Operating Systems– Controls and manages resources for a network of autonomouscomputers• 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.
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, memorymanagement, protection and security
Database Operating Systems– Database systems place increased demands on an operating system toefficiently support:• concept of a transactions• manage large volumes of data• concurrency control• system failure control
Real-time Operating Systems– Place application specific special requirements on an operating system.– Policies and mechanisms are geared to ensuring jobs meet theirdeadlines.– Problem is one of resource scheduling and overall system utilization.
OS Concepts– Processes– Files– System Calls– The command interpreter– Signals
OS Concepts - Processes– A process is program in execution– It consists of following:Executable programProgram’s dataStack and stack pointerProgram counter and other CPU registersDetails of opened files
OS Concepts – Files– Every OS provides system calls for file management which include:File creationFile deletionRead & write operationsFiles are stored in directory with system calls:To put a file in directoryTo remove a file from a directory
OS Concepts – System Calls– Provide an interface to the services made available by an operatingsystem.– 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 managementMemory managementFile operationsInput/output operations
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 beexecuted.• Command is implemented through a system file. To execute a• Command is implemented through a system file. To execute acommand the necessary system file is loaded into memory andexecuted.
OS Concepts – Signals– Are to notify a process that a particular event has occurred.– Can be synchronous and asynchronous.
OS Structure– Monolithic system– Layered system– Virtual machines– Client-server model
OS Structure – Monolithic system– Written as collection of procedures– Services provided through system calls.
OS Structure – Layered systems• Based on layered approach consists of number of layers, each built on topof lower layer.
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
OS Structure – Client server model• Kernel part is very small.• Most of the OS functions are implemented using user processes.
Processes– A process is program in execution– It consists of following:Executable programProgram’s dataStack and stack pointerProgram counter and other CPU registersDetails of opened files
ProcessesControl Block• A data structure used tokeep track of allinformation concerninga process• Includes:• Process numberProcess number• Priority• Process state• Program counter• Registers
Threads• Low cost alternative to processes for certain kind of concurrentapplications.
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 activitywithin the same address space.
Inter Process Communication• Includes thread synchronization and data exchange between threads beyondthe process boundaries.• Two methods of IPC:• Through shared memory• Through message passing.
IPC ProblemsThe Bounded Buffer (Producer-Consumer) Problem– Assume there are n slots capable of holding one item. Process producerwill produce items to fill slots and process consumer will consume theitems 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 theproducer/consumer is needed.– The consumer should not try to consume items which have not beenproduced yet (i.e. the consumer can not consume empty slots).– The producer should not try to put item into filled slots.
IPC ProblemsThe Readers and Writers Problem– Imagine a big database, such as an airline reservation system, withmany competing processes wishing to read and write.– It is acceptable to have multiple processes reading the database at thesame time, if one process is writing to the database, no other processesmay 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 writermust wait.
IPC ProblemsThe Dining Philosophers Problem– There are N philosophers spending their lives thinking and eating in aroom. In their round table there is a plate of infinite rice and Nchopsticks.– 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 hischopsticks to their original position and he starts thinking again.– The question is to write a program which does not let any philosopherto die due to hunger (i.e. no deadlocks).
Process Scheduling– Fundamental operating system function.– Scheduling is a set of policies and mechanisms supported by operatingsystems that controls the order in which the work to be done iscompleted.– 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 beevaluated:1. FAIRNESS: Make sure each process gets its fair share of the CPU2. EFFICIENCY: Keep the CPU busy 100% of the time3. RESPONSE TIME: Minimize response time for interactive users4. TURNAROUND: Minimize the time batch users must wait for output5. THROUGHPUT: Maximize the number of jobs processed in a giventimeframe
Process Scheduling Algorithms• Pre-emptive Scheduling– Scheduler suspends a running process– Allows other processes to run without each process having tocomplete– 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
Optimization Criteria• Each scheduling algorithm must be evaluated from how it optimizes thefollowing variables:– 1. Max CPU utilization2. Max throughput3. Min turnaround time4. Min waiting time5. Min response time
First-Come, First Served– FCFS) same as FIFO– Simple, fair, but poor performance.– Average queuing time may be long.
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 nextprocess to run• Round robin scheduling requires a queue– queue to get next process to run– After process has run for its quantum, Enquire it.
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 thescheduler• 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: buildaging into a variable priority
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 orprevious bursts.
• Traffic only in one direction.DEADLOCKS Bridge CrossingExample• 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 (preemptresources and rollback).• Several cars may have to be backed up if a deadlock occurs.• Starvation is possible.
DEADLOCKSNECESSARY CONDITIONSALL of these four must happen simultaneously for a deadlock to occur:Mutual exclusionOne or more than one resource must be held by a process in a non-sharable(exclusive) mode.Hold and WaitA process holds a resource while waiting for another resource.No PreemptionThere is only voluntary release of a resource - nobody else can make a processgive up a resource.Circular WaitProcess A waits for Process B waits for Process C .... waits for Process A.
• 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 RESOURCEALLOCATION GRAPHR3 Assigned to P3Resource allocation graphP2 Requests P3
DEADLOCKS RESOURCEALLOCATION GRAPHResource allocation graphwith a deadlock.Resource allocation graphwith a cycle but no deadlock.
HOW TO HANDLE DEADLOCKS – GENERAL STRATEGIESThere are three methods:Ignore Deadlocks:Ensure deadlock never occurs using eitherPrevention Prevent any one of the 4 conditions from happening.DEADLOCKS StrategyMost 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.
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) dont need mutual exclusion (and aren’tsusceptible to deadlock.)DEADLOCKS DeadlockPreventionsusceptible 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.
Do not allow one of the four conditions to occur.No preemption:a) Release any resource already being held if the process cant 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 DeadlockPreventionCircular 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 isnt necessarily the besttechnique.Prevention is generally the easiest to implement.
If we have prior knowledge of how resources will be requested, its 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 DeadlockAvoidanceUnsafe 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.
NOTE: All deadlocks are unsafe, but all unsafes are NOT deadlocks.DEADLOCKS DeadlockAvoidanceSAFEDEADLOCKUNSAFEOnly with luck willprocesses avoiddeadlock.O.S. can avoiddeadlock.
A method used to determine if a particular state is safe. Its 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.DEADLOCKSSafety AlgorithmDeadlockAvoidanceNeed[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 + .. + allocated[I-1] Sign of successLet work and finish be vectors of length m and n respectively.
Need an algorithm that determinesif deadlock occurred.Also need a means of recoveringfrom that deadlock.DEADLOCKS Deadlock DetectionSINGLE 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.
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 rollbackDEADLOCKS Deadlock Recoverycompletion, needs for completion, or depth of rollback• In general, its 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 ).
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.DEADLOCKSDeadlock RecoveryIn 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. )
A particular slide catching your eye?
Clipping is a handy way to collect important slides you want to go back to later.