user188276
user188276

Reputation:

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.

Upvotes: 4

Views: 7023

Answers (5)

john
john

Reputation: 1

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.

Upvotes: -2

andrepcg
andrepcg

Reputation: 1331

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

Upvotes: 0

secmask
secmask

Reputation: 8107

A clean, portable, powerful way is use Socket.

Upvotes: 0

Jay
Jay

Reputation: 24895

You can use one of the various Inter Process Communication Mechanisms available.

Use Google. As a reference you can also look here

Upvotes: 2

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215193

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, ...

Upvotes: 2

Related Questions