c pipe inter process

k7t266
k7t266 used Ask the Experts™
on
Hi,


I have a problem. I have 2 programs and they need to exchange informations. i want in fact if possible to have a pipe that the 2 programs know. One write on the pipe and the other read on the pipe.

Do you have any ideas how do i have to do?

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
jkr
Top Expert 2012

Commented:
What OS are you using? On UN*X, you can simply use 'mknod()' to create a pipe (aka FIFO) and open it using 'fopen()', e.g.

mknod ( "/tmp/mypipe", S_IWRITE | S_IREAD | S_IFIFO, (dev_t) 0);

FILE* p = fopen ( "/tmp/mypipe", "rw");

Commented:
use named pipe.. OR /proc file system OR shared memory..
look for further information in
unix programming by stevens
OR
linux documentation

Commented:
in addition to the above comment by jkr..
u can directly use
popen
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

jkr
Top Expert 2012

Commented:
Yes, but 'popen()' will return an unidirectinal pipe and '/proc' is not available on all flavours of UN*X...
Well in UNIX or UNIX like Oses like Linux or Minix this should work –

#define STD_INPUT 0
#define STD_OUTPUT 1

Int fd[2];

 Pipe (&fd[0]);

If ( fork() != 0 )
{
     close ( fd[0] );
     close ( STD_OUTPUT )
     dup ( fd[1] );
     close ( fd [1] );
     cout << “ prosses 1 fin “;
}
else
{
     close ( fd [ 1 ] );
     close ( STD_INPUT );
     dup ( fd [0] );
     close ( fd [0] );
     cout << “prossess 2 fin “;
}

 should work … I never tried to use this code before. Try it and tell me how it works.
When I want to do something like this I use the network library. I open a UDP connection to localhost and then communicate that way.
Why do I do this instead of pipes? Well to reasons. First if I later need to modify the program to exchange information with a process on another computer it is easier and secondly it is more portable. Not all systems support pipes. I don’t think DOS or windows support them. They let you use output redirection but that’s about it. You can easily modify a program that connects to local host for windows.
NOTE: it douse not even take band width. When you send a packet to local host the TCP stack douse not send it into the network to have it sent back. I just puts it in a stream for a second program on the computer to read. This is what X11 douse (X11 is the GUI interface program for most all UNIX platforms). In fact I think the use of sockets makes it superior to other GUIs.
Thank you for your time,
Arrummzen
Top Expert 2006

Commented:
No comment has been added lately and this question is therefore classified abandoned.

If asker wishes to close the question, then refer to
http://www.experts-exchange.com/help/closing.jsp

Otherwise, I will leave a recommendation in the Cleanup topic area that this question is:
PAQed with A grade to Arrummzen

Please leave any comments here within the next seven days. It is assumed that any participant not responding to this request is no longer interested in its final disposition.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Sunny
EE Cleanup Volunteer
jkr
Top Expert 2012

Commented:
Hmm - I'd still use a FS FIFO :o)
Top Expert 2006

Commented:
jkr, I realize that your solution was valid one, however, named pipes have more or less been replaced with other IPC mechanisms like pipe/unix domain sockets etc ... General use of named pipes has declined. Also, mkfifo() is a more intutive and better option for creating a FIFO. Hence I chose Arrumzen's answer.

If you feel that I should change my recommendation, please post yours.
jkr
Top Expert 2012

Commented:
Nope, no objections.
Top Expert 2006

Commented:
thank you,

I appreciate your taking time to respond to recommendations,
thanks:o)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial