Real - Time systems - Week 3: Task and task synchronization

Suppose that system has only one resource for each type such as 1 printer, 1 tape driver, 1 plotter .  To detect a deadlock (the easiest technique)  Draw a graph of relationship between processes and resources  If at least one cycle can be detected, a deadlock exists.

pdf31 trang | Chia sẻ: nguyenlam99 | Lượt xem: 877 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Real - Time systems - Week 3: Task and task synchronization, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
NLT, SoICT, 2015 Real-time Systems Ngo Lam Trung Dept. of Computer Engineering Week 3: Task and Task Synchronization NLT, SoICT, 2015  Task  Task synchronization  Semaphore and mutex  Philosopher’s problem  Deadlock Content NLT, SoICT, 2015 Defining a task  Independent thread of execution that can compete with concurrent tasks for processor execution time.  Thus, schedulable & allocated a priority level according to the scheduling algorithm  Elements of a task  Unique ID  Task control block (TCB)  Stack  Priority (if part of a preemptive scheduling plan)  Task routine NLT, SoICT, 2015 Elements of a task NLT, SoICT, 2015 Task states & scheduling  Task states:  Ready state  Running state  Blocked state  Scheduler determines each task’s state. running blockedready dispatched preempted blocked unblocked unblocked NLT, SoICT, 2015 Task states  ready state-the task is ready to run but cannot because a higher priority task is executing.  blocked state-the task has requested a resource that is not available, has requested to wait until some event occurs, or has delayed itself for some duration.  running state-the task is the highest priority task and is running. NLT, SoICT, 2015 Typical task structure(1) Typical task structures:  Run-to-completion task: Useful for initialization & startup tasks NLT, SoICT, 2015 Typical task structure(2) Typical task structures:  Endless-loop task: Work in an application by handling inputs & outputs NLT, SoICT, 2015 Semaphores  In multi-task systems, concurrently-running tasks should be able to  synchronize their execution, and  to coordinate mutual exclusive access to shared resources.  What is semaphore?  A kernel object to realize synchronization & mutual exclusion  One or more threads of execution can acquire & release to execute an operation to be synchronized or to access to a shared resource. NLT, SoICT, 2015 Semaphore elements NLT, SoICT, 2015 Defining semaphore  Elements of semaphores assigned by the kernel  Semaphore control block (SCB)  Semaphore ID (unique in the system)  Value (binary or count)  Task-waiting list  Classification of semaphores:  Binary semaphore  Counting semaphore  Mutex NLT, SoICT, 2015 Binary semaphore  Provides binary state of unavailable/available (or empty/full) Task 1 Task 2 Semaphore Acquire semaphore (P operation) Can’t acquire semaphore during Task 1’s acquirement Release semaphore (V operation) Acquire semaphore (P operation) Initial value1: availablevalue 0: unavailablevalue 1: available Release semaphore (V operation) l 1 av ilable Task 3 Any tasks can release semaphore if it is global resource NLT, SoICT, 2015 Counting semaphore  If the value of a semaphore is n, it can be acquired n times concurrently. NLT, SoICT, 2015 Mutual exclusion (MUTEX) semaphores  Special binary semaphore  Has states of locked/unlocked & lock count  Difference: signaling vs protecting NLT, SoICT, 2015 Mutual exclusion (MUTEX) semaphores  Special features of MUTEX  Ownership: When a task acquires the mutex, no other task can release it. - General semaphores: released by any task  Recursive locking: allows the owner task to re-acquire the mutex multiple times in the locked state - The number of times of recursive acquirement is managed by lock count. - Useful when the owner task requires exclusive access to shared resource and calls routines that also require the same resource - Problem: recursion and deadlock NLT, SoICT, 2015 Mutual exclusion (MUTEX) semaphores  Special features of MUTEX  Task deletion safety: avoiding a task deletion while it is locking a mutex  Priority inversion avoidance: - Priority inversion problem: a higher priority task is blocked and is waiting for a resource being used by a lower priority task, which has itself been preempted by an unrelated medium-priority task - To avoid priority inversion additional protocols are required, eg priority inheritance protocol, or priority ceiling protocol NLT, SoICT, 2015 Semaphore vs Mutex All processes can release semaphore Only owner is able to unlock mutex NLT, SoICT, 2015 Typical semaphore use  Semaphore are used for:  Synchronizing execution of tasks  Coordinating access to a shared resource  Synchronization design requirements  Wait-and-signal  Multiple-task wait-and-signal  Credit-tracking  Single shared-resource-access  Recursive shared-resource-access  Multiple shared-resource access NLT, SoICT, 2015 Typical semaphore uses NLT, SoICT, 2015 Example:  Consumer – producer problem  Producer:  Task to read data from input device,  Data transferred to 4KB shared buffer memory  Consumer:  Task to read and process data from buffer memory  Synchronization problem  Solution 1: binary semaphore for buffer memory access  Other solution? NLT, SoICT, 2015 Example: The Dining Philosophers Problem  Five philosophers seated around a circular table with a plate of spaghetti for each.  Between each pair of plates is one fork  The spaghetti is so slippery that a philosopher needs two forks to eat it.  When a philosopher gets hungry, he tries to acquire his left and right fork Problem: not enough forks for all  Write a program to control philosophers concurrently without getting stuck? NLT, SoICT, 2015 Solution 1: #define N 5/* number of philosophers */ void philosopher(int i)/* i: philosopher number, from 0 to 4 */ { while (TRUE) { think( ); /* philosopher is thinking */ take_fork(i); /* take left fork */ take_fork((i+1) % N);/* take right fork; */ eat(); /* yum-yum, spaghetti */ put_fork(i); /* Put left fork back on the table */ put_fork((i+1) % N);/* put right fork back table */ } } This is non-solution, because of potential deadlock. Why? NLT, SoICT, 2015 Solution 2:  If philosopher could not acquire fork:  Wait for a random duration  Retry  Simple and efficient  Minimize lock-state time  But not lock-state free  Cannot be used in critical system NLT, SoICT, 2015 Solution 3 #define N 5 /* Number of philosphers */ #define RIGHT(i) (((i)+1) %N) #define LEFT(i) (((i)==N) ? 0 : (i)+1) typedef enum { THINKING, HUNGRY, EATING } phil_state; phil_state state[N]; semaphore mutex =1; semaphore s[N]; /* one per philosopher, all 0 */ void test(int i) { if ( state[i] == HUNGRY && state[LEFT(i)]!= EATING && state[RIGHT(i)] != EATING ) { state[i] = EATING; up(s[i]) } } NLT, SoICT, 2015 Solution 3 void get_forks(int i){ down(mutex); state[i] = HUNGRY; test(i); up(mutex); down(s[i]); //lock } void put_forks(int i){ down(mutex); state[i]= THINKING; test(LEFT(i)); test(RIGHT(i)); up(mutex); } void philosopher(int process) { while(1) { think(); get_forks(process); eat(); put_forks(process); } } NLT, SoICT, 2015 Deadlock  Task uses resources  Two types of resources  Preemtible: memory, printer  Non-preemtible: CD-W drive in disc burning process  Potential deadlock with preemtible resources can be resolved (imagine the case philosophers can share forks without cleaning!)  Deadlock involves non-preemtible resources NLT, SoICT, 2015 Deadlock  Deadlock definition: A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the process can occur.  Conditions for deadlock  Mutual exclusion condition: a resource that cannot be used by more than one process at a time  Hold and wait condition: processes already holding resources may request new resources  No preemption condition: No resource can be forcibly removed from a process holding it, resources can be released only by the explicit action of the process  Circular wait condition: two or more processes form a circular chain where each process waits for a resource that the next process in the chain holds NLT, SoICT, 2015 Deadlock modeling Process A holds resource R Process B acquires resource R Deadlock Process C is waiting resource T, which is currently holding by process D. Process D is waiting resource U, which is currently holding by process C. A BR S C DT U NLT, SoICT, 2015 Deadlock detection and recovery  Let deadlock occurs, tries to detect and attempt recovery if necessary.  2 methods to detect deadlock  Deadlock detection with one resource of each type  Deadlock detection with multiple resource of each type  3 methods to recovery from deadlock  Recovery through preemption  Recovery through rollback  Recovery through killing processes NLT, SoICT, 2015 Deadlock detection with one resource  Suppose that system has only one resource for each type such as 1 printer, 1 tape driver, 1 plotter.  To detect a deadlock (the easiest technique)  Draw a graph of relationship between processes and resources  If at least one cycle can be detected, a deadlock exists. NLT, SoICT, 2015  Process A holds R and wants S  Process B holds nothing but wants T  Process C holds nothing but wants S  Process D holds U and wants S and T  Process E holds T and wants V  Process F holds W and wants S  Process G holds V and wants U Example

Các file đính kèm theo tài liệu này:

  • pdfrt2015_week3_3391_5149.pdf
Tài liệu liên quan