Hi i had written a small program using pthrea_rw locks in linux i.e below program
when i try to executed this program writer thread hangs while taking last write lock and when i try to print the content of it,its displaying like this
(gdb) p rwlock $1 = {__rw_lock = {__status = 0, __spinlock = -1}, __rw_readers = 0, __rw_writer = 0x0, __rw_read_waiting = 0x0, __rw_write_waiting = 0x1, __rw_kind = 0, __rw_pshared = 0} (gdb)
gcc -g -Wall -o2 -D_XOPEN_SOURCE=600 s.c -l pthread
ldd out put of a.out linux-gate.so.1 => (0xffffe000) libpthread.so.0 => /lib/tls/libpthread.so.0 (0x40036000) libc.so.6 => /lib/tls/libc.so.6 (0x40046000) /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
Can any body explain me ,is this correct behaviour .....if not ,its a bug?
#include #include #include #include #include #include #include #include
pthread_rwlock_t rwlock;
void *writer( void *arg ) {
pthread_rwlock_wrlock( &rwlock );
pthread_rwlock_wrlock( &rwlock );
pthread_rwlock_unlock( &rwlock );
pthread_rwlock_unlock( &rwlock );
pthread_rwlock_wrlock( &rwlock ); pthread_rwlock_unlock( &rwlock ); return NULL;
} int main( void ) {
pthread_t wd; pthread_rwlockattr_t rwlock_attr;
if( pthread_rwlock_init (&rwlock, NULL) != 0 ) { pthread_rwlockattr_destroy(&rwlock_attr); return -1; } pthread_create(&wd, NULL, writer, "writer_d"); pthread_join(wd, NULL);
return 0; }
gdb output
40 } (gdb) c Continuing. [New Thread 1077263280 (LWP 20174)]Program received signal SIGINT, Interrupt.
0xffffe410 in ?? () (gdb) bt #0 0xffffe410 in ?? () #1 0xbfffee38 in ?? () #2 0x00004ece in ?? () #3 0x00000000 in ?? () #4 0x4003bf3f in pthread_join () from /lib/tls/libpthread.so.0 #5 0x08048628 in main () at s.c:46 (gdb) thread 2 [Switching to thread 2 (Thread 1077263280 (LWP 20174))]#0 0xffffe410 in ?? () (gdb) bt #0 0xffffe410 in ?? () #1 0x4035ba98 in ?? () #2 0x00000000 in ?? () #3 0x00000000 in ?? () #4 0x4003d262 in pthread_rwlock_wrlock () from /lib/tls/libpthread.so.0 #5 0x080485af in writer (arg=0x8048758) at s.c:27 #6 0x4003b9ed in start_thread () from /lib/tls/libpthread.so.0 #7 0x400fd9ca in clone () from /lib/tls/libc.so.6 (gdb) f 4 #4 0x4003d262 in pthread_rwlock_wrlock () from /lib/tls/libpthread.so.0 (gdb) p rwlock $1 = {__rw_lock = {__status = 0, __spinlock = -1}, __rw_readers = 0, __rw_writer = 0x0, __rw_read_waiting = 0x0, __rw_write_waiting = 0x1, __rw_kind = 0, __rw_pshared = 0} (gdb)