230 likes | 407 Views
Real-Time Threads. Time dependent, usually wrt deadline Periodic Aperiodic Sporadic Priority scheduled Fault tolerant. Priority Threads. struct sched_param parameter; parameter.sched_priority = 55; pthread_setschedparam( pthread_self(), SCHED_FIFO, &parameter);. SCHEDULER. User 2.
E N D
Real-Time Threads • Time dependent, usually wrt deadline • Periodic • Aperiodic • Sporadic • Priority scheduled • Fault tolerant
Priority Threads struct sched_param parameter; parameter.sched_priority = 55; pthread_setschedparam( pthread_self(), SCHED_FIFO, ¶meter);
SCHEDULER User 2 . . . . User N User 1 DELAYQUEUE RESOURCE
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
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);
Typical Inversion Scenario • Low-priority thread in a C.S. • Interrupt starts high-priority thread • High-priority attempts to enter C.S. Blocked!!
Worse Inversion Scenario • Low-priority thread in a C.S. • Interrupt starts medium-priority thread • Interrupt starts high-priority thread • High-priority attempts to enter C.S. Blocked!! But now, medium-priority can delay low- priority’s exit from the C.S. indefinitely
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
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);
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);
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);
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);
Implementation Choices Readers or Writers move to head of wait queue
POSIX Thread Read/Write Lockssupports 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);
The Monitor – blocking in a C.S. pthread_mutex_lock() while (!condition) /* block(self) */ pthread_mutex_unlock()
Condition Variables – always owned by a mutex pthread_cond_t notbusy = PTHREAD_COND_INITIALIZER; pthread_mutex_lock(&m) . . . . . . . . . . . . . . . while (!condition) pthread_cond_wait(¬busy, &m); /* releases C.S. until condition becomes true */ pthread_mutex_unlock(&m)
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); <critical section> void omp_unset_lock(omp_lock_t *lock); void omp_destroy_lock(omp_lock_t *lock);
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);