Skip to content

Commit

Permalink
Material Práctica 6
Browse files Browse the repository at this point in the history
  • Loading branch information
MrRobb committed Nov 6, 2017
1 parent 3a46bf9 commit f9efb39
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 0 deletions.
8 changes: 8 additions & 0 deletions S6/material/BAJA_PRIO_FIB
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

i=0
while [ $i -lt $1 ]
do
/usr/bin/time nice ./fib 45&
i=`expr $i + 1` #incrementa en 1 la variable i
done
17 changes: 17 additions & 0 deletions S6/material/FIB
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

if [ "$#" -ne 1 ]
then
echo "usage $0 num"
echo "num: numero de procesos fibonacci a ejecutar en paralelo"
exit
fi

echo $1

i=0
while [ $i -lt $1 ]
do
/usr/bin/time ./fib 45&
i=`expr $i + 1`
done
10 changes: 10 additions & 0 deletions S6/material/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
all:fibonacci mem1

fibonacci:fibonacci.c
gcc -o fib fibonacci.c

mem1:mem1.c
gcc -o mem1 mem1.c

clean:
rm -f fib mem1
Binary file added S6/material/RendimientoProcesos.ods
Binary file not shown.
29 changes: 29 additions & 0 deletions S6/material/fibonacci.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

long fibonacci(unsigned i) {
if (i == 0) return 0;
if (i == 1) return 1;
return fibonacci(i-1) + fibonacci(i-2);
}

void usage() {
char buffer[80];

strcpy(buffer,"fib n\n");
write(1,buffer,strlen(buffer));
exit(1);
}
void main(int argc,char *argv[]) {
unsigned n;
long f;
char buffer[80];

if (argc != 2) usage();
n = atoi(argv[1]);
f = fibonacci(n);
sprintf(buffer,"El fibonacci de %u es %ld\n",n,f);
write(1, buffer, strlen(buffer));
}

79 changes: 79 additions & 0 deletions S6/material/mem1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

#define REGION_SIZE 256*1024*1024

/*
* rdtsc is an assembler instruction that get the cycles executed by the processor since boot time.
*/
#define getCycles(low,high) __asm__ __volatile__("rdtsc": "=a"(low), "=d"(high))

static inline unsigned long long getTime(void)
{
unsigned long low, high;
getCycles(low, high);
return ((unsigned long long)high << 32) + low;

}

int main(int argc, char **argv)
{
int i, j;
int *p;
unsigned long long initTime;
unsigned long long endTime;
long long region_size;
int nelts;
int nits;
int nprocs;
int ret;
char buff[256];

if (argc != 4) {
sprintf( buff, "usage: mem2_4 region_size processes iterations \n");
write(1, buff, strlen(buff));
exit(1);
}

region_size = atoll(argv[1]);
nprocs = atoi(argv[2]);
nits = atoi(argv[3]);

// compute the number of integers that fits in the region
nelts = region_size / sizeof(int);

//loop to create the number of processes required
i = 1;
ret = 1;
while ((i < nprocs) && (ret > 0)) {
ret = fork();
i++;
}

//code that executes each process
p = malloc(region_size);
if (p == NULL) {
sprintf( buff, "Unable to allocate the memory region\n");
write(1, buff, strlen(buff));
exit(1);
}
//loop to control how many times each process accesses all the elements of the vector
i = 0;
while (i < nits) {
initTime = getTime();
//loop to access all the elements in the vector
for (j = 0; j < nelts; j++)
p[j] = j;
endTime = getTime();
sprintf( buff, "process: %d, iteration %d, access loop time: %llu\n",
getpid(), i, (endTime - initTime));
write(1, buff, strlen(buff));
i++;

}
sprintf( buff, "Entering endless loop\n");
write(1, buff, strlen(buff));
while (1) ;
}

0 comments on commit f9efb39

Please sign in to comment.