Fast way to write a file to a file descriptor or stream?

Fast way to write a file to a file descriptor or stream?

Post by Timur Ta » Fri, 28 Feb 2003 04:12:33



I'm trying to write a routine that reads a file and writes the output
to a socket.  Currently, I have something like this (pseudocode):

while (!eof())
{
    count = read(fd_in, temp, sizeof(temp));
    write(fd_out, temp, count);

Quote:}

The problem is that I need to allocate a buffer 'temp' to store the
data I've read.  Is there a way to get Linux to do the read() (or
fread(), which works best) directly to the output stream, bypassing a
local buffer?
 
 
 

Fast way to write a file to a file descriptor or stream?

Post by Kasper Dupon » Fri, 28 Feb 2003 04:56:30



> I'm trying to write a routine that reads a file and writes the output
> to a socket.  Currently, I have something like this (pseudocode):

> while (!eof())
> {
>     count = read(fd_in, temp, sizeof(temp));
>     write(fd_out, temp, count);
> }

> The problem is that I need to allocate a buffer 'temp' to store the
> data I've read.  Is there a way to get Linux to do the read() (or
> fread(), which works best) directly to the output stream, bypassing a
> local buffer?

I know two ways:
1) mmap the source file and use the write system call.
2) Use the sendfile system call.

--
Kasper Dupont -- der bruger for meget tid p? usenet.

for(_=52;_;(_%5)||(_/=5),(_%5)&&(_-=2))putchar(_);

 
 
 

1. Establishing new streams for file descriptors stdin, stdout, stderr

Greetings!

I'm trying to "daemonize" a server program which runs on a few different
Unix platforms according to Section 1.7 of the comp.unix.programmer FAQ,
but I'm tripping over a couple of details which I don't understand.

I've accomplished the first six steps. (up to closing file descriptors
0, 1, and 2 in order to release stdin, stdout, and stderr).

I'm not quite sure how to go about step 7:

  7. Establish new open descriptors for stdin, stdout and stderr. Even if
  you don't plan to use them, it is still a good idea to have them open.
  The precise handling of these is a matter of taste; if you have a
  logfile, for example, you might wish to open it as stdout or stderr,
  and open `/dev/null' as stdin; alternatively, you could open
  `/dev/console' as stderr and/or stdout, and `/dev/null' as stdin, or
  any other combination that makes sense for your particular daemon.

I want to redirect stdout and stderr to append  file which I know exists, and
redirect stdin to /dev/null

I'm ignorant of how to force this association. I know that I could use open()
to open the file, but then I would just get whatever file descriptor the
system decided to give me. Does open() always use the file descriptors
in ascending order? Such that if I close these three, then do three opens,
I will get first 0, then 1, then 2? If not, is there a semi-portable way to
force an open with a particular file descriptor?

Am I far off here?

Thanks in advance!

Jeremy Beal
Get my e-mail address at www.nvmedia.com/jbeal
(Tired of the damn spam)

2. File locking as a mutual exclusion mechanism

3. file descriptor passing via streams ioctl (AIX 4.3)

4. print quota in solaris

5. passing file descriptors through stream pipes?

6. CGI-File not found

7. buffer flushing; converting file descriptor to stream pointer

8. bernoulli boxes as backup?

9. passing file descriptors through stream pipes?

10. C++ Streams and File Descriptors

11. file descriptors with C++ streams

12. file descriptor passing via streams ioctl (AIX 4.3)

13. interfacing a stream to a file descriptor