[Haskell-beginners] Why does forkIO also create native threads?

Lych lych77 at gmail.com
Mon Apr 13 04:37:22 EDT 2009

I wrote a simple ECHO server like this:
import Network
import System.IO
import Control.Concurrent

service cs = do
        ln <- hGetContents cs
        hPutStr cs ln
        service cs

acceptloop ls = do
     (cs, host, port) <- accept ls
     hSetBuffering cs LineBuffering
     forkIO (service cs)
     acceptloop ls

main = do
     ls <- listenOn (PortNumber 10061)
     acceptloop ls

And tested it with a client that initiates as many parallel
connections as possible. The number of connections can just reach to
1000+ on my machine, but I thought it should be more, since forkIO
generates lightweight threads. I also examined the number of threads
in this process with some tool and it proved there was one native
thread per connection. When I replaced the 'forkIO' in the program
with 'forkOS', I got an exactly similar result. Have I thought
something wrong about forkIO?

More information about the Beginners mailing list