-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion4.c
More file actions
77 lines (60 loc) · 1.48 KB
/
question4.c
File metadata and controls
77 lines (60 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include <pthread.h>
/*--------------------------------------DECLARATION METHODES------------------------------------*/
void* thread_prime_factors(void * u);
void print_prime_factors(uint64_t n);
/*--------------------------------------------METHODES-----------------------------------------*/
void* thread_prime_factors(void * u)
{
//Déréférencement
uint64_t* u2 = (uint64_t *) u;
uint64_t n = *u2;
print_prime_factors(n);
return NULL;
}
void print_prime_factors(uint64_t n)
{
printf("%ju : ", n );
uint64_t i;
for( i=2; n!=1 ; i++ )
{
while (n%i==0)
{
// Tant que i est un facteur premier de n
n=n/i;
printf("%ju ", i);
}
}
//On a fini !
printf("\n");
return;
}
int main(void)
{
uint64_t nb;
uint64_t nb2;
FILE * file;
file = fopen ("fileQuestion4pasEfficace.txt","r");
char str[60];
char str2[60];
pthread_t thread0;
pthread_t thread1;
while ( fgets(str, 60, file)!=NULL && fgets(str2, 60, file)!=NULL )
{
nb=atol(str);
nb2=atol(str2);
printf("2 nb en même temps\n");
//Attention en C l'appel des méthode est synchrone donc il faut d'abord créer un thread
//avant d'appeler des fonctions dans le main
pthread_create(&thread0, NULL, thread_prime_factors, &nb);
pthread_create(&thread1, NULL, thread_prime_factors, &nb2);
//Wait for the thread0 to be done
pthread_join(thread0, NULL);
pthread_join(thread1, NULL);
}
return 0;
}