473,383 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Pipes

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
Nov 14 '05 #1
6 1567
Xarky <be*********@yahoo.com> wrote:
Hi,
I wrote the following program (source code below). I am using
pipes, were the parent reads and the child writes.


please take your question to an appropriate newsgroup. I.e.
comp.unix.programmer

here we try to stick to the C language as defined by the ISO C
standard(s).

--
Z (Zo**********@daimlerchrysler.com)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
Nov 14 '05 #2
Xarky wrote:

I wrote the following program (source code below). I am using
pipes, were the parent reads and the child writes.

^^^^^ ^^^^^^ ^^^^^

You are already off-topic for c.l.c. See refs below

--
Some useful references:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>

Nov 14 '05 #3
In <bc************************@posting.google.com> be*********@yahoo.com (Xarky) writes:
/* Source code */
#include <errno.h>
#include <stdio.h>
#include <unistd.h>

^^^^^^^^^^
At this point, you have left the realm of c.l.c and entered the realm of
comp.unix.programmer. You should have been able to figure this out by
yourself!

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #4
On Thu, 15 Apr 2004 07:58:38 -0700, Xarky wrote:

your program loop is 500 not 50, but I still understand. I dont care if
you've left the scope of comp.lang.c, this is still valid in my opinion.
besides what else would you talk about besides syntax if it wasn't.

[ snip ]


/* Source code */
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h> missing a couple headers like unistd.h, stdlib.h and string.h #define MAX 20


[ snip ]

from the look of the logic, it is possible to do multiple writes, and then
in the parent thread, one read would take in the data from both those
writes. This is possibly what's happening, a better way to check
integrity, and ensure teh data is coming across the pipe would be to
calculate total bytes written and total bytes read.
Nov 14 '05 #5

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

Nov 14 '05 #6
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
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Christian Hammers | last post by:
Hello I would like to call a unix shellscript from within a PHP script and - write data to its STDIN - read data from its STDOUT *and* STDERR - get its exit code afterwards proc_open seems...
5
by: Peng Yuan Fan | last post by:
Hi, sorry if you found I have multi posted in different groups. It didn't appear in that group. I am trying to write an automated testing program with CxxTest, which in turn needs to talk to gdb...
5
by: glenn.owens | last post by:
In the process of doing some routine monitoring/clean-up we've discovered that several (many?) users are apparently set to access our SQL Server 2000 database instances via the Named Pipes...
2
by: Rudolf Bargholz | last post by:
Hi, DB2 7.1 FP11 Windows 2000 Earlier this evening, after dropping and recreating a trigger, DB2 locked up. I am not entirely sure that the cause of the problem was the replacing of the...
9
by: Hans J?rg Brinksmeyer | last post by:
Hi, does anyone have an idea for this problem: I use anonymous pipes to steer a console program under Win2000 with a second 'steering aplication'. The stdin and output are redirected to...
4
by: Ken Allen | last post by:
Is there any built-in facility for handling named pipes in C#/.Net, or must one use unsafe code to access the WIN32 API directly? There exists some code that uses named pipes heavily and there...
7
by: webmaster | last post by:
Sorry if this sounds naive, but I need to know how to create a two-way pipe between my Objective-C MacOSX program and another process, like perl, for example. Is there a simple way to do this in C,...
1
by: Jarrod Morrison | last post by:
Hi All Im looking for a way use named pipes between a service app and an app run when a user logs on and be able to pass string based data, im hoping that the service can contact the app that is...
3
by: ZhukovL | last post by:
I'm having some trouble implementing the handling of multiple pipes in a shell I'm writing. I was hoping someone could point me in the right direction because I really cant see where I'm going...
7
by: andrewb | last post by:
Hi all, Having some trouble using named pipes and Visual Basic .NET and would appreciate and help you could offer. Essentially I am trying to develop a simple client/server application that...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.