C Linux read write function extension

Introduction - presented readn / writen

  The default on Linux read and write functions are soft interrupt signal., And read and write functions in the third parameter count

#include <unistd.h>

extern ssize_t read(int fd, void * buf, size_t count);
extern ssize_t write(int fd, const void * buf, size_t count);

Also due to the internal buffering mechanism, does not guarantee that read or write data to the specified size count.

Here the read and write and writen expand into readn

// 
// readn - n bytes read seek
 // FD: file descriptor
 // buf: Buffer
 // n: read length
 // return: returns the length of the read, identify errors -1, <n identification off, default n-
 //
 an ssize_t 
readn ( int FD, void * buf, n-size_t) { 
    size_t div = n-;
     char * PTR = buf; 

    the while (div> 0 ) { 
        an ssize_t RET = Read (FD, PTR, div) ;
         IF (RET < 0 ) {
             IF (errno == EINTR)
                Continue ;
             return - . 1 ; 
        } 
        IF (RET == 0 ) 
             BREAK ; 
        PTR + = RET; 
        div - = RET; 
    } 

    return n - div; 
} 

// 
// writen - n bytes written to strive
 // FD: file descriptors
 // buf: buffer
 // n-: read length
 // return: return to write length, -1 identification error, the default n-
 //
 an ssize_t 
writen ( int FD, const  void * buf, size_t n) {
    size_t div = n;
    const char * ptr = buf;

    while (div > 0) {
        ssize_t ret = write(fd, ptr, div);
        if (ret <= 0) {
            if (errno == EINTR)
                continue;
            return -1;
        }
        ptr += ret;
        div -= ret;
    }

    return n;
}

With these gains, you may wish to write a small test

#include <stdio.h> 
#include <errno.h> 
#include <the assert.h> 
#include <unistd.h> // // readn - n bytes read seek
 @ FD: file descriptor
 // buf: buffer
 // n-: read length
 // return: returns the length of the read, identify errors -1, <n identification off, default n-
 //
 extern an ssize_t readn ( int FD, void * buf, n-size_t); // // writen - n bytes written to strive
 // FD: file descriptor
 // buf: buffer
 // n: read length
 // return: return to write length, -1 identification error, the default n






//
extern ssize_t writen(int fd, const void * buf, size_t n);

/*
                   _oo0oo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  0\  =  /0
                ___/`---'\___
              .' \\|     |// '.
             / \\|||  :  |||// \
            / _||||| -:- |||||- \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |_/ |
           \  .-\__  '-'  ___/-. /
         ___'. .'  /--.--\  `. .'___
       ."" '<  `.___\_<|>_/___.' >' "".
      | | :  `- \`.;`\ _ /`;.`/ - ` : | |
      \  \ `_.   \_ __\ /__ _/   .-` /  /
   =====`-.____`.___ \_____/___.-`___.-'=====
                     `=---='
 */
int main(int argc, char * argv[]) {
    ssize_t ret = writen(STDOUT_FILENO, "12345\n1", 6);
    printf("ret = %ld\n", ret);

    char buf[4];
    ret = readn(STDIN_FILENO, buf, 3);
    buf[3] = '\0';
    printf("ret = %ld, buf = %s\n", ret, buf);

    return 0;
}

Firelight are a hi a worry, a wing a dry eye are dust, meditation Yan Liang see through things, not dream of people through the ages.

Smart people, blindly forward-looking; wise, everything backwards; wise man, who was victorious others; wise men were victorious themselves.

When the cultivation of the mind to clear your mind as to, when a religious group with no me.

Things past, a past, can not remember; now, but now the heart, you can revel; the next thing, the next heart, do not have to Auntie.

 

Text - read buffer

  In understanding readn routine basis, whether you have ever thought that the realization of ideas buffer read and write it. There is no harm to borrow in-depth understanding of computer systems

The ideas in the book to achieve something.

struct Rio {
     int FD;              // file descriptor 
    char * PTR;          // once beginning of the next read buffer pool buf 
    an ssize_t CNT;         // pool number character buf 
    char buf [BUFSIZ];    // pool 
}; 

// rio_init - rio initialization 
inline void rio_init ( struct Rio R & lt *, int FD) { 
    Assert (R & lt && FD> = 0 ); 
    R & lt -> FD = FD; 
    R & lt -> CNT = 0 ; 
    R & lt -> PTR = R-> buf; 
}

// 
// readn - n bytes read seek
 // R & lt: Object Buffer read
 // buf: Buffer
 // n: read length
 // return: returns the length of the read, identify errors -1, < n identification off, default n
 //
 extern an ssize_t rio_readn ( struct Rio R & lt *, void * buf, size_t n); 

// 
// rio_readline - seeks to read one line data
 // R & lt: Object buffer read
 // buf: buffer
 // n-: read length
 // return: returns the length of the read, identify errors -1, <n identification off, default n-
 //
 extern an ssize_t rio_readline ( struct Rio R & lt *, void * buf, size_t n);

To achieve a fixed character buffer read and buffer read one line. Additional write buffer is similar ideas, can not write a simple point will enter the write buffer,

It can be a self-fulfilling homework.

// RIO_READ - Read buffered version of the 
static an ssize_t RIO_READ ( struct Rio R & lt *, void * buf, n-size_t) {
     // if there is no data in the buffer, the buffer we refill 
    the while (R-> CNT <= 0 ) { 
        R & lt -> Read CNT = (R-> FD, R-> buf, the sizeof R-> buf);
         IF (R-> CNT < 0 ) {
             IF (errno == EINTR)
                 Continue ;
             return - . 1 ; 
        } 
        / / the EOF direct return 
        IF (R-> CNT == 0)
             Return  0 ;
         // reset PTR Buffer 
        R-> PTR = R-> buf; 
    } 
    // try to read the data and return 
    an ssize_t CNT = R-> CNT <n-R->? CNT: n-; 
    the memcpy (buf, R & lt -> PTR, CNT); 
    R & lt -> CNT - = CNT; 
    R & lt -> = PTR + CNT;
     return CNT; 
} 

// 
// readn - n bytes read seek
 // R & lt: Object buffer read
 / / buf: buffer
 // n-: read length
 // return: returns the length of the read, identify errors -1, <n identification off, default n-
 //
  an ssize_t
rio_readn (struct rio * r, void * buf, size_t n) {
    size_t div = n;
    char * ptr = buf;

    while (div > 0) {
        ssize_t ret = rio_read(r, ptr, div);
        if (ret < 0)
            return -1;
        if (ret == 0) 
            break;
        ptr += ret;
        div -= ret;
    }

    return n -div;     
}

// 
// rio_readline - seeks to read one line of data, will eat the last \ n character
 // R & lt: Object Buffer read
 // buf: Buffer
 // n-: read length
 // return: return the read length -1 identifies the error, <n identification off, default n-
 //
 an ssize_t 
rio_readline ( struct Rio R & lt *, void * buf, n-size_t) { 
    size_t I; 
    char * PTR = buf, C; 

    for (I = . 1 ; I < n-; ++ I) { 
        an ssize_t RET = RIO_READ (R & lt, & C, . 1 );
         IF (RET < 0 )
             return -1;
        if (c == '\n' || ret == 0)
            break;
        *ptr++ = c;
    }

    *ptr = '\0';
    return i - 1;
}

To combat packaging buffer write a little more complicated. And the service is bound heavy (or multi-strategy). For example buffer is full at this time on the business strategy

Decision, is the buffer expansion, or waiting for the next trigger event to write. And so, the real battlefield buffer read and write and the need for specific scenes to play with the machine,

To construct a satisfactory reading and writing strategies.

 

Postscript - let the water back

Guess you like

Origin www.cnblogs.com/life2refuel/p/10957979.html