0% found this document useful (0 votes)
15 views10 pages

TP Res

The document contains multiple C programs that perform various tasks including mathematical calculations, matrix creation and display, file splitting and joining, and multithreading operations. Each program has its own usage instructions and handles input parameters for specific functionalities. The programs demonstrate the use of functions, file I/O, and synchronization mechanisms in C.

Uploaded by

Samsidine Dieme
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views10 pages

TP Res

The document contains multiple C programs that perform various tasks including mathematical calculations, matrix creation and display, file splitting and joining, and multithreading operations. Each program has its own usage instructions and handles input parameters for specific functionalities. The programs demonstrate the use of functions, file I/O, and synchronization mechanisms in C.

Uploaded by

Samsidine Dieme
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

void usage(const char *prog_name) {


printf("Usage:\n");
printf(" %s -f <entier> : Calcule 2^n = f(n)\n", prog_name);
printf(" %s -g <entier> : Calcule 2*n = g(n)\n", prog_name);
printf(" %s -f <entier> -g: Calcule f(g(n))\n", prog_name);
printf(" %s -g <entier> -f: Calcule g(f(n))\n", prog_name);
exit(1);
}
int f(int n) {
return (int)pow(2, n);
}

int g(int n) {
return 2 * n;
}

int main(int argc, char *argv[]) {


if (argc < 3) {
usage(argv[0]);
}
int value = 0;
int result = 0;
int has_f = 0, has_g = 0;

for (int i = 1; i < argc; i++) {


if (strcmp(argv[i], "-f") == 0) {
if (i + 1 >= argc) {
printf("Erreur : option -f requiert un entier.\n");
return 1;
}
value = atoi(argv[++i]);
if (has_g) {

result = f(g(value));
printf("Résultat de f(g(%d)) = %d\n", value, result);
return 0;
}
has_f = 1;
} else if (strcmp(argv[i], "-g") == 0) {
if (i + 1 >= argc && !has_f) {
printf("Erreur : option -g requiert un entier.\n");
return 1;
}
if (!has_f) {
value = atoi(argv[++i]);
}
if (has_f) {

result = g(f(value));
printf("Résultat de g(f(%d)) = %d\n", value, result);
return 0;
}
has_g = 1;
} else {
printf("Erreur : argument invalide '%s'.\n", argv[i]);
usage(argv[0]);
}
}

if (has_f) {
result = f(value);
printf("Résultat de f(%d) = %d\n", value, result);
} else if (has_g) {
result = g(value);
printf("Résultat de g(%d) = %d\n", value, result);
}

return 0;
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

void usage(const char *prog_name) {


printf("Usage:\n");
printf(" %s -c -d <dimension> -f <nomfichier> [-t]\n", prog_name);
printf(" %s -a -d <dimension> -f <nomfichier> [-t]\n", prog_name);
exit(1);
}
void creer_matice(const char *filename, int dimension, int is_text_mode) {
FILE *file = fopen(filename, is_text_mode ? "w" : "wb");
if (!file) {
perror("Erreur lors de l'ouverture du fichier");
exit(1);
}

srand(time(NULL));

for (int i = 0; i < dimension; i++) {


for (int j = 0; j < dimension; j++) {
int value = rand() % 100;
if (is_text_mode) {
fprintf(file, "%d ", value);
} else {
fwrite(&value, sizeof(int), 1, file);
}
}
if (is_text_mode) {
fprintf(file, "\n");
}
}

fclose(file);
printf("Matrice générée et enregistrée dans '%s'.\n", filename);
}
void affiche_matice(const char *filename, int dimension, int is_text_mode) {
FILE *file = fopen(filename, is_text_mode ? "r" : "rb");
if (!file) {
perror("Erreur lors de l'ouverture du fichier");
exit(1);
}

printf("Contenu de la matrice dans le fichier '%s' :\n", filename);

for (int i = 0; i < dimension; i++) {


for (int j = 0; j < dimension; j++) {
int value;
if (is_text_mode) {
fscanf(file, "%d", &value);
} else {
fread(&value, sizeof(int), 1, file);
}
printf("%4d ", value);
}
printf("\n");
}

fclose(file);
}

int main(int argc, char *argv[]) {


if (argc < 5) {
usage(argv[0]);
}

int create = 0, display = 0, dimension = 0, is_text_mode = 0;


char filename[256] = {0};

for (int i = 1; i < argc; i++) {


if (strcmp(argv[i], "-c") == 0) {
create = 1;
} else if (strcmp(argv[i], "-a") == 0) {
display = 1;
} else if (strcmp(argv[i], "-d") == 0) {
if (i + 1 >= argc) {
printf("Erreur : option -d requiert un entier.\n");
return 1;
}

dimension = atoi(argv[++i]);
if (dimension <= 0) {
printf("Erreur : la dimension doit être un entier positif.\n");
return 1;
}
} else if (strcmp(argv[i], "-t") == 0) {
is_text_mode = 1;
} else if (strcmp(argv[i], "-f") == 0) {
if (i + 1 >= argc) {
printf("Erreur : option -f requiert un nom de fichier.\n");
return 1;
}
strncpy(filename, argv[++i], sizeof(filename) - 1);
} else {
printf("Erreur : argument invalide '%s'.\n", argv[i]);
usage(argv[0]);
}
}

if (create && display) {


printf("Erreur : Les options -c et -a ne peuvent pas être utilisées
ensemble.\n");
usage(argv[0]);
}
if (!create && !display) {
printf("Erreur : Une des options -c ou -a doit être spécifiée.\n");
usage(argv[0]);
}

if (dimension == 0) {
printf("Erreur : La dimension de la matrice doit être spécifiée avec -d.\
n");
usage(argv[0]);
}

if (strlen(filename) == 0) {
printf("Erreur : Un nom de fichier doit être spécifié avec -f.\n");
usage(argv[0]);
}

if (create) {
creer_matice(filename, dimension, is_text_mode);
} else if (display) {
affiche_matice(filename, dimension, is_text_mode);
}

return 0;
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFFER_SIZE 1024

void split_file(const char *filename, int chunk_size) {


FILE *file = fopen(filename, "rb");
if (!file) {
perror("Erreur lors de l'ouverture du fichier");
exit(1);
}

char chunk_name[256];
int part_number = 1;
size_t bytes_read;
char buffer[BUFFER_SIZE];
while (!feof(file)) {
snprintf(chunk_name, sizeof(chunk_name), "%[Link]%d", filename,
part_number);
FILE *chunk_file = fopen(chunk_name, "wb");
if (!chunk_file) {
perror("Erreur lors de la création d'un fichier de partie");
fclose(file);
exit(1);
}

int bytes_written = 0;
while (bytes_written < chunk_size && (bytes_read = fread(buffer, 1,
BUFFER_SIZE, file)) > 0) {
fwrite(buffer, 1, bytes_read, chunk_file);
bytes_written += bytes_read;
}

fclose(chunk_file);
printf("Créé : %s\n", chunk_name);
part_number++;
}

fclose(file);
printf("Découpage terminé.\n");
}

void joindre_file(const char *output_filename, int num_parts, char **part_files) {


FILE *output_file = fopen(output_filename, "wb");
if (!output_file) {
perror("Erreur lors de l'ouverture du fichier de sortie");
exit(1);
}

char buffer[BUFFER_SIZE];
size_t bytes_read;

for (int i = 0; i < num_parts; i++) {


FILE *part_file = fopen(part_files[i], "rb");
if (!part_file) {
perror("Erreur lors de l'ouverture d'un fichier de partie");
fclose(output_file);
exit(1);
}

while ((bytes_read = fread(buffer, 1, BUFFER_SIZE, part_file)) > 0) {


fwrite(buffer, 1, bytes_read, output_file);
}

fclose(part_file);
printf("Joint : %s\n", part_files[i]);
}

fclose(output_file);
printf("Assemblage terminé.\n");
}

int main(int argc, char *argv[]) {


if (argc < 3) {
printf("Usage pour découper : %s nomfichier taille\n", argv[0]);
printf("Usage pour joindre : %s nomfichier_sortie part1 part2 ...\n",
argv[0]);
return 1;
}

if (strcmp(argv[1], "split") == 0) {
if (argc != 4) {
printf("Usage : %s split nomfichier taille\n", argv[0]);
return 1;
}

const char *filename = argv[2];


int chunk_size = atoi(argv[3]);

if (chunk_size <= 0) {
printf("Erreur : La taille doit être un entier positif.\n");
return 1;
}

split_file(filename, chunk_size);
} else if (strcmp(argv[1], "join") == 0) {
if (argc < 4) {
printf("Usage : %s join nomfichier_sortie part1 part2 ...\n", argv[0]);
return 1;
}

const char *output_filename = argv[2];


int num_parts = argc - 3;
char **part_files = &argv[3];

joindre_file(output_filename, num_parts, part_files);


} else {
printf("Commande invalide. Utilisez 'split' ou 'join'.\n");
return 1;
}

return 0;
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>

int n;
int courant = 0;
sem_t sem_thread1;
sem_t sem_thread2;
FILE *output_file;

void *thread1_function(void *arg) {


for (int i = 1; i <= n; i++) {
sem_wait(&sem_thread1);
courant++;
fprintf(output_file, "%d\n", courant);
printf("Thread 1: %d\n", courant);
sem_post(&sem_thread2);
}
return NULL;
}

void *thread2_function(void *arg) {


for (int i = 1; i <= n; i++) {
sem_wait(&sem_thread2);
courant--;
fprintf(output_file, "%d\n", courant);
printf("Thread 2: %d\n", courant);
sem_post(&sem_thread1);
}
return NULL;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s <n>\n", argv[0]);
return 1;
}

n = atoi(argv[1]);
if (n <= 0) {
printf("Erreur : n doit être un entier positif.\n");
return 1;
}

output_file = fopen("[Link]", "w");


if (!output_file) {
perror("Erreur lors de l'ouverture du fichier");
return 1;
}

sem_init(&sem_thread1, 0, 1);
sem_init(&sem_thread2, 0, 0);

pthread_t thread1, thread2;


pthread_create(&thread1, NULL, thread1_function, NULL);
pthread_create(&thread2, NULL, thread2_function, NULL);

pthread_join(thread1, NULL);
pthread_join(thread2, NULL);

fclose(output_file);
sem_destroy(&sem_thread1);
sem_destroy(&sem_thread2);

printf("Les résultats ont été enregistrés dans '[Link]'.\n");


printf("Vous pouvez tracer la courbe avec un outil comme Python ou gnuplot.\
n");
return 0;
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

#define MAX_THREADS 100

int n;
int thread_courant = 0;
pthread_mutex_t mutex;
pthread_cond_t cond_var;

void *thread_function(void *arg) {


int thread_id = *(int *)arg;

while (1) {
pthread_mutex_lock(&mutex);

while (thread_courant != thread_id) {


pthread_cond_wait(&cond_var, &mutex);
}

printf("Thread %d s'exécute.\n", thread_id + 1);

thread_courant = (thread_courant + 1) % n;

pthread_cond_broadcast(&cond_var);

pthread_mutex_unlock(&mutex);

usleep(500000);
}

return NULL;
}

int main(int argc, char *argv[]) {


if (argc != 2) {
printf("Usage: %s <nombre de threads>\n", argv[0]);
return 1;
}

n = atoi(argv[1]);
if (n <= 0 || n > MAX_THREADS) {
printf("Erreur : Le nombre de threads doit être entre 1 et %d.\n",
MAX_THREADS);
return 1;
}
pthread_t threads[MAX_THREADS];
int thread_ids[MAX_THREADS];
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond_var, NULL);

for (int i = 0; i < n; i++) {


thread_ids[i] = i;
pthread_create(&threads[i], NULL, thread_function, &thread_ids[i]);
}

for (int i = 0; i < n; i++) {


pthread_join(threads[i], NULL);
}

pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond_var);

return 0;
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

EXERCICE_1:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>

extern char*optarg;
extern int optopt;

void f(int n) {
printf("Résultat de f(%d) = %.0f\n", n, pow(2, n));
}

void g(int n) {
printf("Résultat de g(%d) = %d\n", n, 2 * n);
}

int main(int argc, char *argv[]) {


int opt;
int f_value = -1, g_value = -1;

while ((opt = getopt(argc, argv, "f:g:")) != -1) {


switch (opt) {
case 'f':
f_value = atoi(optarg);
break;
case 'g':
g_value = atoi(optarg);
break;
default:

fprintf(stderr, "Usage: %s [-f entier] [-g entier]\n", argv[0]);


exit(EXIT_FAILURE);
}
}

if (f_value != -1 && g_value != -1) {

printf("Résultat de fog(%d) = %.0f\n", g_value, pow(2, 2 * g_value));


} else if (f_value != -1) {
f(f_value);
} else if (g_value != -1) {
g(g_value);
} else {
fprintf(stderr, "Aucune option valide n'a été spécifiée.\n");
fprintf(stderr, "Usage: %s [-f entier] [-g entier]\n", argv[0]);
exit(EXIT_FAILURE);
}

return 0;
}

You might also like