81288

Using sockets in multithread server

Question:

guys!

I'm developing multithread server on c under *nix. In the main thread of the process I have listening socket which waits for connections (accept). When it gets a connection (accept returns client socket descriptor) I start new pthread whith some routine, which answers the request and closes client socket descriptor. It looking something like this:

while(1) { sock_cl =(int *)malloc(sizeof(int)); *sock_cl = accept(sock_serv, NULL, NULL); pthread_create(thread, attr, answer, sock_cl); }

answer function:

void *answer(void *arg) { int sock_cl; char *buf; ssize_t r; lsres_t *ans; char *path; char status[STATUS_SIZE]; sock_cl = *((int *)arg); if((buf = (char *)malloc(BUF_SIZE + 1)) == NULL) { sprintf(status, "%i\n", -1); send_data(sock_cl, status, STATUS_SIZE); close(sock_cl); free(arg); return NULL; } memset(buf, '\0', BUF_SIZE + 1); if((r = recv(sock_cl, buf, BUF_SIZE, 0)) <= 0) { close(sock_cl); free(arg); return NULL; } path = strtok(buf, "\r\0"); if((ans = lscreate()) == NULL) { sprintf(status, "%i\n", -1); send_data(sock_cl, status, STATUS_SIZE); } else { if(myls(path, ans) != 0) { sprintf(status, "%i\n", -1); send_data(sock_cl, status, STATUS_SIZE); } else { sprintf(status, "%i\n", ans->status); send_data(sock_cl, status, STATUS_SIZE); send_data(sock_cl, ans->buf, ans->written_size); } lsdestroy(ans); } close(sock_cl); free(arg); return NULL; }

In answer function I call recv to recive data from client and send some answer. It works fine under Linux systems (Arch, Debian, Ubuntu), but when I try to run it under Unix (Solaris), I get a Segmentation fall in recv function. The problem is not in buffer, because if i try to call answer function in the same thread as answer function, I have no Segmentation fall. So there is some issue in using sockets with pthreads.

Please, advice me any solution how to use sockets in multithread server. Thank's for the future answers!

Answer1:

On Solaris, you generally need to compile with -mt to correctly compile and link a multithreaded program. If you don't use it, you'll get random crashes in threads like you see.

Answer2:

I think in unix recv function fails and the segmentation faut is due to the code segment free(args);

Answer3:

May be this could be the reason

close(sock_cl); free(arg); return NULL;

This sock_cl is shared among threads and because there is no synchronization for the answer function and because you are freeing this pointer other threads bound to crash, if possible you check this same program in by running in valgrind for corruption.

Recommend

  • IPC using pipes?
  • What type should be used to loop through an array? [duplicate]
  • Python, stdout , C and SWIG
  • Using write() to write the bytes of a variable to a file descriptor?
  • Understanding posix barrier mechanism
  • How to pass multiple parameters to a thread function
  • How to dynamically reshape matrix block-wise? [duplicate]
  • How To Pass a MultiDimensional Array from Javascript to server using PageMethods in ASP.Net
  • Can I convert bool in model to yes/no in a QComboBox
  • Does Julia have something equivalent to Ans (Matlab) or Last.value (R)
  • APNs messages are delivered but not received on iOS device
  • PHP socket_read() only gets the first byte of the stream
  • inputParser validation argument not acting as expected
  • java.net.SocketTimeoutException on embedded tomcat with jersey
  • How to eliminate warning for passing multidimensional array as const multidimensional array?
  • vectorized indexing/slicing in numpy/scipy?
  • SyntaxError: (irb):26: both block arg and actual block given
  • Undefined references when compiling gSOAP client
  • how does System.Web.HttpRequest::PathInfo work?
  • IE11 throwing “SCRIPT1014: invalid character” where all other browsers work
  • Reading a file into a multidimensional array
  • How to remove a SwiftyJSON element?
  • RxJava debounce by arbitrary value
  • pyodbc doesn't report sql server error
  • NSScanner Loop Question
  • Display java JPanel in a JFrame
  • copying resource to sdcard gives a damaged file in android
  • how to adjust image in a panel in Java swing?
  • Encrypt data by using a public key in c# and decrypt data by using a private key in php
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • AES padding and writing the ciphertext to a disk file
  • script to move all files from one location to another location
  • ILMerge & Keep Assembly Name
  • Symfony2: How to get request parameter
  • Convert array of 8 bytes to signed long in C++
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • LevelDB C iterator
  • How can i traverse a binary tree from right to left in java?