Sponsored Content
Top Forums Programming pthread_mutex_trylock() overwrites global variable on CentOS5 Post 302308054 by liveshell on Friday 17th of April 2009 02:19:08 AM
Old 04-17-2009
pthread_mutex_trylock() overwrites global variable on CentOS5

Hi all,

I am new to linux and got problem with pthread_mutex_trylock(). I have used mutex in my code. When I try to call pthread_mutex_trylock() on RECURSIVE type of mutex it overwrites adjacent memory location (that is global variable of type structure say x, memory allocated using malloc()).
Actually I put 'awatch' hardware watch on variable x, when i call pthread_mutex_trylock() it changes variable 2 members of x with message like

0x 0xSomeAdd from libpthread.so.0

and that causes segmentation fault in later part of code...Is it any known issue with pthread?? if you want I can paste complete code here...I have searched enough on libpthread but couldnt find anything specific....

Configurations are
CentOs 5 on VM Workstation

>uname -a

Linux SamCentOs.itpl 2.6.18-128.1.6.el5 #1 SMP Wed Apr 1 09:19:18 EDT 2009 i686 i686 i386 GNU/Linux

>rpm -aq | grep libc

glibc-2.5-34
glibc-headers-2.5-34
glibc-common-2.5-34
glibc-devel-2.5-34


>rpm -aq | grep gcc
gcc-c++-4.1.2-44.el5
gcc-4.1.2-44.el5
libgcc-4.1.2-44.el5
gcc-gfortran-4.1.2-44.el5


and the strange thing is the code works fine with following configuration

Linux avSam 2.6.11-1.1369_FC4 #1 Thu Jun 2 22:55:56 EDT 2005 i686 i686 i386 GNU/Linux

glibc-2.3.5-10
glibc-devel-2.3.5-10
glibc-common-2.3.5-10
glibc-headers-2.3.5-10
glibc-kernheaders-2.4-9.1.94

libgcc-4.0.0-8
gcc-4.0.0-8
gcc-c++-4.0.0-8
gcc-java-4.0.0-8

Thanks,
LiveShell
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Global variable becomes local

I have encountered a very weird behavior of a global variable in Korn Shell in AIX: A function f1 in my script pipes the output of the function f2 to a program. A variable defined as global using typeset gets its value in f2. That value is not seen in f1. If I remove the pipe ksh recognizes the... (2 Replies)
Discussion started by: odashe318
2 Replies

2. Shell Programming and Scripting

Global Variable in a script?

How to create a Global variable within a script file. say i want a varaible called LOGFILE to be used within all the script. how to do that? (2 Replies)
Discussion started by: skyineyes
2 Replies

3. Shell Programming and Scripting

global variable not being set

In ksh I thought a global variable was any variable in a script or function that did not have the typeset command. I have a global in my calling script which I increment in a function, but the value does not change in the calling script. Here is the code: function f_open_log { typeset -r... (5 Replies)
Discussion started by: robotball
5 Replies

4. Shell Programming and Scripting

Global variable

I have written a shell scritp in which i am using a variable which is declared before a while loop and i am updaitng the variable in while loop and want to use its updated value outside the loop. I am not able to do so, b'coz the scope of the variable is limited to the while loop only and when i am... (5 Replies)
Discussion started by: deepanshu
5 Replies

5. Shell Programming and Scripting

global variable not holding its value?

dear there, this kept me awake last night, the variable ${TO} in the following script doesn't seem to hold its value. I have a file ./filelist, which lists all files of interests. I group them by keywords in the filename, and would like to count total number of lines in each group. ... (7 Replies)
Discussion started by: patiobarbecue
7 Replies

6. Shell Programming and Scripting

Help with Global Variable

Hi Guyz, I have a requirement like, i have to run a script every hour to count the number of errors encountered. At the end of the day, i need to send them the total number of errors, that have ocurred the entire day. For eg. if 10 errors occurred for starting 1 hr, 5 for next 1 hr, so on.... (1 Reply)
Discussion started by: DTechBuddy
1 Replies

7. Programming

pthread_mutex_trylock usage

solved problem by myself. thanx all anyway and i couldnt find delete post button. (0 Replies)
Discussion started by: unexpected
0 Replies

8. Shell Programming and Scripting

Global variable value

Hi All, Im new to shell scripting. I am running EgA.sh and setting one global variable XYZ=0 . Also calling another EgB.sh from EgA.sh, changing the value of XYZ=10 but after executing EgB.sh, value of XYZ is still 0. Im expecting it to be 10. Anyone for help. Thanks in Advance. :) (5 Replies)
Discussion started by: paliwal
5 Replies

9. Shell Programming and Scripting

Global Variable

Hi, I have created a variable say today at the begin having 123 as its value and inside a for loop it gets resolved to some value say 150 in its first iteration. How can I use this value 150 ( 1st iteration's ) outside the scope of for loop ?. In the same way I wanted to use all iteration's... (1 Reply)
Discussion started by: penqueen
1 Replies

10. Shell Programming and Scripting

Question around Global Variable

Hi, I am using Linux and sh shell count=7 find * -prune -type d | sort -r -n | ( while read d; do if ; then echo "FOUND COUNTER1 is: $count" break 2; fi done echo "FOUND COUNTER2 is: $count" ) if ; then echo "Problem: Multiple or NO records...Please CHECK !!" fi Output: ... (4 Replies)
Discussion started by: mohtashims
4 Replies
pthread_mutex_lock(3C)					   Standard C Library Functions 				    pthread_mutex_lock(3C)

NAME
pthread_mutex_lock, pthread_mutex_trylock, pthread_mutex_unlock - lock or unlock a mutex SYNOPSIS
cc -mt [ flag... ] file... -lpthread [ library... ] #include <pthread.h> int pthread_mutex_lock(pthread_mutex_t *mutex); int pthread_mutex_trylock(pthread_mutex_t *mutex); int pthread_mutex_unlock(pthread_mutex_t *mutex); DESCRIPTION
The mutex object referenced by mutex is locked by calling pthread_mutex_lock(). If the mutex is already locked, the calling thread blocks until the mutex becomes available. This operation returns with the mutex object referenced by mutex in the locked state with the calling thread as its owner. If the mutex type is PTHREAD_MUTEX_NORMAL, deadlock detection is not provided. Attempting to relock the mutex causes deadlock. If a thread attempts to unlock a mutex that it has not locked or a mutex that is unlocked, undefined behavior results. If the mutex type is PTHREAD_MUTEX_ERRORCHECK, then error checking is provided. If a thread attempts to relock a mutex that it has already locked, an error will be returned. If a thread attempts to unlock a mutex that it has not locked or a mutex which is unlocked, an error will be returned. If the mutex type is PTHREAD_MUTEX_RECURSIVE, then the mutex maintains the concept of a lock count. When a thread successfully acquires a mutex for the first time, the lock count is set to 1. Every time a thread relocks this mutex, the lock count is incremented by one. Each time the thread unlocks the mutex, the lock count is decremented by one. When the lock count reaches 0, the mutex becomes available for other threads to acquire. If a thread attempts to unlock a mutex that it has not locked or a mutex that is unlocked, an error will be returned. If the mutex type is PTHREAD_MUTEX_DEFAULT, attempting to recursively lock the mutex results in undefined behavior. Attempting to unlock the mutex if it was not locked by the calling thread results in undefined behavior. Attempting to unlock the mutex if it is not locked results in undefined behavior. The pthread_mutex_trylock() function is identical to pthread_mutex_lock() except that if the mutex object referenced by mutex is currently locked (by any thread, including the current thread), the call fails immediately with EBUSY. The pthread_mutex_unlock() function releases the mutex object referenced by mutex. The manner in which a mutex is released is dependent upon the mutex's type attribute. If there are threads blocked on the mutex object referenced by mutex when pthread_mutex_unlock() is called, resulting in the mutex becoming available, the scheduling policy is used to determine which thread will acquire the mutex. (In the case of PTHREAD_MUTEX_RECURSIVE mutexes, the mutex becomes available when the count reaches 0 and the calling thread no longer has any locks on this mutex.) If a signal is delivered to a thread waiting for a mutex, upon return from the signal handler the thread resumes waiting for the mutex as if it was not interrupted. RETURN VALUES
If successful, the pthread_mutex_lock() and pthread_mutex_unlock() functions return 0. Otherwise, an error number is returned to indi- cate the error. The pthread_mutex_trylock() function returns 0 if a lock on the mutex object referenced by mutex is acquired. Otherwise, an error number is returned to indicate the error. ERRORS
The pthread_mutex_lock() and pthread_mutex_trylock() functions will fail if: EAGAIN The mutex could not be acquired because the maximum number of recursive locks for mutex has been exceeded. EINVAL The mutex was created with the protocol attribute having the value PTHREAD_PRIO_PROTECT and the calling thread's priority is higher than the mutex's current priority ceiling. EPERM The mutex was created with the protocol attribute having the value PTHREAD_PRIO_PROTECT and the calling thread is not in the real-time class (SCHED_RR or SCHED_FIFO scheduling class). The pthread_mutex_trylock() function will fail if: EBUSY The mutex could not be acquired because it was already locked. The pthread_mutex_lock(), pthread_mutex_trylock() and pthread_mutex_unlock() functions may fail if: EINVAL The value specified by mutex does not refer to an initialized mutex object. The pthread_mutex_lock() function may fail if: EDEADLK The current thread already owns the mutex. ENOMEM The limit on the number of simultaneously held mutexes has been exceeded. The pthread_mutex_unlock() function will fail if: EPERM The mutex type is PTHREAD_MUTEX_ERRORCHECK or the mutex is a robust mutex, and the current thread does not own the mutex. When a thread makes a call to pthread_mutex_lock() or pthread_mutex_trylock(), if the mutex is initialized with the robustness attribute having the value PTHREAD_MUTEX_ROBUST (see pthread_mutexattr_getrobust(3C)), the call will return these error values if: EOWNERDEAD The last owner of this mutex died while holding the mutex, or the process containing the owner of the mutex unmapped the memory containing the mutex or performed one of the exec(2) functions. This mutex is now owned by the caller. The caller must now attempt to make the state protected by the mutex consistent. If it is able to clean up the state, then it should call pthread_mutex_consistent() for the mutex and unlock the mutex. Subsequent calls to pthread_mutex_lock() and pthread_mutex_trylock() will behave normally, as before. If the caller is not able to clean up the state, pthread_mutex_consistent() should not be called for the mutex, but the mutex should be unlocked. Subsequent calls to pthread_mutex_lock() and pthread_mutex_trylock() will fail to acquire the mutex with the error value ENOTRECOVERABLE. If the owner who acquired the lock with EOWNERDEAD dies, the next owner will acquire the lock with EOWNERDEAD. ENOTRECOVERABLE The mutex trying to be acquired was protecting the state that has been left irrecoverable by the mutex's last owner. The mutex has not been acquired. This condition can occur when the lock was previously acquired with EOWNERDEAD, and the owner was not able to clean up the state and unlocked the mutex without calling pthread_mutex_consistent(). ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Committed | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ |Standard |See standards(5). | +-----------------------------+-----------------------------+ SEE ALSO
pthread_mutex_consistent(3C), pthread_mutex_init(3C), pthread_mutexattr_setprotocol(3C), pthread_mutexattr_setrobust(3C), pthread_mutex- attr_settype(3C), attributes(5), standards(5) NOTES
In the current implementation of threads, pthread_mutex_lock(), pthread_mutex_unlock(), mutex_lock(), mutex_unlock(), pthread_mutex_try- lock(), and mutex_trylock() do not validate the mutex type. Therefore, an uninitialized mutex or a mutex with an invalid type does not return EINVAL. Interfaces for mutexes with an invalid type have unspecified behavior. Uninitialized mutexes that are allocated locally may contain junk data. Such mutexes need to be initialized using pthread_mutex_init() or mutex_init(). SunOS 5.11 11 Nov 2008 pthread_mutex_lock(3C)
All times are GMT -4. The time now is 11:04 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy