Created
August 9, 2013 21:48
-
-
Save blackheaven/6197546 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
int main(int argc, char** argv) | |
{ | |
int i,x, tmp; | |
if( (argc != 2) || (x=atoi(argv[1])) <1 ) | |
{ | |
printf("usage: %s NUM\n",argv[0]); | |
return -1; | |
} | |
for (i=0; i<x; i++) | |
{ | |
tmp = i; | |
unsigned int l; | |
unsigned int *p = (void *)&l; | |
p[0] = random(); | |
p[1] = random(); | |
printf("%u\n",l); | |
i = tmp; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <pthread.h> | |
#include <limits.h> | |
typedef unsigned int PN; | |
#define NB_MAX (sizeof(PN)*8) | |
FILE *pFile; | |
static pthread_mutex_t mutex; | |
static PN liste[4793]; | |
static PN liste2[4793]; | |
void *threadF ( void *param ); | |
void print_prime_factors_F(PN); | |
int get_prime_factors_F(PN, PN*); | |
void load_PN(); | |
void exF (int nb); | |
int main(int argc, char *argv[]) | |
{ | |
exF(atoi(argv[1])); | |
return EXIT_SUCCESS; | |
} | |
void *threadF ( void *param ) | |
{ | |
PN nombre; | |
pthread_mutex_lock(&mutex); | |
while(fscanf(pFile,"%u",&nombre)!=EOF) | |
{ | |
pthread_mutex_unlock(&mutex); | |
print_prime_factors_F(nombre); | |
pthread_mutex_lock(&mutex); | |
} | |
pthread_mutex_unlock(&mutex); | |
pthread_exit( NULL ); | |
} | |
void print_prime_factors_F(PN n) | |
{ | |
PN factors [NB_MAX]; | |
int j,k, s; | |
char buf[100]; | |
s = 0; | |
k=get_prime_factors_F(n,factors); | |
s = sprintf(buf, "%u", n); | |
buf[s++] = ':'; | |
buf[s++] = ' '; | |
for(j=0; j<k; j++) | |
{ | |
s += sprintf(buf + s, "%u", factors[j]); | |
buf[s++] = ' '; | |
} | |
buf[s++] = '\n'; | |
buf[s] = '\0'; | |
fputs(buf, stdout); | |
} | |
int get_prime_factors_F(PN nb, PN *dest) | |
{ | |
int i, c, j, N; | |
PN temp [NB_MAX]; | |
c = 0; | |
N = nb; | |
for(i = 0; nb > liste2[i]; ++i) | |
{ | |
while( (nb % liste[i]) == 0 ) | |
{ | |
nb /= liste[i]; | |
dest[c++] = liste[i]; | |
} | |
} | |
if(nb > 1U) | |
dest[c++] = nb; | |
return c; | |
} | |
void load_PN ( ) | |
{ | |
PN i, b, j; | |
PN NB = 0; | |
liste[NB++] = 2; | |
liste[NB++] = 3; | |
liste[NB++] = 5; | |
for( i = 7, b = 2; i <= 46337; i += (b = (6 - b))) | |
{ | |
for(j = 0; (i%liste[j]) && (liste[j]*liste[j] < i); ++j); | |
if(i % liste[j]) | |
{ | |
liste2[NB] = i*i; | |
liste[NB++] = i; | |
} | |
} | |
liste2[NB] = UINT_MAX; | |
liste[NB] = UINT_MAX; | |
} | |
void exF (int nb) | |
{ | |
int i; | |
pthread_t threads[100]; | |
pFile = stdin; | |
load_PN(); | |
pthread_mutex_init(&mutex,NULL); | |
for (i = 0; i < nb; ++i) | |
pthread_create( threads + i, NULL, threadF, NULL); | |
for (i = 0; i < nb; ++i) | |
pthread_join(threads[i],NULL); | |
pthread_mutex_destroy(&mutex); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RMFLAGS = -f | |
REAL = main.c | |
OBJ = $(REAL:.c=.o) | |
EXE = tpthreads | |
EFFACE = clean | |
RM = rm | |
COMP = gcc | |
EDL = gcc | |
LIBS = -pthread -O3 -ffast-math | |
API = | |
DEBUG = | |
all: $(EXE) | |
$(EFFACE): | |
$(RM) $(RMFLAGS) $(EXE) $(OBJ) core | |
%.o: %.c | |
$(COMP) $(DEBUG) -o $(<:.c=.o) -c $< | |
$(EXE): $(OBJ) | |
$(EDL) $(LIBS) $(DEBUG) -o $(EXE) $(OBJ) $(API) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment