I am running my program in WinXP with ghc 2.6.8<br><br>If you install netstat and change the parameters it should still work in linux.<br><br><br>Why does thread # 3 dominate over the over threads int the output?<br>Why does thread # 4 never seem to run?<br>
<br>I can&#39;t use the sleep function in System.Process.Win32 since it puts all the <br>threads asleep at the same time. &nbsp;Is there a way to put only one thread asleep?<br><br>That would allow more of a chance for thread #4 to run.<br>
<br><br><br>The simplified program:<br>---------------------------------------------------------------<br><br><br>module Main where<br><br>import Data.IORef<br>import Data.List<br>import System.IO<br>import System.Process<br>
<br>import Control.Concurrent<br>import Control.Concurrent.Chan<br><br><br>data Connection = Null | Last | Active deriving (Eq)<br><br>instance Show Connection where<br>  show Null   = &quot;Null&quot;<br>  show Last   = &quot;Last&quot;<br>
  show Active = &quot;Active&quot;<br><br>instance Read Connection where<br>readsPrec _ s = case take 5 s of<br>    &quot;  UDP&quot;  -&gt; [(Active, &quot;&quot;)]<br>    &quot;  TCP&quot;  -&gt; [(Active, &quot;&quot;)]<br>
    &quot;Last&quot;   -&gt; [(Last,&quot;&quot;)]<br>    _        -&gt; [(Null,&quot;&quot;)]<br><br><br>-- ptrints one 0 and 1<br>main = do<br>  stop &lt;- newIORef False<br>  cbuffer &lt;- newChan :: IO (Chan Connection)<br>
putStr &quot;0&quot;<br>  (_,output,_,ph) &lt;- runInteractiveCommand &quot;netstat -noa 5&quot;<br>  sequence $ map forkIO $ [(processConnections ph output cbuffer), (stopNetstat ph stop False), (printChan cbuffer),(checkStop stop &quot;xxxx&quot;)]<br>
putStr &quot;1&quot;<br>  _ &lt;- waitForProcess ph<br>  --mapM killThread ts<br>  putStrLn &quot;\nDone&quot;<br><br>-- thread # 2<br>processConnections :: ProcessHandle -&gt; Handle -&gt; (Chan Connection) -&gt; IO ()<br>
processConnections ph hout chan = do<br>  h &lt;- hReady hout<br>  e &lt;- getProcessExitCode ph<br>putStr &quot;2&quot;<br>  if (not h &amp;&amp; e /= Nothing) then do writeChan chan Last &gt;&gt; return () else do<br>  if h then do readConnection hout &gt;&gt;= writeChan chan else do<br>
processConnections ph hout chan<br><br>&nbsp;<br>readConnection :: Handle -&gt; IO Connection<br>readConnection hout = do<br>  l &lt;- hGetLine hout<br>  let c = (read l :: Connection)<br>  if (c == Null) <br>    then do (readConnection hout)<br>
    else do (return c)<br><br>-- thread number 3<br>stopNetstat :: ProcessHandle -&gt; (IORef Bool) -&gt; Bool -&gt; IO ()<br>stopNetstat netstat _    True  = terminateProcess netstat<br>stopNetstat netstat gref False = putStr &quot;3&quot; &gt;&gt; yield &gt;&gt; readIORef gref &gt;&gt;= stopNetstat netstat gref<br>
<br><br>--thread 4<br>printChan :: (Chan Connection) -&gt; IO () <br>printChan chan = do<br>putStr &quot;4&quot;<br>  c &lt;- readChan chan<br>printConnection c<br>printChan chan<br><br><br>checkStop :: (IORef Bool) -&gt; String -&gt; IO ()<br>
checkStop ref s = do<br>  if (take 4 s == &quot;stop&quot;) <br>    then do (writeIORef ref True) <br>    else do (getChar &gt;&gt;= (\x -&gt; checkStop ref ((tail s) ++ [x])))<br><br>printConnection :: Connection -&gt; IO ()<br>
printConnection c = case c of<br>  Null -&gt; putStr &quot;N&quot; <br>  Last -&gt; putStr &quot;L&quot;<br>  _    -&gt; putStr &quot;A&quot;<br><br>