How to process a signal on windows with haskell?

Is there something like System.Posix in windows?

I want the code below to run on Windows, should I change it?

import IO import Control.Exception hiding (catch) import Control.Concurrent import Network import System.Posix ---cannot be run on windows. main = withSocketsDo (installHandler sigPIPE Ignore Nothing >> main') --so the signal handler cannot be used main' = listenOn (PortNumber 9900) >>= acceptConnections acceptConnections sock = do putStrLn "trying to accept" -- debug msg conn@ (h,host,port) <- accept sock print conn -- debug msg forkIO $ catch (talk conn `finally` hClose h) (\e -> print e) acceptConnections sock talk conn@ (h,_,_) = hGetLine h >>= hPutStrLn h >> hFlush h >> talk conn 

for example, if I want the program to exit with ctrl + c, I have to add a handler for SIGINT, so in C ++ write some code as follows:

 void callback(int sig) { // printf("catch\n"); } ... signal(SIGINT,callback); 

But I do not know how to do this in haskell, use FFI?

+6
source share
4 answers
 import Foreign import Foreign.C.Types type Handler = CInt->IO () foreign import ccall "wrapper" genHandler:: (Handler) -> IO (FunPtr Handler) foreign import ccall safe "signal.h signal" install:: CInt->FunPtr Handler->IO CInt main=do s<-genHandler (\x->putStrLn $ "catch "++(show x)) res<- install 2 s putStrLn $ show res s<-getLine 

the above code is what i want to do, just import the signal function with haskell callback.

+4
source

Late to the party, but I wrote a library that might help. This allows you to process signals on both Windows and Linux. This is hackable: https://hackage.haskell.org/package/signal

+4
source

I am not an expert on Windows, so I do not know what you need to do to ignore any event that you ignore here. You will probably want to navigate the Win32 documentation for the version you installed. However, I know a little about the build system and how to get one of two versions of the build function. Thus, the strategy will look like this:

  • Make two directories unix-src and Win32-src (or some similar names).
  • In each directory, place the OSCompat module (or some similar name) containing something like this:

     -- unix-src/OSCompat.hs module OSCompat where import System.Posix ignorePipe = installHandler sigPIPE Ignore Nothing -- Win32-src/OSCompat.hs module OSCompat where import System.Win32 ignorePipe = -- ??? 
  • In your project.cabal file, put something like the following into the executable block:

     if os(windows) build-depends: Win32 hs-source-dirs: Win32-src else build-depends: unix hs-source-dirs: unix-src 
  • Modify the top-level module to look like this:

     import OSCompat main = withSocketsDo (ignorePipe >> main') 
+3
source

I do not think you can do something like this on Windows. Windows is not compatible with Posix, so it does not support signals in full, for example. It does not support signaling to other processes. Also, the number of available signals is quite limited .

If I understood correctly, you ignore SIGPIPE to protect your program from exiting if its output was transferred to another process and then this process ends, right? Sorry, I don’t know how to do this on Windows (and in fact you don’t often see how programs communicate through a simple channel in Windows). If your signal processing is socket related, this may help.

+1
source

Source: https://habr.com/ru/post/914223/


All Articles