Named Pipes and FILE vs file descriptors

Named Pipes and FILE vs file descriptors

Post by Anthony Fogl » Fri, 10 Jul 1998 04:00:00



        I'm trying to make a random signature program in C that uses
named pipes and I've come across a few problems.

        If I open the pipe with open(".signature",O_WRONLY) I don't have
any problems with the writing.  It waits for my mail program to read the
signature before it writes to the file, as it should.  But if I end up
replacing one signature with another smaller one, anything beyond the
size of the new one is left in.  (I could just fill it up with white
space, but that's obviously not the best solution.)  So how do I write
an end of file so that my mail program knows to stop reading?

        The only method I could come up with is not to use open, but to
use fopen.  But in that case my program doesn't block upon accessing the
pip and just runs and runs.  Again, that's not a major problem, and the
program would work fine otherwise, but there must be a better solution
out there.  Is the problem that you must use file descriptors and not
FILE *'s to fully use the benefits of pipes?  If so, that brings me back
to the first question.

        Thanks for any help.

--Anthony

 
 
 

Named Pipes and FILE vs file descriptors

Post by Andrew Giert » Fri, 10 Jul 1998 04:00:00


 Anthony>    If I open the pipe with open(".signature",O_WRONLY) I
 Anthony> don't have any problems with the writing.  It waits for my
 Anthony> mail program to read the signature before it writes to the
 Anthony> file, as it should.  But if I end up replacing one signature
 Anthony> with another smaller one, anything beyond the size of the
 Anthony> new one is left in.  (I could just fill it up with white
 Anthony> space, but that's obviously not the best solution.)  So how
 Anthony> do I write an end of file so that my mail program knows to
 Anthony> stop reading?

Close the pipe.

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
                           or <URL: http://www.whitefang.com/unix/>

 
 
 

Named Pipes and FILE vs file descriptors

Post by Anthony Fogl » Fri, 10 Jul 1998 04:00:00


        (First, sorry, Andrew, if this gets sent to you.  I sent a
response over e-mail, but I figure I better follow-up as well, and
there's a bit more here.)


:  Anthony>  If I open the pipe with open(".signature",O_WRONLY) I
:  Anthony> don't have any problems with the writing.  It waits for my
:  Anthony> mail program to read the signature before it writes to the
:  Anthony> file, as it should.  But if I end up replacing one signature
:  Anthony> with another smaller one, anything beyond the size of the
:  Anthony> new one is left in.  (I could just fill it up with white
:  Anthony> space, but that's obviously not the best solution.)  So how
:  Anthony> do I write an end of file so that my mail program knows to
:  Anthony> stop reading?

: Close the pipe.

        I assume you mean use the close(file) function where file is the
file descriptor.  I've tried that and it doesn't seem to work.  Let me
explain that I'm basically hacking together two other programs I've
gotten off the net, one of which randomly chooses the sig, and another
which actually does the writing to the pipe.  That one originally used
file descriptors and did use close() and it didn't work.  Nor did any of
my attempts to fix it, until I tried using FILE *'s.  Now, I'll admit I
may have missed something so I'll try again, but I tend to
doubt it.

        Either way, I would like an answer to my second question.  Can
one use fopen on a pipe so that it blocks until another program reads
the pipe?  Thanks.

 
 
 

Named Pipes and FILE vs file descriptors

Post by Lawrence Kir » Tue, 14 Jul 1998 04:00:00




Quote:>        I'm trying to make a random signature program in C that uses
>named pipes and I've come across a few problems.

>        If I open the pipe with open(".signature",O_WRONLY) I don't have
>any problems with the writing.  It waits for my mail program to read the
>signature before it writes to the file, as it should.  But if I end up
>replacing one signature with another smaller one, anything beyond the
>size of the new one is left in.

A pipe can't forward data you haven't written to it so this suggests that
you are writing too much data to the pipe. You may be overwriting the
larger sign with a smaller one in a buffer in your program and then writing
the whole buffer which will include any rubbish after the smaller sig.

Quote:> (I could just fill it up with white
>space, but that's obviously not the best solution.)  So how do I write
>an end of file so that my mail program knows to stop reading?

The reader will encounter an end of file indication when it has read all
existing data in the pipe and the writer has closed its end of the pipe.

Quote:>        The only method I could come up with is not to use open, but to
>use fopen.  But in that case my program doesn't block upon accessing the
>pip and just runs and runs.  Again, that's not a major problem, and the
>program would work fine otherwise, but there must be a better solution
>out there.  Is the problem that you must use file descriptors and not
>FILE *'s to fully use the benefits of pipes?  If so, that brings me back
>to the first question.

If you are using fopen() presumably you are also using different functions
to actually write the data. I would guess that you've done something
different there so that only the correct amount of data is written.

--
-----------------------------------------


-----------------------------------------

 
 
 

1. FILE vs file descriptor

Hi:
   I wonder where is the FILE structure is defined ? in the liberary header
or linux header file ? And also, what is the difference between FILE * and
the file descriptor?  I know that I can call fopen() and open() to get them,
but what is the point for stream file and the "plain" file?.  Is this
inherited from different Unix standard?

Thank you!
chris.

2. Suggestion Network card

3. File descriptor and file names

4. Kernel 2.2.16

5. query path and file name, given fd file descriptor

6. Building Hylafax on Solaris 7?

7. how to get the name(s) of a file from file descriptor

8. NFS WITHIN AN IP MASQ FIREWALL (fwd)

9. Identify file name given file descriptor

10. Getting file-name from a file descriptor

11. Getting file name from file descriptor

12. File name from file descriptor