Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among...

Post on 01-Jan-2016

274 views 2 download

Tags:

Transcript of Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among...

Schedule 2: Concurrent Serializable Schedule

Timestamp Timestamp-based Protocols

Select order among transactions in advance –timestamp-ordering

Transaction Ti associated with timestamp TS(Ti) before Tistarts

TS(Ti) < TS(Tj) if Ti entered system before Tj

TS can be generated from system clock or as logical counter incremented at each entry of transaction

Timestamp Timestamp-based Protocols

Timestamps determine serializability order

If TS(Ti) < TS(Tj), system must ensure produced schedule equivalent to serial schedule where Ti appears before Tj

Timestamp Timestamp-based Protocols

Data item Q gets two timestamps W-timestamp(Q) –largest timestamp of

any transaction that executed write(Q) successfully

R-timestamp(Q) –largest timestamp of successful read(Q)

Updated whenever read(Q) or write(Q) executed

Timestamp Timestamp-based ProtocolsSuppose Ti executes read(Q)

If TS(Ti) < W-timestamp(Q), Ti needs to read value of Q that was already overwrittenRead operation rejected and Ti rolled back

If TS(Ti) ≥W-timestamp(Q)Read executed, R-timestamp(Q) set to max(R-timestamp(Q), TS(Ti))

Timestamp Timestamp-based ProtocolsSuppose Ti executes write(Q)

If TS(Ti) < R-timestamp(Q), value Q produced by Ti was needed previously and Ti assumed it would never be produced

Write operation rejected, Ti rolled back

If TS(Ti) < W-tiimestamp(Q), Ti attempting to write obsolete value of Q

Write operation rejected and Ti rolled back

Otherwise, write executed

Timestamp Timestamp-based Protocols

Any rolled back transaction Ti is assigned new timestamp and restarted

Algorithm ensures conflict serializability and freedom from deadlock

Exam 1 Review

Bernard Chen Spring 2007

Chapter 1 Introduction Chapter 3 Processes

The Process

Process: a program in execution Text section: program code program counter (PC) Stack: to save temporary data Data section: store global variables Heap: for memory management

Stack and Queue

Stack: First in, last out Queue: First in, first out

Do: push(8) push(17) push(41) pop() push(23) push(66) pop() pop() pop()

Heap (Max Heap)

Provide O(logN) to find the max

97

53 59

26 41 58 31

16 21 36

The Process Program itself is not a process, it’s

a passive entity

A program becomes a process when an executable (.exe) file is loaded into memory.

Process: a program in execution

Schedulers

Short-term scheduler is invoked very frequently (milliseconds) ⇒(must be fast)

Long-term scheduler is invoked very infrequently (seconds, minutes) ⇒(may be slow)

The long-term scheduler controls the degree of multiprogramming

Schedulers

Processes can be described as either:

I/O-bound process–spends more time doing I/O than computations, many short CPU bursts

CPU-bound process–spends more time doing computations; few very long CPU bursts

Schedulers

On some systems, the long-term scheduler maybe absent or minimal

Just simply put every new process in memory for short-term scheduler

The stability depends on physical limitation or self-adjustment nature of human users

Schedulers

Sometimes it can be advantage to remove process from memory and thus decrease the degree of multiprogrammimg

This scheme is called swapping

Addition of Medium Term Scheduling

Interprocess Cpmmunication (IPC)

Two fundamental models (1) Share Memory(2) Message Passing

Share Memory Parallelization System Example

m_set_procs(number): prepare number of child for execution

m_fork(function): childes execute “function”

m_kill_procs(); terminate childs

Real Examplemain(argc , argv){int nprocs=9;m_set_procs(nprocs); /* prepare to launch this many processes */m_fork(slaveproc); /* fork out processes */m_kill_procs(); /* kill activated processes */}

void slaveproc(){ int id; id = m_get_myid();

m_lock(); printf(" Hello world from process %d\n",id); printf(" 2nd line: Hello world from process %d\n",id);

m_unlock();}

Real Exampleint array_size=1000int global_array[array_size]

main(argc , argv){int nprocs=4;m_set_procs(nprocs); /* prepare to launch this many processes */m_fork(sum); /* fork out processes */m_kill_procs(); /* kill activated processes */}

void sum(){ int id; id = m_get_myid();

for (i=id*(array_size/nprocs); i<(id+1)*(array_size/nprocs); i++)global_array[id*array_size/nprocs]+=global_array[i];

}

Shared-Memory Systems

Unbounded Buffer: the consumer may have to wait for new items, but producer can always produce new items.

Bounded Buffer: the consumer have to wait if buffer is empty, the producer have to wait if buffer is full

Bounded Buffer#define BUFFER_SIZE 6

Typedefstruct{. . .} item;

item buffer[BUFFER_SIZE];intin = 0;intout = 0;

Bounded Buffer (producer iew)

while (true) {/* Produce an item */while (((in = (in + 1) % BUFFER SIZE count) ==

out) ; /* do nothing --no free buffers */

buffer[in] = item;in = (in + 1) % BUFFER SIZE;}

Bounded Buffer (Consumer view)

while (true) {while (in == out) ; // do nothing --nothing to consume // until remove an item from the bufferitem = buffer[out];out = (out + 1) % BUFFER SIZE;return item;}

Message-Passing Systems

A message passing facility provides at least two operations: send(message), receive(message)

Message-Passing Systems

If 2 processes want to communicate, a communication link must exist

It has the following variations:1. Direct or indirect communication2. Synchronize or asynchronize

communication3. Automatic or explicit buffering

Message-Passing Systems Direct communication send(P, message) receive(Q, message)

Properties: A link is established automatically A link is associated with exactly 2 processes Between each pair, there exists exactly one

link

Message-Passing Systems

Indirect communication: the messages are sent to and received from mailbox

send(A, message) receive(A, message)

Message-Passing Systems

Properties: A link is established only if both

members of the pair have a shared mailbox

A link is associated with more than 2 processes

Between each pair, there exists a number of links

Message-Passing Systems Synchronization: synchronous and

asynchronous

Blocking is considered synchronous Blocking send has the sender

block until the message is received Blocking receive has the receiver

block until a message is available

Message-Passing Systems

Non-blocking is considered asynchronous

Non-blocking send has the sender send the message and continue

Non-blocking receive has the receiver receive a valid message or null

MPI Program example#include "mpi.h"#include <math.h>#include <stdio.h>#include <stdlib.h>

int main (int argc, char *argv[]){ int id; /* Process rank */ int p; /* Number of processes */ int i,j; int array_size=100; int array[array_size]; /* or *array and then use malloc or vector to increase the

size */ int local_array[array_size/p]; int sum=0; MPI_Status stat; MPI_Comm_rank (MPI_COMM_WORLD, &id); MPI_Comm_size (MPI_COMM_WORLD, &p);

MPI Program example

if (id==0) { for(i=0; i<array_size; i++) array[i]=i; /* initialize array*/ for(i=1; i<p; i++) MPI_Send(&array[i*array_size/p], /* Start from*/ array_size/p, /* Message size*/ MPI_INT, /* Data type*/ i, /* Send to which process*/ MPI_COMM_WORLD);

for(i=0;i<array_size/p;i++) local_array[i]=array[i]; } else MPI_Recv(&local_array[0],array_size/p,MPI_INT,0,0,MPI_COMM_WORLD,&stat);

MPI Program examplefor(i=0;i<array_size/p;i++) sum+=local_array[i]; MPI_Reduce (&sum, &sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); if (id==0) printf("%d ",sum);

}

Chapter4 A thread is a basic unit of CPU

utilization. Traditional (single-thread) process has

only one single thread control Multithreaded process can perform

more than one task at a time example: word may have a thread for

displaying graphics, another respond for key strokes and a third for performing spelling and grammar checking

Multithreading Models Support for threads may be provided

either at the user level, for user threads, or by the kernel, for kernel threads

User threads are supported above kernel and are managed without kernel support

Kernel threads are supported and managed directly by the operating system

Multithreading Models Ultimately, there must exist a relationship

between user thread and kernel thread

User-level threads are managed by a thread library, and the kernel is unaware of them

To run in a CPU, user-level thread must be mapped to an associated kernel-level thread

Many-to-one Model

User Threads

Kernel thread

Many-to-one Model It maps many user-level threads to one

kernel thread Thread management is done by the

thread library in user space, so it is efficient

But the entire system may block makes a block system call.

Besides multiple threads are unable to run in parallel on multiprocessors

One-to-one Model

User Threads

Kernel threads

One-to-one Model It provide more concurrency than the

many-to-one model by allowing another thread to run when a thread makes a blocking system call

It allows to run in parallel on multiprocessors

The only drawback is that creating a user thread requires creating the corresponding kernel thread

Most implementation restrict the number of threads create by user

Many-to-many Model

User Threads

Kernel threads

Many-to-many Model Multiplexes many user-level

threads to a smaller or equal number of kernel threads

User can create as many threads as they want

When a block system called by a thread, the kernel can schedule another thread for execution

Chapter 5 Outline

Basic Concepts Scheduling Criteria Scheduling Algorithms

CPU Scheduler

Whenever the CPU becomes idle, the OS must select one of the processes in the ready queue to be executed

The selection process is carried out by the short-term scheduler

Dispatcher Dispatcher module gives control of the

CPU to the process selected by the short-term scheduler

It should work as fast as possible, since it is invoked during every process switch

Dispatch latency – time it takes for the dispatcher to stop one process and start another running

Scheduling CriteriaCPU utilization – keep the CPU as busy as possible (from 0% to 100%)

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 waiting in the ready queue

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

Scheduling Algorithems

First Come First Serve Scheduling Shortest Job First Scheduling Priority Scheduling Round-Robin Scheduling Multilevel Queue Scheduling Multilevel Feedback-Queue

Scheduling

5.4 Multiple-Processor Scheduling We concentrate on systems in which the

processors are identical (homogeneous)

Asymmetric multiprocessing (by one master) is simple because only one processor access the system data structures.

Symmetric multiprocessing, each processor is self-scheduling. Each processor may have their own ready queue.

Symmetric Multithreading

An alternative strategy for symmetric multithreading is to provide multiple logical processors (rather than physical)

It’s called hyperthreading technology on Intel processors

Symmetric Multithreading The idea behind it is to create multiple

logical processors on the same physical processor (sounds like two threads)

But it is not software provide the feature, but hardware

Each logical processor has its own architecture state, each logical processor is responsible for its own interrupt handling.

Symmetric Multithreading

Algorithm Evaluation

Deterministic Modeling Simulations Implementation

Deterministic Modeling

Deterministic Modeling: Process Burst Time P1 10 P2 29 P3 3 P4 7 P5 12

Simulation

Implementation

Even a simulation is of limited accuracy.

The only completely accurate way to evaluate a scheduling algorithm is to code it up, put it in the operating system and see how it works.

Bounded Buffer In chapter 3, we illustrated the model with

producer-consumer problem.

Suppose that we wanted to provide a solution to the consumer-producer problem that fills all the buffers. We can do so by having an integer count that keeps track of the number of full buffers. Initially, count is set to 0. It is incremented by the producer after it produces a new buffer and is decremented by the consumer after it consumes a buffer.

Bounded Buffer (producer view)

while (true) { /* produce an item and put in next Produced*/ while (count == BUFFER_SIZE) ; // do nothing

buffer [in] = nextProduced; in = (in + 1) % BUFFER_SIZE; count++;}

Bounded Buffer (Consumer view)

while (true) { while (count == 0) ; // do nothing

nextConsumed= buffer[out]; out = (out + 1) % BUFFER_SIZE; count--; /* consume the item in next Consumed}

Race Condition We could have this incorrect state because we

allowed both processes to manipulate the variable counter concurrently

Race Condition: several processes access and manipulate the same data concurrently and the outcome of the execution depends on the particular order in which the access takes place.

Major portion of this chapter is concerned with process synchronization and coordination

6.2 The Critical-Section Problem Consider a system with n processes,

each of them has a segment code, called critical section, in which the process may be changing common variables, updating a table, writing a file…etc.

The important feature is that allow one process execute in its critical section at a time.

6.2 The Critical-Section Problem Each process must request

permission to enter its critical section.

The section of code implementing this request is the entry section

The critical section maybe followed by an exit section

The remaining code is the remainder section

6.2 The Critical-Section ProblemA solution to the critical-section problem must satisfy the

following three requirements:

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

6.3 Peterson’s Solution

It is restricted to 2 processes that alternate execution between their critical section and remainder section

The two processes share two variables: Int turn; Boolean flag[2]

6.3 Peterson’s SolutionThe two processes share two variables: Int turn; 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!

Algorithm for Process Pi

while (true) { flag[i] = TRUE; turn = j; while ( flag[j] && turn == j) ; CRITICAL SECTION flag[i] = FALSE;

REMAINDER SECTION}

Algorithm for Process Pido {

acquire lock critical section

release lock remainder section

}

6.5 Semaphore

It’s a hardware based solution Semaphore S –integer variable

Two standard operations modify S: wait() and signal()

6.5 Semaphore

Can only be accessed via two indivisible (atomic) operations

wait (S) { while S <= 0 ; // no-op S--;}

signal (S) { S++;}

6.5 Semaphore

Binary semaphore –integer value can range only between 0 and 1; can be simpler to implement

Counting semaphore –integer value can range over an unrestricted domain

6.5 Semaphore

The main disadvantage of the semaphore is that it requires busy waiting, which wastes CPU cycle that some other process might be able to use productively

This type of semaphore is also called a spinlock because the process “spins” while waiting for the lock

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

Starvation–indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended.

Deadlock example

Problems with Semaphores

signal (mutex) //violate mutual exclusive critical sectionwait (mutex)

wait (mutex) //deadlock occurs critical sectionwait (mutex)

Omitting of wait (mutex) or signal (mutex) (or both)

6.6 Classical Problems of Synchronization

Bounded-Buffer Problem Readers and Writers Problem Dining-Philosophers Problem

Monitors

A high-level abstraction that provides a convenient and effective mechanism for process synchronization

Only one process may be active within the monitor at a time

Syntax of Monitor

monitor monitor-name{ // shared variable declarations procedure P1 (…) { …. } … procedure Pn(…) {……} Initialization code ( ….) { …} …}

Condition Variables However, the monitor construct, as defined so

far, is not powerful enough We need to define one or more variables of

type condition: condition x, y;

Two operations on a condition variable:

x.wait() –a process that invokes the operation is suspended.

x.signal() –resumes one of processes (if any) that invoked x.wait()

Monitor with Condition Variables

6.8 Synchronization Examples

Solaris Windows XP Linux

Synchronization in Solaris

It provides adaptive mutexes, condition variables, semaphores, reader-writer locks and turnstiles

Adaptive Mutexes Adaptive mutexes protects access to every

critical data item

If a lock is held by a thread that is currently running on another CPU, the thread spins while waiting for the lock, because the thread holding the lock is likely to finish soon

If the thread holding the lock is not currently in run state, the thread blocks, going to sleep until it is awaken by the release of the lock

(kernel preemption)

Synchronization in Linux

Linux uses an interesting approach to disable and enable kernel preemption by two system calls:

preempt disable()preempt enable()

6.9 Atomic Transactions

A collection of instructions that performs a single logical function is called a transaction

If a terminated transaction has completed its execution successfully, it is committed, otherwise, it is aborted