gary burt2/1/2016 1 processes cmsc-421 operating systems principles chapter 2

141
Gary Burt 05/26/22 Processes CMSC-421 Operating Systems Principles Chapter 2

Upload: jordan-sherman

Post on 17-Jan-2018

224 views

Category:

Documents


0 download

DESCRIPTION

Gary Burt2/1/ Multiprogramming of Four Programs. A B C D One program counter Process Switch

TRANSCRIPT

Page 1: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 1

ProcessesCMSC-421

Operating Systems PrinciplesChapter 2

Page 2: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 2

The Process Model All the runnable software on the

computer, including the operating system is organized into a number of sequential processes.

A process is just an executing program, including the current values of the program counter, registers, and variables.

Page 3: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 3

Multiprogramming of Four Programs.

A

B

C

D

One programcounter

ProcessSwitch

Page 4: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 4

Multiprogramming of Four Programs. (II)

Four program counters

A B C D

Page 5: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 5

Multiprogramming of Four Programs. (III)

Time

A

B

C

D

Page 6: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 6

Problem With the CPU switching back and forth

among an unknown number of processes, the rate at which a process performs its computation will not be uniform, and probably not even reproducible.

This causes a problem for real-time processes!

Page 7: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 7

Process Hierarchies Operating systems that support the

process concept must provide some way to create all the processes needed.

Init starts all of the necessary processes when the computer is booted.

All subsequent processes can created new processes.

Page 8: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 8

Process States Possible states:

» Running– Actually using the CPU at that instant.

» Ready– Runnable; temporarily stopped to let another

process run.» Blocked

– Unable to run until some external event happens.

Page 9: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 9

Transition State Diagram

Running

Blocked Ready

12

3

4

1. Process blocksfor input2. Scheduler picksanother process3. Scheduler picks this process4. Input becomes available

Page 10: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 10

Alternative View

0 1 ... n n - 1

scheduler

Page 11: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 11

Scheduler According to this view, the scheduler

not does process scheduling, but also interrupt handling and all the interprocess communication.

Page 12: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 12

Implementation of Processes

OS maintains a process table with one entry per process.

Contains process state, pc, stack pointer, memory allocation, status of open files, accounting and scheduling information.

Page 13: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 13

Sample FieldsProcess mgmt Memory mgmt file mgmtRegisters Pointer to text seg. UMASKPC Pointer to data seg. Root directoryPSW Pointer to bss seg. Working directoryStack pointer Exit status File descriptorsProcess state Signal status Effective UIDTime started PID Effective GIDChildren CPU time PPID System call ParamTime next alarm Real UID Various flag bitsMessage queue Effective UIDPending signal bits Real GIDPID Bit maps for signalsVarious flag bits Various flag bits

Page 14: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 14

Threads In a traditional process, there is a single

thread of control and a single pc in each process.

In some modern OS, support is provided for multiple threads of control within a process.

Page 15: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 15

Heavyweight Threads

ProgramCounter

Thread Process

Computer

Page 16: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 16

Lightweight ThreadsComputer

Page 17: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 17

Threads II An example of when to use threads, is

for a server. The server is one single process, but each connection is a separate thread. This allows critical data to be shared in the process global memory and available without special handling. When one connection is not sending data, only that thread is blocked.

Page 18: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 18

Threads III Another example of using threads is in

Netscape. One process can have a section of code to manage the internet connections, since there is one logical connection for each image on the screen.

There is one standard thread package, POSIX P-threads.

Page 19: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 19

Design Issues The two alternatives seem equivalent. Difference is in performance. Switching

threads is much faster when thread management is done in user space than when a kernel called needed.

When one thread blocks for I/O, all threads are blocked by the kernel.

Page 20: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 20

Design Issues (II) When the parent (with threads) forks,

does the child have threads? Problems with one thread closing a file

that another thread needs! How is memory allocation coordinated? How is error reporting handled?

Page 21: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 21

Design Issues (III) How are signals handled, are they

thread specific? How are interrupts handled, are they

thread specific? Which thread gets keyboard input? How is stack management coordinated?

Page 22: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 22

Good News, Bad News

These problems are not insurmountable, but they do show that just introducing threads into an existing system is not going to work at all.

There is also more work currently for the applications programmer!

Page 23: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 23

InterProcess Communications

Processes frequently need to communicate with other processes.

One example is the shell pipeline. There is a need for communications

between processes, preferably in a well-structured way, not using interrupts.

Page 24: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 24

Three Issues How one process can pass information

to another. Making sure two or more processes do

not get into each other's way when engaging in critical activities.

Proper sequencing when dependencies are present.

Page 25: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 25

Race Conditions One way to sure is with a common

resource (main memory, shared file) One example is the print spooler

» one process enters the file name into a special spooler directory.

» another process *printer daemon" periodically checks to see if there are any files to print.

Page 26: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 26

Race Conditions (II) directory is capable of a large number of

slots. Two shared variables:

» out which points to the next file to be printed

» in which is the next free slot in directory.

Page 27: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 27

Race Conditions Example

Slots 1-3 are empty Slots 4-6 are full Simultaneously, processes A and B

decide they want to queue a file for printing.

Both processes thing the next slot is 7.

Page 28: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 28

Race Conditions Example (II)

4

5

67

8

out = 4

in = 7 A

B

Page 29: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 29

Critical Sections The key to preventing trouble here and

in other situations involving shared resources is to find some way to prohibit more than one process from reading and writing the shared data at the same time

Page 30: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 30

Mutual Exclusion Making sure that if one process is using

a shared resource, the other processes will be excluded from doing the same thing.

Choice of primitive operation is a major design issue in any operating system.

Page 31: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 31

Solution Part of the time, a process is busy doing

internal computations and other things that do not lead to race conditions.

The part of the program where the shared memory is accessed in called the critical region or critical section.

No two processes can be in their critical region at the same time.

Page 32: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 32

Four Conditions No two processes may be simultaneously

inside their critical regions. (Mutual exclusion)

No assumptions may be made about speeds or the number of CPUs (Timing)

No process running outside its critical region may block other processes. (Progress)

No process should have to wait forever to enter its critical region. (Bounded waiting)

Page 33: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 33

Mutual Exclusion with Busy Waiting

Disabling Interrupts after entering critical region and re-enable them just before leaving» Simplest solution.» No clock interrupts can occur.» Very vulnerable » Does not work if multiple processors are

involved

Page 34: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 34

Lock Variables Variable initially set to 0 First process to enter critical section

changes the lock variable to 1. Any other process checks the lock

variable to see if it is 0. If it is not, it must wait until the lock becomes 0 again.

Page 35: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 35

Lock Variables Faults

This is like the spooler problem. If two processes read the variable when the variable is set to zero, both will enter the critical region.

Violates Mutual Exclusion!

Page 36: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 36

Strict AlternationWhile (TRUE) { while ( turn != 0) ; critical_region(); turn = 1; noncritical_region();}

Process A

While (TRUE) { while ( turn != 1) ; critical_region(); turn = 0; noncritical_region();}

Process B

Page 37: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 37

Strict Alternation Faults

Continually testing a variable for some state is called busy waiting. Should be avoided, since it wastes CPU time.

Can end up blocking and violating Progress.

Page 38: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 38

Peterson's Solution#define FALSE 0#define TRUE 1#define N 2int turn;int interested[N];

Page 39: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 39

Peterson's Solution (II)

void enter_region( int process ){ int other; other = 1 - process; interested[process] = TRUE; turn = process; while ( turn == process && interested[ other ] == TRUE )

;}

Page 40: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 40

Peterson's Solution (III)

void leave_region( int process ){ interested[process ] = FALSE;}

Page 41: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 41

TSL Instruction Hardware instruction that is especially

important for computers with multiple processors is TSLTest and Set Lock

Solves problems with Strict Alternation, because there is no chance of two processes reading the variable and getting the same value.

Page 42: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 42

TSLenter_region:

tsl register, lockcmp register, #0jne enter_regionret

leave_region:move lock, #0ret

Page 43: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 43

Peterson's Solution & TSL Instruction

Fault Both are correct. But require busy waiting.

Page 44: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 44

Priority Inversion Problem

Two processes: » H, with high priority, to run whenever it is in

a ready state.» L, with low priority

If L is in critical region and H becomes ready, L never gets scheduled so it can leave it critical region, so H can run.

Page 45: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 45

SLEEP & WAKEUP SLEEP is a system call that causes the

caller to be blocked until something wakes it up.

WAKEUP has one parameter, the process to wakeup.

Page 46: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 46

Producer-Consumer Problem

Also known as the bounded buffer problem.

One process puts data into a buffer. Another takes it out.

Page 47: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 47

Buffer Problems Producer wants to put a new item into

full buffer.» Producer goes to sleep until consumer

empties queue. Consumer wants to to remove an item

when buffer is empty» Consumer goes to sleep until producers

adds to queue.

Page 48: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 48

Fatal Race Condition#define N 100int count = 0;

void producer( void ){ while ( TRUE ) { produce_item(); if ( count == N ) sleep ( ); enter_item( ); count = count + 1; if (count == 1 ) wakeup(consumer ); }}

Page 49: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 49

Fatal Race Condition (II)

void consumer( void ){ while ( TRUE ) { if ( count == 0 ) sleep ( ); remove_item( ); count = count - 1; if ( count == N - 1 ) wakeup ( producer ); consume_item( ); }}

Page 50: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 50

Fatal Race Condition (III)

Because access to count is unconstrained, problems occur.

Consumer sees count is zero, but gets suspended.

Producer adds, gives wakeup call. Consumer misses call, gets resumed and

goes to sleep. Producer goes to sleep.

Page 51: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 51

Semaphores

1965, E.W. Dijkstra suggested using an integer variable to count the number of wakeups saved for future use.

This was called a "semaphore". Proposed having two operations, DOWN

and UP. DOWN checks to see if the value is

greater than 0, and continues if it is.

Page 52: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 52

Semaphores (II)

Checking the value, changing it, and going to sleep is done as a single, indivisible atomic action.

The UP operation increments the value of the semaphore. If there are processes sleeping, then one is given a WAKEUP.

Page 53: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 53

Producer-Consumer Problem Revisited

#define N 100typedef int semaphore;semaphore mutex =1;semaphore empty = N;semaphore full = 0;

Page 54: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 54

Producer-Consumer Problem Revisited

(II)void producer( void ){ int item; while( TRUE ) { produce_item( &item ); down( &empty ); down( &mutex ); enter_item( item ); up( &mutex ); up( &full )}

Page 55: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 55

Producer-Consumer Problem Revisited

(III)void consumer( void ){ int item; while ( TRUE ) { down( &full ); down( &mutex ); remove_item( &item ); up( &mutex); up( &empty ) }}

Page 56: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 56

Monitors

The order of the DOWN operations is the Producer-Consumer problem can lead to a deadlock.

A monitor is a collection of procedures, variables, and data structures that are grouped together in a special kind of module or package.

Page 57: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 57

Monitors (II) Monitors have a property that only one

process can be active in a monitor at a time. Compilers can handle calls to the monitor

procedures in a different manner. Calling processes will be suspended until the other process has left the monitor.

Compiler implements the mutual exclusion on the monitor entries.

Page 58: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 58

Monitors (III)

Blocking is controlled with condition variables. If a monitor procedure discovers it can not continue, it WAITs on a condition variable.

Waking up a waiting monitor procedure is done with a SIGNAL on the condition variable.

Condition variables are not counters.

Page 59: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 59

Monitors (IV)

The advantage of the monitor with WAIT and SIGNAL over SLEEP and WAKEUP is that the monitor allows only one active process. This avoids the fatal race conditions.

By making mutual exclusion automatic, monitors make parallel program much less error-prone.

Page 60: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 60

Down Side

Monitors are not supported in C, Pascal, and most other languages.

Semaphores are not supported either, but can be added as library calls.

Monitors and semaphores are designed to systems that have common memory.

Page 61: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 61

Message Passing

Message passing uses to primitives SEND and RECEIVE. These are not language constructs but can be put into library procedures.

Message passing system have many challenging problems and design issues that are not present for semaphores and monitors.

Page 62: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 62

Message Passing (II)

Problems» Messages can be lost on the network.

(Use acknowledgement messages)» Messages can arrive out of order (Use

consequence number.)» Have to deal with issues of how processes

are named.» How can imposters be kept out?

Page 63: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 63

Message Passing (III)

Problems (continued)» performance is slower because of copying

messages between processes.

Page 64: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 64

Examples

IPC between processes in MINIX and UNIX is done via pipes. These are effectively mailboxes (memory buffers).

However, pipes do not maintain message boundaries.

Page 65: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 65

Classical IPC Problems

The Dining Philosophers Problem The Readers and Writers Problem The Sleeping Barber Problem

Page 66: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 66

The Dining Philosophers

Problem Five philosophers are seated around a

circular table. Each philosopher has a plate of spaghetti. The spaghetti is so slippery that a philosopher needs two forks to eat it. Between each par of plates is one fork.

The life of a philosopher consists of eating and thinking.

Page 67: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 67

The Dining Philosophers Problem

(II) When a philosopher gets hungry, he/she tries

to acquire the fork on each side of the plate. If successful, the philosopher eats for a while

and then puts down the fork and continues to think.

If all philosophers simultaneously take their left fork, they couldn't get the right fork -- starvation.

Page 68: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 68

Readers and Writers Problem

It is acceptable to have multiple processes reading a database at the same time.

But if one process is updating (writing) the database, no other processes may have access to the database.

The problem occurs if there are multiple readers and the writer wants exclusive use.

Page 69: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 69

Sleeping Barber Problem

The barber shop has one barber, one barber chair, and some number of chairs for waiting customers.

If there are no customers, the barber goes to sleep.

When a customer arrives, the customer has to wake up the barber.

Page 70: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 70

Sleeping Barber Problem (II)

If additional customers arrive, they have to sit in one of the empty chairs, or leave the shop.

The problem is to program the barber and the customers without getting into race conditions.

Page 71: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 71

Sleeping Barber Problem Code

#define CHAIRS 5typedef int semaphore;semaphore barbers = 0;semaphore customers = 0;semaphore mutex = 1;int waiting = 0;

Page 72: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 72

Sleeping Barber Problem Code (II)

void barber( void ){ while ( TRUE ) {

down ( customers );down ( mutex );waiting = waiting - 1;up ( barbers );up ( mutex );cut_hair( );

}}

Page 73: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 73

Sleeping Barber Problem Code (III)

void customers( void ){ down( mutex ); if ( waiting < CHAIRS ) {

waiting = waiting + 1;up ( customers );up ( mutex );down ( barbers );get_haircut( )

} elseup ( mutex )

}

Page 74: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 74

Process Scheduling

When two or more processes are runnable, the operating system must decide which on to run first.

The part of the operating system that makes this decision is called the scheduler.

The algorithm used is called the scheduling algorithm.

Page 75: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 75

Scheduling Criteria Fairness -- make sure each process gets its fair

share of the CPU. Efficiency -- keep the CPU busy 100 percent of the

time. Response time -- minimize response time for

interactive users. Turnaround -- minimize the time batch users must

wait for output. Throughput -- maximize the number of jobs

processed per hour.

Page 76: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 76

Scheduling Criteria (II)

The criteria are contradictory.» interactive» batch

Any scheduling algorithm that favors some class of processes hurts another class.

Every process is unique and unpredictable.

Page 77: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 77

Clocks To make sure that no process runs too long,

nearly all computers have an electronic clock built in, which causes an interrupt periodically.

A frequency of 50 or 60 times a second ( called 50 or 60 Hz [Hertz]) is common.

At each interrupt, the OS gets to decide whether the currently running process should be allowed to continue.

Page 78: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 78

Preemptive Scheduling

The strategy of allowing processes that are logically runnable to be temporarily suspended is called preemptive scheduling.

Run to completion (non-preemptive) was the method for early batch modes.

Processes can be suspended at an arbitrary instant, without warning.

Can lead to race conditions.

Page 79: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 79

Round Robin

A

B

C

D

E

Page 80: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 80

Round Robin (II)

This is one of the oldest, simplest, fairest, and most widely used algorithms.

Each process is assigned a time interval called a quantum.

If the process is still running at the end of its quantum, the CPU is preempted and given to another process.

If a process is finished or blocked before the quantum is up, CPU switching is done.

Page 81: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 81

Round Robin (III) The length of the quantum is an issue. If the context switch takes 5 msec, and

the quantum is 20 msecs, 20% of the CPU is wasted on administrative overhead.

If the quantum is 500 msecs, the only 1% is wasted, but if 10 interactive users hit the Enter key at the same time, one user will have a 5 second delay.

Page 82: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 82

Priority Scheduling Not all processes are equally important. Some users should have a higher priority. Some processes should have a higher

priority.» Processes that are extremely I/O bound might

be higher. Processes can have static and

dynamically assigned priorities.

Page 83: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 83

Priority Scheduling (II)

High priority processes can run indefinitely.» One solution is lower the priority after each

quantum.» Another solution is to assign a maximum

quantum. Can combine priority and round-robin.

Page 84: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 84

Priority Scheduling (III)

Priority 4

Highest

Priority 3

Priority 2

Priority 1

Runnable Processes

Low priorities may all starve to death!

Page 85: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 85

Multiple Queues

High priority classes have smaller quantums.

Processes that use up their quantums are moved down to the next priority class, but get twice as large a quantum.

Quantums: 1, 2, 4, 8, 16, 32, 64. A process needing 100 quantums only

gets swapped 7 times.

Page 86: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 86

Shortest Job First

(Previous algorithms were designed for interactive systems.)

A B C D

8 4 4 4

AB C D

8444

Page 87: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 87

Guaranteed Scheduling

If there are n users on the system, you will bet 1/n of the CPU power

Actual time vs. entitled time should be 1.0.

The process with the lowest ratio, gets run.

Page 88: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 88

Lottery Scheduling Give processes lottery tickets for resources. Hold a random drawing and the winner gets

20msec of CPU time as a price. Important processes are given more tickets. If there are 100 tickets and one process has

20 tickets, it will run 20 percent of the time.

Page 89: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 89

Two-level Scheduling

Previous algorithms assumed that all processes were in memory.

Two-level schedules in-memory and swapped to disk as two different scheduling problems.

Schedule those in memory only. Schedule those on disk only.

Page 90: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 90

Two-level Scheduling (II)

Criteria of higher-level schedule could be:» How longs has it been since the process was

swapped in or out?» How much CPU time has the process had

recently?» How big is the process? (Small ones do not

get in the way.)» How high is the priority of the process?

Page 91: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 91

Policy versus Mechanism

What if different processes do not belong to different users?

DBMS can have children processes to handle sub-functions.

Separate scheduling mechanism from the scheduling policy.

Allow the kernel to modify the priority of children processes.

Page 92: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 92

Real-Time Scheduling

When the computer must react appropriately to external stimuli within a fixed amount of time.

types:» hard real time: Must meet absolute

deadlines.» soft real time: Missing an occasional

deadline is acceptable.

Page 93: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 93

Real-Time Scheduling(II)

Events can be:» periodic» aperiodic

A system may have to respond to multiple periodic event streams.

Real-time systems can meet all of the periodic events is said to be schedulable.

Scheduled dynamically or statically.

Page 94: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 94

Real-Time Scheduler Algorithms

rate monotonic algorithm -- assigns to each process a priority based on the frequency of occurrence.

earliest deadline first -- run the process which must be done the soonest.

least laxity: Run those on a critical path instead of others.

Page 95: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 95

Internal Structure of MINIX

Structured in four layers» Process management» I/O tasks» Server processes» User processes

Page 96: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 96

Internal Structure of MINIX (II)

User Processes

Server Processes

I/O tasks

Process Management

DiskTask

TTYTask

ClockTask

SystemTask

EthernetTask ...

MemMgr

FileSystem

Networkserver ...

Init UserProcess

UserProcess

UserProcess ...

Page 97: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 97

Internal Structure of MINIX (III)

Bottom layer (Process management) catches all interrupts, traps, does scheduling, and provides higher layers with a model of independent sequential processes that communicate using messages.

Page 98: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 98

Process Management

Functions:» catching traps and interrupts» saving and restoring registers» scheduling» Providing services to the next layer.

Page 99: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 99

Process Management (II)

» Handling mechanics of messages» Checking for legal destinations» locating sending and receiving buffers in

physical memory» copying bytes from sender to receiver.

Page 100: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 100

Process Management (III)

The part of the layer dealing with the lowest level interrupt handling is written in assembly language.

Everything else is written in C.

Page 101: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 101

I/O Tasks Layer Contains the I/O processes, one per device

type. Processes are called tasks or device drivers. A task is needed for reach device type,

including disks, printers, terminals, network interfaces, and clocks, plus any others present on your system.

Also there is one system task.

Page 102: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 102

Kernel

All of the tasks in layer 2 and all of the code in layer 1 are linked together into one single binary program called the kernel.

Some of the tasks common subroutines, otherwise the are independent from one another, scheduled independently, and communicate using messages.

Page 103: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 103

Kernel (II) The true kernel and interrupt handlers are

executing, they are accorded more privileges than tasks.

The true kernel can access any part of memory and any processor register, execute any instruction, using all parts of memory.

Tasks can not execute all instructions nor access all memory and registers.

Page 104: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 104

System Task

Does no I/O in the normal sense. Exists in order to provide services, such

as copying between different memory regions, for processes which are not allowed to do such things themselves.

Page 105: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 105

Server Processes

Layer 3 contains processes that provide useful services to the user processes.

These server processes run a a less-privileged level then the kernel and tasks and cannot access I/O ports directly.

They also can not access memory outside the segments allocated to them.

Page 106: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 106

Server Processes (II)

The memory manager (MM) carries out all the MINIX system calls that involve memory management such as FORK, EXEC, and BRK.

the file system carries out all the file system calls, such as READ, MOUNT, and CHDIR.

Page 107: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 107

User Processes

All the user processes are in Layer 4. Includes shells, editors, compilers, and

user-written a.out programs. Includes daemons -- processes that

are started when the system is booted and run forever.

Page 108: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 108

Process Management in

MINIX MINIX follows the general process

model previously described. All processes are a part of the tree

started at init.

Page 109: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 109

Boot Process

At power on, the hardware reads the first sector of the first track of the book disk into memory and executes the code it finds there.

On a diskette, it is a bootstrap program which then loads the boot program.

On a hard disk, the first sector contains a small program and the disk's partition table. Combined are the Master Boot Record.

Page 110: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 110

Boot Process (II) boot loads the kernel, the memory

manager, the file system, and init, the first user process.

The bottom three layers are all initialized. init is started. init reads the file /etc/ttytab and forks off a

new process for each terminal that can be used as a login device.

Page 111: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 111

Boot Process (III)

Normally, each process is /usr/bin/getty, which prints a message and waits for a name to be typed.

Then /usr/bin/login is called with the name as its argument.

After successful login, login executes the shell that is specified in the /etc/passwd file.

Page 112: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 112

Boot Process (IV)

The shell waits for commands to be typed and then forks off a new process for each command.

This is how the process tree grows for all processes.

Page 113: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 113

Creating Processes The two principle MINIX system calls for

process management are FORK and EXEC. FORK is the only way to create a new

process. EXEC (and variants) allows a process to

execute a specific program. When a new program is executed, it is

allocated a portion of memory.

Page 114: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 114

IPCs in MINIX

Three primitives for sending and receiving messages are provided:» send( dest, &message );» receive( source, &message );» send_rec( src_dst, &message );

When a message is sent to process that is not currently waiting for a message, the sender blocks. -- rendezvous.

Page 115: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 115

Process Scheduling The interrupt system is the heart. When processes block waiting for input,

other processes can run. When input becomes available, the current

running process is interrupted by the device. Clock also generates interrupts. The lowest layer hides these interrupts by

turning them into messages.

Page 116: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 116

Process Scheduling (II)

The message gets sent to some process. At each interrupt (and process termination)

the OS redetermines which process to run. MINIX uses a multi-level queuing system with

three levels, corresponding to Layers 2, 3, and 4.

Within task and server levels, processes run until they block.

Page 117: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 117

Process Scheduling (III)

User processes are scheduled using round-robin.

Tasks have the highest priority. Memory manager and File system are

next. User processes are last.

Page 118: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 118

Process Scheduling (IV)

When picking a process to run, the scheduler checks to see if any tasks are ready to run. If one or more are ready, the one at the head of the queue is run. Otherwise it goes down the priority queue.

Page 119: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 119

Process Scheduling (V)

At each clock tick, a check is made to see if the current process is a user process that has run more than 100 msec.

If it is and there is another runnable process, the current process is moved to the end of the scheduling queue, and the process at the head of the queue is run.

Page 120: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 120

Process Scheduling (VI)

Tasks, the memory manager, and the file system are never preempted by the clock, no matter how long they have been running.

Page 121: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 121

Resource

CGI interface to browse the entire LINUX kernel source:

http://sunsite.unc.edu/linux-source

Page 122: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 122

Organization of source code

/usr/include/» sys/ POSIX headers» minix/ headers for the operating system» ibm/ IBM PC specific definitions

/usr/src/» kernel/ layers 1 and 2 (processes, messages,

drivers» mm/ memory manager» fs code for the file system

Page 123: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 123

Others» src/lib library procedures (read,

open)» src/tools init program» src/boot booting and installing MINIX» src/commands utility programs» src/inet network support

Page 124: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 124

Conventions Normally all related code is in one

directory. Some files have the same name (but

are in different directories). const.h

Page 125: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 125

Memory Layout

Interrupt Vectors

Mem availablefor user progs Ethernet taskPrinter taskTerminal taskMemory taskClock taskDisk Task

Kernel

Unused

01K

2K

129K, depending

640K

Page 126: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 126

Memory Layout (II)

ReadOnly & I/O

MemoryAvailable foruserPrograms

Inet task

File System

Memory Mgmr

640K1024K

1077K

Depending

2372K

Init 2383K

Limit of memory

Page 127: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 127

Some Code Items #ifndef _ANSI_H

#endif types defined in typedefs use “_t” as suffix. Type 16-bit 32-bit

gid_t8 8dev_t 16 16pid_t16 32ino_t16 32

Page 128: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 128

Some Code Items Six message types (see page 110). #if (CHIP == INTEL)

#endif Bootstrapping MINIX

» Look at figure 2-31, page 120

Page 129: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 129

Interrupt Processing Hardware

System data

bus

INT

CPU

INTA

INT

MasterinterruptController

ACK INT

SlaveinterruptController

ACK

IRQ 0IRQ 1..IRQ 7

IRQ 8IRQ 9..IRQ 15

Page 130: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 130

Interrupt Handling Details vary from system to system. The task-switching mechanism of a 32-bit

Intel processor is complex. Much support is built into the hardware. Only a tiny part of the MINIX kernel

actually sees the interrupt. This code is in mpx386.s and there is an entry point for each interrupt.

Page 131: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 131

Interrupt Handling (II)

In addition to hardware and software interrupts, various error conditions can cause the initiation of an exception.

Exceptions are not always bad.» They can be used to stimulate the operating

system to provide a service: provide more memory,etc

» Swap in swapped out pages Don’t ignore!

Page 132: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 132

Interprocess Communications

It is the kernel's job to translate either a hardware or software interrupt into a message.

MINIX uses the rendezvous principle.» One process does a send and waits.» The other process does a receive.» Both processes are then marked as

runnable.

Page 133: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 133

Queues

2: USER_Q

1: SERVER_Q

0: TASK_Q

2: USER_Q

1: SERVER_Q

0: TASK_Q

3 5 4

FS MM

Clock

Page 134: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 134

Hardware-Dependent Code

There are several C functions that are very dependent upon the hardware.

To facilitate porting MINIX to other systems, these functions are segregated in the files exception.c, i8259.c, protect.c.

Page 135: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 135

Utilities and the Kernel Library

_Monitor makes it possible to return to the boot monitor.

_check_mem is used at startup time to determine the size of a block of memory.

_cp_mess is used to faster copying of messages.

Env_parse is used at startup time.

Page 136: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 136

Summary To hide the effects of interrupts,

operating systems provide a conceptual model consisting of sequential processes, running in parallel.

Processes can communicate with each other using interprocess communication primitives.

Page 137: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 137

Summary (II) Primitives are used to ensure that no

two processes are ever in their critical sections at the same time.

A process can be running, runnable, or blocked and can change when it or another process executes one of the interprocess communications primitives.

Page 138: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 138

Summary (III) Many scheduling algorithms are known,

including round-robin, priority scheduling, multi-level queues, and policy-driving schedulers.

Messages are not buffered, so a SEND succeeds only when the receiver is waiting for it. Similarly, a RECEIVE only succeeds when a message is available.

Page 139: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 139

Summary (IV) If either a SEND or RECEIVE does not

succeed, the caller is blocked. When a interrupt occurs, the lowest level

of the kernel creates and sends a message to the task associated with the interrupting device.

Task switching can also occur when the kernel itself is running.

Page 140: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 140

Summary (V) When all interrupts have been serviced ,

a process is restarted. MINIX scheduling algorithm used three

priority queues:» The highest one for tasks,» Next for the file system, memory manager,

and other servers,» The lowest for user processes

Page 141: Gary Burt2/1/2016 1 Processes CMSC-421 Operating Systems Principles Chapter 2

Gary Burt 05/03/23 141

Summary (VI) User processes are run round robin for

one quantum at a time. All others run until they block or are

preempted