
Hello.
import System.Cmd import GHC.Conc
main :: IO () main = forkIO ( do putStrLn "fork" system "ls" return ()) >> getChar >> return ()
When I run this code, I get fork and the result of ls only after I press a key. Does getChar blocks the other threads? Greetings. -- marcot Página: http://marcotmarcot.iaaeee.org/ Blog: http://marcotmarcot.blogspot.com/ Correio: marcot@riseup.net XMPP: marcot@jabber.org IRC: marcot@irc.freenode.net Telefone: 25151920 Celular: 98116720 Endereço: Rua Turfa, 639/701 Prado 30410-370 Belo Horizonte/MG Brasil

On Sun, Sep 14, 2008 at 02:24:23PM -0300, Marco Túlio Gontijo e Silva wrote:
and the result of ls only after I press a key. Does getChar blocks the other threads?
yes, but you can use forkOS from Control.Concurrent and compile with -threaded. See the relevant documentation for the details. Hope this helps, Andrea

Andrea Rossato wrote:
On Sun, Sep 14, 2008 at 02:24:23PM -0300, Marco Túlio Gontijo e Silva wrote:
and the result of ls only after I press a key. Does getChar blocks the other threads?
yes, but you can use forkOS from Control.Concurrent and compile with -threaded.
See the relevant documentation for the details.
forkOS not relevant here. -threaded is necessary to allow haskell code to run whilst FFI calls are blocked. getChar doesn't count as an FFI call (the RTS does its own IO multiplexing) but system does. forkOS is to do with bound threads, that's something else. Jules

On Sun, Sep 14, 2008 at 10:24 AM, Marco Túlio Gontijo e Silva
When I run this code, I get
fork
and the result of ls only after I press a key. Does getChar blocks the other threads?
I think this behavior is caused by (or at least related to) the following GHC bug: http://hackage.haskell.org/trac/ghc/ticket/2363 That bug has been fixed for the 6.10 release; my testing indicated that your program behaves correctly in that version. See the above bug report for some suggestions on how to work around the 6.8 getChar blocking. Hope that helps, -Judah

Em Dom, 2008-09-14 às 11:08 -0700, Judah Jacobson escreveu:
On Sun, Sep 14, 2008 at 10:24 AM, Marco Túlio Gontijo e Silva
wrote: < When I run this code, I get
fork
and the result of ls only after I press a key. Does getChar blocks the other threads?
I think this behavior is caused by (or at least related to) the following GHC bug:
Thanks, I got it to work running threadWaitRead stdInput before getChar. Only changing forkIO for forkOS gave me the same result. Greetings. -- marcot Página: http://marcotmarcot.iaaeee.org/ Blog: http://marcotmarcot.blogspot.com/ Correio: marcot@riseup.net XMPP: marcot@jabber.org IRC: marcot@irc.freenode.net Telefone: 25151920 Celular: 98116720 Endereço: Rua Turfa, 639/701 Prado 30410-370 Belo Horizonte/MG Brasil

Em Dom, 2008-09-14 às 16:07 -0300, Marco Túlio Gontijo e Silva escreveu:
Thanks, I got it to work running
threadWaitRead stdInput
before getChar.
Now I've got another problem:
import Control.Concurrent import System.IO import System.Process
main :: IO () main = do process <- runCommand "wget http://ftp.br.debian.org/debian/pool/main/g/ghc6/ghc6_6.8.2-6_amd64.deb" forkIO $ putStrLn "fork" >> getChar >>= putChar >> terminateProcess process waitForProcess process return ()
Not even fork is shown. Any hints? Greetings. -- marcot Página: http://marcotmarcot.iaaeee.org/ Blog: http://marcotmarcot.blogspot.com/ Correio: marcot@riseup.net XMPP: marcot@jabber.org IRC: marcot@irc.freenode.net Telefone: 25151920 Celular: 98116720 Endereço: Rua Turfa, 639/701 Prado 30410-370 Belo Horizonte/MG Brasil

marcot:
Em Dom, 2008-09-14 às 16:07 -0300, Marco Túlio Gontijo e Silva escreveu:
Thanks, I got it to work running
threadWaitRead stdInput
before getChar.
Now I've got another problem:
import Control.Concurrent import System.IO import System.Process
main :: IO () main = do process <- runCommand "wget http://ftp.br.debian.org/debian/pool/main/g/ghc6/ghc6_6.8.2-6_amd64.deb" forkIO $ putStrLn "fork" >> getChar >>= putChar >> terminateProcess process waitForProcess process return ()
Not even fork is shown. Any hints?
Daemonic threads. When the main thread exits, everything exits. Check the docs for Control.Concurrent. You better use an MVar to ensure the main thread waits on its child. -- Don

Em Dom, 2008-09-14 às 14:52 -0700, Don Stewart escreveu:
marcot:
Em Dom, 2008-09-14 às 16:07 -0300, Marco Túlio Gontijo e Silva escreveu:
Thanks, I got it to work running
threadWaitRead stdInput
before getChar.
Now I've got another problem:
import Control.Concurrent import System.IO import System.Process
main :: IO () main = do process <- runCommand "wget http://ftp.br.debian.org/debian/pool/main/g/ghc6/ghc6_6.8.2-6_amd64.deb" forkIO $ putStrLn "fork" >> getChar >>= putChar >> terminateProcess process waitForProcess process return ()
Not even fork is shown. Any hints?
Daemonic threads. When the main thread exits, everything exits. Check the docs for Control.Concurrent.
You better use an MVar to ensure the main thread waits on its child.
I don't think this is the problem because the mais thread is not exiting. wget takes a lot of time to end, and I never really wait it to finish. I wanted to enable the user to interrupt it, but if it's finished, I don't want to wait for the user input anymore, so the child thread can exit with the main one. I just noticed that if I add a putStrLn "wait" before waitForProcess, it'll print wait and then fork. I couldn't understand why this happened, but I can't still pass through getChar to get to terminateProcess. Is it right to call getChar inside a forkIO? Greetings. -- marcot Página: http://marcotmarcot.iaaeee.org/ Blog: http://marcotmarcot.blogspot.com/ Correio: marcot@riseup.net XMPP: marcot@jabber.org IRC: marcot@irc.freenode.net Telefone: 25151920 Celular: 98116720 Endereço: Rua Turfa, 639/701 Prado 30410-370 Belo Horizonte/MG Brasil
participants (5)
-
Andrea Rossato
-
Don Stewart
-
Judah Jacobson
-
Jules Bean
-
Marco Túlio Gontijo e Silva