LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 07-25-2010, 01:29 PM   #1
zak100
Member
 
Registered: Jul 2009
Posts: 262

Rep: Reputation: 2
Linux: FIFO problem


Hi,
I have written following program. Its not working. Can somebody guide me with this?
Code:
  
//Reader
#include <fcntrl.h>
#include <stdio.h>
#define MSGSIZE 10
main(){
int fd, len;
char msgbuf[MSGSIZE+1];
if((fd=open("testfile",O_RDWR))<0)
perror("pipe open failed");
for(;;){
len=read(fd, msgbuf, MSGSIZE+1);
msgbuf[len]='\0';
printf("msg received =%s\n", msgbuf);
}}

//Writer
#include <fcntrl.h>
#include <stdio.h>
#define MSGSIZE 10
main(){
main(argc, argv)
int argc, char *argv[];{
int fd, j, nwrite, len;
char msgbuf[MSGSIZE];
if(argc <2) { printf("Error"); exit(-1);
}
if((fd=open("testfile", O_WRONLY))<0)
perror("fifo open failed");
for(j=1; j<argc;++j){
strcpy(msgbuf,argv[j]);
len=strlen(msgbuf);
if((nwrite=write(fd, msgbuf, len))<=0)
perror("message write failed");
}
exit(0);
}
I have first created the FIFO
Code:
  
$/bin/mknod testfile p

I am not getting three messages printed by Reader. Also terminal is behaving abnormally.

Can somebody plz help me with problem? There is no compilation error.

Zulfi.
 
Old 07-25-2010, 01:46 PM   #2
zirias
Member
 
Registered: Jun 2010
Posts: 361

Rep: Reputation: 59
This MAY sound a little rude, but after all, we're all lazy, so ... post a decently formatted version, please.

Oh and .. just a quick notice .. if you define the buffer being "MSGSIZE+1" bytes and you want the last byte be a null-byte ... you should only read a maximum of "MSGSIZE" bytes, not "MSGSIZE+1"
 
Old 07-25-2010, 11:46 PM   #3
zak100
Member
 
Registered: Jul 2009
Posts: 262

Original Poster
Rep: Reputation: 2
[CODE]
//Reader
#include <fcntrl.h>
#include <stdio.h>
#define MSGSIZE 10
main(){
int fd, len;
char msgbuf[MSGSIZE+1];
if((fd=open("testfile",O_RDWR))<0)
perror("pipe open failed");
for(; {
len=read(fd, msgbuf, MSGSIZE+1);
msgbuf[len]='\0';
printf("msg received =%s\n", msgbuf);
}
}


//Writer
#include <fcntrl.h>
#include <stdio.h>
#define MSGSIZE 10
main(argc, argv)
int argc, char *argv[];
{
int fd, j, nwrite, len;
char msgbuf[MSGSIZE];
if(argc <2) {
printf("Error");
exit(-1);
}
if((fd=open("testfile", O_WRONLY))<0)
perror("fifo open failed");
for(j=1; j<argc;++j){
strcpy(msgbuf,argv[j]);
len=strlen(msgbuf);
if((nwrite=write(fd, msgbuf, len))<=0)
perror("message write failed");
}
exit(0);
}

[\CODE]

I hope now its in proper format.
Plz help me with this.
Zulfi.

Last edited by zak100; 07-25-2010 at 11:50 PM. Reason: Formatting
 
Old 08-09-2010, 01:34 PM   #4
zak100
Member
 
Registered: Jul 2009
Posts: 262

Original Poster
Rep: Reputation: 2
Kindly provide me help with this.

Zulfi.
 
Old 08-09-2010, 01:38 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by zak100 View Post
[CODE]
//Reader
#include <fcntrl.h>
#include <stdio.h>
#define MSGSIZE 10
main(){
int fd, len;
char msgbuf[MSGSIZE+1];
if((fd=open("testfile",O_RDWR))<0)
perror("pipe open failed");
for(; {
len=read(fd, msgbuf, MSGSIZE+1);
msgbuf[len]='\0';
printf("msg received =%s\n", msgbuf);
}
}


//Writer
#include <fcntrl.h>
#include <stdio.h>
#define MSGSIZE 10
main(argc, argv)
int argc, char *argv[];
{
int fd, j, nwrite, len;
char msgbuf[MSGSIZE];
if(argc <2) {
printf("Error");
exit(-1);
}
if((fd=open("testfile", O_WRONLY))<0)
perror("fifo open failed");
for(j=1; j<argc;++j){
strcpy(msgbuf,argv[j]);
len=strlen(msgbuf);
if((nwrite=write(fd, msgbuf, len))<=0)
perror("message write failed");
}
exit(0);
}

[\CODE]

I hope now its in proper format.
Plz help me with this.
Zulfi.
No, it isn't - in the last tag you used backslash instead of forward slash.
 
  


Reply



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
Weird FIFO problem on Linux 2.6 darkwoods Programming 8 06-27-2010 03:40 AM
Linux 2.6 FIFO vs Linux 2.4 FIFO gjpc Programming 4 03-19-2008 07:26 PM
parport0: FIFO is stuck - problem with printing Debian_Poland Linux - Hardware 1 02-06-2006 01:32 PM
Clonning data, fifo/pipe/tee problem : Resource temporarily unavailable rmarco Linux - General 2 05-05-2005 11:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:37 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration