Solved

Multi-threaded server

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

728 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