Date post: | 14-Jan-2016 |
Category: |
Documents |
Upload: | rosalind-boyd |
View: | 214 times |
Download: | 2 times |
Silberschatz and Galvin1999 7.1
Operating Systems Overview
D. Manivannan
Department of Computer Science
University of Kentucky
Silberschatz and Galvin1999 7.2
What is an Operating System?
• Resource Manager of a Computer
• Provides an user friendly environment
Silberschatz and Galvin1999 7.3
Processes
• Processes
– States of process, process control block,
– Scheduling of processes Long term scheduler, short term scheduler, medium-
term scheduler (swapping) Degree of multiprogramming Context switch
– Operation on processes Process creation, fork, execlp, execvp system calls in
UNIX
• Threads
– Difference between a process and a thread
Silberschatz and Galvin1999 7.4
Process Scheduling
• What is CPU scheduling?– Short term scheduler– Preemptive scheduling, – Scheduling criteria
CPU utilization, throughput, turnaround time, waiting time, response time
• Scheduling algorithms– FCFS, SJF, Priority scheduling, Round-robin scheduling– Multilevel queue scheduling, multilevel feed back queue scheduling
• Real-time scheduling
• How are various scheduling algorithms evaluated– Deterministic modeling, queuing models, simulations,
implementations
Silberschatz and Galvin1999 7.5
Process Synchronization
• The critical section problem
• Synchronization mechanisms– Hardware instruction support (e.g. test and set instruction, swap instruction).– Semaphores
Binary semaphores, counting semaphores Should know how to use semaphores to solve synchronization problems Problems with using semaphores
– Deadlock, starvation, wrong use of semaphores– Monitors
Should know how to use monitors for solving synchronization problems
• Classical synchronization problems– The bounded buffer problem– Readers writers problem– Dining philosophers problem
Silberschatz and Galvin1999 7.6
Process Synchronization - Semaphores
• Semaphores serve as a synchronization tool, usually supported by the operating system.
• Semaphore S – integer variable
• S can only be accessed via two indivisible (atomic) operations
wait (S): while S 0 do no-op;S := S – 1;
signal (S): S := S + 1;
(A problem with this implementation is processes will “busy wait” wasting CPU cycles)
Silberschatz and Galvin1999 7.7
Usual Implementation of semaphores
Each semaphore is an integer variable; it also has a queue associated with it. (this implementation prevents “busy wait”)
wait (S): if S >= 1 then S := S - 1;else block the process on the semaphore queue
signal (S): if some processes are blocked in the semaphore queue
then unblock a process
else S := S + 1;
Silberschatz and Galvin1999 7.8
Solution to Critical Section using Semaphores
• Shared variables– var mutex : semaphore– initially mutex = 1
• Process Pi
repeat
wait(mutex);
critical section
signal(mutex);
remainder section
until false;
Silberschatz and Galvin1999 7.9
Semaphore as General Synchronization Tool
• Execute B in Pj only after A is executed in Pi
• Use semaphore flag initialized to 0
• Code:
Pi Pj
A wait(flag)
signal(flag) B
Silberschatz and Galvin1999 7.10
What Problems can Arise When Semaphores are Used : Deadlock and
Starvation
• 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
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.
Silberschatz and Galvin1999 7.11
Two Types of Semaphores
• Counting semaphore – integer value can range over an unrestricted domain.
• Binary semaphore – integer value can range only between 0 and 1; can be simpler to implement.
• Can implement a counting semaphore S using binary semaphores.
Silberschatz and Galvin1999 7.12
Classical Problems of Synchronization
• Bounded-Buffer Problem
• Readers and Writers Problem
• Dining-Philosophers Problem
Silberschatz and Galvin1999 7.13
Bounded-Buffer Problem
• Shared datatype item = …var buffer = …
full, empty, mutex: semaphore;nextp, nextc: item;full :=0; empty := n; mutex :=1;
Silberschatz and Galvin1999 7.14
Bounded-Buffer Problem (Cont.)
• Producer processrepeat
…produce an item in nextp
…wait(empty);wait(mutex);
…signal(mutex);signal(full);
until false;
Silberschatz and Galvin1999 7.15
Bounded-Buffer Problem (Cont.)
• Consumer processrepeat
wait(full)wait(mutex);
…remove an item from buffer to nextc
…signal(mutex);signal(empty);
…consume the item in nextc
…until false;
Silberschatz and Galvin1999 7.16
Readers-Writers Problem
• Readers Writers Problem: Writers have exclusive access to shared data; Readers can access shared data simultaneously.
• A solution to this problem using semaphores:var mutex, wrt: semaphore (=1);
Shared Var readcount : integer (=0);
• Writer processwait(wrt);
…writing is performed
…signal(wrt);
Silberschatz and Galvin1999 7.17
Readers-Writers Problem (Cont.)
• Reader processwait(mutex);
readcount := readcount +1;if readcount = 1 then wait(wrt);
signal(mutex); …
reading is performed …
wait(mutex);readcount := readcount – 1;if readcount = 0 then signal(wrt);
signal(mutex):
Silberschatz and Galvin1999 7.18
Dining-Philosophers Problem
• Shared data
var chopstick: array [0..4] of semaphore;(=1 initially)
Silberschatz and Galvin1999 7.19
Dining-Philosophers Problem (Cont.)
• Philosopher i:repeat
wait(chopstick[i])wait(chopstick[(i+1) mod 5])
…eat …
signal(chopstick[i]);signal(chopstick[(i+1) mod 5]);
…think …
until false;Problem with this solution: deadlock and starvation
Silberschatz and Galvin1999 7.20
• High-level synchronization construct that allows the safe sharing of an abstract data type among concurrent processes.
type monitor-name = monitorvariable declarationsprocedure entry P1 (…);
begin … end;procedure entry P2(…);
begin … end;
procedure entry Pn (…);begin…end;
begininitialization code
end
MonitorsMonitors
Silberschatz and Galvin1999 7.21
• Only one process can be active inside the monitor.
• To allow a process to wait within the monitor, a condition variable must be declared, as
var x, y: condition
• Condition variable can only be used with the operations wait and signal.
– The operationx.wait;
means that the process invoking this operation is suspended until another process invokes
x.signal;Wait operation releases the monitor.
– The x.signal operation resumes exactly one suspended process. If no process is suspended, then the signal operation has no effect.
Monitors (Cont.)Monitors (Cont.)
Silberschatz and Galvin1999 7.22
Schematic view of a monitorSchematic view of a monitor
Silberschatz and Galvin1999 7.23
Monitor with condition variablesMonitor with condition variables
Silberschatz and Galvin1999 7.24
type dining-philosophers = monitorvar state : array [0..4] of :(thinking, hungry, eating);var self : array [0..4] of condition;procedure entry pickup (i: 0..4);
beginstate[i] := hungry,test (i);if state[i] eating then self[i]. wait,
end;
procedure entry putdown (i: 0..4);begin
state[i] := thinking;test (i+4 mod 5);test (i+1 mod 5);
end;
Dining Philosophers Example Dining Philosophers Example
Silberschatz and Galvin1999 7.25
procedure test(k: 0..4);begin
if state[(k+4) mod 5] eatingand state[k] = hungryand state[[k+1 mod 5] ] eatingthen begin
state[k] := eating;self[k].signal;
end;
end;
beginfor i := 0 to 4
do state[i] := thinking;
end.
Dining Philosophers (Cont.)Dining Philosophers (Cont.)
Silberschatz and Galvin1999 7.26
• Philosopher i
dining-philosophers.pickup(i)
Start eating;
dining-philosophers.putdown(i)
Silberschatz and Galvin1999 7.27
Memory Management
• Overlays, physical and logical address space
• Swapping
• Memory allocation for processes– Contiguous allocation – Advantages Disadvantages :
external and internal fragmentation– Paged allocation
Address translation under paged allocation Page table implementation Associative registers, Translation look aside buffers Effective access time calculation Two level paging scheme Inverted page table architecture Sharing pages between processes – adv. Disadv.
– Segmentation-based allocation Address translation under segmentation based
allocation
Silberschatz and Galvin1999 7.28
Virtual Memory
• What is virtual memory?
• Demand paging (an implementation of virtual memory)– Page faults, effective access time calculation– Page replacement algorithms
FIFO, Optimal algorithm, LRU, Additional reference bit algorithm, Second chance algorithm – comparison of these algorithms
Counting algorithms– LFU (least frequently used )– MFU (most frequently used)
– Allocation of frames Issues that need to be taken into consideration for Different allocation schemes
– Fixed, priority-based, equal, proportionate– Thrashing
What is thrashing? Causes for thrashing Working set model to solve the problem of thrashing How can program structure contribute to thrashing?
Silberschatz and Galvin1999 7.29
File System Interface
• File structures
• File attributes, operation, types, access methods
• Device directory
• Different directory structures
– Single level
– Two level
– Tree Structured
– Acyclic graph structured
– General graph structured
• File access protection
Silberschatz and Galvin1999 7.30
File System Implementation
• File system
– Disk space allocation Contiguous, linked, indirect, Mapping under each allocation UNIX allocation scheme
– Free Space management Bit vector, linked list approach, grouping, counting
Silberschatz and Galvin1999 7.31
Secondary and tertiary storage
• Disk Structure– Cylinders, tracks, sector(block)
• Disk Scheduling– Goal of a good scheduling algorithm– Various scheduling algorithms
FCFS, SSTF, SCAN, C-SCAN, C-LOOK Performance of these algorithms
• Reliability– RAID
• Tertiary storage– Removable disks, WORM disks, Tapes, – Hierarchical storage management
Silberschatz and Galvin1999 7.32
Price per Megabyte of DRAM, From 1981 to 2004
Silberschatz and Galvin1999 7.33
Price per Megabyte of Magnetic Hard Disk, From 1981 to 2004