1 / 23

Real-Time Threads

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.

moe
Download Presentation

Real-Time Threads

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 2 . . . . User N User 1 DELAYQUEUE RESOURCE

  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 • Low-priority thread in a C.S. • Interrupt starts high-priority thread • High-priority attempts to enter C.S. Blocked!!

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

  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. Implementation Choices Readers or Writers move to head of wait queue

  16. 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);

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

  18. 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)

  19. 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);

  20. 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);

  21. Message Passing -- Remote Procedure Call (RPC)

  22. And Much More

More Related