Click here to Skip to main content
15,117,830 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C++
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:

C++
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);	// casting to integer
	int get_percentage = (int) (my_var->count - put_percentage);			// casting to integer

	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)			// <<<<<<<<<<<<<<<
	{	
		// dhmioyrghsa ena struct gia na perasw 2 parametrous sthn readwrite_test mesw nhmatwn
		struct vars my_var;
		
		my_var.count = atoi(argv[2]);
		my_var.r = 0;
		my_var.percentage = atoi(argv[4]);	// percentage of PUT

		_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;
}
Posted
Updated 19-Mar-21 13:17pm
v4
Comments
Richard MacCutchan 20-Mar-21 4:43am
   
You are passing the same structure to each thread of _readwrite_test, so you probably need to add the lock at the very beginning of the function, and the unlock at the very end. However, you still need to use the debugger to find out exactly where the segv occurs, as there is always the possibility that there is another cause.
Nick_is_asking 20-Mar-21 9:59am
   
I tried it before I ask here.Nothing happened

1 solution

SIGSEGV means that you dereferenced an invalid pointer, most often one that was NULL or uninitialized.

EDIT: To follow up, let me start by saying that I haven't used this pthread stuff. But the following looks suspicious:
pthread_mutex_t my_mutex = PTHREAD_MUTEX_INITIALIZER;
A mutex needs to be allocated from the kernel, and I think this only initializes my_mutex to a default value, after which you try to lock it. This could be the source of your bad pointer problem. I would suggest looking for a pthread function that appears to allocate a mutex.
   
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900