Tasks and Threads
Tasks and Threads
Tasks and Threads
Ludovic Saint-Bauzel
Polytech'Pqris UPMC
2011-2012
Scheduling Model
Layout
Scheduling
Model
Algorithms
Thread Scheduling
Management
Time Services
Synchronization
IO Control
Scheduler
Aim : address any needs of a real-time software like emergency stop, high
Scheduler
Scheduler
Analysis recipe
Scheduling problem :
I To Model tasks of the system and their constraints
I To Choose a scheduling algorithm
I To Validate feasibility on a set of tasks
I Theoretical feasibility : scheduling ability and complexity
I Empirical feasibility : implement scheduler
Task model
Task families :
I Linked task or not (Precedence constraint).
I Important task or not
I Repetitive or periodic task : activated regularly
I Non periodic task
I Sporadic : irregular activation but a minimum time between each
activation
I Aperiodic : deadline is less strict and no minimum time between each
activation
Task model
(Si , Ci , Di , Ri )
I Periodic task is dened by :
(Si , Ci , Di , Pi )
Task model
Scheduler Structure
Election phase
I Priority, deadline, ...
Layout
Scheduling
Model
Algorithms
Thread Scheduling
Management
Time Services
Synchronization
IO Control
I Low Complexity
I Optimal Algorithm in xed priority algorithm set
Example
I Task 1 : C = 6, P = 10
1 1
I Task 2 : C = 9, P = 30
2 1
I Pre-emptive case
I Non pre-emptive case
Can be scheduled if load rate of the CPU U agrees with the following
sucient condition :
n i ≤ n × (2 − 1)
C
=∑
1
U n
i Ti
If every tasks both arrive at the same time in a system if they respect their
rst deadline,
then
every other following deadlines will be respected whatever time they arrive
in the system
I it's a necessary and sucient condition if every tasks arrive at the
same time
I else, it is a sucient condition
If Di = Ti , nishing test is :
i C j
t
∀i , 1 ≤ i ≤ n min ≤t ≤D
0 ∑ j
≤1
j=
i
t T
1
Response time TR: Duration between time when a task begin and time
when it is nished. Result can be exact relying on task model.
TR i = Ci + ∑ j I
j ∈hp (i )
i
TR
TR i = Ci + ∑ j
j
C
j ∈hp (i ) P
assume that every task have the same priority and are ran in this order
TR1 =3 TR2 =5 TR3 = 18
EDF Example
EDF Properties
I Sucient condition if ∃i , Di ≤ Pi :
n Cj
U = ∑ ≤1
j = Dj 1
I Periodic server :
I Idle if waiting queue is empty
I Running in its time
PS
e1
e2
0 5 10 15 20
e1 e2
Layout
Scheduling
Model
Algorithms
Thread Scheduling
Management
Time Services
Synchronization
IO Control
Services
Properties :
I Threads and process capability
I Fixed priorities, pre-emptive ⇒ RM easy. A minimum of 32 levels is a
mandatory
I One waiting queue for each priority and scheduling policy (SCHED_F
IFO, SCHED_RR, SCHED_OTHERS).
I Available Services to specic users (like root)
Standard say that scheduling policies must be able to be applied each time
the choice threadprocess exists (ex. : choice of a threadprocess to release a
semaphore).
Policies
POSIX.4 policies :
# define SCHED_OTHER 0
# define SCHED_FIFO 1
# define SCHED_RR 2
I Parameters modication:
I Thread : creation of a thread from an attribute or modication of a
running thread.
I Inherit from a fork() or modify of a running process.
API
Example
Layout
Scheduling
Management
Process
Posix Thread
Time Services
Synchronization
IO Control
Fork
Kill - Signal
Layout
Scheduling
Management
Process
Posix Thread
Time Services
Synchronization
IO Control
POSIX Threads
POSIX Threads
POSIX threads
POSIX Threads
POSIX Threads
POSIX Threads
POSIX Threads
POSIX Threads
I With Solaris :
gcc - D_REENTRANT create . c - lpthread - lrt
>a . out
I am thread 4 ; process 5539
I am thread 5 ; process 5539
End of main thread 1 ; process 5539
I With Linux :
gcc - D_REENTRANT create . c - lpthread
>a . out
I am thread 1026 ; process 1253
I am thread 2051 ; process 1254
End of main thread 1024 ; process 1251
Thread Attributes
Thread Attributes
Thread Attributes
Thread Attributes
Thread Attributes
The TSD (Thread Specic Data area) : is an area of memory where are
stored specic information of each thread.
Allow extension of regular attributes.
pthread_key_create Creation of a key.
pthread_key_delete Destruction of the key.
pthread_getspecic get pointer value linked
to the key of the running thread.
pthread_setspecic set pointer value linked
to the key of the running thread.
Thread Attributes
Thread Attributes
Layout
Scheduling
Management
Time Services
Posix Time
Xenomai Time services
Synchronization
IO Control
Time manipulation
Time Manipulation
make
int go =1;
void too_late ( int sig ){
printf (" Signal % d received \ n " , sig );
go =0;
}
Execution :
Layout
Scheduling
Management
Time Services
Posix Time
Xenomai Time services
Synchronization
IO Control
API :
I void rt_timer_spin (RTIME ns) : busy clock
I int rt_timer_set_mode (RTIME nstick) : TM_ONESHOT or
period time
Hardware level :
I oneshot
I periodic
Layout
Scheduling
Management
Time Services
Synchronization
Mutex
Cond Var
Counting Semaphore
Xenomai Native API Synchronisation tools
IO Control
Mutex
Mutex
Layout
Scheduling
Management
Time Services
Synchronization
Mutex
Cond Var
Counting Semaphore
Xenomai Native API Synchronisation tools
IO Control
Rationale
API
Mechanism
Mechanism
Example
int y =2 , x =0;
pthread_mutex_t mut ;
pthread_cond_t cond ;
void * th ( void * arg )
{
int cont =1;
while ( cont ){
pthread_mutex_lock (& mut );
x ++;
printf (" x ++\ n ");
if ( x > y ){
pthread_cond_signal (& cond );
cont =0; }
pthread_mutex_unlock (& mut );}
}
Example
Execution :
x ++ x ++
x ++ x > y is true
Layout
Scheduling
Management
Time Services
Synchronization
Mutex
Cond Var
Counting Semaphore
Xenomai Native API Synchronisation tools
IO Control
Counting semaphore
Counting Semaphore
Execution
thread 4 waiting
main thread 1 : free from
the other thread
thread 4 unblocked
Layout
Scheduling
Management
Time Services
Synchronization
Mutex
Cond Var
Counting Semaphore
Xenomai Native API Synchronisation tools
IO Control
API Mutexes
Layout
Scheduling
Management
Time Services
Synchronization
IO Control
Real-Time Signals
Real-Time Signals
Execution :
$sig &
$kill - USR1 14090
Signal 10 received
Execution :
\ $rt - sig
Received signal 38 , val = 0
Received signal 38 , val = 1
Received signal 38 , val = 2
Received signal 38 , val = 3
Received signal 38 , val = 4
Layout
Scheduling
Management
Time Services
Synchronization
IO Control
Mechanism
API
Example
Example
Layout
Scheduling
Management
Time Services
Synchronization
IO Control
RT_TASK task_desc ;
RT_PIPE pipe_desc ;
Layout
Scheduling
Management
Time Services
Synchronization
IO Control
Memory
Control I/O
Management
int rt_heap_create (RT_HEAP *heap, const char *name, size_t heapsize, int mode)
int rt_heap_alloc (RT_HEAP *heap, size_t size, RTIME timeout, void **blockp)
int rt_heap_free (RT_HEAP *heap, void *block)
Ludovic Saint-Bauzel (Polytech'Pqris UPMC) OS : Tasks And Threads 2011-2012 100 / 104
IO Control Control I/O
Layout
Scheduling
Management
Time Services
Synchronization
IO Control
Memory
Control I/O
Ludovic Saint-Bauzel (Polytech'Pqris UPMC) OS : Tasks And Threads 2011-2012 101 / 104
IO Control Control I/O
Ludovic Saint-Bauzel (Polytech'Pqris UPMC) OS : Tasks And Threads 2011-2012 102 / 104
IO Control Control I/O
RT_INTR intr_desc ;
RT_TASK server_desc ;
Ludovic Saint-Bauzel (Polytech'Pqris UPMC) OS : Tasks And Threads 2011-2012 104 / 104