signaux
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
||||
# Signaux
|
||||
|
||||
- partie 1 : [Parallèlisme avec processus](parallelisme.md)
|
||||
- partie 2 : [Signaux](ordonnancement.md)
|
||||
- partie 2 : [Signaux](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.
|
||||
@@ -0,0 +1,59 @@
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include "helpers.h"
|
||||
#include <stdint.h>
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
#ifndef _HELPERS_H
|
||||
#define _HELPERS_H
|
||||
#include <stdint.h>
|
||||
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
|
||||
@@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#include <stdint.h>
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user