silberschatz and galvin 1999 4.1 operating system concepts workshop 2: agenda homework review:...

70
Operating System Concepts Silberschatz and Galvin 1999 4.1 Workshop 2: Agenda Homework Review: ch’s 4-7 Study group papers & presentations Lecture & discussion on process types & concepts Group activity on processes and resources Lecture & discussion on resource allocation* Group activity on process management Summary & preview *= no graph theory!

Upload: marilynn-young

Post on 03-Jan-2016

217 views

Category:

Documents


4 download

TRANSCRIPT

Operating System Concepts Silberschatz and Galvin 1999 4.1

Workshop 2: Agenda

• Homework Review: ch’s 4-7

• Study group papers & presentations

• Lecture & discussion on process types & concepts

• Group activity on processes and resources

• Lecture & discussion on resource allocation*

• Group activity on process management

• Summary & preview

• *= no graph theory!

Operating System Concepts Silberschatz and Galvin 1999 4.2

Workshop 2: Homework Review

• Chapter 4: 4.4, 4.5

• Chapter 5: 5.3, 5.9

• Chapter 6: 6.1, 6.8

• Chapter 7: 7.1, 7.3

Operating System Concepts Silberschatz and Galvin 1999 4.3

Study Group Project

How does O/S manage processes & concurrency

SchedulingInterprocess comm.SynchronizationDeadlocks

3 - 5 page paper

Presentation 5 - 10 minutes1 person can represent group

Operating System Concepts Silberschatz and Galvin 1999 4.4

Module 4: Processes

• Process Concept

• Process Scheduling

• Operation on Processes

• Threads

• Interprocess Communication

Operating System Concepts Silberschatz and Galvin 1999 4.5

Process Concept

• An operating system executes a variety of programs:– Batch system – jobs– Time-shared systems – user programs or tasks– PC/Mac -- applications

• Textbook uses the terms job and process almost interchangeably.

• Process – a program in execution; process execution must progress in sequential fashion.

• A process includes:– program counter – stack– data section– Code or text section (may be shared)

Operating System Concepts Silberschatz and Galvin 1999 4.6

Diagram of Process State

Operating System Concepts Silberschatz and Galvin 1999 4.7

Process Control Block (PCB)

Operating System Concepts Silberschatz and Galvin 1999 4.8

CPU Switch From Process to Process

Operating System Concepts Silberschatz and Galvin 1999 4.9

Ready Queue And Various I/O Device Queues

Operating System Concepts Silberschatz and Galvin 1999 4.10

Representation of Process Scheduling

Operating System Concepts Silberschatz and Galvin 1999 4.11

Schedulers

• Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue.

– Infrequent: seconds or minutes intervals

– Controls degree of multi-programming

• Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates CPU.

– Millisecond intervals

• Medium-term scheduler (swapper)

Operating System Concepts Silberschatz and Galvin 1999 4.12

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.

Operating System Concepts Silberschatz and Galvin 1999 4.13

Process Creation

• Parent process creates children processes, which, in turn create other processes, forming a tree of processes.

• Resource sharing

– Parent and children share all resources.

– Children share subset of parent’s resources.

– Parent and child share no resources.

• Execution

– Parent and children execute concurrently.

– Parent waits until children terminate.

Operating System Concepts Silberschatz and Galvin 1999 4.14

Process Creation (Cont.)

• Address space

– Child duplicate of parent.

– Child has a program loaded into it.

• UNIX examples

– fork system call creates new process, returns PID

– execve system call used after a fork to replace the process’ memory space with a new program.

– See sample code if available

Operating System Concepts Silberschatz and Galvin 1999 4.15

A Tree of Processes On A Typical UNIX System

Operating System Concepts Silberschatz and Galvin 1999 4.16

Process Termination

• Process executes last statement and asks the operating system to decide it (exit).

• Parent may terminate execution of children processes (abort).

– Child has exceeded allocated resources.

– Task assigned to child is no longer required.

– Parent is exiting.

– Etc.

• Parent termination may cascade

Operating System Concepts Silberschatz and Galvin 1999 4.17

Threads

• A thread (or lightweight process) is a basic unit of CPU utilization; it consists of:

– program counter– register set – stack space

• A thread shares with its peer threads its:– code section– data section– operating-system resourcescollectively known as a task.

• A traditional or heavyweight process is equal to a task with one thread

• See process viewer example if available

Operating System Concepts Silberschatz and Galvin 1999 4.18

Threads (Cont.)

• In a multiple threaded task, while one server thread is blocked and waiting, a second thread in the same task can run.

– Cooperation of multiple threads in same job confers higher throughput and improved performance.

– Examples: Web servers, Database servers

• Allow sequential processes to make blocking system calls while also achieving parallelism.

• Kernel-supported threads

• User-level threads; supported above the kernel, via a set of library calls

• Hybrid approach implements both user-level and kernel-supported threads (Solaris 2).

Operating System Concepts Silberschatz and Galvin 1999 4.19

Multiple Threads within a Task

Operating System Concepts Silberschatz and Galvin 1999 4.20

Interprocess Communication (IPC)

• Mechanism for processes to communicate and to synchronize their actions.

• Example: Windows NT Local Procedure Call Facility

• Message system – processes communicate with each other without resorting to shared variables.

• IPC facility provides two operations:– send(message) to <name>– receive(message) from <name>– See examples if available

• Processes need to:– establish a communication link between them– exchange messages via send/receive

Operating System Concepts Silberschatz and Galvin 1999 4.21

IPC (Cont’d)

• Implementation of communication link– Shared memory– hardware bus– Etc.

• Direct communication link– Shared memory– Pipes– Sockets– Semaphores

• Indirect communication link– Mailboxes

Operating System Concepts Silberschatz and Galvin 1999 4.22

Module 5: CPU Scheduling

• Basic Concepts

• Scheduling Criteria

• Scheduling Algorithms

• Multiple-Processor Scheduling

• Real-Time Scheduling

• Algorithm Evaluation

Operating System Concepts Silberschatz and Galvin 1999 4.23

Basic Concepts

• Maximum CPU utilization obtained with multiprogramming

• CPU–I/O Burst Cycle – Process execution consists of a cycle of CPU execution and I/O wait.

• CPU burst distribution

Operating System Concepts Silberschatz and Galvin 1999 4.24

Alternating Sequence of CPU And I/O Bursts

Operating System Concepts Silberschatz and Galvin 1999 4.25

Histogram of CPU-burst Times

Operating System Concepts Silberschatz and Galvin 1999 4.26

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.

4. Terminates.

• Scheduling can be nonpreemptive. (ex: Win3x)

• Or, preemptive.(ex: Win95+)

• A dispatch routine does the actual context switch

Operating System Concepts Silberschatz and Galvin 1999 4.27

Scheduling Criteria

• CPU utilization – keep the CPU as busy as possible

• Throughput – # of processes that complete their execution per time unit

• Turnaround time – amount of time to execute a particular process

• Waiting time – amount of time a process has been wiating in the ready queue

• Response time – amount of time it takes from when a request was submitted until the first response is produced, not output

Operating System Concepts Silberschatz and Galvin 1999 4.28

First-Come, First-Served (FCFS) Scheduling

• Example: Process Burst Time

P1 24

P2 3

P3 3

• Suppose that the processes arrive in the order: P1 , P2 , P3

The Gantt Chart for the schedule is:

• Waiting time for P1 = 0; P2 = 24; P3 = 27

• Average waiting time: (0 + 24 + 27)/3 = 17

• Turnaround for P2 = 24 + 3 = 27

P1 P2 P3

24 27 300

Operating System Concepts Silberschatz and Galvin 1999 4.29

FCFS Scheduling (Cont.)

Suppose that the processes arrive in the order

P2 , P3 , P1 .

• The Gantt chart for the schedule is:

• Waiting time for P1 = 6; P2 = 0; P3 = 3

• Average waiting time: (6 + 0 + 3)/3 = 3

• Much better than previous case.

• Convoy effect: short process stack up behind long process

P1P3P2

63 300

Operating System Concepts Silberschatz and Galvin 1999 4.30

Shortest-Job-First (SJR) Scheduling

• Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time

– But: how do we know what the next burst is? (more on this).

• Two schemes:

– Non preemptive

– 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

Operating System Concepts Silberschatz and Galvin 1999 4.31

Process Arrival Time Burst Time

P1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4

• SJF (non-preemptive)

• Average waiting time = (w1+w3+w2+w4)/n = (0 + 3 + 6 + 7)/4 = 4

Example of Non-Preemptive SJF

P1 P3 P2

73 160

P4

8 12

Operating System Concepts Silberschatz and Galvin 1999 4.32

Example of Preemptive SJF

Process Arrival Time Burst Time

P1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4

• SJF (preemptive)

• Average waiting time = (9 + 1 + 0 +2)/4 = 3

P1 P3P2

42 110

P4

5 7

P2 P1

16

Operating System Concepts Silberschatz and Galvin 1999 4.33

Priority Scheduling

• A priority number (integer) is associated with each process

• The CPU is allocated to the process with the highest priority (smallest integer highest priority).

– Preemptive

– nonpreemptive

• SJF is a priority scheduling where priority is the predicted next CPU burst time.

• Problem Starvation – low priority processes may never execute.

• Solution Aging – as time progresses increase the priority of the process.

Operating System Concepts Silberschatz and Galvin 1999 4.34

Round Robin (RR)

• Each process gets a small unit of CPU time (time quantum), usually 10-100 milliseconds. After this time has elapsed, the process is preempted.

• 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.

Operating System Concepts Silberschatz and Galvin 1999 4.35

Example: RR with Time Quantum = 20

Process Burst Time

P1 53

P2 17

P3 68

P4 24

• The Gantt chart is:

• Typically, higher average turnaround than SJF, but better response time.

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162

Operating System Concepts Silberschatz and Galvin 1999 4.36

Multilevel Queue Scheduling

Operating System Concepts Silberschatz and Galvin 1999 4.37

Multilevel Feedback Queues

Operating System Concepts Silberschatz and Galvin 1999 4.38

Multiple-Processor Scheduling

• CPU scheduling more complex when multiple CPUs are available.

• Homogeneous processors within a multiprocessor.

• Load sharing

• Asymmetric multiprocessing – only one processor accesses the system data structures, alleviating the need for data sharing.

Operating System Concepts Silberschatz and Galvin 1999 4.39

Real-Time Scheduling

• Hard real-time systems – required to complete a critical task within a guaranteed amount of time.

• Soft real-time computing – requires that critical processes receive priority over less fortunate ones.

Operating System Concepts Silberschatz and Galvin 1999 4.40

Group Activity: Processes, Scheduling

Select a process area

scheduling a single cpuscheduling multiple cpu'sreal-time schedulingthreadsetc.

Prepare "blackboard" presentation

resources requiredhow coordinatedwhen required in relation to each otheretc.

Group ActivityGroup Activity

Operating System Concepts Silberschatz and Galvin 1999 4.41

Module 6: Process Synchronization

• Background

• The Critical-Section Problem

• Synchronization Hardware

• Semaphores

• Classical Problems of Synchronization

• Critical Regions

• Monitors

• Atomic Transactions

Operating System Concepts Silberschatz and Galvin 1999 4.42

Background

• Concurrent access to shared data may result in data inconsistency.

• Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes.

Operating System Concepts Silberschatz and Galvin 1999 4.43

The Critical-Section Problem

• n processes all competing to use some shared data

• Each process has a code segment, called critical section, in which the shared data is accessed.

• Problem – ensure that when one process is executing in its critical section, no other process is allowed to execute in its critical section.

• Structure of process Pi

repeat

entry section

critical section

exit section

remainder section

until false;

Operating System Concepts Silberschatz and Galvin 1999 4.44

Solution to Critical-Section Problem

1. Mutual Exclusion. If process Pi is executing in its critical section, then no other processes can be executing in their critical sections.

2. Progress. If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely.

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.

Operating System Concepts Silberschatz and Galvin 1999 4.45

Initial Attempts to Solve Problem

• Only 2 processes, P0 and P1

• General structure of process

• repeat

entry section

critical section

exit section

reminder section

until false;

• Processes may share some common variables to synchronize their actions.

Operating System Concepts Silberschatz and Galvin 1999 4.46

Algorithm for 2 Processes

• Uses shared variables flag, turn

• Initially flag[0] = flag[1] = false; turn can be either 0 or 1

• Process Pi

repeat

flag [i] := true; /* set pi ready */ turn := j; /* set turn to pj */

/* wait while other job is ready and it’s that job’s turn */

while (flag [j] = true and turn = j) do no-op;

critical section

flag [i] := false;

remainder section

until false;

• Meets all three requirements; solves the critical-section problem for two processes.

Operating System Concepts Silberschatz and Galvin 1999 4.47

Synchronization Hardware

• Can be used to implement mutual exclusion

• Test and modify the content of a word atomically.

function Test-and-Set (var target: boolean): boolean;

begin

Test-and-Set := target;target := true;

end;

Operating System Concepts Silberschatz and Galvin 1999 4.48

Semaphore

• Synchronization tool that does not require busy waiting (spinlocks)

- Can use block, wakeup

• Semaphore S – integer variable

• 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;

Operating System Concepts Silberschatz and Galvin 1999 4.49

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.

Operating System Concepts Silberschatz and Galvin 1999 4.50

Classical Problems of Synchronization

• Readers and Writers Problem

• Dining-Philosophers Problem

Operating System Concepts Silberschatz and Galvin 1999 4.51

Readers-Writers Problem

• Example: file operations

• Shared datavar mutex, wrt: semaphore (=1); //common

readcount : integer (=0); //readers

• Writer processwait(wrt); //until no one using

…writing is performed

…signal(wrt); //make avail again

Operating System Concepts Silberschatz and Galvin 1999 4.52

Readers-Writers Problem (Cont.)

• Reader processwait(mutex);

readcount := readcount +1;if readcount = 1 then wait(wrt);

signal(mutex); /* signal readers */ …

reading is performed …

wait(mutex);readcount := readcount – 1;if readcount = 0 then signal(wrt);

signal(mutex):

Operating System Concepts Silberschatz and Galvin 1999 4.53

Dining-Philosophers Problem

• Shared data

var chopstick: array [0..4] of semaphore;(=1 initially)

Operating System Concepts Silberschatz and Galvin 1999 4.54

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;

Operating System Concepts Silberschatz and Galvin 1999 4.55

Module 7: Deadlocks

• System Model

• Deadlock Characterization

• Methods for Handling Deadlocks

• Deadlock Prevention

• Deadlock Avoidance

• Deadlock Detection

• Recovery from Deadlock

• Combined Approach to Deadlock Handling

Operating System Concepts Silberschatz and Galvin 1999 4.56

The Deadlock Problem

• A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set.

• Example

– System has 2 tape drives.

– P1 and P2 each hold one tape drive and each needs another one.

• Resources could be of different types, too.

Operating System Concepts Silberschatz and Galvin 1999 4.57

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.

Operating System Concepts Silberschatz and Galvin 1999 4.58

Deadlock Characterization

• Mutual exclusion: only one process at a time can use a resource.

• Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes.

• No preemption: a resource can be released only voluntarily by the process holding it, after that process has completed its task.

• Circular wait: P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by P2, …, Pn–1 is waiting for a resource that is held by Pn, and Pn is waiting for a resource that is held by P0.

• All four must hold

Deadlock can arise if four conditions hold simultaneously.

Operating System Concepts Silberschatz and Galvin 1999 4.59

Methods for Handling Deadlocks

• Don’t allow it: Ensure that the system will never enter a deadlock state.

– Prevention

– Avoidance

• Detection: Allow the system to enter a deadlock state and then recover.

• Ignore the problem; used by most operating systems, including UNIX.

Operating System Concepts Silberschatz and Galvin 1999 4.60

Deadlock Prevention

• Make sure one of four conditions does not hold

• Mutual Exclusion – not required for sharable resources; must hold for nonsharable resources. (unlikely to be enforceable)

• Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources.

– Require process to request and be allocated all its resources before it begins execution, or allow process to request resources only when the process has none.

– Low resource utilization; starvation possible.

– Not too useful in general; maybe OK for some batch jobs

Restrain the ways request can be made.

Operating System Concepts Silberschatz and Galvin 1999 4.61

Deadlock Prevention (Cont.)

• No Preemption –

– If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released.

– Preempted resources are added to the list of resources for which the process is waiting.

– Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting.

– OK for some resources such as CPU or memory

• Circular Wait – impose a total numeric ordering of all resource types, and require that each process requests resources in an increasing order of enumeration.

– Will work in general case (can be proven mathematically)

Operating System Concepts Silberschatz and Galvin 1999 4.62

Deadlock Avoidance

• Simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need.

• The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that there can never be a circular-wait condition.

• Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes.

Requires that the system has some additional a priori information available.

Operating System Concepts Silberschatz and Galvin 1999 4.63

Avoidance via Safe States

• If a system is in safe state no deadlocks.

• If a system is in unsafe state possibility of deadlock.

• Avoidance ensure that a system will never enter an unsafe state.

Operating System Concepts Silberschatz and Galvin 1999 4.64

Deadlock Detection

• Allow system to enter deadlock state

• Detection algorithm

– Maintain wait-for structure

– Periodically scan for circular wait-fors

– Take into account #rollbacks, how likely to occur, etc.

• Recovery scheme

Operating System Concepts Silberschatz and Galvin 1999 4.65

Recovery from Deadlock

• Inform the operator.

• Abort all deadlocked processes.

• Abort one process at a time until the deadlock cycle is eliminated.

• In which order should we choose to abort?– Priority of the process.– How long process has computed, and how much longer to

completion.– Resources the process has used.– Resources process needs to complete.– How many processes will need to be terminated. – Is process interactive or batch?

Operating System Concepts Silberschatz and Galvin 1999 4.66

Recovery from Deadlock: Resource Preemption

• Selecting a victim – minimize cost.

• Rollback – return to some safe state, restart process from that state.

• Starvation – same process may always be picked as victim, include number of rollback in cost factor.

Operating System Concepts Silberschatz and Galvin 1999 4.67

Combined Approach to Deadlock Handling

• Combine the three basic approaches– prevention– avoidance– detection

allowing the use of the optimal approach for each of resources in the system.

• Group resources into classes.– Memory– Swap space– CPU– Etc.

• Use most appropriate technique for handling deadlocks within each class.

Operating System Concepts Silberschatz and Galvin 1999 4.68

Workshop 2: Resource Allocation

• Select resource

– Tapes

– Memory

– Disk files (updates)

– Swap file

– CD-ROM

– Internet connection

– Etc.

Give brief “blackboard presentation)

- Allocate resource

- Prevent or avoid deadlocks or recover

- 5 minutes or so

1 presenter OK

Group Activity

Operating System Concepts Silberschatz and Galvin 1999 4.69

Next Week: Agenda

• Homework Review of chapters 8 & 9

• Study Group Reports

• Logical & physical memory concepts

• Group activity on memory management

• Lecture and discussion on virtual memory

• Group activity on memory management algorithms

• Summary & preview

Operating System Concepts Silberschatz and Galvin 1999 4.70

Next Week – Workshop 3

Study Group ProjectStudy Group Project

How does O/S manage main & secondary backing memory

AlgorithmData structure

3 - 5 page paper

Presentation 5 - 10 minutes1 person can represent group