Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Multi-threaded server

Posted on 2006-11-30
5
Medium Priority
?
197 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
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 opening and writing to files in the C programming language.
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.

636 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