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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
| #define _GNU_SOURCE
#include <pthread.h>
#include <stdio.h>
#include <sched.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <semaphore.h>
#include <sys/syscall.h>
//////////////////////
#define sigev_notify_function _sigev_un._sigev_thread._function
#define sigev_notify_attributes _sigev_un._sigev_thread._attribute
#define sigev_notify_thread_id _sigev_un._tid
//////////////////////
#define PERIOD_SIG SIGRTMIN
#define NBR_CYCLE 20
#define NBR_TRAME 1
#define PTU_NS 2000000 // 2ms
#define HIGHEST_PRIORITY 99
/////////////////// CREATION DES SEMAPHORES
sem_t sync_pass, slave_pass;
//////////////////Gobals
int master_ID;
int sync_sig=-1;
int nodes_nbr=1;
struct timeval last;
///////////////////Function qui retourne le TID
static pid_t mygettid(void)
{
return syscall(SYS_gettid);
}
/////////////////// MASTER_FUNC
void * master_func()
{
///
struct timeval begin;
struct timeval last;
long long int difference = 0;
////
int global_cycle = 0 ;
//////////Creer un timer de 2 ms
struct itimerspec ptu_period;
timer_t ptu_timer;
sigset_t mask;
struct sigevent ptu_event = {
.sigev_notify = SIGEV_THREAD_ID,
.sigev_signo = PERIOD_SIG,
.sigev_notify_thread_id = mygettid(),
};
/* Bloque le signal PERIOD_SIG pour le thread appelant uniquement */
if (sigemptyset(&mask) ||
sigaddset(&mask, PERIOD_SIG) ||
sigprocmask(SIG_BLOCK, &mask, NULL))
pthread_exit((void *) 10);
siginfo_t si;
int sig;
if (timer_create(CLOCK_REALTIME, &ptu_event, &ptu_timer))
pthread_exit((void *) 11);
ptu_period.it_value.tv_sec = 0;
ptu_period.it_value.tv_nsec = PTU_NS;
ptu_period.it_interval.tv_sec = 0;
ptu_period.it_interval.tv_nsec = PTU_NS;
if (timer_settime(ptu_timer, 0, &ptu_period, NULL))
pthread_exit((void *) 12);
/////////////////////////////
while(1)
{
if ((sig = sigwaitinfo(&mask, &si)) < 0)
pthread_exit((void *) 13);
if (sig != PERIOD_SIG)
continue;
///////////////////////////// Tache a repeter chaque 2 ms
gettimeofday(&begin, NULL);
difference += 1000000 * (begin.tv_sec - last.tv_sec);
difference += (begin.tv_usec - last.tv_usec);
last=begin;
sem_wait(&sync_pass);
sync_sig++;
sync_sig %= NBR_CYCLE;
if (sync_sig==0)
{
global_cycle=global_cycle+1;
fprintf(stdout,"New cycle = %d\n", global_cycle);
}
fprintf(stdout,"The Master is talking (%d) elapsed time (%6lld) \n",sync_sig,difference);
difference = 0;
sem_post(&sync_pass);
sched_yield();
}
return(0);
}
///////////////////// SLAVE_FUNC
void *slave_func(void *slave_data)
{
while(1)
{
sem_wait(&sync_pass);
sem_wait(&slave_pass);
fprintf(stdout,"slave (%lu) is talking the current sync is (%d) \n",pthread_self(),sync_sig);
sem_post(&slave_pass);
sem_post(&sync_pass);
pause();
}
return(0);
}
////////////////MAIN
int main ()
{
int val,i,j;
int err,priority;
struct sched_param param_processus;
struct sched_param param_master_thr , param_slave_thr ;
pthread_attr_t master_attr , slave_attr;
pthread_t master_thr, slave_thr[nodes_nbr];
///////////////// 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);
}
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;
}
///////////////// PREPARATION DES ATTRIBUTS DU MASTER_THREAD
pthread_attr_init(&master_attr);
if ((err=pthread_attr_setschedpolicy(&master_attr,SCHED_FIFO))!=0)
{
fprintf(stderr,"setschedpolicy master thread: %s\n",strerror(err));
exit(EXIT_FAILURE);
}
if ((err=pthread_attr_setinheritsched(&master_attr,PTHREAD_EXPLICIT_SCHED))!=0)
{
fprintf(stderr,"setinheritshed master thread: %s\n",strerror(err));
exit(EXIT_FAILURE);
}
param_master_thr.sched_priority = 99;
if ((err=pthread_attr_setschedparam(&master_attr,¶m_master_thr))!=0)
{
fprintf(stderr,"setschedparam master thread: %s\n",strerror(err));
exit(EXIT_FAILURE);
}
///////////////// PREPARATION DES ATTRIBUTS DU SLAVE_THREAD
pthread_attr_init(&slave_attr);
if ((err=pthread_attr_setschedpolicy(&slave_attr,SCHED_FIFO))!=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 = 50;
if ((err=pthread_attr_setschedparam(&slave_attr,¶m_slave_thr))!=0)
{
fprintf(stderr,"setschedparam slave thread: %s\n",strerror(err));
exit(EXIT_FAILURE);
}
////////////////// INITIALISATION DES SEMAPHORES
if (sem_init(&sync_pass,0,1)==-1)
{
perror("sync sem init");
exit (EXIT_FAILURE);
}
if (sem_init(&slave_pass,0,nodes_nbr)==-1)
{
perror("slave sem init");
exit (EXIT_FAILURE);
}
///
gettimeofday(&last, NULL);
///////////////////// EXECUTION DES THREAD
if((err=pthread_create(&master_thr,&master_attr,master_func,NULL)!=0))
{
fprintf(stderr,"can't create master thread %s \n",strerror(err));
exit(EXIT_FAILURE);
}
for ( i=0;i<nodes_nbr;i++)
{
if((err=pthread_create(&slave_thr[i],&slave_attr,slave_func,NULL)!=0))
{
fprintf(stderr,"can't create slave thread %s \n",strerror(err));
exit(EXIT_FAILURE);
}
}
//
/// ATTENTE DE TOUS LES THREAD
pthread_join(master_thr, NULL);
for ( i=0;i<nodes_nbr;i++)
{
pthread_join(slave_thr[i], NULL);
}
return (0);
} |
Partager