Solved

Multi-threaded server

Posted on 2006-11-30
5
191 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

856 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