tags:

views:

132

answers:

2

Hello

gcc 4.4.1

I am writing a server program where many clients will connect to the client and the server will manage the clients request and manage the state of the client. Many clients under high load would be a deciding factor in using Asynchronous programming. However, I have never done Async in C.

However, from what I understand from my research I think I need to use Async.

1) Using Synchronous programming would mean that calls would be blocked and would create a bottle neck. The system would just crash under high load

2) Using Synchronous programming using threads would create a new thread for each client, which would mean under high load the system resources could be used up and could make the system over complex handling all those threads.

3) Using Async would mean I would have to develop a state machine???

Can anyone point me in the right direction? In programming in Aysnc?

Many thanks for any advice,

+1  A: 

put the incoming requests into a queue

use one thread to dispatch the queue

requests taken from the queue are executed in their own thread; the queue can throttle the number of active threads (the thread pool) to prevent overloading the server. Requests won't be lost, but they may sit in the queue for a little while awaiting processing.

the queue manager can also kill threads that are taking too long, if you like

good luck!

Steven A. Lowe
Hello. Thanks for your answer. Would the queue be in the form of a linked list. And would the dispatcher be getting requests from the queue? Many thanks.
robUK
@[robUK]: you can implement the queue however you like, and yes, the dispatcher pulls requests from the queue and processes them in their own thread
Steven A. Lowe
+1  A: 

There are many ways to handle this problem.

You can write a program that monitors a port and spawns a thread whenever a request is received.

You can write a single-thread program and use inetd to set it up as a handler for a given port. An instance of your program will start whenever a client connects.

You can use state machines, queues, event driven models, all sorts of things.

Seriously, you need to read a couple of books on network programming. Until you understand what you are doing wou will struggle to produce anything useful.

Michael J