Solved

Multi-threaded server

Posted on 2006-11-30
5
188 Views
Last Modified: 2010-04-15
To write a server that fulfills request simultaneously, how would you launch a thread?  Do you do it after the accept(), sort of like this:

pthread_t thread[100];

for (;;) {

   if (accept (m_sock, (sockaddr *) &m_addr, (socklen_t *) &addr_length) != -1) {
       pthread_create(&thread[n], NULL, func2, (void*)param);
   }
}

void* func2(void* param) {
  recv();
  process_request();
  send();
}

Is that essentially how it would work?
0
Comment
Question by:chsalvia
  • 3
  • 2
5 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 250 total points
ID: 18051359
Hi chsalvia,

> Do you do it after the accept(), sort of like this:
Yes ... that is one way of doing it ... note that it will spawn a thread for each request and each thread would process only one request before it dies.

An alternative approach is to keep a thread pool and use threads from this pool to service requests .. threads are returned to this pool when they finish respective tasks. Max and min number of threads are maintained to give optimal performance. This saves overhead of spwaning anf killing threads for each request.

Former is suitable for lightly loaded servers while latter suits heavily loaded servers.

Cheers!
sunnycoder
0
 

Author Comment

by:chsalvia
ID: 18051403
So, threads in the pool are created initially when the program executes, and then just sleep until called?
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 18051413
Yes ... There are two main parameters ... MaxThreads and MinThreads

Upon startup, main thread spawns MinThreads threads which stay free and sleep/wait until they are assigned some work by the main thread. If all MinThreads are in use, server will spawn more threads until MaxThreads limit is reached. When system is lightly loaded, main threads kills a few threads to free up the resources and bring thread count to MinThreads.

This kind of startegy was used in Apache web server.
0
 

Author Comment

by:chsalvia
ID: 18054829
I see.  How does the main thread "call" a sleeping thread?  With POSIX threads, I thought that the main thread basically calls pthread_creat, but then after that how can the main thread communicate with a child thread?
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 18059765
You can use conditional variables
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now