LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-30-2003, 07:50 AM   #1
powerplane
LQ Newbie
 
Registered: Apr 2003
Location: P.R. China
Distribution: FreeBSD 4.8 stable
Posts: 26

Rep: Reputation: 15
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 ?
 
Old 06-01-2003, 04:08 AM   #2
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
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
 
Old 06-01-2003, 04:59 AM   #3
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
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).
 
Old 06-01-2003, 07:51 AM   #4
powerplane
LQ Newbie
 
Registered: Apr 2003
Location: P.R. China
Distribution: FreeBSD 4.8 stable
Posts: 26

Original Poster
Rep: Reputation: 15
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.
 
Old 05-05-2006, 03:07 AM   #5
vskgopu
LQ Newbie
 
Registered: May 2006
Posts: 17

Rep: Reputation: 0
Which is best for implementing SMTP servers in unix.. fork or pthread ?
 
Old 05-06-2006, 01:19 AM   #6
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
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.
 
Old 05-06-2006, 05:28 AM   #7
sibtay
Member
 
Registered: Aug 2004
Location: U.S
Distribution: Ubuntu
Posts: 145

Rep: Reputation: 15
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
 
Old 05-06-2006, 05:32 AM   #8
sibtay
Member
 
Registered: Aug 2004
Location: U.S
Distribution: Ubuntu
Posts: 145

Rep: Reputation: 15
(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).
 
Old 05-07-2006, 09:43 PM   #9
vskgopu
LQ Newbie
 
Registered: May 2006
Posts: 17

Rep: Reputation: 0
Thanks friend
 
Old 05-08-2006, 08:07 AM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

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

http://www.radwin.org/michael/blog/2...d_harmful.html
 
Old 05-09-2006, 01:46 AM   #11
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
Beware the "XYZ sucks" stance and carefully weigh the alternatives. Please tell me where does Stevens say "don't use threads".
 
Old 05-09-2006, 03:02 AM   #12
vskgopu
LQ Newbie
 
Registered: May 2006
Posts: 17

Rep: Reputation: 0
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 ??
 
Old 05-09-2006, 04:42 AM   #13
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
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.
 
Old 05-09-2006, 09:19 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
Old 01-20-2010, 02:41 PM   #15
manish041083
LQ Newbie
 
Registered: Jun 2007
Posts: 5

Rep: Reputation: 0
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/
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
what happens if i create a thread and then fork the process? Thinking Programming 7 10-31-2005 02:22 PM
Sockets, pthread, pipe, and fork -- a messaging server ggravarr Programming 3 06-28-2005 02:39 PM
[thread control suggestion] add a "solved" button that the thread starter can click atom LQ Suggestions & Feedback 3 03-24-2005 11:55 AM
Main thread sending notification to child thread rajesh_b Programming 1 09-22-2004 09:15 AM
configure qt thread issue (just compiled qt w/ -thread option) cleff Linux - Software 8 05-07-2004 11:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration