Need precisions on pipes
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Need precisions on pipes




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Need precisions on pipes  
Guillaume Vasselin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-21-05 01:49 AM

Hello,

I'm a beginner both in C and unix. I wrote a toy program where a process
sends through a pipe a single character every second, with the child
printing them on stdout.
I wanted the output te be one character every second, but they are
printed all at once.

Given my lack of knowledge, I highly suspect errors on my side, but I
need to be sure if that's the kind of thing you can do with pipes. I did
some research on how they work exactly, but what I found wasn't very clear.

I include the code of my program below, I'd appreciate very much a few
pointers on what is wrong and/or missing to achieve the desired effect.
Or should I use a completely different method ? (I want two processes,
I've already done it using threads)

Thanks
Glm



#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>


int main(int argc, char **argv)
{
int fd[2];
char c;

if (pipe(fd) == -1) {
perror("pipe");
exit(-1);
}
switch (fork()) {
case -1:
perror("fork");
exit(-1);
break;

case 0:
/* redirection of stdin */
dup2(fd[0], STDIN_FILENO);
close(fd[0]); close(fd[1]);

while ((c = getchar()) !=EOF)
putchar(c);
break;

default:
/* redirection of stdout */
dup2(fd[1], STDOUT_FILENO);
close(fd[0]); close(fd[1]);

int i;
c = 'a';
for (i = 0; i <= 3; ++i) {
putchar(c);
++c;
sleep(1);
}
}
return 0;
}






[ Post a follow-up to this message ]



    Re: Need precisions on pipes  
Rich Teer


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-21-05 01:49 AM

On Wed, 21 Sep 2005, Guillaume Vasselin wrote:
[vbcol=seagreen]
> I'm a beginner both in C and unix. I wrote a toy program where a process
> sends through a pipe a single character every second, with the child
> printing them on stdout.
> I wanted the output te be one character every second, but they are
> printed all at once.
>
> Given my lack of knowledge, I highly suspect errors on my side, but I
> need to be sure if that's the kind of thing you can do with pipes. I did
> some research on how they work exactly, but what I found wasn't very clear.[/vbcol
]

The reason why you're seeing the behaviour you describe is because you're
using the standard I/O library (putchar et al), which for efficiency buffers
I/O.    Try replacing your putchar and getchars with read and write (which
are not buffered).

In short: your program is sound, but you're being bitten by the standard
I/O library buffering.

HTH,

--
Rich Teer, SCNA, SCSA, OpenSolaris CAB member

President,
Rite Online Inc.

Voice: +1 (250) 979-1638
URL: http://www.rite-group.com/rich





[ Post a follow-up to this message ]



    Re: Need precisions on pipes  
Guillaume Vasselin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-21-05 07:50 AM

Rich Teer wrote:
> Try replacing your putchar and getchars with read and write (which
> are not buffered).
>
> In short: your program is sound, but you're being bitten by the standard
> I/O library buffering.
>
> HTH,
>

Thank you very much, it works now!
I tried using read and write before asking for help - I was suspecting
it could be the answer - but it didn't yield any results.
Obviously I did it wrong the first time...

Thanks again.
Glm





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 07:33 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register