Real-Time Threads

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

description

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. - PowerPoint PPT Presentation

Transcript of Real-Time Threads

Page 1: Real-Time Threads

Real-Time Threads

• Time dependent, usually wrt deadline– Periodic– Aperiodic– Sporadic

• Priority scheduled• Fault tolerant

Page 2: Real-Time Threads

Priority Threads

struct sched_param parameter;

parameter.sched_priority = 55;

pthread_setschedparam(

pthread_self(),

SCHED_FIFO,

&parameter);

Page 3: Real-Time Threads

SCHEDULER

User 1 User 2 . . . . User N

DELAY

QUEUE

Page 4: Real-Time Threads

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

Page 5: Real-Time Threads

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

Page 6: Real-Time Threads

Priority Inversion (low blocks high)

Page 7: Real-Time Threads

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

Page 8: Real-Time Threads

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

Page 9: Real-Time Threads

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

Page 10: Real-Time Threads

Priority Inversion Managementpthread_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);

Page 11: Real-Time Threads

Other PThread Features

Page 12: Real-Time Threads

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

Page 13: Real-Time Threads

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

Page 14: Real-Time Threads

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

Page 15: Real-Time Threads
Page 16: Real-Time Threads

Implementation ChoicesReaders or Writers move to head of wait queue

Page 17: Real-Time Threads

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

Page 18: Real-Time Threads

The Monitor – blocking in a C.S.

pthread_mutex_lock()

while (!condition) /* block(self) */

pthread_mutex_unlock()

Page 19: Real-Time Threads

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)

Page 20: Real-Time Threads

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

Page 21: Real-Time Threads

POSIX Thread Barriers

pthread_barrier_tint 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 barrierint pthread_barrier_destroy ( pthread_barrier_t * barrier);

Page 22: Real-Time Threads

Message Passing -- Remote Procedure Call (RPC)

Page 23: Real-Time Threads

And Much More