How to pass data from one process to another in 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.
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
A clean, portable, powerful way is use Socket.
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).
- Unnamed pipes only allow communication between hierarchically related processes (parent and child processes);
- Named pipes allow the communication between any process. A special file is created in the file-system through
If you want some example code just go here: http://pastebin.com/1W216nyN
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.
精彩评论