LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-09-2010, 07:06 AM   #1
MarijaS
LQ Newbie
 
Registered: Jul 2010
Posts: 11

Rep: Reputation: 0
Named pipes - Problem with write() and read()


Hi,

I am doing programming in C++, under LINUX.
I have two independent processes. I should provide communication using named pipe.

Reader:
-creates FIFO using mkfifo - status = mkfifo(myFIFO, 0666)
-opens the pipe using open - fifo = open (myFIFO,O_RDONLY)
-reads from the pipe - num = read(fifo, temp, sizeof(temp))

Writer:
-opens pipe - fifo = open(myFIFO, O_WRONLY);
-writes to the pipe - num = write(fifo, string, strlen(string));

I have noticed that the file descriptor returned for read process
and write process are 0. Also, after command write, I can see on my terminal, the string which should be written to the pipe. I don't know why it is shown on terminal... Also, the number of bytes that are written is 0...

Would you please help me?
Thank you!!!

Problem solved! Please ignore!

Last edited by MarijaS; 08-09-2010 at 08:51 AM.
 
Old 08-09-2010, 10:58 AM   #2
brucehinrichs
Member
 
Registered: Mar 2008
Location: US
Distribution: Debian Sid; Sabayon, UbuntuStudio, Slackware-multilib 13.1, Peppermint Ice, CentOS
Posts: 575

Rep: Reputation: 67
I'm glad you got it solved. Would you please post your solution? It may be helpful to others.
 
Old 08-10-2010, 08:48 AM   #3
MarijaS
LQ Newbie
 
Registered: Jul 2010
Posts: 11

Original Poster
Rep: Reputation: 0
Of course I can... But there is nothing smart in my code. Just simple example for using named pipes...


// read.cpp:

#define myFIFO "/temp/FIFO"

int main(){
int num, fifo, status;
char temp[32];

if ((status = mkfifo(myFIFO, 0666)) < 0) {
printf("\n %s \n", strerror(errno));
return 0;
}

if ((fifo = open(myFIFO, O_RDONLY)) < 0) {
printf("\n %s \n", strerror(errno));
return 0;
}

if ((num= read(fifo, temp, sizeof(temp))) < 0) {
printf("\n %s \n", strerror(errno));
return 0;
}

printf("In FIFO is %s \n", temp);
}


// write.cpp:

#define myFIFO "/temp/FIFO"

int main() {
int status, num, fifo;
char string[]="Testing...";

if ((fifo = open(myFIFO, O_WRONLY)) < 0) {
printf("\n %s \n", strerror(errno));
return 0;
}

if ((num= write(fifo, string, strlen(string))) < 0) {
printf("\n %s \n", strerror(errno));
return 0;
}
}
 
  


Reply

Tags
c++, ipc, named, pipe, processes


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problems using named pipes shayke23 Linux - Software 2 08-13-2009 12:18 AM
Named pipes and C jobless Programming 2 10-21-2008 01:31 PM
named pipes lamtab Programming 12 12-11-2007 10:44 AM
Write command in named pipes sahel Programming 7 12-28-2005 06:05 AM
Use of Named Pipes casey0999 Linux - Software 3 08-03-2003 01:21 PM


All times are GMT -5. The time now is 08:03 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration