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
| #define _GNU_SOURCE
#include <pthread.h>
#include <stdio.h>
#include <sched.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <sys/time.h>
#define PTU 2000 // 2ms
#define HIGHEST_PRIORITY 99
typedef struct milcan_data
{
int sync ;
int slave_ID;
}milcan_data;
struct milcan_data milcan_arg;
void *slave_func(void *slave_data)
{
int j;
struct milcan_data * slave_signals= (struct milcan_data *)slave_data;
while(1)
{
usleep(2000000+(100*(slave_signals->slave_ID)));
slave_signals->sync++;
fprintf(stdout,"slave (%d) is talking the current sync is (%d) \n",slave_signals->slave_ID,slave_signals->sync);
}
return(NULL);
}
int main ()
{
int val,i,j;
int err;
struct sched_param param_processus;
struct sched_param param_slave_thr ;
pthread_attr_t slave_attr;
///////////////// AFFECTATION PRIORITE TEMPS REEL FIFO
param_processus.sched_priority = HIGHEST_PRIORITY;
if (sched_setscheduler(0, SCHED_FIFO, ¶m_processus) != 0)
{
perror("processus sched_setscheduler");
exit(EXIT_FAILURE);
}
///////////////// PREPARATION DES ATTRIBUTS DU SLAVE_THREAD
pthread_attr_init(&slave_attr);
if ((err=pthread_attr_setschedpolicy(&slave_attr,SCHED_RR))!=0)
{
fprintf(stderr,"setschedpolicy slave thread: %s\n",strerror(err));
exit(EXIT_FAILURE);
}
if ((err=pthread_attr_setinheritsched(&slave_attr,PTHREAD_EXPLICIT_SCHED))!=0)
{
fprintf(stderr,"setinheritshed slave thread: %s\n",strerror(err));
exit(EXIT_FAILURE);
}
param_slave_thr.sched_priority = HIGHEST_PRIORITY;
if ((err=pthread_attr_setschedparam(&slave_attr,¶m_slave_thr))!=0)
{
fprintf(stderr,"setschedparam slave thread: %s\n",strerror(err));
exit(EXIT_FAILURE);
}
///////////////////////////////////////////////////// PREPARATION MILCAN_DATA
pthread_t slave_thr[3];
milcan_arg.sync = 0;
///////////////////// EXECUTION DES THREAD
for ( i=0;i<3;i++)
{
milcan_arg.slave_ID=i+1;
if((err=pthread_create(&slave_thr[i],NULL,slave_func,&milcan_arg)!=0))
{
fprintf(stderr,"can't create slave thread %s \n",strerror(err));
exit(EXIT_FAILURE);
}
}
// ATTENTE DE TOUS LES THREAD
for ( i=0;i<3;i++)
{
pthread_join(slave_thr[i], NULL);
}
return 0;
} |
Partager