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
| #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 NBR_CYCLE 5
#define NBR_TRAME 16
#define PTU 2000 // 2ms
#define HIGHEST_PRIORITY 99
////////////////////////////////////////////////////////////////////
typedef struct signaux_thread
{
int sync ;
char *(*schedule_table)[NBR_TRAME];
}signaux_thread;
//////////////////// THREAD GESTIONNAIRE
void *sync_sig(void *arg)
{
struct signaux_thread * signal_ptu= (struct signaux_thread *)arg;
signal_ptu->sync++;
}
//////////////////FONCTION DU THREAD
void *func_thread (void *arg)
{
int j;
struct signaux_thread * signal= (struct signaux_thread *)arg;
fprintf(stderr,"sync frame number: %d\n ",signal->sync);
for (j=0;j<NBR_TRAME;j++)
{
fprintf(stderr,"la trame %s est envoyée\n",signal->schedule_table[signal->sync][j]);
}
}
/////////////////////////// MAIN
int main(void)
{
int i,j;
int err;
pid_t pid;
pthread_attr_t attr;
struct sched_param param_processus;
struct sched_param param_thread ;
pthread_t thread;
char *ScheduleTable[NBR_CYCLE][NBR_TRAME];
struct signaux_thread arg;
struct timeval elapsed_time ;
struct timeval chrono;
int priority ;
struct sigevent ptu_event;
struct itimerspec ptu_period;
timer_t ptu_timer;
///////initialisation du elapsed time
gettimeofday(&elapsed_time,NULL);
chrono=elapsed_time;
fprintf(stdout,"elapsed time= %d \n",elapsed_time.tv_usec-chrono.tv_usec);
//////////////// AFFECTATION PRIORITE TEMPS REEL FIFO
param_processus.sched_priority = HIGHEST_PRIORITY;
if (sched_setscheduler(0, SCHED_FIFO, ¶m_processus) != 0)
{
perror("sched_setscheduler");
exit(EXIT_FAILURE);
}
///////////////// AFFECTATION PRIORITE THREAD
pthread_attr_init(&attr);
if ((err=pthread_attr_setschedpolicy(&attr,SCHED_FIFO))!=0)
{
fprintf(stderr,"setschedpolicy: %s\n",strerror(err));
exit(EXIT_FAILURE);
}
if ((err=pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED))!=0)
{
fprintf(stderr,"setinheritshed: %s\n",strerror(err));
exit(EXIT_FAILURE);
}
param_thread.sched_priority=HIGHEST_PRIORITY;
if ((err=pthread_attr_setschedparam(&attr,¶m_thread))!=0)
{
fprintf(stderr,"setschedparam: %s\n",strerror(err));
exit(EXIT_FAILURE);
}
/////////////////////////////////////////////////////
arg.sync = 0;
arg.schedule_table = ScheduleTable;
priority=sched_getscheduler(0);
switch (priority)
{
case SCHED_FIFO :
fprintf(stdout,"FIFO PRIORITY IS : %d \n",param_processus.sched_priority);
break;
case SCHED_RR :
fprintf(stdout,"ROUND ROBIN PRIORITY IS : %d \n",param_processus.sched_priority);
break;
default:
fprintf(stdout,"OTHER PRIORITY USED \n");
break;
}
//////////// REMPLISSAGE SCHEDULE TABLE
for ( i=0;i<NBR_CYCLE;i++)
{
for ( j=0;j<NBR_TRAME;j++)
{
ScheduleTable[i][j]=(char*)malloc(3 * sizeof(char));
sprintf(ScheduleTable[i][j],"%d.%d",i,j);
}
}
///////////////////// CREER UN SIGNAL
signal(SIGRTMIN,sync_sig);
ptu_event.sigev_notify=SIGEV_SIGNAL;
ptu_event.sigev_signo=SIGRTMIN;
if(timer_create(CLOCK_REALTIME,&ptu_event,&ptu_timer)!=0)
{
perror("timer_create");
exit(EXIT_FAILURE);
}
PTU*=1000;
ptu_period.it_value.tv_sec=PTU/1000000000;
ptu_period.it_value.tv_nsec=PTU%1000000000;
ptu_period.it_interval=ptu_period.it_value;
if(timer_settime(ptu_timer,0,&ptu_period,NULL)!=0)
{
perror("timer_settime");
exit(EXIT_FAILURE);
}
///////////////////// EXECUTION DU THREAD
while(1)
{
if((err=pthread_create(&thread,&attr,func_thread,&arg)!=0))
{
fprintf(stderr,"can't create thread %s \n",strerror(err));
exit(EXIT_FAILURE);
}
chrono=elapsed_time;
gettimeofday(&elapsed_time,NULL);
fprintf(stdout,"elapsed time= %d \n",((elapsed_time.tv_usec-chrono.tv_usec)/1000));
// usleep(PTU);
// arg.sync++;
if (arg.sync==NBR_CYCLE)
{arg.sync=0;}
pause();
return EXIT_SUCCESS;
}
} |
Partager