Reputation: 581
I am doing socket programming in C. In essence, I need a server that runs the indefinite listening loop in a separate thread and whenever it accepts a new connection, it must create a new client thread to handle the client's requests.
Below I have the main function that declares a port number and calls the function createListenThread. This function creates a new thread and invokes the function listenLoop.
int main(int argc, char *argv[])
{
int client_port = 6000;
createListenThread(client_port);
}
void createListenThread(int listen_port)
{
pthread_t listen_tid;
printf("In createListenThread\n");
// listenLoop(&listen_port);
if(pthread_create(&listen_tid, NULL, listenLoop, &listen_port) != 0)
socketError("Could not create thread\n");
}
void *listenLoop(void *arg)
{
pthread_detach(pthread_self());
int listen_socket, listen_port, *client_socket, struct_size;
struct sockaddr_in client_addr;
pthread_t client_tid;
listen_port = *((int *)arg);
listen_socket = createSocket(listen_port);
struct_size = sizeof(struct sockaddr_in);
while(1)
{
printf("In ListenLoop\n");
client_socket = malloc(sizeof(int));
*client_socket = -1;
*client_socket = accept(listen_socket, (struct sockaddr *)&client_addr, &struct_size);
printf("Received connection request from (%s , %d)\n",
inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
pthread_create(&client_tid, NULL, starterFunction, client_socket);
}
close(listen_socket);
}
My problem is that whenever I run the server, only "In ListenThread" and "In ListenLoop" is never printed. I have even tried fprintf(stdout, "In ListenLoop") and fflush(stdout) but the statement is still not printed. When I comment out:
if(pthread_create(&listen_tid, NULL, listenLoop, &listen_port) != 0)
socketError("Could not create thread\n");
and simply invoke ListenLoop as follows:
listenLoop(&listen_port);
Both the print statements appear. Is there an obvious mistake in the way I'm creating the thread and invoking the ListenLoop function? Is the function ListenLoop ever executed?
Edit: I ran the program in gdb which printed the following:
In createListenThread
[New Thread 0xb7e30b70 (LWP 10024)]
[Thread 0xb7e30b70 (LWP 10024) exited]
[Inferior 1 (process 10021) exited normally]
Why is the thread exiting??
Upvotes: 0
Views: 196
Reputation: 148890
I hope that you declare all your functions above main ... But you should really call your listenLoop
in main thread. Else, you create a thread to run the loop and main thread passively waits it. And you will not be bothered by argument passing, thread waiting and so on.
listenLoop
could then be a true void and no longer a void *, as you do not return anything.
If you were forking a process, it would make sense, since it is the correct way to create a daemon process immediateley adopted by init process, but for threads it is simply useless.
Upvotes: 0
Reputation: 21351
The problem will be that your main function returns immediately after calling createListenThread
. You should wait for your thread function to finish using pthread_join
within your createListenThread
otherwise the program may terminate before the thread function can finish. Calling pthread_join
on your thread will wait until the thread function returns and will therefore definitely get chance to run before main()
returns.
Upvotes: 2