I am writing a program to benchmark the memory using read+write functions in c.
I have written a program below which accepts the block size of memory to be copied to another memory location using either sequential OR random access.
Each time I give a value greater than 12 bytes , the programmes give below message :
Elapsed time for reading and writing seconds in random manner : 0.000006
Error in `./a.out': free(): invalid next size (fast): 0x00000000022cc030 Aborted (core dumped)
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
int rand_lim(int blocksize) {
/* return a random number between 0 and limit inclusive.
*/
int divisor = RAND_MAX/(blocksize+1);
int random_byte;
do {
random_byte = rand() / divisor;
} while (random_byte > blocksize);
return random_byte;
}
int main(int argc , char *argv[])
{
char temp;
int num_threads=0;
char *buffer, *write_buffer, access;
int i=0, block_size;
printf("Enter the block size of data in number of bytes:");
scanf("%d",&block_size);
buffer = (char*)malloc(block_size);
write_buffer = (char*)malloc(block_size);
for (i=0; i<block_size; i++)
{
memset(&buffer[i],'a',block_size);
//printf("%s",&buffer[i]);
}
printf("\nEnter the acess method --> random(R) OR seq(S) : ");
scanf("%s", &access);
printf("\nEnter the number of threads 1 , 2 , 4 , 8 \n");
scanf("%d", &num_threads);
if (access == 'S')
{
clock_t tic1 = clock();
for (i=0; i<block_size; i++)
{
memcpy(&write_buffer[i],&buffer[i],block_size);
//printf("%s",&write_buffer[i]);
}
clock_t toc1 = clock();
printf("Elapsed time for reading and writing in sequential manner : %f seconds \n", (double)(toc1 - tic1)/CLOCKS_PER_SEC);
free(buffer);
free(write_buffer);
}
else if(access == 'R')
{
clock_t tic1 = clock();
for (i=0; i<block_size; i++)
{
int j = rand_lim(block_size);
memcpy(&write_buffer[j],&buffer[j],block_size);
int dummy = i;
}
clock_t toc1 = clock();
printf("Elapsed time for reading and writing seconds in random manner : %f \n", (double)(toc1 - tic1)/CLOCKS_PER_SEC);
free(buffer);
free(write_buffer);
}
else
{
printf ("\nPlease enter the correct method for bench marking. Exiting...");
exit(0);
}
return 0;
}
I am trying to benchmark with large data like 1 GB but the programme doesn't allow this. Any help is appreciated.