Skip to content
Extraits de code Groupes Projets
Valider 7c8f6624 rédigé par Laurent Paucot's avatar Laurent Paucot
Parcourir les fichiers

no more leaks

parent 6bb3d269
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Pipeline #7900 réussi
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <math.h>
int N;
//travailler avec des linkendList
typedef struct node{
unsigned long long value;
struct node *next;
}node_t;
typedef struct queue{
struct node *tail;
int size;
int final;
}queue_t;
struct buffer_rc{
char **tab;
int size;
int len;
int head;
int tail;
int stop;
FILE *file1;
};
struct buffer_cw
{
queue_t **tab;
int size;
int len;
int head;
int tail;
int stop;
FILE *file2;
};
struct buffer_rccw{
struct buffer_rc *struct1;
struct buffer_cw *struct2;
};
/* sémaphore est une structure de donné qui contient :
*un entier qui stocke la valeur, positive ou nulle du sémaphore.
*une queue qui contient les pointeurs vers les threads
*/
pthread_mutex_t mutex1;
sem_t empty1;
sem_t full1;
pthread_mutex_t mutex2;
sem_t empty2;
sem_t full2;
#include "run.h"
void put_in_buffer_1(char *c, struct buffer_rc *ptr){ //ajouter un element dans le buffer 1
......@@ -83,7 +24,6 @@ char *get_from_buffer_1(struct buffer_rc *ptr){ // chercher dans le buffer 1
}
void put_in_buffer_2(struct queue *ptr, struct buffer_cw *buf){ //ajouter un element dans le buffer 2
buf->tab[buf->head] = malloc(sizeof(queue_t*));
buf->tab[buf->head] = ptr;
buf->len++; //augmente l'espace occupé dans le buffer
buf->head = (buf->head + 1)%buf->size;
......@@ -126,28 +66,21 @@ void enqueue(queue_t* q, unsigned long long val){
struct node *ptr;
ptr = malloc(sizeof(node_t));
if (ptr == NULL){
free(ptr);
//free(ptr);
return;}
ptr->value = val;
ptr->next = malloc(sizeof(node_t));
if (ptr->next == NULL){
free(ptr);
return;
}
if (q->size == 0)
{
*q->tail = *ptr;
*ptr->next = *ptr;
q->tail = ptr;
ptr->next = ptr;
}
else{
*ptr->next = *q->tail->next;
*q->tail->next = *ptr;
ptr->next = q->tail->next;
q->tail->next = ptr;
}
q->size++;
free(ptr);
}
......@@ -160,7 +93,6 @@ queue_t* prime_divs(unsigned long long number){
return NULL;}
ptr->size = 0;
ptr->final = 0;
ptr->tail = malloc(sizeof(node_t));
for (unsigned long long i = 2; i <= number/2; i++){
if (is_div(number, i) && is_prime(i) == 1){
......@@ -190,13 +122,8 @@ void *writing(void *param){
{
if (stop == N-1){
for (int i = 0; i < param1->size; i++)
{
param1->tab[i] = NULL;
}
free(param1->tab);
free(pr_divs->tail);
free(pr_divs);
free(param1->tab);
return NULL;
}
free(pr_divs);
......@@ -206,17 +133,18 @@ void *writing(void *param){
FILE *file2 = param1->file2;
node_t *current;
current = malloc(sizeof(node_t));
if (current == NULL){return NULL;}
*current = *pr_divs->tail;
current = pr_divs->tail;
current = current->next;
fprintf(file2,"%llu",current->value);
node_t *toFree = current;
current = current->next;
for (int i = 1; i < pr_divs->size; i++)
{
free(toFree);
toFree = current;
fprintf(file2," %llu",current->value);
current = current->next;
}
......@@ -224,7 +152,6 @@ void *writing(void *param){
free(pr_divs->tail);
free(pr_divs);
free(current);
}
......
#ifndef RUN
#define RUN
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <math.h>
#include <pthread.h>
#include <semaphore.h>
int N;
typedef struct node{
int N;
//travailler avec des linkendList
typedef struct node{
unsigned long long value;
struct node *next;
}node_t;
typedef struct queue{
typedef struct queue{
struct node *tail;
int size;
int final;
}queue_t;
struct buffer_rc{
......@@ -21,8 +27,11 @@ struct buffer_rc{
int len;
int head;
int tail;
int stop;
FILE *file1;
};
struct buffer_cw
{
queue_t **tab;
......@@ -30,6 +39,8 @@ struct buffer_cw
int len;
int head;
int tail;
int stop;
FILE *file2;
};
struct buffer_rccw{
......@@ -37,6 +48,10 @@ struct buffer_rccw{
struct buffer_cw *struct2;
};
/* sémaphore est une structure de donné qui contient :
*un entier qui stocke la valeur, positive ou nulle du sémaphore.
*une queue qui contient les pointeurs vers les threads
*/
pthread_mutex_t mutex1;
sem_t empty1;
sem_t full1;
......@@ -46,10 +61,10 @@ sem_t empty2;
sem_t full2;
void put_in_buffer_1(char *c);
char *get_from_buffer_1(void);
void put_in_buffer_2(struct queue *ptr);
queue_t *get_from_buffer_2(void);
void put_in_buffer_1(char *c, struct buffer_rc *ptr);
char *get_from_buffer_1(struct buffer_rc *ptr);
void put_in_buffer_2(struct queue *ptr, struct buffer_cw *buf);
queue_t *get_from_buffer_2(struct buffer_cw *buf);
int is_div(unsigned long long number, unsigned long long i);
int is_prime(unsigned long long number);
......@@ -71,6 +86,4 @@ struct buffer_rccw *buff_init_12(struct buffer_rc *ptr1,struct buffer_cw *ptr2);
void thread_create_join(struct buffer_rc *ptr1,struct buffer_cw *ptr2,struct buffer_rccw *ptr3);
void mut_sem_destroy(void);
int run (char *input,char *output,int n_threads);
#endif
\ No newline at end of file
int run (char *input,char *output,int n_threads);
\ No newline at end of file
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter