For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > September 2005 > Need precisions on pipes









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Need precisions on pipes
Guillaume Vasselin

2005-09-20, 7:02 pm

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;
}

Rich Teer

2005-09-20, 7:02 pm

On Wed, 21 Sep 2005, Guillaume Vasselin wrote:

> 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.


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
Guillaume Vasselin

2005-09-21, 3:56 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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com