Sponsored Content
Top Forums Programming segmnetation fault by pthread_mutex_unlock on a successfully locked mutex Post 302186516 by jonas.gabriel on Thursday 17th of April 2008 12:10:22 PM
Old 04-17-2008
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
 

10 More Discussions You Might Find Interesting

1. Programming

Threads and Mutex

Hi all, I am working in a UNIX/C environment. I would like to understand more about MUTEX and Threads. Can someone explain me these concepts and how they are related. Vijay (2 Replies)
Discussion started by: vthasan
2 Replies

2. UNIX for Dummies Questions & Answers

mutex

Can anyone explain me what mutexes are in multithreading environment? (2 Replies)
Discussion started by: sagar
2 Replies

3. Shell Programming and Scripting

mutex in shell programing

A shell in crontab per 5 min write a file B shell in crontab per 6 min read a file how to lock the share file a ;avioid confilict in write and read? Thx : -) (1 Reply)
Discussion started by: zz_xm
1 Replies

4. UNIX for Dummies Questions & Answers

what is diff b/w semaphore and mutex

can u tell me what is the exact difference b/w mutex and semaphore and what is the diff b/w counting semaphore and binary semaphore amit (1 Reply)
Discussion started by: amitpansuria
1 Replies

5. Shell Programming and Scripting

Mutex in Perl

Hello Everyone, I just joined this forum and this is my first post. I would like to know how can I impliment basic read/write locks in perl. I have a database (file) which can be accessed simultaneously but has to be locked while writing. If there is no such support in perl, my next... (6 Replies)
Discussion started by: superuser84
6 Replies

6. Programming

Mutex will not work

I am trying to use mutex in my multi-tread project, but they don't seem to work. I have created a simple demonstration of the problem. This is NOT how I would use a mutex, only a demonstration of the problem: #include <stdio.h> #include <pthread.h> int main() { int val; ... (3 Replies)
Discussion started by: ChrisWilliams
3 Replies

7. Programming

Mutex lock question

Hi all, I have a scenario where I need to use the mutex locks. The mutex locks are working fine, but sometimes I am getting into the dead lock situation. Below is the summary of my code : MUTEX LOCK performTask(); MUTEX UNLOCK. In some cases I get into the situation where... (2 Replies)
Discussion started by: cjjoy
2 Replies

8. Programming

pthread and mutex question

Hello, I have got some issue with the struct variable with passed arguments the variable in the sturct is only recognize the last value their assigned to I'm pretty confused why the mutex didn't work out here is my program: #include<stdio.h> #include<pthread.h> pthread_mutex_t lock... (3 Replies)
Discussion started by: michael23
3 Replies

9. Programming

Can Mutex be replaced with anything?

Hi All, To avoid race condition, instead of using mutex, semaphore, spinlock etc.... Is there any other mechanism by which we can avoid race condition in an multi-threading environment. -Thanks (6 Replies)
Discussion started by: rvan
6 Replies

10. Programming

Using gdb, ignore beginning segmentation fault until reproduce environment segmentation fault

I use a binary name (ie polo) it gets some parameter , so for debugging normally i do this : i wrote script for watchdog my app (polo) and check every second if it's not running then start it , the problem is , if my app , remain in state of segmentation fault for a while (ie 15 ... (6 Replies)
Discussion started by: pooyair
6 Replies
_lwp_mutex_lock(2)						   System Calls 						_lwp_mutex_lock(2)

NAME
_lwp_mutex_lock, _lwp_mutex_unlock, _lwp_mutex_trylock - mutual exclusion SYNOPSIS
#include <sys/lwp.h> int _lwp_mutex_lock(lwp_mutex_t *mp); int _lwp_mutex_trylock(lwp_mutex_t *mp); int _lwp_mutex_unlock(lwp_mutex_t *mp); DESCRIPTION
These functions serialize the execution of lightweight processes. They are useful for ensuring that only one lightweight process can exe- cute a critical section of code at any one time (mutual exclusion). LWP mutexes must be initialized to 0 before use. The _lwp_mutex_lock() function locks the LWP mutex pointed to by mp. If the mutex is already locked, the calling LWP blocks until the mutex becomes available. When _lwp_mutex_lock() returns, the mutex is locked and the calling LWP is the "owner". The _lwp_mutex_trylock() function attempts to lock the mutex. If the mutex is already locked it returns with an error. If the mutex is unlocked, it is locked and _lwp_mutex_trylock() returns. The _lwp_mutex_unlock() function unlocks a locked mutex. The mutex must be locked and the calling LWP must be the one that last locked the mutex (the owner). If any other LWPs are waiting for the mutex to become available, one of them is unblocked. RETURN VALUES
Upon successful completion, 0 is returned. A non-zero value indicates an error. ERRORS
If any of the following conditions are detected, _lwp_mutex_lock(), _lwp_mutex_trylock(), and _lwp_mutex_unlock() fail and return the cor- responding value: EINVAL The mp argument points to an invalid LWP mutex. EFAULT The mp argument points to an illegal address. If any of the following conditions occur, _lwp_mutex_trylock() fails and returns the corresponding value: EBUSY The mp argument points to a locked mutex. SEE ALSO
Intro(2), _lwp_cond_wait(2) SunOS 5.11 30 Jul 1992 _lwp_mutex_lock(2)
All times are GMT -4. The time now is 08:27 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy