haskell - 如何用haskell处理Windows上的信号?

标签 haskell signals

Windows 上有类似 System.Posix 的东西吗?

我希望下面的代码在 Windows 上运行,我应该更改它吗?

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

例如,如果我希望程序在 ctrl+c 时退出,我必须为 SIGINT 添加一个处理程序,因此在 C++ 中,编写如下代码:

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

但我不知道如何在 haskell 中执行此操作,使用 FFI 吗?

最佳答案

虽然迟到了,但我写了一个可以提供帮助的库。它允许在 Windows 和 Linux 上进行信号处理。它可以在 hackage 上使用: https://hackage.haskell.org/package/signal

关于haskell - 如何用haskell处理Windows上的信号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10333161/

相关文章:

haskell - 当没有给出上下文时,Haskell 中 return 5 的类型是什么?

file - 如果在读取之前它不存在,则haskell 将其写入文件

C 使用信号停止子进程

linux - 我可以在哪里或如何将我的代码挂接到 TDaemonApplication 上的 Unix 信号(主要是 SIGHUP)?

handler - 信号处理程序中的不可重入函数?

function - Haskell 整数奇数检查器

haskell - 为什么 GHC 提示类型错误?

haskell - 如何创建一个包含可变向量的类型?

c++ - Qt-C++ 防止插槽执行方法,当前一个插槽已经 dit 并且尚未完成时

linux - 是否可以在信号处理程序中设置计时器?