Fork or Thread ?

I have researched this a couple of times, but still have not gotten any "aha!" moment on this subject.
Context: www on hosted service.
Wrote a TCP/IP server, listening on a port.
When it gets a data stream I need to perform some actions to it.
Anyway, all works well, but in its current incarnation it is a 'iterative' server, i.e. one request at the time ( with some limited amount of requests that can be buffered, up to 5 I believe ). In order to make it 'concurrent' I'll need to create a process for each request.
Question is: do I fork it or do I create a thread? Currently it looks like fork will potentially give me the best security but probably the most overhead, even with WOC (Write On Change; OS:Linux 2.6), which I'm almost positive will be evoked 90% of the time. I currently have no traffic statistics, but would like to accommodate at least 20 requests or so at the time.
Could anyone educate me a bit on this? Fork or Thread?
Thank you,
yves.
As you pointed out, forking a new process involves processing overhead. As a good rule of thumb, do this if it needs to be there for a while servicing multiple TCP messages from the one client, or if it needs to do a lot of processing or I/O. Use a thread if it's a quick job and there are a lot of requests. There are always exceptions to this, system resources; processors and memory have an influence on this decision for example, and all the service requests having to use shared resources puts more weight behind using threads. It's all a bit of a balancing act.

Hope this helps

Bertha

EDIT: Forgot to mention that the open file handles/sockets limit (usually about 64) for a single process can force you in to forking processes.
Last edited on
Topic archived. No new replies allowed.