c语言中如何将数据从一个进程传递到另一个进程?
有没有一种方法可以将数据(例如:int值)从一个进程传递到c中的另一个进程?
根据我的经验,我们只能从一个进程向另一个进程发送信号。但看起来没有办法将一些信息与该信号一起“附加”到另一个进程。
Is there a way to pass data (ex: int value) from one process to another process in c?
In my experience, we just can send signal from one process to another. But looks like there is no way to "attach" some information along with that signal to another process.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 sigqueue 函数,您可以将单个整数或指针与信号一起传递(但请记住,如果信号的目标是另一个进程,则指针将毫无用处,因为不同的进程不会共享地址空间)。
其他一些方法有管道、共享内存(POSIX 或 SysV 风格)、文件……
With the
sigqueue
function, you can pass a single integer or pointer along with a signal (but keep in mind, pointers will be useless if the target of the signal is another process, since different processes don't share address space).Some other methods are pipes, shared memory (POSIX or SysV style), files, ...
您可以使用各种可用的进程间通信机制之一。
使用谷歌。作为参考,您还可以查看此处
You can use one of the various Inter Process Communication Mechanisms available.
Use Google. As a reference you can also look here
一种干净、便携、强大的方法是使用 Socket。
A clean, portable, powerful way is use Socket.
您可以使用管道来做到这一点。管道的主要用途是在不同进程之间进行数据通信。
管道是操作系统为进程间通信提供的最简单的机制。管道是两个进程之间的通信缓冲区:它有两个描述符,一个用于写入,另一个用于读取。写入和读取操作按照 FIFO 顺序(先进先出)完成。
有两种管道:无名管道和命名管道(也称为 FIFO)。
通过如果您想要一些示例代码,请转到此处: http ://pastebin.com/1W216nyN
You can use pipes to do that. The main purpose of pipes is to communicate data between different processes.
Pipes are the simplest mechanism offered by the operating system for inter-process communication. A pipe is a communication buffer between two processes: it has two descriptors, one for writing another for reading. Write and read operations are done in a FIFO order (first-in-first-out).
There are two kinds of pipes: unnamed pipes and named pipes (also known as FIFOs).
If you want some example code just go here: http://pastebin.com/1W216nyN
我认为我们可以在进程之间使用全局变量,但不确定。如果有人尝试过,请告诉我。如果我们使用包含 extern valriable 的标头,我们可以在另一个 main() 中使用它,它只不过是一个独立的程序(进程)。但我们必须将两个正在执行的 main() 链接在一起。
I think we can use global variable between process, not sure but. If any one tried then please let me know. If we use a header which contain extern valriable , we can use this in another main() which is nothing but a independednt program (process). but we have to link the two main() together which executing.