Schedule 2 concurrent serializable schedule
1 / 87

Schedule 2: Concurrent Serializable Schedule - PowerPoint PPT Presentation

  • Uploaded on

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about ' Schedule 2: Concurrent Serializable Schedule ' - keira

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Timestamp timestamp based protocols
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 protocols1
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 protocols2
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 protocols3
Timestamp Timestamp-based Protocols

  • Suppose Ti executes read(Q)

  • If TS(Ti) < W-timestamp(Q), Ti needs to read value of Q that was already overwritten

    Read 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 protocols4
Timestamp Timestamp-based Protocols

Suppose 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 protocols5
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

Exam 1 Review

Bernard Chen

Spring 2007

The process
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 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
Heap (Max Heap)

Provide O(logN) to find the max


53 59

26 41 58 31

16 21 36

The process1
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


  • 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


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


  • 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


  • 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
Addition of Medium Term Scheduling

Interprocess cpmmunication ipc
Interprocess Cpmmunication (IPC)

  • Two fundamental models

  • Share Memory

  • Message Passing

Share memory parallelization system example
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 example
Real Example

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


printf(" Hello world from process %d\n",id);

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



Real example1
Real Example

int array_size=1000

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



Shared memory systems
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
Bounded Buffer

#define BUFFER_SIZE 6



. . .

} item;

item buffer[BUFFER_SIZE];

intin = 0;

intout = 0;

Bounded buffer producer iew
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
Bounded Buffer (Consumer view)

while (true)


while (in == out)

; // do nothing --nothing to consume

// until remove an item from the buffer

item = buffer[out];

out = (out + 1) % BUFFER SIZE;

return item;


Message passing systems
Message-Passing Systems

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

Message passing systems1
Message-Passing Systems

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

    It has the following variations:

  • Direct or indirect communication

  • Synchronize or asynchronize communication

  • Automatic or explicit buffering

Message passing systems2
Message-Passing Systems

  • Direct communication

    send(P, message)

    receive(Q, message)


  • A link is established automatically

  • A link is associated with exactly 2 processes

  • Between each pair, there exists exactly one link

Message passing systems3
Message-Passing Systems

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

    send(A, message)

    receive(A, message)

Message passing systems4
Message-Passing Systems


  • 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 systems5
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 systems6
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
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 example1
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 program example2
MPI Program example



MPI_Reduce (&sum, &sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);

if (id==0)

printf("%d ",sum);



  • 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
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 models1
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
Many-to-one Model

User Threads

Kernel thread

Many to one model1
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
One-to-one Model

User Threads

Kernel threads

One to one model1
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
Many-to-many Model

User Threads

Kernel threads

Many to many model1
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
Chapter 5 Outline

  • Basic Concepts

  • Scheduling Criteria

  • Scheduling Algorithms

Cpu scheduler
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 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 criteria
Scheduling Criteria

CPU utilization – keep the CPU as busy as possible (from 0% to


Throughput – # of processes that complete their execution per

time unit

Turnaround time – amount of time to execute a particular


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

Algorithm evaluation
Algorithm Evaluation

  • Deterministic Modeling

  • Simulations

  • Implementation

Deterministic modeling
Deterministic Modeling

  • Deterministic Modeling:

  • Process Burst Time

    P1 10

    P2 29

    P3 3

    P4 7

    P5 12


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



Bounded buffer consumer view1
Bounded Buffer (Consumer view)

while (true) {

while (count == 0)

; // do nothing

nextConsumed= buffer[out];

out = (out + 1) % BUFFER_SIZE;


/* consume the item in next Consumed


Race condition
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
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 problem1
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 problem2
6.2 The Critical-Section Problem

A 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
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 solution1
6.3 Peterson’s Solution

The 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
Algorithm for Process Pi

while (true) {

flag[i] = TRUE;

turn = j;

while ( flag[j] && turn == j)



flag[i] = FALSE;



Algorithm for process pi1
Algorithm for Process Pi

do {

acquire lock

critical section

release lock

remainder section


6 5 semaphore
6.5 Semaphore

  • It’s a hardware based solution

  • Semaphore S –integer variable

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

6 5 semaphore1
6.5 Semaphore

Can only be accessed via two indivisible (atomic) operations

wait (S) {

while S <= 0

; // no-op



signal (S) {



6 5 semaphore2
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 semaphore3
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 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.

Problems with semaphores
Problems with Semaphores

signal (mutex) //violate mutual exclusive

critical section

wait (mutex)

wait (mutex) //deadlock occurs

critical section

wait (mutex)

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

6 6 classical problems of synchronization
6.6Classical Problems of Synchronization

  • Bounded-Buffer Problem

  • Readers and Writers Problem

  • Dining-Philosophers Problem


  • 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
Syntax of Monitor

monitor monitor-name


// shared variable declarations

procedure P1 (…) { …. }

procedure Pn(…) {……}

Initialization code ( ….) { …}


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

6 8 synchronization examples
6.8 Synchronization Examples

  • Solaris

  • Windows XP

  • Linux

Synchronization in solaris
Synchronization in Solaris

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

Adaptive mutexes
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
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
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