Linux with no buffer file I/O

Source: Internet
Author: User

I/O designed in this blog does not belong to the file I/O of ANSI C, and every function involved is a system call. It mainly involves the basic part of posix.1 I/O operations without caching.

1. In the Linux kernel, only one identifier is required for the description of a file. Instead of a complex pointer, it is just an integer of the int type. A file descriptor can be used to directly operate kernel resources through system APIs. In other words, all the following Operation APIs will generate a system call. In this way, the size of int limits the maximum number of files that can be opened by the operating system. But the file opening limit is mainly restricted by open_max. This file concept is Linux Kernel File concept, which includes the specific files in the file directory, Sock, pipeline, serial Port and other collection can be seen as a file operation. The commonly used file descriptors 0, 1, and 2 represent stdin_fileno, stdout_fileno, and stderr_fileno respectively. Define 2. OPEN function in the file <unistd. h>. This is a function to open the file.
#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>int open(const char *filename, int flags, /*, mode_t mode */);

The flag is a file status tag, which consists of the following parameters or operations (defined in fcntl. h): o_rdonlyo_wronlyo_rdwro_appendo_creat: only when this parameter is available, the open function must call the third parameter, specifying the permission o_excl: if the file exists, an error occurs in o_trunc: if a file exists, it is truncated to 0o_nocttyo_nonblock: non-blocking. o_sync: returns the file descriptor every time the write operation is successful and other physical writes are successful, otherwise, the negative number 3 is returned. With the OPEN function, the create function is not required. Because files can be created by o_reat.

#include <sys/types.h>#include <sys/stat.h>#include <stat.h>int create(const char *filename, mode_t mode);

Error returned-1
4 close function this is a slap in the face to close the file Function

#include <unistd.h>int close(int fd);

Error returned-1

Close an open file. At the same time, it should be noted that when the process exits, it will implicitly close the file opened by the process 5 lseek function to locate the current operation position of the file. Generally, if o_append is not specified for an opened file, the Operation position of the file starts
# Include <sys/types. h> # include <unistd. h> off_t lseek (int fd, off_t offset, int whence); Return-1 if an error occurs, and return a new offset if a success occurs.

The description of offset is related to the whence (where) of the last parameter:
Seek_set, calculated from the start position. The offset cannot be negative.
Seek_cur. calculated from the current position. The offset value can be positive or negative.
Seek_end, calculated from the last position. offset can be positive or negative. Note: The file offset is unique to each process. Different processes open the same file, but different file offsets may exist. 6. The READ function reads data from a file.
# Include <unistd. h> ssize_t read (int fd, void * buffer, size_t nbytes)
7. The Write function writes data to the file.
# Include <unistd. h> ssize_t write (int fa, void * buffer, size_t nbytes); the number of written bytes is returned successfully, and error-1 is returned.

The DUP and dup2 functions can be used to copy a file description)

# Include <unistd. h> int DUP (int fd); int dup2 (INT fd1, int fd2); a new file descriptor is returned successfully, and an error is returned-1

The difference between the two functions is:

DUP replication FD returns an available minimum file descriptor; dup2 uses the file descriptor fd2 to copy fd1. If fd2 is enabled, disable it first. The copied file descriptor shares the file table item, which is represented by the same file status tag and file offset 9 fcntl function. This function can change the nature of opened files.
# Include <sys/types. h> # include <unistd. h> # include <fcntl. h> int fcntl (int fd, int CMD/*, int Arg */); dependent on CMD successfully, error returned-1

Fcntl has five functions

  • Copy an existing Descriptor (cmd = f_dupfd)
  • GET/set a file status mark (cmd = f_getfd/f_setfd)
  • GET/set the File status mark (cmd = f_getfl/f_setfl)
  • Obtain/set asynchronous Io ownership (cmd = f_getown/f_setown)
  • Obtain/set the record lock (cmd = f_getlk, f_setlk, f_setlkw)

Fcntl is very powerful and should be used in specific environment programming as needed.

10 ioctl function this is not very familiar, omitted

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.