Unit 1
Unit 1
com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
OPERATING SYSTEMS
LECTURE NOTES
Prepared by
P.Parvathi
Assistant Professor
Dept.of CSE
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
UNIT-I
Chapter-1 Introduction
What is an Operating System?
1.Mainframe Systems
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Job scheduling: which jobs in the job pool should be brought into memory?
Memory management: the system must allocate the memory to several jobs.
CPU scheduling: choose among jobs in memory that are ready to run.
Allocation of devices: what if more than one job wants to use a device?
Multiple jobs running concurrently should not affect one Another
Time-Sharing Systems:
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Allow many users to share the computer simultaneously, users have the
impression that they have their own machine
CPU is multiplexed among several jobs that are kept in memory and on
disk
2.Desktop Systems:
3.Multiprocessor Systems:
Advantages:
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
b.Asymmetric multiprocessing:
4.Distributed Systems:
Loosely coupled system – each processor has its own local memory and clock;
processors communicate with one another through a network.
Need protocol to communicate: TCP/IP is the most common and best supported
protocol
Three types of networks (based on distances between nodes)
Local area network (LAN): exists within a room/floor/building/campus
Wide area network (WAN): span a country or continent
Metropolitan area network (MAN): span a city
Transmission media: copper wires, optical fiber, radio
a. Client-server systems
i)File servers: allow clients to create, read, update, and delete files
ii) Compute servers: execute actions requests by clients and send back results to
clients
b. Peer-to-peer systems:
5. Clustered Systems
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Hard real-time:
Secondary storage limited or absent, data stored in short term memory, or read-
only memory (ROM)
Conflicts with time-sharing systems, not supported by general-purpose operating
systems
Soft real-time:
Handheld Systems
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Computer Startup
Computer-system operation
One or more CPUs, device controllers connect through common bus
providing access to shared memory
Concurrent execution of CPUs and devices competing for memory cycles
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Computer-System Operation:
Interrupt Handling:
The operating system preserves the state of the CPU by storing registers
and the program counter.
Determines which type of interrupt has occurred:
polling
vectored interrupt system
Separate segments of code determine what action should be taken for each type of
interrupt
Interrupt Timeline
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
I/O Structure
After I/O starts, control returns to user program only upon I/O completion.
Wait instruction idles the CPU until the next interrupt
Wait loop (contention for memory access).
At most one I/O request is outstanding at a time, no simultaneous I/O
processing.
After I/O starts, control returns to user program without waiting for I/O
completion.
System call – request to the operating system to allow user to wait for I/O
completion.
Device-status table contains entry for each I/O device indicating its type,
address, and state.
Operating system indexes into I/O device table to determine device status
and to modify table entry to include interrupt.
Synchronous Asynchronous
10
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Device-Status Table
Used for high-speed I/O devices able to transmit information at close to memory
speeds.
Device controller transfers blocks of data from buffer storage directly to main
memory without CPU intervention.
Only one interrupt is generated per block, rather than the one interrupt per byte.
Storage Structure:
Main memory – only large storage media that the CPU can access directly.
11
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Storage Hierarchy:
Storage-Device Hierarchy:
Caching:
12
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
13
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Objectives:
One set of operating-system services provides functions that are helpful to the
user:
User interface - Almost all operating systems have a user interface (UI)
Varies between Command-Line (CLI), Graphics User Interface (GUI),
Batch
Program execution - The system must be able to load a program into
memory and to run that program, end execution, either normally or
abnormally (indicating error)
I/O operations - A running program may require I/O, which may involve
a file or an I/O device.
File-system manipulation - The file system is of particular interest.
Obviously, programs need to read and write files and directories, create
and delete them, search them, list file Information, permission
management.
Communications – Processes may exchange information, on the same
computer or between computers over a network
Communications may be via shared memory or through message passing
(packets moved by the OS)
14
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Another set of OS functions exists for ensuring the efficient operation of the
system itself via resource sharing
System Calls:
15
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
System call sequence to copy the contents of one file to another file
16
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
17
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Often, more information is required than simply identity of desired system call
o Exact type and amount of information vary according to OS and call
Three general methods used to pass parameters to the OS
o Simplest: pass the parameters in registers
In some cases, may be more parameters than registers
o Parameters stored in a block, or table, in memory, and address of block
passed as a parameter in a register
This approach taken by Linux and Solaris
o Parameters placed, or pushed, onto the stack by the program and popped
off the stack by the operating system
o Block and stack methods do not limit the number or length of parameters
being passed
18
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Process control
File management
Device management
Information maintenance
Communications
MS-DOS execution:
19
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
System Programs:
20
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Simple Structure
MS-DOS – written to provide the most functionality in the least space
Not divided into modules
Although MS-DOS has some structure, its interfaces and levels of
functionality are not well separated
Layered Approach:
21
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
The operating system is divided into a number of layers (levels), each built on top
of lower layers. The bottom layer (layer 0), is the hardware; the highest (layer N)
is the user interface.
With modularity, layers are selected such that each uses functions (operations)
and services of only lower-level layers
Layered Operating System:
UNIX
UNIX – limited by hardware functionality, the original UNIX operating system
had limited structuring. The UNIX OS consists of two separable parts
o Systems programs
o The kernel
Consists of everything below the system-call interface and
above the physical hardware
Provides the file system, CPU scheduling, memory
management, and other operating-system functions; a large
number of functions for one level
22
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Mac OS X Structure:
Virtual Machines:
23
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
A virtual machine takes the layered approach to its logical conclusion. It treats
hardware and the operating system kernel as though they were all hardware
A virtual machine provides an interface identical to the underlying bare hardware
The operating system creates the illusion of multiple processes, each executing on
its own processor with its own (virtual) memory
Process Management
Chapter 4: Process
Process Concept
Process Scheduling
Operations on Processes
Cooperating Processes
Interprocess Communication
Communication in Client-Server Systems
24
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Process Concept:
Process in Memory:
Process State:
25
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
26
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
27
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Schedulers:
28
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
CPU-bound process – spends more time doing computations; few very long
CPU bursts
Context Switch:
When CPU switches to another process, the system must save the state of the old
process and load the saved state for the new process
Context-switch time is overhead; the system does no useful work while switching
Time dependent on hardware support
Process Creation:
Parent process create children processes, which, in turn create other processes,
forming a tree of processes
Resource sharing
o Parent and children share all resources
o Children share subset of parent’s resources
o Parent and child share no resources
Execution
o Parent and children execute concurrently
o Parent waits until children terminate
Address space
o Child duplicate of parent
o Child has a program loaded into it
UNIX examples
o fork system call creates new process
o exec system call used after a fork to replace the process’ memory space
with a new program
29
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Process Termination:
Process executes last statement and asks the operating system to delete it (exit)
o Output data from child to parent (via wait)
o Process’ resources are deallocated by operating system
Parent may terminate execution of children processes (abort)
o Child has exceeded allocated resources
o Task assigned to child is no longer required
o If parent is exiting
30
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Cooperating Processes:
Producer-Consumer Problem:
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
n Solution is correct, but can only use BUFFER_SIZE-1 elements
while (true) {
/* Produce an item */
while (((in = (in + 1) % BUFFER SIZE count) == out)
; /* do nothing -- no free buffers */
31
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
buffer[in] = item;
in = (in + 1) % BUFFER SIZE;
}
Communications Models :
32
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Direct Communication:
Indirect Communication:
Messages are directed and received from mailboxes (also referred to as ports)
o Each mailbox has a unique id
o Processes can communicate only if they share a mailbox
Properties of communication link
o Link established only if processes share a common mailbox
o A link may be associated with many processes
o Each pair of processes may share several communication links
o Link may be unidirectional or bi-directional
Operations
o create a new mailbox
o send and receive messages through mailbox
o destroy a mailbox
Primitives are defined as:
send(A, message) – send a message to mailbox A
receive(A, message) – receive a message from mailbox A
Mailbox sharing
o P1, P2, and P3 share mailbox A
o P1, sends; P2 and P3 receive
o Who gets the message?
Solutions
o Allow a link to be associated with at most two processes
o Allow only one process at a time to execute a receive operation
o Allow the system to select arbitrarily the receiver. Sender is notified who
the receiver was.
Synchronization:
33
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Buffering:
Socket Communication:
34
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Execution of RPC
Marshalling Parameters:
35
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Chapter 5: Threads
Overview
Multithreading Models
Threading Issues
Pthreads
Windows XP Threads
Linux Threads
Java Threads
36
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Benefits:
Responsiveness
Resource Sharing
Economy
Utilization of MP Architectures
User Threads:
Kernel Threads:
Multithreading Models:
37
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Many-to-One
One-to-One
Many-to-Many
Many-to-One:
Many-to-One Model:
One-to-One:
One-to-one Model:
38
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Many-to-Many Model:
Two-level Model:
Similar to M:M, except that it allows a user thread to be bound to kernel thread
Examples
o IRIX
o HP-UX
o Tru64 UNIX
o Solaris 8 and earlier
39
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Two-level Model:
Threading Issues:
Thread Cancellation:
Signal Handling:
Signals are used in UNIX systems to notify a process that a particular event has
occurred
A signal handler is used to process signals
o Signal is generated by particular event
o Signal is delivered to a process
o Signal is handled
Options:
40
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Thread Pools:
Scheduler Activations:
Pthreads:
A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization
API specifies behavior of the thread library, implementation is up to development
of the library
Common in UNIX operating systems (Solaris, Linux, Mac OS X)
Windows XP Threads:
41
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Linux Threads:
Java Threads:
42
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
UNIT-II
Basic Concepts:
43
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
CPU Scheduler:
Selects from among the processes in memory that are ready to execute, and
allocates the CPU to one of them
CPU scheduling decisions may take place when a process:
1.Switches from running to waiting state
2.Switches from running to ready state
3.Switches from waiting to ready
44
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
4.Terminates
Scheduling under 1 and 4 is nonpreemptive
All other scheduling is preemptive
Dispatcher:
Dispatcher module gives control of the CPU to the process selected by the short-
term scheduler; this involves:
o switching context
o switching to user mode
o jumping to the proper location in the user program to restart that program
Dispatch latency – time it takes for the dispatcher to stop one process and start
another running
Scheduling Criteria:
Optimization Criteria:
45
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
P P P
1 2 3
0 24 27 30
00
P2 P3 P1
0 3 6 30
Associate with each process the length of its next CPU burst. Use these lengths
to schedule the process with the shortest time
Two schemes:
o nonpreemptive – once CPU given to the process it cannot be preempted
until completes its CPU burst
o preemptive – if a new process arrives with CPU burst length less than
remaining time of current executing process, preempt. This scheme is
know as the Shortest-Remaining-Time-First (SRTF)
SJF is optimal – gives minimum average waiting time for a given set of
processes
46
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
SJF (non-preemptive)
P1 P3 P2 P4
0 3 7 8 12 16
SJF (preemptive)
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
47
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
=0
n+1 = n
Recent history does not count
=1
n+1 = tn
Only the actual last CPU burst counts
If we expand the formula, we get:
n+1 = tn+(1 - ) tn -1 + …
+(1 - )j tn -j + …
+(1 - )n +1 0
Since both and (1 - ) are less than or equal to 1, each successive term has less
weight than its predecessor
48
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Priority Scheduling:
Each process gets a small unit of CPU time (time quantum), usually 10-100
milliseconds. After this time has elapsed, the process is preempted and added to
the end of the ready queue.
If there are n processes in the ready queue and the time quantum is q, then each
process gets 1/n of the CPU time in chunks of at most q time units at once. No
process waits more than (n-1)q time units.
Performance
o q large FIFO
o q small q must be large with respect to context switch, otherwise
overhead is too high
49
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
Multilevel Queue:
50
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
A process can move between the various queues; aging can be implemented this
way
Multilevel-feedback-queue scheduler defined by the following parameters:
o number of queues
o scheduling algorithms for each queue
o method used to determine when to upgrade a process
o method used to determine when to demote a process
o method used to determine which queue a process will enter when that
process needs service
Three queues:
o Q0 – RR with time quantum 8 milliseconds
o Q1 – RR time quantum 16 milliseconds
51
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
o Q2 – FCFS
Scheduling
o A new job enters queue Q0 which is served FCFS. When it gains CPU, job
receives 8 milliseconds. If it does not finish in 8 milliseconds, job is
moved to queue Q1.
o At Q1 job is again served FCFS and receives 16 additional milliseconds.
If it still does not complete, it is preempted and moved to queue Q2.
Multiple-Processor Scheduling:
Real-Time Scheduling:
Thread Scheduling:
Local Scheduling – How the threads library decides which thread to put onto an
available LWP
Global Scheduling – How the kernel decides which kernel thread to run next
Local Scheduling – How the threads library decides which thread to put onto an
available LWP
52
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Global Scheduling – How the kernel decides which kernel thread to run next
#include <pthread.h>
#include <stdio.h>
#define NUM THREADS 5
int main(int argc, char *argv[])
{
int i;
pthread t tid[NUM THREADS];
pthread attr t attr;
/* get the default attributes */
pthread attr init(&attr);
/* set the scheduling algorithm to PROCESS or SYSTEM */
pthread attr setscope(&attr, PTHREAD SCOPE SYSTEM);
/* set the scheduling policy - FIFO, RT, or OTHER */
pthread attr setschedpolicy(&attr, SCHED OTHER);
/* create the threads */
for (i = 0; i < NUM THREADS; i++)
pthread create(&tid[i],&attr,runner,NULL);
/* now join on each thread */
for (i = 0; i < NUM THREADS; i++)
pthread join(tid[i], NULL);
}
Solaris scheduling
Windows XP scheduling
Linux scheduling
53
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Solaris 2 Scheduling:
Windows XP Priorities:
54
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Linux Scheduling:
55
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Algorithm Evaluation:
Background
The Critical-Section Problem
Peterson’s Solution
Synchronization Hardware
Semaphores
Classic Problems of Synchronization
Monitors
Synchronization Examples
Atomic Transactions
Background:
56
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
register1 = count
register1 = register1 + 1
count = register1
count-- could be implemented as
register2 = count
register2 = register2 - 1
count = register2
Consider this execution interleaving with ―count = 5‖ initially:
57
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
3.Bounded Waiting - A bound must exist on the number of times that other processes are
allowed to enter their critical sections after a process has made a request to enter its
critical section and before that request is granted
Assume that each process executes at a nonzero speed
No assumption concerning relative speed of the N processes
Peterson’s Solution:
Two process solution
Assume that the LOAD and STORE instructions are atomic; that is, cannot be
interrupted.
The two processes share two variables:
o int turn;
o Boolean flag[2]
The variable turn indicates whose turn it is to enter the critical section.
The flag array is used to indicate if a process is ready to enter the critical section.
flag[i] = true implies that process Pi is ready.
CRITICAL SECTION
flag[i] = FALSE;
REMAINDER SECTION
Synchronization Hardware
Many systems provide hardware support for critical section code
Uniprocessors – could disable interrupts
o Currently running code would execute without preemption
o Generally too inefficient on multiprocessor systems
Operating systems using this not broadly scalable
Modern machines provide special atomic hardware instructions
Atomic = non-interruptable
o Either test memory word and set value
o Or swap contents of two memory words
58
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Definition:
// critical section
lock = FALSE;
// remainder section
Swap Instruction
Definition:
Shared Boolean variable lock initialized to FALSE; Each process has a local
Boolean variable key.
Solution:
while (true) {
key = TRUE;
while ( key == TRUE)
59
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
// critical section
lock = FALSE;
// remainder section
Semaphore
Semaphore Implementation
Must guarantee that no two processes can execute wait () and signal () on the
same semaphore at the same time
60
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Thus, implementation becomes the critical section problem where the wait and
signal code are placed in the crtical section.
o Could now have busy waiting in critical section implementation
But implementation code is short
Little busy waiting if critical section rarely occupied
Note that applications may spend lots of time in critical sections and therefore this
is not a good solution.
Two operations:
o block – place the process invoking the operation on the appropriate
waiting queue.
o wakeup – remove one of processes in the waiting queue and place it in the
ready queue.
Implementation of wait:
wait (S){
value--;
if (value < 0) {
add this process to waiting queue
block(); }
}
Implementation of signal:
Signal (S){
value++;
if (value <= 0) {
remove a process P from the waiting queue
wakeup(P); }
}
Deadlock – two or more processes are waiting indefinitely for an event that can be
caused by only one of the waiting processes
Let S and Q be two semaphores initialized to 1
61
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
P0 P1
wait (S); wait (Q);
wait (Q); wait (S);
. .
. .
. .
signal (S); signal (Q);
signal (Q); signal (S);
Starvation – indefinite blocking. A process may never be removed from the
semaphore queue in which it is suspended.
Bounded-Buffer Problem
Readers and Writers Problem
Dining-Philosophers Problem
Bounded-Buffer Problem
while (true) {
// produce an item
wait (empty);
wait (mutex);
signal (mutex);
signal (full);
}
while (true) {
wait (full);
wait (mutex);
62
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
signal (mutex);
signal (empty);
Readers-Writers Problem
Shared Data
Data set
Semaphore mutex initialized to 1.
Semaphore wrt initialized to 1.
Integer readcount initialized to 0.
while (true) {
wait (wrt) ;
// writing is performed
signal (wrt) ;
}
while (true) {
wait (mutex) ;
readcount ++ ;
if (readcount == 1) wait (wrt) ;
signal (mutex)
// reading is performed
63
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
wait (mutex) ;
readcount - - ;
if (readcount == 0) signal (wrt) ;
signal (mutex) ;
}
Dining-Philosophers Problem
Shared data
Bowl of rice (data set)
Semaphore chopstick [5] initialized to 1
The structure of Philosopher i:
While (true) {
wait ( chopstick[i] );
wait ( chopStick[ (i + 1) % 5] );
// eat
signal ( chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
// think
64
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
monitor monitor-name
{
// shared variable declarations
procedure P1 (…) { …. }
…
Condition Variables
condition x, y;
65
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
suspended.
x.signal () – resumes one of processes (if any) that
invoked x.wait ()
monitor DP
{
enum { THINKING; HUNGRY, EATING) state [5] ;
condition self [5];
66
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
state[i] = EATING ;
self[i].signal () ;
}
}
initialization_code() {
for (int i = 0; i < 5; i++)
state[i] = THINKING;
}
}
Each philosopher I invokes the operations pickup()
and putdown() in the following sequence:
dp.pickup (i)
EAT
dp.putdown (i)
Variables
semaphore mutex; // (initially = 1)
semaphore next; // (initially = 0)
int next-count = 0;
Each procedure F will be replaced by
wait(mutex);
…
body of F;
…
if (next-count > 0)
signal(next)
else
signal(mutex);
Mutual exclusion within a monitor is ensured.
Monitor Implementation
67
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
x-count++;
if (next-count > 0)
signal(next);
else
signal(mutex);
wait(x-sem);
x-count--;
Synchronization Examples
Solaris
Windows XP
Linux
Pthreads
Solaris Synchronization
68
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Windows XP Synchronization
Linux Synchronization
Linux:
disables interrupts to implement short critical sections
Linux provides:
semaphores
spin locks
Pthreads Synchronization
Atomic Transactions
System Model
Log-based Recovery
Checkpoints
Concurrent Atomic Transactions
System Model
Assures that operations happen as a single logical unit of work, in its entirety, or
not at all
Related to field of database systems
Challenge is assuring atomicity despite computer system failures
69
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Volatile storage – information stored here does not survive system crashes
o Example: main memory, cache
Nonvolatile storage – Information usually survives crashes
o Example: disk and tape
Stable storage – Information never lost
o Not actually possible, so approximated via replication or RAID to devices
with independent failure modes
Log-Based Recovery
Using the log, system can handle any volatile memory errors
o Undo(Ti) restores value of all data updated by Ti
o Redo(Ti) sets values of all data in transaction Ti to new values
Undo(Ti) and redo(Ti) must be idempotent
o Multiple executions must have the same result as one execution
If system fails, restore state of all updated data via log
o If log contains <Ti starts> without <Ti commits>, undo(Ti)
o If log contains <Ti starts> and <Ti commits>, redo(Ti)
70
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Checkpoints
Concurrent Transactions
Serializability
Schedule 1: T0 then T1
Nonserial Schedule
71
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Locking Protocol
72
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Timestamp-based Protocols
Timestamp-ordering Protocol
Timestamp-ordering Protocol
73
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
74
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Chapter 8: Deadlocks
Objectives
75
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
P0 P1
wait (A); wait(B)
wait (B); wait(A)
System Model
Deadlock Characterization
76
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Resource-Allocation Graph
Pi requests instance of Rj
Pi
Pi
Pi is holding an instance of Rj
77
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Basic Facts
78
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Deadlock Prevention
Deadlock Avoidance
79
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Safe State
Basic Facts
Avoidance algorithms
80
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Resource-Allocation Graph
81
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
The request can be granted only if converting the request edge to an assignment
edge does not result in the formation of a cycle in the resource allocation graph
Banker’s Algorithm
Multiple instances.
Each process must a priori claim maximum use.
When a process requests a resource it may have to wait.
When a process gets all its resources it must return them in a finite amount of
time.
Safety Algorithm
82
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
1.If Requesti Needi go to step 2. Otherwise, raise error condition, since process has
exceeded its maximum claim.
2.If Requesti Available, go to step 3. Otherwise Pi must wait, since resources are not
available.
3.Pretend to allocate requested resources to Pi by modifying the state as follows:
Available = Available – Request;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
o If safe the resources are allocated to Pi.
o If unsafe Pi must wait, and the old resource-allocation state is restored
Need
ABC
P0 74 3
P1 12 2
P2 60 0
P3 01 1
P4 43 1
The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies
safety criteria.
83
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
P4 002 431
Executing safety algorithm shows that sequence < P1, P3, P4, P0, P2> satisfies
safety requirement.
Can request for (3,3,0) by P4 be granted?
Can request for (0,2,0) by P0 be granted?
Deadlock Detection
84
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
Detection Algorithm
Algorithm requires an order of O(m x n2) operations to detect whether the system is in
deadlocked state.
85
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
P1 20 1
P2 00 1
P3 10 0
P4 00 2
State of system?
o Can reclaim resources held by process P0, but insufficient resources to
fulfill other processes; requests.
o Deadlock exists, consisting of processes P1, P2, P3, and P4.
Detection-Algorithm Usage
86
www.Vidyarthiplus.com
www.Vidyarthiplus.com
Operating Systems Lecture Notes
Prepared By
P.PARVATHI,
Asst.Professor/CSE
UNITIII
87
www.Vidyarthiplus.com