Evo i prevedene verzije, i dodao sam samo
ogranicenja za min i max mem unos, ostaje pitanje zasto program ne radi sa nizim vrednostima unosa (oko 30 i nize),
i da se implementira preciznije merenje vremena (bar stotinke):
Code:
#include <stdlib.h>
#include <semaphore.h>
#include <pthread.h>
#include <stdio.h>
#include <time.h>
time_t timer1,timer2;
struct tm *tarray1,*tarray2;
sem_t sema,sema1;
int BLCK_SIZE;
int memparse ();
int memload ();
int memparse()
{
int d;
int i;
i = memload ();
d = i * 65536;
return d;
}
int memload()
{
int memory;
scanf("%d", &memory);
return memory;
}
void* tf(void* p)
{
int j = 80;
while(j--)
{
int i = 0;
for(;i<BLCK_SIZE;++i)
{
*((int*)p+i) = i;
}
if(j%20==0)
{
sem_post(&sema);
sem_wait(&sema1);
}
}
return 0;
}
int main ()
{
int min1,min2,sec1,sec2,minf,secf;
timer1 = time(NULL);
tarray1 = localtime(&timer1);
min1 = tarray1->tm_min;
sec1 = tarray1->tm_sec;
sem_init(&sema,0,0);
sem_init(&sema1,0,0);
int mb;
printf ("\n");
printf("Unesite koliko memorije alocirate za test (64 do 512 MB):");
BLCK_SIZE = memparse ();
mb = 0.000015259 * BLCK_SIZE;
int* mem = calloc(4,BLCK_SIZE*sizeof(int));
if (mb < 64)
{
printf("\n");
printf("Postavljena vrednost nije dozvoljena.\n");
printf("\n");
return 0;
}
if (mb > 512)
{
printf("\n");
printf("Postavljena vrednost nije dozvoljena.\n");
printf("\n");
return 0;
}
if(!mem)
{
printf("Nema dovoljno memorije.\n");
return 0;
}
else printf("Program je alocirao %d MB RAM-a.\n", mb);
pthread_t tid[4];
pthread_create(tid+0,0,tf,mem+0*BLCK_SIZE);
pthread_create(tid+1,0,tf,mem+1*BLCK_SIZE);
pthread_create(tid+2,0,tf,mem+2*BLCK_SIZE);
pthread_create(tid+3,0,tf,mem+3*BLCK_SIZE);
sem_wait(&sema);
sem_wait(&sema);
sem_wait(&sema);
sem_wait(&sema);
printf ("25 procenata komletirano... \n");
sem_post(&sema1);
sem_post(&sema1);
sem_post(&sema1);
sem_post(&sema1);
sem_wait(&sema);
sem_wait(&sema);
sem_wait(&sema);
sem_wait(&sema);
printf ("50 procenata komletirano... \n");
sem_post(&sema1);
sem_post(&sema1);
sem_post(&sema1);
sem_post(&sema1);
sem_wait(&sema);
sem_wait(&sema);
sem_wait(&sema);
sem_wait(&sema);
printf ("75 procenata komletirano... \n");
sem_post(&sema1);
sem_post(&sema1);
sem_post(&sema1);
sem_post(&sema1);
sem_wait(&sema);
sem_wait(&sema);
sem_wait(&sema);
sem_wait(&sema);
printf ("100 procenata komletirano! \n");
sem_post(&sema1);
sem_post(&sema1);
sem_post(&sema1);
sem_post(&sema1);
pthread_join(tid[0],NULL);
pthread_join(tid[1],NULL);
pthread_join(tid[2],NULL);
pthread_join(tid[3],NULL);
timer2 = time(NULL);
tarray2 = localtime(&timer2);
min2 = tarray2->tm_min;
sec2 = tarray2->tm_sec;
if (min2 < min1)
{
minf = min2 + 60 - min1;
}
else
minf = min2 - min1;
if (sec2 < sec1)
{
secf = sec2 + 60 - sec1;
minf -= 1;
}
else
secf = sec2 - sec1;
printf ("\n");
printf ("Vreme izvrsavanja je %d minuta i %d sekundi.\n",minf,secf);
printf ("\n");
free(mem);
sem_destroy(&sema);
sem_destroy(&sema1);
return 0;
}
U attachment-u je izvrsna verzija ovoga za 64-bit sistem, program se vise ne mora startovati sa "time" (10x to combuster) :)
Interesantno je pratiti i krivulju rasta potrebnog vremena kada se kolicina memorije uduplava:
Na mom racunaru izgleda ovako:
64mb --> 4 sekunde,
128mb --> 10 sekundi,
256mb --> 14 sekundi,
512mb --> 20 sekundi.
/edit: btw, jel ima neko eeeAsus-a za probu? :D
[Ovu poruku je menjao Kernel-1 dana 07.11.2008. u 06:13 GMT+1]