1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
/*
* SAMBIA39
* Exemple de code source attention il
* Est susceptible de comporter des erreurs
*/
#define NTHREAD 2
#define DEF_FIFO 99
#define DEF_OTHER 0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
/*
* Détermination des fonctionnalités POSIX
* pour bascule en mode temps réel le cas échéant
* adapter le code aux gentillesses.
*/
#ifdef _POSIX_PRIORITY_SCHEDULING
#include <sched.h>
#else
#warning "!!! Attention le mode temps réel ne peut être définis."
#endif
/*
* Fonction utilisée par les threads
* temps réel
*/
void *f_thread( void *p ){
size_t i = 1000;
size_t const id = (size_t)p;
(void)fprintf(stderr,
"Thread(%zu)\t START\n", id);
for( ; 0 != i; --i )
;
(void)fprintf(stderr,
"Thread(%zu)\t OFF\n", id);
return NULL;
}
/*
* Fonction principale
*/
int main( void ){
int i = 0;
extern int errno;
pthread_t tThread[NTHREAD];
pthread_attr_t attr_thread[2];
struct sched_param thread_param[2];
/*
* Acquisition des priorités max définis par le système
* pour les attribuer aux différends thread attention le
* noyau pourrais attribuer la valeur statique ou dynamique
* par défaut du processus en cours tout dépend du noyau à
* vérifier avants ou solution optimale définir soit même les
* priorités.
* => Dé-commneter et commenter la partie des définitions
* fixées des priorités pour utiliser les priorités définies
* par le système
const int PRIORITER_SYS_FIFO = sched_get_priority_max(SCHED_FIFO);
const int PRIORITER_SYS_OTHER = sched_get_priority_min(SCHED_OTHER);
(void)fprintf(stderr, "FIFO\t:%d\n", PRIORITER_SYS_FIFO );
(void)fprintf(stderr, "OTHER\t:%d\n", PRIORITER_SYS_OTHER );
*/
/*
* Ordonnancement temps réel
*/
#ifdef _POSIX_PRIORITY_SCHEDULING
errno = 0;
/*
* Initialisation des valeurs par
* défaut des attributs des threads
*/
pthread_attr_init(&attr_thread[0]);
pthread_attr_init(&attr_thread[1]);
/*
* Définition des priorités des thread
* avec les valeurs propre définis par le système.
* pour tester ne pas mettre sous commentaire
*
thread_param[0].sched_priority = PRIORITER_SYS_OTHER;
thread_param[1].sched_priority = PRIORITER_SYS_FIFO;
*/
/*
* Définition volontaire des priorités
* indépendamment de celui du noyau
* /!\Attention valeur max FIFO est de 99.
*/
thread_param[0].sched_priority = DEF_OTHER; // thread un en OTHER
thread_param[1].sched_priority = DEF_FIFO; // thread deux en FIFO
/*
* Définition du type d'ordonnancement
* aux attributs des différents threads
* avec les paramètre indiquer.
* paramètre OTHER = temps partager (defaut)
* paramètre FIFO = temps réel.
*/
if( 0 !=
(errno = pthread_attr_setschedpolicy(&attr_thread[0],
SCHED_FIFO)) && ( 0 !=
(errno = pthread_attr_setschedpolicy(&attr_thread[1],
SCHED_RR)))){
// Destruction des attributs
(void)pthread_attr_destroy(&attr_thread[0]);
(void)pthread_attr_destroy(&attr_thread[1]);
(void)fprintf(stderr,
"Erreur(%d)\t:%s\n\t:%s",errno,
"Erreur setschedpolicy", strerror(errno) );
return EXIT_FAILURE;
}
// Suite definition des ordonnancement.
if( 0 !=
(errno = pthread_attr_setinheritsched(&attr_thread[0],
PTHREAD_EXPLICIT_SCHED))&& (0 !=
(errno = pthread_attr_setinheritsched(&attr_thread[1],
PTHREAD_EXPLICIT_SCHED)))){
// Destruction des attributs
(void)pthread_attr_destroy(&attr_thread[0]);
(void)pthread_attr_destroy(&attr_thread[1]);
(void)fprintf(stderr,
"Erreur(%d)\t:%s\n\t:%s",errno,
"Erreur setinheritsched", strerror(errno) );
return EXIT_FAILURE;
}
/*
* Définition individuels des threads
* et de façon distincte l'un OTHER et
* l'autre FIFO
*/
if( 0 !=
(errno = pthread_attr_setschedparam(&attr_thread[0],
&thread_param[0])) && (0 !=
(errno = pthread_attr_setschedparam(&attr_thread[1],
&thread_param[1])))){
(void)fprintf(stderr, "Erreur(%d)\t:%s\n\t:%s",errno,
"Erreur pthread_attr_setschedparam",
strerror(errno) );
return EXIT_FAILURE;
}
/*
* Création thread un
*/
if( 0 != (errno = pthread_create(&tThread[0],
&attr_thread[0], f_thread, (void*)(1)))){
(void)fprintf(stderr,
"Erreur(%d)\t:%s[%d]\n\t:%s",errno,
"Erreur pthread_create", i,
strerror(errno) );
return EXIT_FAILURE;
}
/*
* Création thread deux
*/
if( 0 != (errno = pthread_create(&tThread[1],
&attr_thread[1], f_thread, (void*)(2)))){
(void)fprintf(stderr,
"Erreur(%d)\t:%s[%d]\n\t:%s",errno,
"Erreur pthread_create", i,
strerror(errno) );
return EXIT_FAILURE;
}
/*
* Attente de la fin des threads et
* code retour des threads est volontairement
* ignorée.
*/
for( i = 0; i < NTHREAD; i++ )
(void)pthread_join(tThread[i], NULL );
/*
* Destruction des attributs.
* des deux threads
*/
(void)pthread_attr_destroy(&attr_thread[0]);
(void)pthread_attr_destroy(&attr_thread[1]);
return EXIT_SUCCESS;
#else
/*
* Cas contraire on opte pour nice par exemple
* en fixant la gentillesse avec soit la commande depuis le terminal
* nice -n valeur_increment ./app argument
* ou par la fonction nice.
*/
return EXIT_SUCCESS;
#endif
return EXIT_SUCCESS; // Jamais atteinte.
} |
Partager