LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   fork vs thread/pthread (https://www.linuxquestions.org/questions/programming-9/fork-vs-thread-pthread-62667/)

powerplane 05-30-2003 07:50 AM

fork vs thread/pthread
 
To built a TCP server(the most simplest is a tcp echo server), there two ways to support multi-client connections in general: fork and pthread/thread.

fork is more expansive than thread, I think fork is the old fashion things, and thread should be pop in some new softwares. But when I take quick look("grep -r") on some very pop server's src, I surprisingly found that most of them use fork and only very few of them use thread/pthread.

Fork:
telnetd(freebsd), vsftpd, proftpd, Apache13, Apache2, thttpd, firebird(a bbsd, not the database), PostgreSQL, MySQL-323

pthread:
Apache13, Apache2, MySQL 323

If I want to built a new server, which should I use? fork or thread?
Any suggestions? Or other alternatives ?

llama_meme 06-01-2003 04:08 AM

Well fork is pretty universal; it'll work on any UNIX system. Most UNIXs support POSIX threads by now, but the quality of the implementation varies.

I'd use pthreads, because they're much easier to work with (since you can easily have access to data structures shared between threads, etc.)

Alex

Mara 06-01-2003 04:59 AM

I use pthreads, too. They're 'lighter' and it's easier to share data.
You can also use 'select' (use 'man 2 select' for more info).

powerplane 06-01-2003 07:51 AM

I also post a thread on bsdforums.org, I get some other opinons, I think you may be interested.
http://www.bsdforum.org/forums/showt...threadid=10384
Quote:

Originally posted by AbEnd
Ever heard of multiplexed (man 2 select poll kqueue) or signal driven (man 2 fcntl) IO? AFAIK multiplexed might be generally faster than threaded.

You're wrong to categorize some of those daemons as "uses fork for parallel stuff", some uses fork for other things (not based on the connections).

What you should use really depends on how you want it to work, not just performance... I think inetd is good for telnet because it isolates the different sessions (since telnet is important) and FTP cause you can change the creds of the processes.

But HTTP always get alot of [new] connections, that's why thttpd uses multiplexed IO, Apache 1 uses preforking, Apache 2 can use threading, etc.


vskgopu 05-05-2006 03:07 AM

Which is best for implementing SMTP servers in unix.. fork or pthread ?

primo 05-06-2006 01:19 AM

On Linux it is said that "processes are threads", so there's not much of a difference if these new processes do some precise jobs without tinkering with global data that triggers the Copy-On-Write scheme so they work on their own environment. There is an entire chapter in Stevens' book on many of the approaches that you may use on servers: preemptive forks, threads and so on. With threads, be prepared to use locking around global data. It would be better to start the core of the server itself and networking/protocol stuff and later try the threads stuff.

sibtay 05-06-2006 05:28 AM

Personally i would use fork, because of the following reasons:

1) Fork is more universally accepted than threads.

2) Considering the type of application which you are working on, there wont be much of Interprocess communication (ipc) required. Actually threads
really win the race when it comes to inter communication. Since threads share the same memory space hence sharing data between them is really faster as
compared to seperate processes. Where you have to either employ costly approaches like pipes, fifos, shared memory etc.

3) Developement is much easier on a fork based implementation.

4) Fork based implementations are far more maintainable.

5) If the application is in C, then it must be having some global data. In a multi threaded application, all that global data *must* be protected with locks,
since it will be shared by all the threads. And locks can prove very costly (refer to the laws of mutual exclusion and critical sections). In contrast in
a multi process implementation each process has its own copy of global data.

Regards,
Sibtay

sibtay 05-06-2006 05:32 AM

(sorry, i forgot to add in my previous post)

I have'nt discussed the advantages of threads. However it should be considered that if properly designed and implemented threads give you
more speed (because there aint any process level context switching in a multi threaded application).

vskgopu 05-07-2006 09:43 PM

Thanks friend

bigearsbilly 05-08-2006 08:07 AM

the stevens' book says don't use threads.
that's good enough for me.

http://www.radwin.org/michael/blog/2...d_harmful.html

primo 05-09-2006 01:46 AM

Beware the "XYZ sucks" stance and carefully weigh the alternatives. Please tell me where does Stevens say "don't use threads".

vskgopu 05-09-2006 03:02 AM

I got confused now....shall i use thread to implement a SMTP relay server...?
What is the max number of threads/fork that can be created a process/parent process..?
My friend is using fork to implement the SMTP relay server....I thought of using thread..
What shall i do now ??

primo 05-09-2006 04:42 AM

Quote:

Originally Posted by vskgopu
What is the max number of threads/fork that can be created a process/parent process..?

Run "getconf CHILD_MAX" in the shell. It prints the value of the sysconf(_SC_CHILD_MAX) call. It's related to process limits (run "ulimit -Ha" and "ulimit -Sa") that may be increased and/or decreased by normal users with some limitations (see "man getrlimit"). Also, in some Unix systems, the hard limits may be increased by means of a sysctl() call.

chrism01 05-09-2006 09:19 PM

It's easier to share global values in threads, but be aware that in 2.6 kernel they are created as lightweight processes within the main thread/process, so you only get 1 pid.
If you don't need to share data, then fork is an option and each process will have it's own pid.
I've written a threaded Perl prog that works fine on 2.4 & 2.6 kernel, but I need some global shared data. Note that in Perl, NO vars are shared by default, so it's easy to avoid cross-thread variable update issues.

manish041083 01-20-2010 02:41 PM

I find this link which could be helpful in clearing few facts about forking and threading.

http://www.geekride.com/index.php/20...-linux-kernel/


All times are GMT -5. The time now is 11:51 PM.