87
Schedule 2: Concurrent Serializable Schedule

Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

Embed Size (px)

Citation preview

Page 1: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

Schedule 2: Concurrent Serializable Schedule

Page 2: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 3: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering 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

Page 4: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 5: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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))

Page 6: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 7: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

Timestamp Timestamp-based Protocols

Any rolled back transaction Ti is assigned new timestamp and restarted

Algorithm ensures conflict serializability and freedom from deadlock

Page 8: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

Exam 1 Review

Bernard Chen Spring 2007

Page 9: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

Chapter 1 Introduction Chapter 3 Processes

Page 10: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 11: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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()

Page 12: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

Heap (Max Heap)

Provide O(logN) to find the max

97

53 59

26 41 58 31

16 21 36

Page 13: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 14: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 15: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 16: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 17: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

Schedulers

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

This scheme is called swapping

Page 18: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

Addition of Medium Term Scheduling

Page 19: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

Interprocess Cpmmunication (IPC)

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

Page 20: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 21: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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();}

Page 22: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

}

Page 23: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 24: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

Bounded Buffer#define BUFFER_SIZE 6

Typedefstruct{. . .} item;

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

Page 25: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 26: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 27: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

Message-Passing Systems

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

Page 28: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 29: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 30: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

Message-Passing Systems

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

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

Page 31: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 32: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 33: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 34: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 35: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 36: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

}

Page 37: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 38: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 39: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 40: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

Many-to-one Model

User Threads

Kernel thread

Page 41: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 42: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

One-to-one Model

User Threads

Kernel threads

Page 43: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 44: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

Many-to-many Model

User Threads

Kernel threads

Page 45: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 46: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

Chapter 5 Outline

Basic Concepts Scheduling Criteria Scheduling Algorithms

Page 47: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 48: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 49: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 50: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

Scheduling Algorithems

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

Scheduling

Page 51: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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.

Page 52: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 53: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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.

Page 54: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

Symmetric Multithreading

Page 55: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

Algorithm Evaluation

Deterministic Modeling Simulations Implementation

Page 56: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

Deterministic Modeling

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

Page 57: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

Simulation

Page 58: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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.

Page 59: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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.

Page 60: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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++;}

Page 61: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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}

Page 62: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction
Page 63: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 64: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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.

Page 65: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 66: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 67: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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]

Page 68: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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!

Page 69: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

Algorithm for Process Pi

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

REMAINDER SECTION}

Page 70: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

Algorithm for Process Pido {

acquire lock critical section

release lock remainder section

}

Page 71: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

6.5 Semaphore

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

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

Page 72: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

6.5 Semaphore

Can only be accessed via two indivisible (atomic) operations

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

signal (S) { S++;}

Page 73: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 74: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 75: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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.

Page 76: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

Deadlock example

Page 77: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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)

Page 78: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

6.6 Classical Problems of Synchronization

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

Page 79: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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

Page 80: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

Syntax of Monitor

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

Page 81: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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()

Page 82: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

Monitor with Condition Variables

Page 83: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

6.8 Synchronization Examples

Solaris Windows XP Linux

Page 84: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

Synchronization in Solaris

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

Page 85: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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)

Page 86: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

Synchronization in Linux

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

preempt disable()preempt enable()

Page 87: Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction

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