Newer
Older
//travailler avec des linkendList
typedef struct node{
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;
void put_in_buffer_1(char *c, struct buffer_rc *ptr){ //ajouter un element dans le buffer 1
ptr->tab[ptr->head] = malloc(sizeof(c)); // on stoxke de espace
//ptr->tab[ptr->head] = c;
strcpy(ptr->tab[ptr->head],c); //copie
ptr->head = (ptr->head + 1)%ptr->size; //chaque fois qu'un élément est ajouté,
//on augmente le pointeur de 1 mais l'espace du buffer est de size donc faire un modulo
char *get_from_buffer_1(struct buffer_rc *ptr){ // chercher dans le buffer 1
result = ptr->tab[ptr->tail]; // donne un valeur a result
void put_in_buffer_2(struct queue *ptr, struct buffer_cw *buf){ //ajouter un element dans le buffer 2
queue_t *get_from_buffer_2(struct buffer_cw *buf){ // trouver un element
result = buf->tab[buf->tail]; //donne une valeur a result
buf->len--; //diminue l'espace occupé par le buffer
buf->tail = (buf->tail + 1)%buf->size;
int is_div(unsigned long long number, unsigned long long i) { // Vérifie si i est un diviseur de number.
return (number % i == 0) ; // renvoie 0 si le nombre n'est pas divisible par i et 1 si il est divisible
int is_prime(unsigned long long number) { // Vérifie si number est un nombre premier. Return 1 si il est premier, 0 sinon
if (number != 2) {
if (number % 2 == 0) {
return 0 ;
}
}
for (unsigned long long i = 3 ; i <= (number/2); i = i + 2) { //regarde les differents nombres pouvant etre diviser number
if (is_div(number,i) != 0) { // si i peut diviser number
return 0 ; //renvoi 0 => FAUX
}
}
return 1 ; //sinon => VRAI
}
struct node new_node;
new_node.value = val;
new_node.next = malloc(sizeof(node_t));
else{
*ptr->next = *q->tail->next;
*q->tail->next = *ptr;
}
q->size++;
new_queue.tail = malloc(sizeof(node_t));
if(new_queue.tail==NULL){return NULL;}
if (ptr == NULL){
free(new_queue.tail);
free(ptr);
return NULL;}
struct buffer_cw *param1 = (struct buffer_cw *) param;
sem_post(&empty2);
if (pr_divs->final == 1)
{
for (int i = 0; i < param1->size; i++)
{
param1->tab[i] = NULL;
}
free(param1->tab);
free(pr_divs);
stop++;
}
else{
FILE *file2 = param1->file2;
struct node *current;
current = malloc(sizeof(node_t));
if (current == NULL){return NULL;}
*current = *pr_divs->tail;
current = current->next;
for (int i = 1; i < pr_divs->size; i++)
current = current->next;
}
fputc('\n',file2);
struct buffer_rccw *param1 = (struct buffer_rccw *) param;
struct buffer_rc *buffer_1 = param1->struct1;
struct buffer_cw *buffer_2 = param1->struct2;
sem_post(&empty1);
if (strcmp("stop",chaine) == 0)
{
struct queue *final;
final = malloc(sizeof(queue_t));
if (final == NULL){return NULL;}
final->final = 1;
sem_wait(&empty2);
pthread_mutex_lock(&mutex2);
put_in_buffer_2(final,buffer_2);
pthread_mutex_unlock(&mutex2);
sem_post(&full2);
pr_divs = malloc(sizeof(queue_t));
if (pr_divs == NULL){return NULL;}
sem_wait(&empty2);
pthread_mutex_lock(&mutex2);
struct buffer_rc *param1= (struct buffer_rc *) param;
sem_wait(&empty1);
pthread_mutex_lock(&mutex1);
put_in_buffer_1(chaine,param1); //put each line in the buffer #1
pthread_mutex_unlock(&mutex1);
sem_post(&full1);
}
for (int i = 0; i < N; i++)
{
sem_wait(&empty1);
pthread_mutex_lock(&mutex1);
put_in_buffer_1("stop\0",param1); //put each stop line in the buffer #1
pthread_mutex_unlock(&mutex1);
sem_post(&full1);
}
}
void sem_2_initializer (struct buffer_cw *buf){
struct buffer_rc *ptr1;
ptr1 = malloc(sizeof(struct buffer_rc));
if (ptr1 == NULL){
free(ptr1);
ptr1->size = 2*N;
ptr1->len = 0;
ptr1->head = 0;
ptr1->tail = 0;
ptr1->stop = 0;
ptr1->file1 = file1;
return ptr1;
}
struct buffer_cw *ptr2;
ptr2 = malloc(sizeof(struct buffer_cw));
ptr2->size = 2*N;
ptr2->len = 0;
ptr2->head = 0;
ptr2->tail = 0;
ptr2->stop = 0;
ptr2->file2 = file2;
return ptr2;
}
struct buffer_rccw *buff_init_12(struct buffer_rc *ptr1,struct buffer_cw *ptr2){
struct buffer_rccw buffer_12;
buffer_12.struct1 = ptr1;
buffer_12.struct2 = ptr2;
struct buffer_rccw *ptr3;
ptr3 = malloc(sizeof(struct buffer_rccw));
void thread_create_join(struct buffer_rc *ptr1,struct buffer_cw *ptr2,struct buffer_rccw *ptr3){
pthread_create(&calculators[i],NULL,&calculating,ptr3);
{
pthread_join(calculators[i],NULL);
}
pthread_join(writer,NULL);
int run (char *input,char *output, int n_threads){
N = n_threads;
FILE *file1 = NULL;
FILE *file2 = NULL;
file1 = fopen(input,"r");
if (file1 == NULL){
printf("Sorry, file %s doesn't exist ): \n",input);
return -1;}
file2 = fopen(output,"w+");
if (file2 == NULL){
fclose(file1);
return -1;
}
struct buffer_rc *ptr1 = buff_init_1(file1);
struct buffer_cw *ptr2 = buff_init_2(file2);
struct buffer_rccw *ptr3 = buff_init_12(ptr1,ptr2);
sem_1_initializer(ptr1);
sem_2_initializer(ptr2);