cours_prog_systeme/TP/TP1/tube_nomme.c
2021-08-22 13:28:10 +02:00

110 satır
No EOL
2,4 KiB
C

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#define NB_ITR 1000
void utilisation() {
printf("Usage : ./a.out [c] [s]\n");
exit(-1);
}
void server() {
printf("Server mode (ping only)...\n");
// Variables tube nommé
int d_lect_1, d_ecr_1;
int d_lect_2, d_ecr_2;
printf("Création des tubes...");
if (mkfifo("/tmp/tube1", S_IRUSR | S_IWUSR | S_IRGRP)
|| mkfifo("/tmp/tube2", S_IRUSR | S_IWUSR | S_IRGRP)) {
printf("Erreur tube.\n");
exit(-1);
}
printf("OK\n");
printf("Ouverture des tubes...");
if (!(d_lect_1 = open("/tmp/tube1", O_RDONLY))){
printf("Erreur ouverture tube (READ).\n");
exit(-1);
}
printf("OK1...");
if (!(d_ecr_2 = open("/tmp/tube2", O_WRONLY))){
printf("Erreur ouverture tube (WRITE).\n");
exit(-1);
}
printf("OK2\n");
// On lit, on incrémente et on renvoie
int tsfr = 0;
while (1) {
read(d_lect_1, &tsfr, sizeof(tsfr));
++tsfr;
write(d_ecr_2, &tsfr, sizeof(tsfr));
}
}
void client() {
printf("Client mode...\n");
// Variables tube nommé
int d_lect_1, d_ecr_1;
int d_lect_2, d_ecr_2;
printf("Ouverture des tubes...\n");
if (!(d_ecr_1 = open("/tmp/tube1", O_WRONLY))){
printf("Erreur ouverture tube (WRITE).\n");
exit(-1);
}
if (!(d_lect_2 = open("/tmp/tube2", O_RDONLY))){
printf("Erreur ouverture tube (READ).\n");
exit(-1);
}
printf("Tubes ouverts.\n");
// Outils pour le chronometre
struct timeval begin, end;
// Ping pong
int tsfr = 0;
int nb_boucle = 0;
printf("Lancement du chronometre.\n");
gettimeofday(&begin, NULL);
while (nb_boucle < NB_ITR) {
++tsfr;
write(d_ecr_1, &tsfr, sizeof(tsfr));
read(d_lect_2, &tsfr, sizeof(tsfr));
nb_boucle++;
}
gettimeofday(&end, NULL);
// 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(tsfr) * 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);
}
int main(int argc, char **argv) {
// Arguments parsing
if (argc != 2) {
utilisation();
}
if (*argv[1] == 's') {
server();
} else if (*argv[1] == 'c') {
client();
} else {
utilisation();
}
}