Professional Documents
Culture Documents
Process
Processes do not share memory!
Different processes
execute different code;
access distinct memory spaces.
Processes can communicate through a message passing
mechanism:
P1 P2
channel
data data
1
07/11/2018
2
07/11/2018
3
07/11/2018
4
07/11/2018
pthread_join(tid, NULL);
pthread_attr_destroy(&myatt);
}
SCHED_OTHER EDF
/proc/sys/kernel/sched_rt_period_us
1 1
set the period (in microseconds) of the reservation.
SCHED_BATCH SCHED_FIFO SCHED_RR SCHED_DEADLINE
Default = 1000000 µs (1 second).
sched_fair sched_rt
/proc/sys/kernel/sched_rt_runtime_us
sched_dl
low priority medium priority high priority
set the budget (in microseconds) to be assigned to the
real‐time activities every period. Default = 950000 µs.
Linux scheduling classes 27
5
07/11/2018
> echo 980000 > /proc/sys/kernel/sched_rt_runtime_us The type time_t depends on the implementation, but
Sets the budget to 98% of the period. usually it is a 32‐bit integer.
Unfortunately, the standard library does not provide
> echo ‐1 > /proc/sys/kernel/sched_rt_runtime_us functions to perform operations on time variables, thus
Sets the budget to 100% of the period. it is necessary to define auxiliary functions.
Time copy
This function copies a source time variable ts in a
destination variable pointed by td:
6
07/11/2018
7
07/11/2018
clock_gettime(CLOCK_MONOTONIC, &t);
time_add_ms(&t, period); Application
while (1) {
To be
/* do useful work */ Ptask Library developed
clock_gettime(CLOCK_MONOTONIC, &now);
if (time_cmp(now, t) > 0) exit(-1);
Pthread Library
clock_nanosleep(CLOCK_MONOTONIC,
TIMER_ABSTIME, &t, NULL);
time_add_ms(&t, period);
} Linux primitives
}
8
07/11/2018
9
07/11/2018
wait_for_activation() Semaphores
void wait_for_activation(int i) General semaphores are not part of the pthread library,
{ since are defined in the POSIX standard.
clock_nanosleep(CLOCK_MONOTONIC,
TIMER_ABSTIME, &(tp[i].at), NULL);
To use them, you need to include <semaphore.h>.
time_add_ms(&(tp[i].at), tp[i].period); A semaphore is a variable of type sem_t.
time_add_ms(&(tp[i].dl), tp[i].period);
} Main functions:
sem_init: initializes a semaphore.
Suspends the calling thread until the next activation and,
sem_destroy: reclaims the memory needed for a
when awaken, updates activation time and deadline. semaphore that is not used.
NOTE: Even though the thread calls time_add_ms() after sem_wait: wait on a semaphore.
the wake‐up time, the computation is correct. sem_post: signal on a semaphore.
sem_getvalue: returns the value of a semaphore.
10
07/11/2018
11
07/11/2018
12
07/11/2018
Y pthread_mutex_unlock(&mux);
x != 8 sleep Y If more threads are waiting,
x == 8
it is possible to wake up a /* do some useful work */
N
N single one or all together
pthread_exit(NULL);
}
13
07/11/2018
Brodcasting Barrier
void *inc_count() #define NT 10
{ pthread_barrier_t barr;
A barrier must
be declared first
/* do some useful work */ All waiting threads can be int main()
pthread_mutex_lock(&mux); awaken by a broadcast { and initialized with the number of
pthread_t tid[NT];
count++; threads that need to synchronize
int i;
if (count == THRESHOLD) {
pthread_mutex_unlock(&mux); /* initialize barrier */
pthread_cond_broadcast(&cv); pthread_barrier_init(&barr, NULL, NT);
}
else pthread_mutex_unlock(&mux); for (i=0; i<NT; i++)
pthread_create(&tid[i], NULL, &task, (void*)i));
/* do some useful work */
/* do some useful work */
pthread_exit(NULL);
} return 0;
}
14