Sponsored Content
Top Forums Programming Thread parameter in ANSI C makes a segmentation fault Post 302519718 by sehang on Wednesday 4th of May 2011 11:16:57 PM
Old 05-05-2011
Thread parameter in ANSI C makes a segmentation fault

The creation of thread.
Code:
void Client_Constructor ( const char* IPAddr ) {
    pthread_t tid; 
    pthread_attr_t rx;  

    /* Create separate memory for client argument */
    struct ThreadArgs *threadArgs;
    if ( ( threadArgs = ( struct ThreadArgs* ) malloc( sizeof( struct ThreadArgs ) ) ) == NULL )
        exit( -1 );
    threadArgs->pos = 111;

    pthread_rwlock_wrlock( &rwlock );
    node = ....;
    if ( node->state == DISCONNECTED ) {  <-node requires a r/w lock.
        pthread_attr_init( &rx );
        pthread_attr_setdetachstate( &rx, PTHREAD_CREATE_DETACHED );
        pthread_create( &tid, &rx, THREAD_BODY, ( void* )threadArgs );
    }
    pthread_rwlock_unlock( &rwlock );
}

Thread body
Code:
static void* THREAD_BODY ( void* v ) {
    int sockfd;
    char IPStr[ 16 ];
    int index = ( ( struct ThreadArgs* ) v )->pos; free( v ); <-segmentation fault here
.....
}

These code is so strange, when I run the program more than few hours, then a segmentation fault comes. I also dump the log and use gdb to analyze it, the information is shown below
Code:
Program terminated with signal 6, Aborted.
#0  0xb7de1a16 in ?? () from /lib/libc.so.6
(gdb) where
#0  0xb7de1a16 in ?? () from /lib/libc.so.6
#1  0xb7de3318 in ?? () from /lib/libc.so.6
#2  0xb7e1cfdd in fputws () from /lib/libc.so.6
#3  0xb7e1d114 in ?? () from /lib/libc.so.6
#4  0xb7e287ba in realloc () from /lib/libc.so.6
#5  0xb7e28040 in ?? () from /lib/libc.so.6
#6  0xb7e28339 in ?? () from /lib/libc.so.6
#7  0xb7e2476b in ?? () from /lib/libc.so.6
#8  0x0805e945 in THREAD_BODY (v=0x1838b9b0) at Client.c:167
#9  0xb7efffc0 in pthread_create@@GLIBC_2.1 () from /lib/libpthread.so.0
#10 0xb7e8497e in ?? () from /lib/libc.so.6

It anyone give me some suggestions? Is it related to pthread_rwlock?
 

10 More Discussions You Might Find Interesting

1. Programming

Hi! segmentation fault

I have written a program which takes a directory as command line arguments and displays all the dir and files in it. I don't know why I have a problem with the /etc directory.It displays all the directories and files untill it reaches a sub directory called peers which is in /etc/ppp/peers.the... (4 Replies)
Discussion started by: vijlak
4 Replies

2. Programming

segmentation fault

ive written my code in C for implementation of a simple lexical analyser using singly linked list hence am making use of dynamic allocation,but when run in linux it gives a segmentation fault is it cause of the malloc function that ive made use of????any suggestions as to what i could do??? thank... (8 Replies)
Discussion started by: rockgal
8 Replies

3. AIX

Segmentation fault

Hi , During execution a backup binary i get following error "Program error 11 (Segmentation fault), saving core file in '/usr/datatools" Riyaz (2 Replies)
Discussion started by: rshaikh
2 Replies

4. Programming

segmentation fault

If I do this. Assume struct life { char *nolife; } struct life **life; // malloc initialization & everything if(life->nolife == 0) Would I get error at life->nolife if it is equal to 0. wrong accession? (3 Replies)
Discussion started by: joey
3 Replies

5. Programming

segmentation fault.

This code is causing a segmentation fault and I can't figure out why. I'm new to UNIX and I need to learn how to avoid this segmentation fault thing. Thank you so much. Thanks also for the great answers to my last post.:):b: int main() { mysqlpp::Connection conn(false); if... (3 Replies)
Discussion started by: sepoto
3 Replies

6. Programming

Segmentation fault in C

i have this code int already_there(char *client_names, char *username) { int i; for(i = 0; i<NUM; i++) { printf("HERE\n"); if (strcmp(client_names, username)==0) return(1); } return(0); } and i get a segmentation fault, whats wrong here? (7 Replies)
Discussion started by: omega666
7 Replies

7. UNIX for Advanced & Expert Users

segmentation fault with ps

What does this mean and why is this happening? $ ps -ef | grep ocular Segmentation fault (core dumped) $ ps -ef | grep ocular Segmentation fault (core dumped) $ ps aux | grep ocular Segmentation fault (core dumped) $ ps Segmentation fault (core dumped) $ pkill okular $ ps... (1 Reply)
Discussion started by: cokedude
1 Replies

8. 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

9. Programming

Segmentation fault

I keep getting this fault on a lot of the codes I write, I'm not exactly sure why so I'd really appreciate it if someone could explain the idea to me. For example this code #include <stdio.h> main() { unsigned long a=0; unsigned long b=0; int z; { printf("Enter two... (2 Replies)
Discussion started by: sizzler786
2 Replies

10. Programming

C. To segmentation fault or not to segmentation fault, that is the question.

Oddities with gcc, 2.95.3 for the AMIGA and 4.2.1 for MY current OSX 10.14.1... I am creating a basic calculator for the AMIGA ADE *NIX emulator in C as it does not have one. Below are two very condensed snippets of which I have added the results inside the each code section. IMPORTANT!... (11 Replies)
Discussion started by: wisecracker
11 Replies
pthread_rwlock_wrlock(3C)				   Standard C Library Functions 				 pthread_rwlock_wrlock(3C)

NAME
pthread_rwlock_wrlock, pthread_rwlock_trywrlock - lock or attempt to lock read-write lock object for writing SYNOPSIS
cc -mt [ flag... ] file... -lpthread [ library... ] #include <pthread.h> int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock); int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock); DESCRIPTION
The pthread_rwlock_wrlock() function applies a write lock to the read-write lock referenced by rwlock. The calling thread acquires the write lock if no other thread (reader or writer) holds the read-write lock rwlock. Otherwise, the thread blocks until it can acquire the lock. The pthread_rwlock_trywrlock() function applies a write lock like the pthread_rwlock_wrlock() function, with the exception that the func- tion fails if any thread currently holds rwlock (for reading or writing). Writers are favored over readers of the same priority to avoid writer starvation. See pthread_rwlock_rdlock(3C). Results are undefined if any of these functions are called with an uninitialized read-write lock. If a signal is delivered to a thread waiting for a read-write lock for writing, upon return from the signal handler the thread resumes waiting for the read-write lock for writing as if it was not interrupted. RETURN VALUES
If successful, the pthread_rwlock_wrlock() function returns 0. Otherwise, an error number is returned to indicate the error. The pthread_rwlock_trywrlock() function returns 0 if the lock for writing on the read-write lock object referenced by rwlock is acquired. Otherwise an error number is returned to indicate the error. ERRORS
The pthread_rwlock_wrlock() function will fail if: EDEADLK The current thread already owns the read-write lock for writing or reading. The pthread_rwlock_trywrlock() function will fail if: EBUSY The read-write lock could not be acquired for writing because it was already locked for reading or writing. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ SEE ALSO
pthread_rwlock_init(3C), pthread_rwlock_unlock(3C), pthread_rwlockattr_init(3C), pthread_rwlock_rdlock(3C), attributes(5), standards(5) SunOS 5.11 23 Mar 2005 pthread_rwlock_wrlock(3C)
All times are GMT -4. The time now is 06:39 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy