Re: [LIP] Fork and thread

Saju Pillai <[email protected]>
Newsgroups gmane.user-groups.linux.india.programmers
Message-ID <[email protected]>
On 22/09/05, Shijithlal <[email protected]> wrote:
> I am creating a Unix based application which consist of a TCP Server
> part. I am creating my aplicationa as a daemon.
> In most of the examples I looked at contains the usage of
> "fork()" when a new client request received at the server side. May I
> can use thread instaed of this fork. If possible which one is more
> better and why?.

You can use threads instead of forking a child per request. The answer
as to which model is better is not simple, there are many models that
one could choose from ...

a) fork on demand model : A child process is created to handle the
request once accept() returns. Easy to implement - bad overhead of
forking per request delays throughput.

b) prefork : Fork "N" number of children on startup each blocked on
accept(). The currently scheduled child gets to return from accept and
process the request when the request comes in.  Slightly harder to
implement than (A)  - avoids fork() overhead - this is the unix
solution used in Apache 1.3 series

c) Create Thread on demand : You could create a thread as soon as the
request comes in - thread creation is costly - though less costlier
than creating a process (on most platforms).

d) pre-create threads : Same as (c) except a listener thread can
listen for requests and dequeue accepted sockets to userland and hand
them over to free "worker" threads ... Apache 2.0 uses this model on
unix

You can choose from the following - or mix and match as per your needs.


>
> Actually I am develping the application in Linux and I want to port
> the application to other Unix flavours.

If you go for threads, I would suggest you use pthreads - a well
written pthread program should be easily portable to any platform that
supports pthreads

>
> When i am seraching o net, I found a word from one programmer that, a
> thread calling fork may create problem. Is it correct??

If you call fork() from a thread, the resultant process will hold the
entire image of the parent - but will be single threaded and will hold
all resources (like mutexes) that belonged to the calling parent
thread. So you should not do async stuff

regards
srp

>
> Please comment your suggestions nad opinions..
>
>
> --
> Shijithlal L V
> Fischer Systems India
> 224 NILA Technopark
> Trivandrum 695581
> INDIA
>
> Ph:  91 471 2527427
> Cell: 91 9349100636
> email: [email protected],[email protected]
>
>
> -------------------------------------------------------
> SF.Net email is sponsored by:
> Tame your development challenges with Apache's Geronimo App Server. Download
> it for free - -and be entered to win a 42" plasma tv or your very own
> Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
> _______________________________________________
> linux-india-programmers mailing list
> linux-india-programmers-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
> https://lists.sourceforge.net/lists/listinfo/linux-india-programmers
>


-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.