Skip to content

Instantly share code, notes, and snippets.

@parastuffs
Created March 21, 2014 14:50
Show Gist options
  • Save parastuffs/9687925 to your computer and use it in GitHub Desktop.
Save parastuffs/9687925 to your computer and use it in GitHub Desktop.
INFOF410 - Real Time Linux Task 3
// ---------------------------------------------------------------------
// exemple-threads-temps-reel.c
// Adapté d'un ficher d'exemple du livre "Solutions Temps-Reel sous Linux"
// (C) 2012 - Christophe BLAESS
// http://christophe.blaess.fr
// ---------------------------------------------------------------------
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#define NB_THREADS 4
void * fonction_thread(void * arg)
{
int numero = (int) arg;
int i, j;
sleep(numero*2);
fprintf(stderr, "Debut thread %d a %ld\n", numero, time(NULL));
for (i = 0; i < 100000; i ++) {
if (i%1000==0) fprintf(stderr, "%d",numero) ;
for (j = 0; j < 10000; j ++)
;
}
fprintf(stderr, "Fin thread %d a %ld\n", numero, time(NULL));
return NULL;
}
int main(void)
{
int priorities[4] = {60, 55, 50, 45};
pthread_t thr[NB_THREADS];
pthread_attr_t attributes;//reusable
struct sched_param parameters;//reusable
pthread_attr_init(&attributes);//Default init
pthread_attr_setschedpolicy(&attributes, SCHED_FIFO);//Scheduling policy
pthread_attr_setinheritsched(&attributes, PTHREAD_EXPLICIT_SCHED);//Activate the value set in attributes. Otherwise, value ignored and parent's inheritage.
int i;
for(i=0;i<NB_THREADS;++i) {
printf("Loop #%d", i);
parameters.sched_priority = priorities[i];//Priority
pthread_attr_setschedparam(&attributes, &parameters);//Set priority
pthread_create(&thr[i], &attributes, fonction_thread, (void *)i);
}
/* Créer NB_THREADS threads temps réel qui exécutent chacun la fonction fonction_thread
ci-dessus avec un numéro différent (0...NB_THREADS-1)*/
for (i = 0; i < NB_THREADS; i ++)
pthread_join(thr[i], NULL);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment