Xenos <do**********@spamhate.com> wrote:
"Xarky" <be*********@yahoo.com> wrote in message
news:bc************************@posting.google.com ... Hi,
I wrote the following program (source code below). I am using
pipes, were the parent reads and the child writes.
My problem is that I am writing the same line in the pipe for a
fixed number of times 50 (made a for loop). When writing it back it
to string I made a counter(cnt) to count the occurances of each
string. The problem is that never gives me 50, but 43.
Probably my problem is with the file descriptors.
Can some one help me pls.
Thanks in advance
/* Source code */
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#define MAX 20
main()
{
system ("clear");
pid_t pid;
int fd[2];
ssize_t data;
char buff[MAX];
char msg[] = "hello, world.\n\0";
if (pipe(fd) < 0) // creating pipe
printf ("Error encountered: %s.\n", strerror(errno));
else // no error encountered
{
if ((pid = fork()) == -1)
printf ("Error encountered: %s.\n", strerror(errno));
else if (pid == 0) // child to write
{
close (fd[0]); // closing reading part
int i;
for (i=0; i<500; i++)
write (fd[1], msg, sizeof(msg));
close (fd[1]); // terminating writing
_exit(0);
} // end child process
else // parent to read
{
close (fd[1]); // closing writing part
int cnt=0;
data = read (fd[0], buff, sizeof(buff)-1);
while (data != 0)
{
cnt++;
write (fileno(stdout), buff, data);
data = read (fd[0], buff, sizeof(buff)-1);
} // end loop
printf ("Number of strings counted is %d.\n", cnt);
close (fd[0]); // terminating reading
} // end parent process
} // end else
} // end main
your problem is that write and read are not guaranteed to give you the exact
count you ask for, you have to check the return code. If the result is
neg., you have an error; 0, the pipe has been closed, and pos. is the number
actually read/written.
for example for you tell it to read 50 byte, if may give you any number
between 1 and 50 depending how much is actually in its buffer at the time.
you have to continue to read (adjusting your current pointer into the buffer
and the number to read) until you get all that you want.
Xenos, please respect that these kind of platform specific questions
are off-topic in this group as the OP already has been told, even
with a redirect to a probably more appropriate newsgroup like
comp.unix.programmer.
You can easily see why when you look at your own answer. On the
system I am working on the non-standard C function read() may
return values between -1 and 19 for the program of the OP and not
values between 1 to 50. And also what you claim to be the problem
in the code would be definitely _not_ the problem (i.e. the OPs
question why he has to call read() only 43 times instead of 50
times). Since I do not know which system you're talking about I
won't claim that your answer is wrong (on my prefered platform,
Linux, the result of 422 would actually be the most probable result,
going by the code, and 43 after replacing the 500 in the childs code
by 50 as according to what the OP wrote in the text) but it hope-
fully makes you understand why discussing platform specific problems
in clc is heavily frowned upon - it simply doesn't make sense. I would
be quite willing to discuss the OPs problem in comp.unix.programmer,
but here (in clc) it would be totally out of place - and I would also
guess that at least 80% of the regulars here know the answer very well
but all of them feeling the same way.
There are some C specific issues with that program (e.g. not declaring
main() to return an int and neglecting to return an int from main() or
the question why the OP feels it necessary to add another '\0' to the
end of a string (msg) that, from its definition, already has a '\0')
but that's obviously not of much concern for the OP at the moment (but
it actually plays some role in the unexpected results he gets, assuming
that the OP is using some kind of UNIX system).
Regards, Jens
--
\ Jens Thoms Toerring ___
Je***********@physik.fu-berlin.de
\__________________________
http://www.toerring.de