?
Solved

Multi-threaded server

Posted on 2006-11-30
5
Medium Priority
?
195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures 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.
Suggested Courses

752 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