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
| #include <pthread.h>
#include <stdio.h>
#include <sched.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#define NBR_CYCLE 10
#define NBR_TRAME 2
#define PTU 1
#define HIGHEST_PRIORITY 99;
////////////////////////////////////////////////////////////////////
typedef struct signaux_thread
{
int sync ;
char *(*schedule_table)[NBR_TRAME];
}signaux_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]);
}
}
///////////////////////////////////////////////////////////////////
int main(void)
{
int i,j;
int err;
pthread_attr_t attr;
struct sched_param param ;
pthread_t thread;
char *ScheduleTable[NBR_CYCLE][NBR_TRAME];
struct signaux_thread arg;
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.sched_priority=HIGHEST_PRIORITY;
if ((err=pthread_attr_setschedparam(&attr,¶m))!=0)
{
fprintf(stderr,"setschedparam: %s\n",strerror(err));
exit(EXIT_FAILURE);
}
arg.sync = 0;
arg.schedule_table = ScheduleTable;
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);
}
}
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);
}
sleep(PTU);
arg.sync++;
if (arg.sync==NBR_CYCLE)
{arg.sync=0;}
}
} |
Partager