Thread parameter in ANSI C makes a segmentation fault


 
Thread Tools Search this Thread
Top Forums Programming Thread parameter in ANSI C makes a segmentation fault
# 15  
Old 05-09-2011
Quote:
Originally Posted by Loic Domaigne
Without digging deeper in your code, the first thoughts that come to my mind:
- How many simultaneous client connection do you have?
- What are your ulimits?
Code:
$ ulimit -aS  // currently set
$ ulimit -aH  // hard limits

HTH, Loïc
1. There are a lot of client connections, normally, 70-80ms may have a new client connection comes.
Code:
$ ulimit -aS
time(seconds)        unlimited
file(blocks)         unlimited
data(kb)             unlimited
stack(kb)            8192
coredump(blocks)     0
memory(kb)           unlimited
locked memory(kb)    64
process              4096
nofiles              1024
vmemory(kb)          unlimited
locks                unlimited

$  ulimit -aH
time(seconds)        unlimited
file(blocks)         unlimited
data(kb)             unlimited
stack(kb)            unlimited
coredump(blocks)     0
memory(kb)           unlimited
locked memory(kb)    64
process              4096
nofiles              1024
vmemory(kb)          unlimited
locks                unlimited

---------- Post updated at 10:27 PM ---------- Previous update was at 12:31 PM ----------

Ask for a better suggestion

- Each PC is a TCP server.
- Each PC needs to connect few servers.
- Once a connection is established, I want to keep it and don't want to close it.
- TCP client only sends requests to server when some events trigger

Q: How to keep a client connection and reuse it? Since I used cond_signal and cond_wait to hold the connection, but the TCP server always returns the client has been closed.

The currently solution like that:

Client.c
Code:
socket = socket( ... );
while ( 1 ) {
        /* Wait until messages come. */
        Pthread_mutex_lock( &node->mutex );
        while ( QSize == 0 ) {
            Pthread_cond_wait( &node->isEmpty, &node->mutex );
             ....
            if ( Qsize == 0 )
                continue;
            break;
        }
         data = QPop( ... );
        Pthread_cond_signal( &node->isFull );
        Pthread_mutex_unlock( &node->mutex );

        Writen( sockfd, data, strlen( data ) );
}

A function for push data into the queue.
Code:
void Client_Pend ( ... ) {
        /* Blocking if the queue is full. */
        Pthread_mutex_lock( &node->mutex );
        while ( MAX_SIZE <= qSize ) {
            Pthread_cond_wait( &node->isFull, &node->mutex );
            if ( MAX_SIZE <= qSize )
                continue;   /* If the Queue is full, then blocking. */
            break;
        }
        .....
        Insert data into the queue
    
        /* Let the consumer read the inserted item. */
        Pthread_cond_signal( &node->isEmpty );
        Pthread_mutex_unlock( &node->mutex );
}

However, I use select() as TCP server, the pesude code like this:
Code:
void Server () {
    ssize_t n;
    int i, maxi, maxfd, listenfd, connfd, sockfd;
    int nready, client[ FD_SETSIZE ];
    fd_set rset, allset;
    char buf[ TCP_MAXLINE ];
    struct sockaddr_in cliaddr, srvAddr;
    socklen_t clilen;

    if ( ( listenfd = Socket( AF_INET, SOCK_STREAM, 0) ) == -1 )
        return; /* Fail to create a socket. */
    i = 1;
    Setsockopt( listenfd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof( int ) );  /* Reuse the socket. */

    /* Initiate server's address. */
    bzero( &srvAddr, sizeof( srvAddr ) );
    srvAddr.sin_family      = AF_INET;
    srvAddr.sin_addr.s_addr = htonl( INADDR_ANY );
    srvAddr.sin_port        = htons( TCP_PORT );

    if ( Bind( listenfd, ( struct sockaddr* )&srvAddr, sizeof( srvAddr ) ) == -1 )
        return; /* Fail to bind the socket. */
    if ( Listen( listenfd, TCP_MAX_PENGING ) == -1 )
        return; /* Fail to listen to the socket. */

    /* Initiate select. */
    maxfd = listenfd;   /* Initialize. */
    maxi  = -1;         /* Index into client[] array. */
    for ( i = 0; i < FD_SETSIZE; client[ i ] = -1, i++ );   /* -1 indicates available entry. */
    FD_ZERO( &allset );
    FD_SET( listenfd, &allset );

    for ( ; ; ) {
        rset = allset;      /* Structure assignment. */
        nready = Select( maxfd + 1, &rset, NULL, NULL, NULL );
        if ( FD_ISSET( listenfd, &rset ) ) {    /* New client connection. */
            clilen = sizeof( cliaddr );
            if ( ( connfd = Accept( listenfd, ( struct sockaddr* )&cliaddr, &clilen ) ) == -1 )
                continue;

            for ( i = 0; i < FD_SETSIZE ; i++ )
                if ( client[ i ] < 0 ) {
                    client[ i ] = connfd; /* save descriptor. */
                    break;
                }

            if ( i == FD_SETSIZE ) 
                return;

            FD_SET( connfd, &allset );              /* Add new descriptor to set. */
            if ( connfd > maxfd ) maxfd = connfd;   /* For select. */
            if ( i > maxi ) maxi = i;               /* Max index in client[] array. */
            if ( --nready <= 0 ) continue;          /* No more readable descriptors. */
        }

        /* Check all clients for data. */
        for ( i = 0; i <= maxi ; i++ ) {
            if ( ( sockfd = client[ i ] ) < 0 )
                continue;
            if ( FD_ISSET( sockfd, &rset ) ) {
                if ( readable_timeo( sockfd, 1 ) <= 0 ) {
                    sprintf( returnBuf, "%s(%d) -> Unable read!");
                } else if ( ( n = Readline( sockfd, buf, TCP_MAXLINE ) ) <= 0 ) {  /* For connection closed by client. */
                        fprintf( stderr, "[SERVER] Client closed!" ); This statement always true after a connection from client. Is it no data from client since I used cond_wait()?
                    Close( sockfd );
                    FD_CLR( sockfd, &allset );
                    client[ i ] = -1;
                } else {
                    PacketHandler( sockfd, buf, n );
                }
                if ( --nready <= 0 )  break;    /* No more readable descriptors. */
            }
        }
    }
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Login or Register to Ask a Question