Presentation is loading. Please wait.

Presentation is loading. Please wait.

Real-Time Threads Time dependent, usually wrt deadline –Periodic –Aperiodic –Sporadic Priority scheduled Fault tolerant.

Similar presentations


Presentation on theme: "Real-Time Threads Time dependent, usually wrt deadline –Periodic –Aperiodic –Sporadic Priority scheduled Fault tolerant."— Presentation transcript:

1 Real-Time Threads Time dependent, usually wrt deadline –Periodic –Aperiodic –Sporadic Priority scheduled Fault tolerant

2 Priority Threads struct sched_param parameter; parameter.sched_priority = 55; pthread_setschedparam( pthread_self(), SCHED_FIFO, &parameter);

3 SCHEDULER User 1 User 2.. User N DELAY QUEUE

4 Real-Time Scheduling Periodic Jobs –Rate monotonic -- assign priority in frequency order (high to low) –To achieve 100% utilization when using fixed priorities, assign periods so that all tasks are harmonic; for each task, its period is an integer multiple of every other task that has a shorter period. Aperiodic –Nearest deadline first (preemptive) Sporadic

5 Multiprocessor Scheduling Processor Affinity Vector - trys to keep process on the same processor int sched_setaffinity(pid_t processid, unsigned int cpusetsize, cpu_set_t *mask); int sched_getaffinity(pid_t processid, unsigned int cpusetsize, cpu_set_t *mask);

6 Priority Inversion (low blocks high)

7 Typical Inversion Scenario 1)Low-priority thread in a C.S. 2)Interrupt starts high-priority thread 3)High-priority attempts to enter C.S. Blocked!!

8 Worse Inversion Scenario 1)Low-priority thread in a C.S. 2)Interrupt starts medium-priority thread 3)Interrupt starts high-priority thread 4)High-priority attempts to enter C.S. Blocked!! But now, medium-priority can delay low- priority’s exit from the C.S. indefinitely

9 Inversion Solutions Priority Ceiling (static) –Raise priority on C.S. entry to highest priority of all possible users –Prevents all threads at intermediate levels from blocking any thread that owns a C.S. Priority Inheritance (dynamic) –Any thread blocking on entry to a C.S. “pushes” its priority onto the current owner and so on

10 Priority Inversion Management pthread_mutexattr_t attr; pthread_mutex_t m; pthread_mutexattr_init(&attr); pthread_mutexattr_setprotocol(&attr, PRIO_INHERIT); pthread_mutexattr_setprotocol(&attr, PRIO_PROTECT); pthread_mutexattr_setprioceiling(&attr, 55); pthread_mutex_init(&m, &attr); pthread_mutex_lock(&m); pthread_mutex_unlock(&m);

11 Other PThread Features

12 Simpler than a Count/Queue -- spinlock pthread_spinlock_t lock; /* only a count ***/ pthread_spin_init(&lock, PTHREAD_PROCESS_PRIVATE ); pthread_spin_lock(&lock); /* busy wait loop ***/ /*** CRITICAL SECTION ***/ pthread_spin_unlock(&lock); pthread_spin_destroy(&lock);

13 Waiting for Work to Complete - Barriers pthread_barrier_t b; pthread_barrier_init(&b, NULL, 5); /* BLOCK UNTIL 5 THREADS STOP HERE */ if (pthread_barrier_wait(&b)==SERIAL_THREAD) { /*** WORK COMPLETED ***/ } pthread_barrier_destroy(&b);

14 Multiple Readers – Serial Writers Data Locks Mutexes serialize threads’ access to data. If a thread only reads data, multiple reader threads can run simultaneously. pthread_rwlock_t rw = PTHREAD_RWLOCK_INITIALIZER ; pthread_rwlock_wrlock(&rw); pthread_rwlock_rdlock(&rw); /*** CRITICAL SECTION ***/ pthread_rwlock_unlock(&rw);

15

16 Implementation Choices Readers or Writers move to head of wait queue

17 POSIX Thread Read/Write Locks supports exclusive writes but concurrent readers Read/Write Lock Type pthread_rwlock_t int pthread_rwlock_init(pthread_rwlock_t *rwlck,NULL); int pthread_rwlock_destroy (pthread_rwlock_t *rwlck); int pthread_rwlock_rdlock (pthread_rwlock_t *rwlock); int pthread_rwlock_unlock (pthread_rwlock_t *rwlock); int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlk); int pthread_rwlock_wrlock (pthread_rwlock_t *rwlck); int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlck);

18 The Monitor – blocking in a C.S. pthread_mutex_lock() while (!condition) /* block(self) */ pthread_mutex_unlock()

19 Condition Variables – always owned by a mutex pthread_cond_t notbusy = PTHREAD_COND_INITIALIZER ; pthread_mutex_lock(&m)............... while (!condition) pthread_cond_wait(&notbusy, &m); /* releases C.S. until condition becomes true */ pthread_mutex_unlock(&m)

20 OpenMP Atomic Actions, Critical Sections and Locks #pragma omp atomic x binop= expr or x++ or x-- #pragma omp critical [(name)] structured-block void omp_init_lock(omp_lock_t *lock); void omp_set_lock(omp_lock_t *lock); void omp_unset_lock(omp_lock_t *lock); void omp_destroy_lock(omp_lock_t *lock);

21 POSIX Thread Barriers pthread_barrier_t int pthread_barrier_init ( pthread_barrier_t * barrier, NULL, unsigned int count); int pthread_barrier_wait ( pthread_barrier_t * barrier); //returns PTHREAD_BARRIER_SERIAL_THREAD // to the last thread to reach the barrier int pthread_barrier_destroy ( pthread_barrier_t * barrier);

22 Message Passing -- Remote Procedure Call (RPC)

23 And Much More


Download ppt "Real-Time Threads Time dependent, usually wrt deadline –Periodic –Aperiodic –Sporadic Priority scheduled Fault tolerant."

Similar presentations


Ads by Google