LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 10-20-2008, 09:27 PM   #1
jobless
LQ Newbie
 
Registered: Sep 2008
Posts: 4

Rep: Reputation: 0
Named pipes and C


Hello guys,

I created a named pipe on my Ubuntu machine using the command
Code:
mkfifo mypipe
Now I am trying to read from that pipe in a C program doing the following.

Code:
void listen() {
    int fp;
    char buf[255];

    fp = open("mypipe", O_RDONLY);
    if(fp == -1) {
        printf("Could not open the pipe\n");
    }

    while(1) {
        read(fp, buf, sizeof(buf));
        printf("%s", buf);
    }

    close(fp);
}
When I write to the pipe using the following command

Code:
echo "Hello" > mypipe
The C program just prints the text continuously without any break. Is it something wrong with my program or is it how the pipes work? And I can't check the returned string and see if it matched the previous read.

Thanks for your help.

Last edited by jobless; 10-20-2008 at 09:32 PM. Reason: add code tags
 
Old 10-20-2008, 10:36 PM   #2
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
I think the problem is your code. You assume read() alters the buffer when it hits the end-of-file, a mistake I believe:

Code:
void listen() {
    int fp;
    char buf[255];
    int l; // read length

    buf[0] = '\0'; // in case there is nothing to read
    buf[255] = '\0'; // garentee a end-of-line

    fp = open("mypipe", O_RDONLY);
    if(fp == -1) {
        printf("Could not open the pipe\n");
    }

    do {
        l = read(fp, buf, 254);
        printf("%s", buf);
    } while (l > 0);

    close(fp);
}

Last edited by SciYro; 10-20-2008 at 10:39 PM.
 
Old 10-21-2008, 02:31 PM   #3
jobless
LQ Newbie
 
Registered: Sep 2008
Posts: 4

Original Poster
Rep: Reputation: 0
It works but a few more changes

The solution you provided worked. But it printed duplicates. So I changed it to print only when the read length is greater than 0. Also, reinitialize the buffer so that you guarantee that only the current information will be printed instead of the one from the previous read.

Thanks for all your help.

Code:
void listen() {
    int fp;
    char buf[255];
    int l; // read length
    int i;

    buf[0] = '\0'; // in case there is nothing to read
    buf[255] = '\0'; // garentee a end-of-line

    fp = open("mypipe", O_RDONLY);
    if(fp == -1) {
        printf("Could not open the pipe\n");
    }

    while(1) {
        l = read(fp, buf, 254);
        if(l) // print only if the read length is greater than 0
            printf("%s", buf);

        for(i=0; i<255; i++) // initialize the whole buffer again
            buf[i] = '\0';
    }

    close(fp);
}
 
  


Reply

Tags
named, pipes


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
named pipes not working over nfs the_ultimate_samurai Linux - Networking 2 09-02-2008 01:22 PM
named pipes lamtab Programming 12 12-11-2007 11:44 AM
Bash - named pipes - mathew42 insight please PAix Programming 3 12-11-2007 10:09 AM
Write command in named pipes sahel Programming 7 12-28-2005 07:05 AM
Use of Named Pipes casey0999 Linux - Software 3 08-03-2003 02:21 PM


All times are GMT -5. The time now is 01:48 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration