gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:04:51 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com