Running a "sub-process" which dies with the main program
Hi I couldn't come up with a better subject than this one, so anyways... I have a small program which spawns a subprocess. However, when I hit C-c, the subprocess won't die, instead it will just keep running until it's done or until I kill it. I've looked around in System.Process for something suitable for my needs, but I can't seem to find it. Any ideas? -- Deniz Dogan
Quoth Deniz Dogan
I have a small program which spawns a subprocess. However, when I hit C-c, the subprocess won't die, instead it will just keep running until it's done or until I kill it. I've looked around in System.Process for something suitable for my needs, but I can't seem to find it. Any ideas?
What you want, is the normal behavior for Berkeley/UNIX/POSIX ttys: signals generated by the (pseudo)tty handler from control characters are delivered to all processes in the foreground process group, which is a hereditary distinction where you have to actively opt out. Berkeley job control shells (bash, ksh et al.) reset process group on commands issuing from the terminal, so the foreground process group is whatever you invoked, and any subprocesses thereof (barring further process group resets), and that's what will abort if you press ctrl-C. If it isn't working that way, possibilities might include: - not a POSIX operating system - subprocess set its process group and is no longer in the terminal foreground process group. - a signal handler caught/ignored ctrl-C. Along with process group stuff in System.Process, if both processes are Haskell you might look at System.Posix.Terminal getTerminalProcessGroupID, as a way to directly check the second item above (and I suppose indirectly the first.) Unfortunately, none of these explanations come with any suggested remedy - they're just three ways to say "platform [OS or Haskell implementation] doesn't support POSIX ttys". Donn
2009/6/18 Deniz Dogan
Hi
I couldn't come up with a better subject than this one, so anyways...
I have a small program which spawns a subprocess. However, when I hit C-c, the subprocess won't die, instead it will just keep running until it's done or until I kill it. I've looked around in System.Process for something suitable for my needs, but I can't seem to find it. Any ideas?
With a tip from a person outside of the mailing list I found System.Process.system, which essentially does exactly what I was asking for. However, I would really like some more control over what file descriptors the subprocess should use (specifically stdout and stderr). Looking at the source code for System.Process.system, I find that it uses the "syncProcess" function, which would be useful to me, but isn't exported. So why is syncProcess not exported? Is there any good reason not to? -- Deniz Dogan
Cum, 2009-06-19 tarihinde 11:58 +0200 saatinde, Deniz Dogan yazdı:
2009/6/18 Deniz Dogan
: Hi
I couldn't come up with a better subject than this one, so anyways...
I have a small program which spawns a subprocess. However, when I hit C-c, the subprocess won't die, instead it will just keep running until it's done or until I kill it. I've looked around in System.Process for something suitable for my needs, but I can't seem to find it. Any ideas?
With a tip from a person outside of the mailing list I found System.Process.system, which essentially does exactly what I was asking for.
Hey I'm already subscribed :) You can read from "sout" and "serr" with below example. Hope that it helps. module Main where import System.Process -- using process-1.0.1.1 main = do (_, sout, serr, p) <- createProcess (proc "sleep" ["10"]) { std_out = CreatePipe , std_err = CreatePipe } r <- waitForProcess p return () Regards, -- aycan
2009/6/19 Aycan iRiCAN
Cum, 2009-06-19 tarihinde 11:58 +0200 saatinde, Deniz Dogan yazdı:
2009/6/18 Deniz Dogan
: Hi
I couldn't come up with a better subject than this one, so anyways...
I have a small program which spawns a subprocess. However, when I hit C-c, the subprocess won't die, instead it will just keep running until it's done or until I kill it. I've looked around in System.Process for something suitable for my needs, but I can't seem to find it. Any ideas?
With a tip from a person outside of the mailing list I found System.Process.system, which essentially does exactly what I was asking for.
Hey I'm already subscribed :) You can read from "sout" and "serr" with below example. Hope that it helps.
module Main where
import System.Process -- using process-1.0.1.1
main = do (_, sout, serr, p) <- createProcess (proc "sleep" ["10"]) { std_out = CreatePipe , std_err = CreatePipe } r <- waitForProcess p return ()
Thanks! But this was the approach I used before I went to System.Process.system and it did not work on my Linux machine. Looking at the source code for "system", we see that it uses "syncProcess", which has #ifdef mingw32_HOST_OS (IIRC) in which the code you gave me resides. If mingw32_HOST_OS is not defined, one has to go through quite a bit more trouble to get the same effect. This is why it bugs me a bit that syncProcess is not exported. I can't find any reason not to export it, but what do I know? -- Deniz Dogan
Cum, 2009-06-19 tarihinde 12:42 +0200 saatinde, Deniz Dogan yazdı:
2009/6/19 Aycan iRiCAN
: Cum, 2009-06-19 tarihinde 11:58 +0200 saatinde, Deniz Dogan yazdı:
2009/6/18 Deniz Dogan
: Hi
I couldn't come up with a better subject than this one, so anyways...
I have a small program which spawns a subprocess. However, when I hit C-c, the subprocess won't die, instead it will just keep running until it's done or until I kill it. I've looked around in System.Process for something suitable for my needs, but I can't seem to find it. Any ideas?
With a tip from a person outside of the mailing list I found System.Process.system, which essentially does exactly what I was asking for.
Hey I'm already subscribed :) You can read from "sout" and "serr" with below example. Hope that it helps.
module Main where
import System.Process -- using process-1.0.1.1
main = do (_, sout, serr, p) <- createProcess (proc "sleep" ["10"]) { std_out = CreatePipe , std_err = CreatePipe } r <- waitForProcess p return ()
Thanks!
But this was the approach I used before I went to System.Process.system and it did not work on my Linux machine.
Give it a try. Try to send CTRL-C and look if "sleep 10" (which is a subprocess) process terminates. aycan@aycan:~/haskell$ time ./deniz2 && ps -ef | grep sleep ^C real 0m0.707s user 0m0.001s sys 0m0.004s aycan 13098 4430 0 13:50:23 pts/7 0:00 grep sleep It terminates with ghc 6.10.3 on OpenSolaris. Best Regards, -- aycan
2009/6/19 Aycan iRiCAN
Cum, 2009-06-19 tarihinde 12:42 +0200 saatinde, Deniz Dogan yazdı:
2009/6/19 Aycan iRiCAN
: Cum, 2009-06-19 tarihinde 11:58 +0200 saatinde, Deniz Dogan yazdı:
2009/6/18 Deniz Dogan
: Hi
I couldn't come up with a better subject than this one, so anyways...
I have a small program which spawns a subprocess. However, when I hit C-c, the subprocess won't die, instead it will just keep running until it's done or until I kill it. I've looked around in System.Process for something suitable for my needs, but I can't seem to find it. Any ideas?
With a tip from a person outside of the mailing list I found System.Process.system, which essentially does exactly what I was asking for.
Hey I'm already subscribed :) You can read from "sout" and "serr" with below example. Hope that it helps.
module Main where
import System.Process -- using process-1.0.1.1
main = do (_, sout, serr, p) <- createProcess (proc "sleep" ["10"]) { std_out = CreatePipe , std_err = CreatePipe } r <- waitForProcess p return ()
Thanks!
But this was the approach I used before I went to System.Process.system and it did not work on my Linux machine.
Give it a try. Try to send CTRL-C and look if "sleep 10" (which is a subprocess) process terminates.
aycan@aycan:~/haskell$ time ./deniz2 && ps -ef | grep sleep ^C real 0m0.707s user 0m0.001s sys 0m0.004s aycan 13098 4430 0 13:50:23 pts/7 0:00 grep sleep
It terminates with ghc 6.10.3 on OpenSolaris.
This is copied verbatim from my terminal. I used the exact some code that you gave me. % time ./test && ps -ef | grep sleep ^C real 0m10.005s user 0m0.003s sys 0m0.003s deniz 14095 14047 0 13:05 pts/1 00:00:00 grep sleep What's strange though is that when I hit C-c *twice*, I get this behavior: time ./test && ps -ef | grep sleep ^C^C real 0m0.915s user 0m0.003s sys 0m0.000s This is with GHC 6.10.3 on Arch Linux i686. -- Deniz Dogan
Cum, 2009-06-19 tarihinde 13:09 +0200 saatinde, Deniz Dogan yazdı:
2009/6/19 Aycan iRiCAN
: Cum, 2009-06-19 tarihinde 12:42 +0200 saatinde, Deniz Dogan yazdı:
2009/6/19 Aycan iRiCAN
: Cum, 2009-06-19 tarihinde 11:58 +0200 saatinde, Deniz Dogan yazdı:
2009/6/18 Deniz Dogan
: Hi
I couldn't come up with a better subject than this one, so anyways...
I have a small program which spawns a subprocess. However, when I hit C-c, the subprocess won't die, instead it will just keep running until it's done or until I kill it. I've looked around in System.Process for something suitable for my needs, but I can't seem to find it. Any ideas?
With a tip from a person outside of the mailing list I found System.Process.system, which essentially does exactly what I was asking for.
Hey I'm already subscribed :) You can read from "sout" and "serr" with below example. Hope that it helps.
module Main where
import System.Process -- using process-1.0.1.1
main = do (_, sout, serr, p) <- createProcess (proc "sleep" ["10"]) { std_out = CreatePipe , std_err = CreatePipe } r <- waitForProcess p return ()
Thanks!
But this was the approach I used before I went to System.Process.system and it did not work on my Linux machine.
Give it a try. Try to send CTRL-C and look if "sleep 10" (which is a subprocess) process terminates.
aycan@aycan:~/haskell$ time ./deniz2 && ps -ef | grep sleep ^C real 0m0.707s user 0m0.001s sys 0m0.004s aycan 13098 4430 0 13:50:23 pts/7 0:00 grep sleep
It terminates with ghc 6.10.3 on OpenSolaris.
This is copied verbatim from my terminal. I used the exact some code that you gave me.
% time ./test && ps -ef | grep sleep ^C real 0m10.005s user 0m0.003s sys 0m0.003s deniz 14095 14047 0 13:05 pts/1 00:00:00 grep sleep
What's strange though is that when I hit C-c *twice*, I get this behavior:
Hmm, I think GHC RTS handles SIGINT. I recompiled with thread support and got the same behavour. See: http://hackage.haskell.org/trac/ghc/wiki/Commentary/Rts/Signals When the interrupt signal is received, the default behaviour of the runtime is to attempt to shut down the Haskell program gracefully. It does this by calling interruptStgRts() in rts/Schedule.c (see Commentary/Rts/Scheduler#ShuttingDown). If a second interrupt signal is received, then we terminate the process immediately; this is just in case the normal shutdown procedure failed or hung for some reason, the user is always able to stop the process with two control-C keystrokes. You better install signal handlers using installHandler. Best Regards, -- aycan
2009/6/19 Aycan iRiCAN
Cum, 2009-06-19 tarihinde 13:09 +0200 saatinde, Deniz Dogan yazdı:
2009/6/19 Aycan iRiCAN
: Cum, 2009-06-19 tarihinde 12:42 +0200 saatinde, Deniz Dogan yazdı:
2009/6/19 Aycan iRiCAN
: Cum, 2009-06-19 tarihinde 11:58 +0200 saatinde, Deniz Dogan yazdı:
2009/6/18 Deniz Dogan
: > Hi > > I couldn't come up with a better subject than this one, so anyways... > > I have a small program which spawns a subprocess. However, when I hit > C-c, the subprocess won't die, instead it will just keep running until > it's done or until I kill it. I've looked around in System.Process for > something suitable for my needs, but I can't seem to find it. Any > ideas? With a tip from a person outside of the mailing list I found System.Process.system, which essentially does exactly what I was asking for.
Hey I'm already subscribed :) You can read from "sout" and "serr" with below example. Hope that it helps.
module Main where
import System.Process -- using process-1.0.1.1
main = do (_, sout, serr, p) <- createProcess (proc "sleep" ["10"]) { std_out = CreatePipe , std_err = CreatePipe } r <- waitForProcess p return ()
Thanks!
But this was the approach I used before I went to System.Process.system and it did not work on my Linux machine.
Give it a try. Try to send CTRL-C and look if "sleep 10" (which is a subprocess) process terminates.
aycan@aycan:~/haskell$ time ./deniz2 && ps -ef | grep sleep ^C real 0m0.707s user 0m0.001s sys 0m0.004s aycan 13098 4430 0 13:50:23 pts/7 0:00 grep sleep
It terminates with ghc 6.10.3 on OpenSolaris.
This is copied verbatim from my terminal. I used the exact some code that you gave me.
% time ./test && ps -ef | grep sleep ^C real 0m10.005s user 0m0.003s sys 0m0.003s deniz 14095 14047 0 13:05 pts/1 00:00:00 grep sleep
What's strange though is that when I hit C-c *twice*, I get this behavior:
Hmm, I think GHC RTS handles SIGINT. I recompiled with thread support and got the same behavour.
See: http://hackage.haskell.org/trac/ghc/wiki/Commentary/Rts/Signals
When the interrupt signal is received, the default behaviour of the runtime is to attempt to shut down the Haskell program gracefully. It does this by calling interruptStgRts() in rts/Schedule.c (see Commentary/Rts/Scheduler#ShuttingDown). If a second interrupt signal is received, then we terminate the process immediately; this is just in case the normal shutdown procedure failed or hung for some reason, the user is always able to stop the process with two control-C keystrokes.
You better install signal handlers using installHandler.
Best Regards,
But that's what syncProcess does when mingw32_HOST_OS is not defined. Also, compiling without -threaded doesn't help the problem on my machine, it still acts the same. -- Deniz Dogan
participants (3)
-
Aycan iRiCAN -
Deniz Dogan -
Donn Cave