98 lines
No EOL
2.1 KiB
C
98 lines
No EOL
2.1 KiB
C
#include <signal.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <sys/time.h>
|
|
|
|
#define NB_ITR 100000
|
|
|
|
void handler_sigusr1(int sig) {
|
|
printf("PID %d a reçu un SIGUSR1 : %d\n", getpid(), sig);
|
|
exit(2);
|
|
}
|
|
|
|
void handler_sigchld(int sig) {
|
|
printf("PID %d a reçu un SIGCHLD : %d\n", getpid(), sig);
|
|
printf("Le fils est mort...\n");
|
|
}
|
|
|
|
int main() {
|
|
// Variables fork
|
|
int pid = 0;
|
|
|
|
// Variables gestion des signaux
|
|
struct sigaction action;
|
|
struct sigaction action2;
|
|
action.sa_handler = handler_sigusr1;
|
|
action2.sa_handler = handler_sigchld;
|
|
|
|
// Variables tubes
|
|
int p1[2];
|
|
int p2[2];
|
|
int tsfr = 0;
|
|
int tsfr2 = 0;
|
|
|
|
// Outils pour le chronometre
|
|
struct timeval begin, end;
|
|
|
|
// Initialisation des signaux
|
|
if (sigaction(SIGUSR1, &action, NULL)) {
|
|
printf("Erreur handler (SIGUSR1).\n");
|
|
exit(-1);
|
|
}
|
|
if (sigaction(SIGCHLD, &action2, NULL)) {
|
|
printf("Erreur handler (SIGCHLD).\n");
|
|
exit(-1);
|
|
}
|
|
|
|
// Initialisation des tubes
|
|
if (pipe(p1) || pipe(p2)) {
|
|
printf("Erreur pipe.\n");
|
|
exit(-1);
|
|
}
|
|
|
|
// Fork
|
|
switch (pid = fork()) {
|
|
case -1:
|
|
printf("Erreur fork.\n");
|
|
exit(-1);
|
|
break;
|
|
|
|
case 0:
|
|
printf("Le fils a correctement démarré (PID %d)\n", getpid());
|
|
|
|
// On lit, on incrémente et on renvoie
|
|
while (1) {
|
|
read(p1[0], &tsfr2, sizeof(tsfr2));
|
|
++tsfr2;
|
|
write(p2[1], &tsfr2, sizeof(tsfr2));
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
printf("Le père est ok (PID %d), le PID du fils est %d\n", getpid(), pid);
|
|
|
|
// Comme le fils, mais on chronometre
|
|
int nb_boucle = 0;
|
|
gettimeofday(&begin, NULL);
|
|
while (nb_boucle < NB_ITR) {
|
|
++tsfr;
|
|
write(p1[1], &tsfr, sizeof(tsfr));
|
|
read(p2[0], &tsfr, sizeof(tsfr));
|
|
nb_boucle++;
|
|
}
|
|
gettimeofday(&end, NULL);
|
|
kill(pid, SIGUSR1);
|
|
|
|
// On calcul le débit
|
|
int delta = ((end.tv_sec - begin.tv_sec)*1000000L + end.tv_usec) - begin.tv_usec;
|
|
int debit = (nb_boucle*2*sizeof(tsfr2)*8)/(delta); // (delta est déjà en us)
|
|
|
|
printf("Débit calculé : %d Mb/s\n", debit);
|
|
printf("[DEBUG] delta : %d us, tsfr : %d\n", delta, tsfr);
|
|
|
|
return debit;
|
|
break;
|
|
}
|
|
} |