user3606309
user3606309

Reputation: 61

Pipelines in C - Do I have to use fork?

Let's just assume I'm working with C only, in a Linux environment.

Normally, if you want to use a pipe() function, you would create a pipe and then fork it, thereby allowing the parent to communicate with the child, and vice-versa.

But what if It's not a parent and child? If I have an old process that's running, is it possible to communicate with it using the pipe() function? This process is not a parent of(or related in any way to) my current process, but I have it's pid. Am I restricted to file or socket for interprocess communication?

Is there any way I can possibly specify a pid and receive information from it without using sockets?

Upvotes: 1

Views: 142

Answers (2)

Or use fifo(7)-s, a.k.a. named pipes, or use unix(7) sockets; read also Advanced Linux Programming to get some more possibilities. See also intro(2) & syscalls(2)

You could also use some shared memory and semaphores, see shm_overview(7) & sem_overview(7), or (as commented by RADAR) message queues, see mq_overview(7). But using signal(7)-s for IPC is generally a bad idea.

Be aware that inter-processor communication requires generally cooperation and modifications of both processes; in other words you should probably change the code of both you old A & your initiating B processes! And you probably don't want process A to leak information without consent.

BTW, you might also share memory using mmap(2) e.g. on a common file. But you need some synchronization.

Read also proc(5); thru /proc/1234/ you can query some informations about process 1234.

BTW, while indeed pipe(7)-s are very often set up (using pipe(2)) before calling fork(2) between parent and child processes this is not mandatory. In particular, there are cases where you want a process to pipe to itself (e.g. for Unix signal delivery in Qt).

Upvotes: 1

kris123456
kris123456

Reputation: 501

For Your question

But what if It's not a parent and child? If I have an old process that's running, is it possible to communicate with it using the pipe() function?

You will not be able to communicate with any other process, which aren't created by the parent process. Well technically, you shouldn't be allowed to.

You need to go through OS or use other IPC mechanisms to attain this functionality.

The Databases are Widely used just because of this primary reason. Multiple processes will be able to read and write data to a single DB. With protection from Multiple updates.

Upvotes: 1

Related Questions