Sponsored Content
Full Discussion: Socket memory
Top Forums Programming Socket memory Post 302443148 by kumaran_5555 on Saturday 7th of August 2010 04:02:35 AM
Old 08-07-2010
Hi,

This is my code.

Code:
 
#include "../hdr/isamhdr.h" 
#include <mcheck.h>
 
int main(int argc, char *argv[]) 
{
    struct addrinfo hints, *res, *p;
    struct sockaddr *accepted_conc;
    int status, socket_fd,accept_fd,sockaddr_size=sizeof(accepted_conc),curr_conc=0,max_conc;
    pthread_t *threads;

    if(argc != 3)
    {
        printf("ITF_ERR: Usage %s <port numebr> <max_connections>\n",argv[0]);
        return 2;
    }

    max_conc=atoi(argv[2]);
    threads=calloc(max_conc,sizeof(pthread_t));
    accepted_conc=calloc(max_conc,sizeof(struct sockaddr));

    memset(&hints, 0, sizeof hints);

    hints.ai_family = AF_UNSPEC; 
    hints.ai_socktype = SOCK_STREAM;    // open tcp connection
    hints.ai_flags = AI_PASSIVE;                // server always runs on localhost


    if ((status = getaddrinfo(NULL,argv[1], &hints, &res)) != 0) 
    {
        printf("ITF_ERR: getaddrinfo():%s\n", gai_strerror(status));
        return 2;
    }
    // socket()
    if((socket_fd = socket(res->ai_family,res->ai_socktype,res->ai_protocol)) <0 )
    {
        printf("ITF_ERR: socket() %s\n",strerror(errno));
        return 2;
    }
    else
        printf("socket() call sucess\n");
    
    // bind()
    if((status = bind(socket_fd,res->ai_addr,res->ai_addrlen)) < 0)
    {
        printf("ITF_ERR: bind() %s\n",strerror(errno));
        return 2;
    }
    else
        printf("bind() call sucess\n");

    // listen()
    if((status = listen(socket_fd,10)) < 0)
    {
        printf("ITF_ERR: listen() %s\n",strerror(errno));
        return 2;
    }
    else
        printf("listen() call success\n");

    while(curr_conc < max_conc)
    {
        printf("waiting for new connections ...\n");
        // accept()
        if((accept_fd = accept(socket_fd,accepted_conc+curr_conc,&sockaddr_size)) <0 )
        {
            printf("ITF_ERR: accept() %s\n",strerror(errno));
            return 2;
        }
        else
        {
            curr_conc++;
            printf("%d -accept() call success\n",curr_conc);
        }
        status = pthread_create(&threads[curr_conc], NULL,_server_thread, (void *)accept_fd);
        if(status)
        {
            printf("ITF_ERR; return code from pthread_create() is %d\n", status);
            return 2;
        }
    }
    close(socket_fd);
    printf("no room for further threads\n");
    
    return 0; 
} 
 
void *_server_thread(void *sock_fd)
{ 
    void *lib_hdl; 
    USR_qry_ptr qry; 
    USR_rslt_set_ptr rslt; 
    unsigned char qry_str[LINE_MAX];
    int fd=(int)sock_fd,sts;

    if((lib_hdl=dlopen(ISAM_LIB,RTLD_NOW)) == NULL) 
    { 
        printf("%s\n",dlerror()); 
        return NULL; 
    } 
 
    dl_qry_parser=dlsym(lib_hdl,"qry_parser"); 
     
    if(dl_qry_parser == NULL) 
    { 
        printf("%s\n",dlerror()); 
        return NULL; 
    } 
    dl_execute_qry=dlsym(lib_hdl,"execute_qry"); 
     
    if(dl_execute_qry == NULL) 
    { 
        printf("%s\n",dlerror()); 
        return NULL; 
    } 
    dl_print_rslt_set=dlsym(lib_hdl,"_print_rslt_set"); 
     
    if(dl_print_rslt_set == NULL) 
    { 
        printf("%s\n",dlerror()); 
        return NULL; 
    }
    dl_free_rslt_set=dlsym(lib_hdl,"_free_rslt_set"); 
     
    if(dl_free_rslt_set == NULL) 
    { 
        printf("%s\n",dlerror()); 
        return NULL; 
    }
    
    dl_free_qry=dlsym(lib_hdl,"_free_qry"); 
     
    if(dl_free_qry == NULL) 
    { 
        printf("%s\n",dlerror()); 
        return NULL; 
    }
    
    
    while(1)
    {
        sts=recv(fd,qry_str,LINE_MAX,0);
        if(sts < 0)
        {
            printf("SRV_ERR: recv() %s\n",strerror(errno));
            close(fd);
            pthread_exit(NULL);
        }            
        if(!strcmp(qry_str,"quit"))
        {
            printf("SRV_ERR: Thread exiting by quit\n");
            close(fd);
            pthread_exit(NULL);
        }
    
        qry=(*dl_qry_parser)((unsigned char *)qry_str);        
        rslt=(*dl_execute_qry)(qry);

        (*dl_print_rslt_set)(rslt);

        // do clean up        
        (*dl_free_rslt_set)(rslt);
        (*dl_free_qry)(qry);
    }

    return NULL;
}

I have checked all my functions using mtrace() for memory leak. It is under control.

When i give any number of inputs keeping the number of connections constant, then there is no memory increase.

But as and when i start new a connection, i am able to see 10 MB increase in private memory under anonymous using pmap.

Could this be a socket memory and that should be freed using some system call,

Please advice.

Thanks
Kumaran
 

10 More Discussions You Might Find Interesting

1. Programming

Socket Programming socket

Hello, I actually try to make client-server program. I'm using SCO OpenServer Release 5.0.0 and when I try to compile my code (by TELNET) I've got this error : I'm just using this simple code : and I get the same error if I use : If someone can help me, Thanks (2 Replies)
Discussion started by: soshell
2 Replies

2. Solaris

How to find Total and Free Physical Memory and Logical Memory in SOLARIS 9

Hi, Im working on Solaris 9 on SPARC-32 bit running on an Ultra-80, and I have to find out the following:- 1. Total Physical Memory in the system(total RAM). 2. Available Physical Memory(i.e. RAM Usage) 3. Total (Logical) Memory in the system 4. Available (Logical) Memory. I know... (4 Replies)
Discussion started by: 0ktalmagik
4 Replies

3. UNIX for Advanced & Expert Users

connect problem for sctp socket (ipv6 socket) - Runtime fail Invalid Arguments

Hi, I was porting ipv4 application to ipv6; i was done with TCP transports. Now i am facing problem with SCTp transport at runtime. To test SCTP transport I am using following server and client socket programs. Server program runs fine, but client program fails giving Invalid Arguments for... (0 Replies)
Discussion started by: chandrutiptur
0 Replies

4. Programming

which socket should socket option on be set

Hi all, On the server side, one socket is used for listening, the others are used for communicating with the client. My question is: if i want to set option for socket, which socket should be set on? If either can be set, what's the different? Again, what's the different if set option... (1 Reply)
Discussion started by: blademan100
1 Replies

5. Programming

How to deal with lots of data in memory in order not to run out of memory

Hi, I'm trying to learn how to manage memory when I have to deal with lots of data. Basically I'm indexing a huge file (5GB, but it can be bigger), by creating tables that holds offset <-> startOfSomeData information. Currently I'm mapping the whole file at once (yep!) but of course the... (1 Reply)
Discussion started by: emitrax
1 Replies

6. Solaris

restrcit physical memory with zone.max-locked-memory

Is it possible to restrict physical memory in solaris zone with zone.max-locked-memory just like we can do with rcapd ? I do not want to used rcapd (1 Reply)
Discussion started by: fugitive
1 Replies

7. Programming

socket function to read a webpage (socket.h)

Why does this socket function only read the first 1440 chars of the stream. Why not the whole stream ? I checked it with gdm and valgrind and everything seems correct... #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> #include... (3 Replies)
Discussion started by: cyler
3 Replies

8. Programming

Error with socket operation on non-socket

Dear Experts, i am compiling my code in suse 4.1 which is compiling fine, but at runtime it is showing me for socket programming error no 88 as i searched in errno.h it is telling me socket operation on non socket, what is the meaning of this , how to deal with this error , please... (1 Reply)
Discussion started by: vin_pll
1 Replies

9. IP Networking

Clarification - Setting socket options at the same time when socket is listening

I need clarification on whether it is okay to set socket options on a listening socket simultaneously when it is being used in an accept() call? Following is the scenario:- -- Task 1 - is executing in a loop - polling a listen socket, lets call it 'fd', (whose file descriptor is global)... (2 Replies)
Discussion started by: jake24
2 Replies

10. Solaris

[DOUBT] Memory high in idle process on Solaris 10 (Memory Utilization > 90%)

Hi Experts, Our servers running Solaris 10 with SAP Application. The memory utilization always >90%, but the process on SAP is too less even nothing. Why memory utilization on solaris always looks high? I have statement about memory on solaris, is this true: Memory in solaris is used for... (4 Replies)
Discussion started by: edydsuranta
4 Replies
All times are GMT -4. The time now is 04:26 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy