如何在Haskell中的父级和分叉子进程之间共享数据?

时间:2022-01-29 17:43:15

How would I even go about forking a child process using Haskell in the first place?

我怎么会首先使用Haskell来分配子进程呢?

Also, if pipes are an obvious solution to the data sharing question - is there any other way to do it besides using pipes? I'm familiar with the use of shared memory segments in C (the shmget, *shmat, shmdt and shmctl functions). Could Haskell be able to imitate this? If so, how?

此外,如果管道是数据共享问题的明显解决方案 - 除了使用管道之外还有其他方法吗?我熟悉在C中使用共享内存段(shmget,* shmat,shmdt和shmctl函数)。哈斯克尔能够模仿这个吗?如果是这样,怎么样?

I'd be very grateful for any help you could spare.

我非常感谢你能提供的任何帮助。

I must admit I'm very much new to functional programming languages, even more so when it comes to Haskell. So forgive me (and please correct me) if I said something silly.

我必须承认我对函数式编程语言非常陌生,在Haskell方面更是如此。如果我说傻话,请原谅我(请纠正我)。

4 个解决方案

#1


2  

Better yet, use Software Transactional Memory - that is, TVars and TChannels.

更好的是,使用软件事务内存 - 即TVars和TChannels。

Will recommend the same book, different chapter: http://book.realworldhaskell.org/read/software-transactional-memory.html

将推荐相同的书,不同的章节:http://book.realworldhaskell.org/read/software-transactional-memory.html

Here is a good small example of this technique in action: http://sequence.complete.org/node/257

以下是该技术的一个很好的小例子:http://sequence.complete.org/node/257

#2


1  

The OP asked about communicating with a subprocess, not a thread. For that, pipes are a perfectly fine way of doing it. You can also call the C library function directly from Haskell if you want to, although that could get tricky.

OP询问与子进程通信,而不是线程。为此,管道是一种非常好的方式。如果你愿意,你也可以直接从Haskell调用C库函数,尽管这可能会变得棘手。

This question has a better answer over here: Is there some standard Haskell library dealing with process communication?

这个问题在这里有一个更好的答案:是否有一些标准的Haskell库处理进程通信?

#3


0  

use MVars or Channels. See chapter 24 of RealWorld Haskell: http://book.realworldhaskell.org/read/concurrent-and-multicore-programming.html

使用MVars或频道。请参阅RealWorld Haskell的第24章:http://book.realworldhaskell.org/read/concurrent-and-multicore-programming.html

#4


0  

If you want to actually fork a process, Unix style, you need to use forkProcess as given by http://hackage.haskell.org/package/unix-2.4.2.0/docs/System-Posix-Process.html

如果你想实际分叉一个进程,Unix风格,你需要使用http://hackage.haskell.org/package/unix-2.4.2.0/docs/System-Posix-Process.html给出的forkProcess。

In this case, MVars and TVars do not do interprocess communication, so you cannot use them to do IPC. All standard techniques for IPC (pipes, sockets, etc) still work. If you want something more high-level, check out Cloud Haskell http://www.haskell.org/haskellwiki/Cloud_Haskell

在这种情况下,MVars和TVars不进行进程间通信,因此您不能使用它们来进行IPC。 IPC(管道,插座等)的所有标准技术仍然有效。如果你想要更高级别的东西,请查看Cloud Haskell http://www.haskell.org/haskellwiki/Cloud_Haskell

#1


2  

Better yet, use Software Transactional Memory - that is, TVars and TChannels.

更好的是,使用软件事务内存 - 即TVars和TChannels。

Will recommend the same book, different chapter: http://book.realworldhaskell.org/read/software-transactional-memory.html

将推荐相同的书,不同的章节:http://book.realworldhaskell.org/read/software-transactional-memory.html

Here is a good small example of this technique in action: http://sequence.complete.org/node/257

以下是该技术的一个很好的小例子:http://sequence.complete.org/node/257

#2


1  

The OP asked about communicating with a subprocess, not a thread. For that, pipes are a perfectly fine way of doing it. You can also call the C library function directly from Haskell if you want to, although that could get tricky.

OP询问与子进程通信,而不是线程。为此,管道是一种非常好的方式。如果你愿意,你也可以直接从Haskell调用C库函数,尽管这可能会变得棘手。

This question has a better answer over here: Is there some standard Haskell library dealing with process communication?

这个问题在这里有一个更好的答案:是否有一些标准的Haskell库处理进程通信?

#3


0  

use MVars or Channels. See chapter 24 of RealWorld Haskell: http://book.realworldhaskell.org/read/concurrent-and-multicore-programming.html

使用MVars或频道。请参阅RealWorld Haskell的第24章:http://book.realworldhaskell.org/read/concurrent-and-multicore-programming.html

#4


0  

If you want to actually fork a process, Unix style, you need to use forkProcess as given by http://hackage.haskell.org/package/unix-2.4.2.0/docs/System-Posix-Process.html

如果你想实际分叉一个进程,Unix风格,你需要使用http://hackage.haskell.org/package/unix-2.4.2.0/docs/System-Posix-Process.html给出的forkProcess。

In this case, MVars and TVars do not do interprocess communication, so you cannot use them to do IPC. All standard techniques for IPC (pipes, sockets, etc) still work. If you want something more high-level, check out Cloud Haskell http://www.haskell.org/haskellwiki/Cloud_Haskell

在这种情况下,MVars和TVars不进行进程间通信,因此您不能使用它们来进行IPC。 IPC(管道,插座等)的所有标准技术仍然有效。如果你想要更高级别的东西,请查看Cloud Haskell http://www.haskell.org/haskellwiki/Cloud_Haskell