diff --git a/tp/tp5/README.md b/tp/tp5/README.md index 89b7dd1..afd2621 100644 --- a/tp/tp5/README.md +++ b/tp/tp5/README.md @@ -1,6 +1,6 @@ # Signaux - partie 1 : [Parallèlisme avec processus](parallelisme.md) -- partie 2 : [Signaux](ordonnancement.md) +- partie 2 : [Signaux](signaux.md) diff --git a/tp/tp5/signaux.md b/tp/tp5/signaux.md new file mode 100644 index 0000000..c8e0542 --- /dev/null +++ b/tp/tp5/signaux.md @@ -0,0 +1,12 @@ +# Signaux + +- partie 1 : [Parallèlisme avec processus](parallelisme.md) +- partie 2 : [Signaux](ordonnancement.md) + + +#### Ex1 +Le programme [prime.c](src/) permet de générer le n-ième nombre premier. +Le but est d'afficher une barre de progression pendant le calcul. + +Complétez le programme en utilisant le signal `SIGALARM` et la fonction `setitimer` pour afficher la +barre de progression. diff --git a/tp/tp5/src/helpers.c b/tp/tp5/src/helpers.c new file mode 100644 index 0000000..5a6e87a --- /dev/null +++ b/tp/tp5/src/helpers.c @@ -0,0 +1,59 @@ +#include +#include +#include "helpers.h" +#include +size_t entier_vers_chaine(long valeur, char *tampon) +{ + char chiffres[24]; + int i = 0; + unsigned long v = (unsigned long)valeur; + if (v == 0) chiffres[i++] = '0'; + while (v > 0) { chiffres[i++] = (char)('0' + (v % 10)); v /= 10; } + size_t pos = 0; + while (i > 0) tampon[pos++] = chiffres[--i]; + return pos; +} + +void dessiner_barre(long p, long dp,long cible,long width) { + char ligne[128]; + size_t pos = 0; + + long pourcent = (p * 100) / cible; + long rempli = (p * width) / cible; + + ligne[pos++] = '\r'; + ligne[pos++] = '['; + for (long i = 0; i < width; i++) + ligne[pos++] = (i < rempli) ? '#' : '-'; + ligne[pos++] = ']'; + ligne[pos++] = ' '; + pos += entier_vers_chaine(pourcent, ligne + pos); + ligne[pos++] = '%'; + ligne[pos++] = ' '; + ligne[pos++] = '('; + pos += entier_vers_chaine(p, ligne + pos); + ligne[pos++] = '/'; + pos += entier_vers_chaine(cible, ligne + pos); + ligne[pos++] = ')'; + ligne[pos++] = ' '; + ligne[pos++] = 'd'; + ligne[pos++] = 'e'; + ligne[pos++] = 'r'; + ligne[pos++] = ':'; + ligne[pos++] = ' '; + pos += entier_vers_chaine(dp, ligne + pos); + ligne[pos++] = ' '; /* espace final : efface un residu si la ligne raccourcit */ + + ssize_t r = write(STDOUT_FILENO, ligne, pos); + (void)r; +} + +int est_premier(uint64_t n) { + if (n < 2) return 0; + if (n % 2 == 0) return n == 2; + for (uint64_t d = 3; d * d <= n; d += 2) + if (n % d == 0) return 0; + return 1; +} + + diff --git a/tp/tp5/src/helpers.h b/tp/tp5/src/helpers.h new file mode 100644 index 0000000..afd82e8 --- /dev/null +++ b/tp/tp5/src/helpers.h @@ -0,0 +1,8 @@ +#ifndef _HELPERS_H +#define _HELPERS_H +#include +size_t entier_vers_chaine(long valeur, char *tampon); +int est_premier(uint64_t n); +void dessiner_barre(long p, long dp,long cible,long width); + +#endif diff --git a/tp/tp5/src/prime.c b/tp/tp5/src/prime.c new file mode 100644 index 0000000..b3ee5f6 --- /dev/null +++ b/tp/tp5/src/prime.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "helpers.h" + +#define N_CIBLE 1000000UL /* on cherche le N_CIBLE-ieme nombre premier */ +#define LARGEUR_BARRE 50 + +static volatile sig_atomic_t compte_trouve = 0; +static volatile sig_atomic_t dernier_premier = 0; + +static void handler_alarm(int sig) +{ + dessiner_barre(compte_trouve, dernier_premier,N_CIBLE,LARGEUR_BARRE); +} + +int main(void) { + struct sigaction sa; + struct itimerval it; + uint64_t candidat = 1, compte = 0; + + while (compte < N_CIBLE) { + candidat++; + if (est_premier(candidat)) { + compte++; + compte_trouve = (sig_atomic_t)compte; + dernier_premier = (sig_atomic_t)candidat; + } + } + dessiner_barre((long)compte, (long)candidat,N_CIBLE,LARGEUR_BARRE); + printf("\nLe %lu-ieme nombre premier est %lu.\n", (unsigned long)N_CIBLE, (unsigned long)candidat); + return 0; +}