Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Schedule 2: Concurrent Serializable Schedule. Timestamp Timestamp-based Protocols Select order among transactions in advance – timestamp-ordering Transaction."— Presentation transcript:

1 Schedule 2: Concurrent Serializable Schedule

2 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

3 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

4 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

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

6 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

7 Timestamp Timestamp- based Protocols Any rolled back transaction Ti is assigned new timestamp and restarted Algorithm ensures conflict serializability and freedom from deadlock

8 Exam 1 Review Bernard Chen Spring 2007

9 Chapter 1 Introduction Chapter 3 Processes

10 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

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

12 Heap (Max Heap) Provide O(logN) to find the max 97 53 59 26 41 58 31 16 21 36

13 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

14 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

15 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

16 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

17 Schedulers Sometimes it can be advantage to remove process from memory and thus decrease the degree of multiprogrammimg This scheme is called swapping

18 Addition of Medium Term Scheduling

19 Interprocess Cpmmunication (IPC) Two fundamental models (1) Share Memory (2) Message Passing

20 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

21 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(); m_lock(); printf(" Hello world from process %d\n",id); printf(" 2nd line: Hello world from process %d\n",id); m_unlock(); }

22 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++) global_array[id*array_size/nprocs]+=global_array[i]; }

23 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

24 Bounded Buffer #define BUFFER_SIZE 6 Typedefstruct {... } item; item buffer[BUFFER_SIZE]; intin = 0; intout = 0;

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

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

27 Message-Passing Systems A message passing facility provides at least two operations: send(message), receive(message)

28 Message-Passing Systems If 2 processes want to communicate, a communication link must exist It has the following variations: 1. Direct or indirect communication 2. Synchronize or asynchronize communication 3. Automatic or explicit buffering

29 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

30 Message-Passing Systems Indirect communication: the messages are sent to and received from mailbox send(A, message) receive(A, message)

31 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

32 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

33 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

34 MPI Program example #include "mpi.h" #include 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);

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

36 MPI Program example for(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); }

37 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

38 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

39 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

40 Many-to-one Model User Threads Kernel thread

41 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

42 One-to-one Model User Threads Kernel threads

43 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

44 Many-to-many Model User Threads Kernel threads

45 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

46 Chapter 5 Outline Basic Concepts Scheduling Criteria Scheduling Algorithms

47 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

48 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

49 Scheduling Criteria CPU 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

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

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

52 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

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

54 Symmetric Multithreading

55 Algorithm Evaluation Deterministic Modeling Simulations Implementation

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

57 Simulation

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

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

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

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

62

63 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

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

65 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

66 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

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

68 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!

69 Algorithm for Process Pi while (true) { flag[i] = TRUE; turn = j; while ( flag[j] && turn == j) ; CRITICAL SECTION flag[i] = FALSE; REMAINDER SECTION }

70 Algorithm for Process Pi do { acquire lock critical section release lock remainder section }

71 6.5 Semaphore It’s a hardware based solution Semaphore S –integer variable Two standard operations modify S: wait() and signal()

72 6.5 Semaphore Can only be accessed via two indivisible (atomic) operations wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; }

73 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

74 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

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

76 Deadlock example

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

78 6.6 Classical Problems of Synchronization Bounded-Buffer Problem Readers and Writers Problem Dining-Philosophers Problem

79 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

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

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

82 Monitor with Condition Variables

83 6.8 Synchronization Examples Solaris Windows XP Linux

84 Synchronization in Solaris It provides adaptive mutexes, condition variables, semaphores, reader-writer locks and turnstiles

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

86 Synchronization in Linux Linux uses an interesting approach to disable and enable kernel preemption by two system calls: preempt disable() preempt enable()

87 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


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

Similar presentations


Ads by Google