Hello everybody.I have an exercise (kiwi engine) in university (OS course) and I have to use thread.
The code is big (10.000 lines) and I only have to use threads to make it faster.
In the following code if you can see a comment like: // <<<<<<<<<<<<<<<
it means that I have added this line(the other lines already exists).
So,as you can see below,I make an array of threads given in command line
If 2nd string is "readwrite" the program runs only this if statement.
So, I create a these threads to call _readwrite(...) function.
The _readwrite_test(...) lock a mutex to call function _write_test(...) ,unlock it and the do the same thing for _read_test(...)
These 2 functions write and read data from a data base
Do not pay attention what these function do.
Anyway,when I run gdb ,I recieve
Thread 2 "kiwi-bench" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff78b1700 (LWP 53813)]
__memmove_avx_unaligned_erms () at ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:306
306 ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S: No such file or directory.
Do not run the code(nothing will happen).
If I run only read_test or write_test all works fine.I think the problem is in the lock/unlock of mutex.
What can I do?What do you think?
I hope you understand what I want to do.
I need your help!!
Thank you very much.
What I have tried:
struct vars {
int r;
long int count;
double percentage;
};
pthread_mutex_t my_mutex = PTHREAD_MUTEX_INITIALIZER;
void *_readwrite_test(void *arg) {
struct vars *my_var = (struct vars *)arg;
int put_percentage = (int) ((my_var->percentage/100) * my_var->count); int get_percentage = (int) (my_var->count - put_percentage);
printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ put=%d --- get=%d\n" , put_percentage , get_percentage);
pthread_mutex_lock(&my_mutex);
_write_test(put_percentage , my_var->r);
pthread_mutex_unlock(&my_mutex);
pthread_mutex_lock(&my_mutex);
_read_test(get_percentage , my_var->r);
pthread_mutex_unlock(&my_mutex);
return NULL;
}
int main(int argc,char** argv)
{
long int count;
pthread_t threads[ atoi(argv[3]) ];
srand(time(NULL));
if (argc < 4) {
fprintf(stderr,"Usage: db-bench <write | read | readwrite> <count>\n");
exit(1);
}
if (strcmp(argv[1], "write") == 0) {
int r = 0;
count = atoi(argv[2]);
_print_header(count);
_print_environment();
if (argc == 4)
r = 1;
_write_test(count, r);
} else if (strcmp(argv[1], "read") == 0) {
int r = 0;
count = atoi(argv[2]);
_print_header(count);
_print_environment();
if (argc == 4)
r = 1;
_read_test(count, r);
}
else if(strcmp(argv[1] , "readwrite") == 0) {
struct vars my_var;
my_var.count = atoi(argv[2]);
my_var.r = 0;
my_var.percentage = atoi(argv[4]);
_print_header(my_var.count);
_print_environment();
if (argc == 4)
my_var.r = 1;
for(int i = 0; i < atoi(argv[3]); i++)
{
pthread_create(&threads[i] , NULL , _readwrite_test , &my_var);
}
for(int i = 0; i < atoi(argv[3]); i++)
{
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$??????????????????????????????????????????????????????????????????????????????????????? Thread join\n");
pthread_join(threads[i] , NULL);
}
}
else {
fprintf(stderr,"Usage: db-bench <write | read | readwrite> <count> <random>\n");
exit(1);
}
return 1;
}