The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how could I check whether ftp a file is successfully done or not rinku Shell Programming and Scripting 1 11-20-2007 02:34 AM
Where we can get the logs for a command execution successfully? susinthaa SUN Solaris 4 07-10-2007 12:35 AM
Mail delivered successfully? Amruta Pitkar UNIX for Dummies Questions & Answers 7 08-22-2006 08:21 AM
Successfully Installed Solaris 8 after all. abidmalik UNIX for Dummies Questions & Answers 0 12-17-2002 03:56 PM
mutex sagar UNIX for Dummies Questions & Answers 2 02-04-2002 06:00 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-17-2008
jonas.gabriel jonas.gabriel is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 17
segmnetation fault by pthread_mutex_unlock on a successfully locked mutex

Hi Everyone

I have this quite simple "tread" function
Code:
void* row_calc(void *input)
{
	struct thread_arg* th_arg;
	th_arg=(struct thread_arg *) input;
	int start,width,end,seg,k,l,tmp;
	
	start=th_arg->start;
	width=th_arg->width;
	end=start+width-1;
	seg=th_arg->seg;
	
	fprintf(logger,"Segment %d: Hello Start: %d\t End: %d\n\n",seg,start,end);
	fflush(logger);
	
	
	if(th_arg->up_neighbour != NULL)
	{
		fprintf(logger,"Segment %d: Before Up_Neighbour Mutex Lock\n",seg);
		fflush(logger);

		pthread_mutex_lock(th_arg->up_neighbour);
                perror("");

		fprintf(logger,"Segment %d: After Up_Neighbour Mutex Lock\n\n",seg);
		fflush(logger);
		
		for (k=1;k<NC-1;k++)
			after[start][k]=0.25*(before[start-1][k]+before[start+1][k]+before[start][k-1]+before[start][k+1]);

		fprintf(logger,"Segment %d: Before Up_Neighbour Mutex UnLock\n",seg);
		fflush(logger);
		
		pthread_mutex_unlock(th_arg->up_neighbour);

		fprintf(logger,"Segment %d: After Up_Neighbour Mutex UnLock\n\n",seg);
		fflush(logger);
	}
	fprintf(logger,"Segment %d: Ok, Bye Bye!\n",seg);
	fflush(logger);
}
I can't unlock the mutex pointed by th_arg->up_neighbour, I'm getting a segmentation fault.The mutex is previously successfully locked, perror prints success.

The aren't any race conditions,since only one thread is created in main()
Code:
int main()
{
	int tmp,i,j;
	int width=NR/NT;
	pthread_mutex_t border[NT-1];
	pthread_t row_worker[NT];

	logger=fopen("treads.log","w");
	for(i=0;i<NR;i++)
	{
		for (j=0;j<NC;j++)
		{
			before[i][j]=0.0;
			after[i][j]=0.0;
		}
	}

	for(i=0;i<2*NT-2;i++)
	{
		pthread_mutex_init(border+1,NULL);
	}

	tmp=0;
	for(i=0;i<NT;i++)
	{
		printf("TEST\n");
		arg_vec[i].up_neighbour=(i==0)?NULL:(border+(tmp++));
		arg_vec[i].up=(i==0)?NULL:(border+(tmp++));
		arg_vec[i].down=(i==NT-1)?NULL:(border+(tmp));
		arg_vec[i].down_neighbour=(i==NT-1)?NULL:(border+(tmp+1));

		arg_vec[i].start=i*width;
		arg_vec[i].width=(i==NT-1)?width:width+NR%NT;
		arg_vec[i].seg=i+1;
}

pthread_create(row_worker+NT-1,NULL,row_calc,(void *)(&arg_vec[NT-1]));

pthread_join(row_worker[NT-1],NULL);
printf("That's all folks\n");
The struct of arguments passed is declared as global
Code:
double before[NR][NC], after[NR][NC];

struct thread_arg
{
	pthread_mutex_t *up, *up_neighbour;
	pthread_mutex_t *down, *down_neighbour;
	int start;
	int width;
	int seg;
};

struct thread_arg arg_vec[NT];
FILE *logger;
My log file finishes with line "Segment %d: Before Up_Neighbour Mutex UnLock\n" so I know it's not the critical code section.I even tried commenting out.

Any advice ? Thank you in advance
  #2 (permalink)  
Old 04-18-2008
jonas.gabriel jonas.gabriel is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 17
I found my errors...

mutex array size didn't match the initialization loop.
&
inside initialization loop mispelled i with 1.

Excuse me for this useless thread...
  #3 (permalink)  
Old 04-18-2008
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,953
Quote:
Excuse me for this useless thread
This is not at all useless.

Indeed very helpful and useful for me.

Thanks for this post.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:59 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0