Thread: Pthreads help

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    9

    Pthreads help

    Hi I have built a simple web server in C, and I now have to make it multi threaded. I also have to produce a multi threaded command line client to test the multi-threaded capability of the server. I have finished the client and the server however I get read errors when making more than 1 request to the server. I believe it is because I need to look some resources with mutexes but I'm not quite sure how and where to do it.

    Code:
    while (1) {
        //Accept new connection and fork
        remote_len = sizeof(remote_addr);
        sd = accept(lsd, (struct sockaddr *) &remote_addr, &remote_len);
        printf("Connection Received!\n");
        //Store remote IP in "inetadddr" for error logging
        inet_ntop(AF_INET, &(remote_addr.sin_addr), inetaddr, INET_ADDRSTRLEN);
        if (sd < 0) {
          logerrors("Socket Error: Cannot accept connection from",inetaddr);
        }
        params->sd = sd;
        params->inetaddr = inetaddr;
        threadr = pthread_create(&thread[i++], NULL, web, (void*) params);
    That is the accept code in main and the new thread starts with web function:

    Code:
    void *web (void *arg) 
    {
      int n, sd;
      char buffer[BUFFERSIZE];
      char *inetaddr;
      
      struct funcparams *params = (void *) arg;
      sd = params->sd;
      inetaddr = params->inetaddr;
      
      bzero(buffer, BUFFERSIZE);
      //Read browser request
      n = read(sd,buffer,BUFFERSIZE);
    So this read request fails, well it works for the first connection then fails on the rest. Like I said i'm sure its because I need to lock the resources sd and inetaddr but i'm not sure where to lock and unlock. Any help would be greatly appreciated

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You really need

    params = malloc( sizeof *params );

    If your params is anything else (pointer to a local, an array of some sort), then MULTIPLE threads are going to end up referencing the same (or possibly non-existent) block of memory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    9
    The malloc is before the accept loop, it is there:

    Code:
    params = (struct funcparams *) malloc(sizeof(struct funcparams));
    The struct is:

    Code:
    struct funcparams {
    	int sd;
    	char *inetaddr;
    };

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> The malloc is before the accept loop
    It needs to be in the loop - each thread gets its own parameters. You're passing a shallow copy of 'inetaddr' too. That needs to be a unique pointer per-thread as well.

    gg

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    9
    I don't understand what you mean, can you elaborate please?

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    9
    OK I got it working now, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Threads other than pthreads?
    By dayalsoap in forum C Programming
    Replies: 10
    Last Post: 05-28-2010, 01:56 AM
  2. using pthreads with an API that uses pthreads
    By MK27 in forum C Programming
    Replies: 3
    Last Post: 03-06-2009, 02:47 PM
  3. Pthreads performance
    By C_ntua in forum C Programming
    Replies: 42
    Last Post: 06-17-2008, 11:29 AM
  4. Difference between win32 and linux pthreads
    By philipsan in forum C Programming
    Replies: 1
    Last Post: 02-07-2006, 04:57 PM
  5. pthreads and resources
    By ubermensch in forum C Programming
    Replies: 2
    Last Post: 02-07-2006, 02:27 AM