can I use pipes to communicate with existing application?

I was reading about pipes today, it seems an interesting for me to use it or to start about it. but it's written there that the processes should be like a parent and child or client server and the communication will be between them.

there are some things I don't understand and before start I should be sure about them.

If I have a running process for example on my machine, and I want to send message to it, of course I can't control it, so how can I make it respond to my messages?? should I create the client and server and run them both?? Or the running process application can be a server for example and I can send some events to it??

if you can help me in this :)

thanks :)
omarkkkAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gplanaCommented:
Pipes are useful for communicate processes, yes.

Here is a simple example in C:
        #include <stdio.h>
        #include <unistd.h>
        #include <sys/types.h>

        main()
        {
                int     fd[2];
                pid_t   childpid;

                pipe(fd);
        
                if((childpid = fork()) == -1)
                {
                        perror("fork");
                        exit(1);
                }

                if(childpid == 0)
                {
                        /* Child process closes up input side of pipe */
                        close(fd[0]);
                }
                else
                {
                        /* Parent process closes up output side of pipe */
                        close(fd[1]);
                }
                .
                .
        }

Open in new window


As you can see, you create the pipe with pipe(fd)
After this call you have a pipe created and fd contains an array of 2 file descriptors, which one pointing to an edge of the pipe, so you can now communicate "inside your process" by sending something on fd[0] and receiving on fd[1], or vice-versa.

As what you want is to communicate two processes, then a fork() is invoked. Fork duplicates the process. So now code is running twice. Then there is an if to detect if I'm the parent or I'm the child. Any one have to close the file descriptor that he is not going to use. For example, if parent wants to use file descriptor 0 and child wants to use file descriptor 1, parent will close file descriptor 1 and child will close file descriptor 1.

After this, parent and child will use its file descriptor just as a normal file descriptor, so you can read, write, etc...

This is very common on C programs run on Linux. Here you have more information about this: http://www.tldp.org/LDP/lpg/node11.html

Hope it helps.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sarabandeCommented:
generally, a program that responds to messages sent by another program is a server. the program sending the request is a client. sometimes programs are both server and client if they can both send and receive requests.

what is the platform you are developing for? on Linux platform you could use fork as shown by gplana to easily create a parent and a child process where one is the server and the other is the client. by using standard input and output of the fork processes you would use an existing pipe for process-to-process communication (p2p).

if you want to make p2p on a non-unix platform or by using stand-alone processes, it is somewhat more complex.

on windows platform you could  use windows messages granted that both processes are running  a windows message loop. this is even possible for background processes without gui, which then would need a 'pseudo window' to be able to send/receive messages. you can pass a handle to global memory with a windows message and in so doing exchange any information between server and client. alternatively named shared memory is a commonly used method for p2p information exchange.

the classic method of p2p which works on every platform (with little differences) is to using socket programming. the server would create a socket for a port of a local ip address and wait for clients. the client would connect to the port and if the connection was accepted by the server both processes would use a further socket connection where they can send or receive messages or wait for response. p2p via sockets is a proved method but not the easiest one. nevertheless you should consider to go this way if you want to support different platforms or need to do communication in a network rather than locally on a pc.

Sara
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.