The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM
Home Forums Register Rules & FAQ Members List Arcade Search Today's Posts Mark Forums Read


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


Other UNIX.COM Threads 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-19-2007 10:34 PM
Where we can get the logs for a command execution successfully? susinthaa SUN Solaris 4 07-09-2007 09:35 PM
Mail delivered successfully? Amruta Pitkar UNIX for Dummies Questions & Answers 7 08-22-2006 05:21 AM
Successfully Installed Solaris 8 after all. abidmalik UNIX for Dummies Questions & Answers 0 12-17-2002 11:56 AM
mutex sagar UNIX for Dummies Questions & Answers 2 02-04-2002 02:00 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-17-2008
Registered User
 

Join Date: Nov 2006
Posts: 17
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-18-2008
Registered User
 

Join Date: Nov 2006
Posts: 17
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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...
Reply With Quote
  #3 (permalink)  
Old 04-18-2008
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,394
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
Quote:
Excuse me for this useless thread
This is not at all useless.

Indeed very helpful and useful for me.

Thanks for this post.
Reply With Quote
Google UNIX.COM
Reply



Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB 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 -7. The time now is 06:12 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger

Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102