UNIX 上的进程间通信
我必须在 SOLARIS 9 SPARC 上用 C 实现一些机制,以允许进程间通信。
简而言之,我必须实现一个多线程程序,其中线程的父级一旦收到信号,或者无论您想要什么,都必须触发一组负责加密文件的线程。
我无法使用某些 TCP 套接字解决方案与该程序进行通信。
我正在考虑使用系统信号(并通过kill -s SIGNAL PID触发进程)或通过域unix套接字。
你怎么认为?您能给我建议一些其他解决方案吗?
I have to implement some mechanism in C on SOLARIS 9 SPARC in order to allow interprocess communication.
In a nutshell, i have to implement a multithread program where the father of the thread once receive a signal, or whatever you want, has to trigger a set of thread that are in charge of encrypting files.
I cannot use some TCP socket solution to communicate with this program.
I was thinking on using system signals (and triggering the process by kill -s SIGNAL PID) or by domain unix socket.
What do you think? Can you suggest me some other solutions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
其他解决方案:共享内存段、管道、Unix 套接字(嘿,它们不是 TCP :)),...
other solutions: shared memory segments, pipelining, Unix sockets (hey, they're not TCP :)), ...
信号是一个完美的解决方案,而且比许多解决方案都简单。我自己可能会用它。其他大多数涉及对主机环境的更多更改,这些更改将使进程寿命更长和/或使同时运行系统的多个副本变得更加困难(因为您必须自己管理不同的文件名、端口号、共享内存 ID 等,而操作系统已经跟踪 PID 并允许它们作为杀掉的目标)。
不过,如果您想要其他选择,命名管道非常非常容易使用。在您的 shell 中,只需“mkfifo xyz”即可在当前目录中创建一个名为“xyz”的管道。然后你可以让你想要的线程阻塞读取管道,然后 echo "go boy go" > xyz 进入管道,您的控制线程将退出 read()。
A signal is a perfectly good solution, and simpler than many. I would probably use that myself. Most of the others involve more changes to the host environment that will outlive the process and/or make it harder to run multiple copies of the system concurrently (because you have to manage distinct filenames, port numbers, shared memory ids etc yourself whereas the OS already tracks PIDs and allows them as a target for kill).
Still, if you want another option, a named pipe is very, very easy to use. In your shell, just "mkfifo xyz" to create a pipe named "xyz" in your current directory. Then you can have the thread you want to signal block reading the pipe, then
echo "go boy go" > xyz
into the pipe and your control thread will exit the read().如果我理解您的问题,请查看 POSIX 消息队列,特别是 mq_notify()。您可以通过消息队列进行通信,并将其设置为在收到消息时自动生成线程。该线程读取队列中的文件名等并进行加密。
If I understand your question, take a look at POSIX message queues, specifically mq_notify(). You can communicate through the message queue and set it up to automatically spawn a thread when a message is received. That thread reads the queue for the filename and whatever and does the encryption.