Web & Social Media Analytics Previous Year Question Paper.pdf
Unit 4
1. Unit –IV
Introduction to Operating System, Process and
threads and Deadlocksthreads and Deadlocks
2. 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
3. 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
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.
5. 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
7. 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.
8. 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
9. 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
10. 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)
11. 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
12. 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
13. 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
14. 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,list
10 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,return
2 Instruction set Evaluation stack Load,store,add
1 Electronic circuit Registers,gates,buses Clear,transfer,activate
15. 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
16. 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
17. Types of Advanced OSs
Distributed Operating Systems
Multiprocessor Operating Systems
Database Operating Systems
Real-time Operating Systems
18. 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.
19. 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
20. 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
21. 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.
23. 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
24. 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
26. 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
27. 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.
28. OS Concepts – Signals
– Are to notify a process that a particular event has occurred.
– Can be synchronous and asynchronous.
30. OS Structure – Monolithic system
– Written as collection of procedures
– Services provided through system calls.
31. OS Structure – Layered systems
• Based on layered approach consists of number of layers, each built on top
of lower layer.
32. 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
33. OS Structure – Client server model
• Kernel part is very small.
• Most of the OS functions are implemented using user processes.
34. 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
36. 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
37. Threads
• Low cost alternative to processes for certain kind of concurrent
applications.
38. 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.
39. 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.
40. 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.
41. 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.
42. 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).
43. 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
44. 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
45. 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
46. First-Come, First Served
– FCFS) same as FIFO
– Simple, fair, but poor performance.
– Average queuing time may be long.
47.
48. 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.
49.
50. 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
51. 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.
52.
53. • 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.
54. 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 process
give up a resource.
Circular Wait
Process A waits for Process B waits for Process C .... waits for Process A.
55. • 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
57. 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 happen
and stop dangerous operations..
Allow deadlock to happen. This requires using both:
Detection Know a deadlock has occurred.
Recovery Regain the resources.
58. 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’t
susceptible 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. A
sequence of resources is always collected beginning with the same one.
c) Utilization is low, starvation possible.
59. 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 additional
resource.
b) Allow preemption - if a needed resource is held by another process, which is also
waiting 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 utilization
and/or resources. For this reason, prevention isn't necessarily the best
technique.
Prevention is generally the easiest to implement.
60. If we have prior knowledge of how resources will be requested, it's possible to determine if we
are 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 enough
resources for the first to finish, and as each finishes and releases its
resources there are enough for the next to finish.
The rule is simple: If a request allocation would cause an unsafe state, do not honor that
request.
NOTE: All deadlocks are unsafe, but all unsafes are NOT deadlocks.
61. NOTE: All deadlocks are unsafe, but all unsafes are NOT deadlocks.
DEADLOCKS Deadlock
Avoidance
SAFE
DEADLOCK
UNSAFE
Only with luck will
processes avoid
deadlock.
O.S. can avoid
deadlock.
62. A method used to determine if a particular state is safe. It's safe if there exists a sequence
of 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.
63. Need an algorithm that determines
if deadlock occurred.
Also need a means of recovering
from that deadlock.
DEADLOCKS Deadlock Detection
SINGLE INSTANCE OF A RESOURCE TYPE
• Wait-for graph == remove the resources
from the usual graph and collapse edges.
• An edge from p(j) to p(i) implies that p(j) is
waiting for p(i) to release.
64. 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 to
completion, 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 the
process.
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 ).
65. COMBINED APPROACH TO DEADLOCK HANDLING:
Type of resource may dictate best deadlock handling. Look at ease of
implementation, 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. )