File I/O functions in Linux

Source: Internet
Author: User
Tags bitwise function prototype sendfile

First, Lseek function

Each open file has a "current file offset" associated with it. It is usually a non-negative integer used to measure from the beginning of the file

The number of bytes computed. Typically, read and write operations start at the current file offset and increase the number of bytes read and written by the offset. When you open a text

, the offset is set to 0 unless the O_append option is specified (O_append is used when the open function is called). Call the Lseek function explicitly

To set an offset for an open file. The function prototype is:

#include <unistd.h>off_t lseek (int fd,off_t offset,int whence);

the function returns a new file offset if it succeeds, and returns 1 if an error occurs. Parameter fd is open file descriptor, parameter offset and parameter whence

The values for:

    • If whence is seek_set, the offset of the file is set to a byte of offset from the beginning of the file.
    • If whence is seek_cur, the offset of the file is set to its current value plus offset,offset can be positive negative.
    • If whence is seek_end, the offset of the file is set to the file length plus offset,offset can be negative.

If Lseek executes successfully, a new file offset is returned. If the file descriptor points to a pipe, FIFO, or network socket, the Lseek

Returns-1, and sets errno to Espipe.

Second, DUP and DUP2 functions

Both DUP and DUP2 system calls can be used to copy file descriptors, and the prototypes of the functions are:

#include <unistd.h>int dup (int  FD); int dup2 (int fd,int fd2);

The function executes successfully returns a new file descriptor, and returns 1 if an error occurs. The new file descriptor returned by the DUP must be in the currently available file descriptor

The minimum value. For dup2, you can specify the value of the new file descriptor with the FD2 parameter. If the FD2 is already open, turn it off first. If FD equals FD2,

The dup2 returns FD2 without closing it. Otherwise, the FD2 fd_cloexec file descriptor flag is cleared so that FD2 is called when the process calls exec

Open state.

DUP (FD); equivalent to Fcntl (fd,f_dupfd,0); Dup2 (FD,FD2); equivalent to close (FD2); Fcntl (FD,F_DUPFD,FD2);

And dup2 is not exactly the same as close plus fcntl. The differences between them are:

(1) dup2 is an atomic operation, and close and fcntl include two function calls. It is possible to call a signal capture between close and Fcntl

function, which may modify the file descriptor. The same problem can occur if different threads change the file descriptor.

(2) Dup2 and fcntl have some different errno.

Three, Fcntl function

The FCNTL system call can be used to perform various control actions on a file descriptor that has already been opened to alter the various properties of the open file, the function's

The prototypes are:

#include <unistd.h><fcntl.h>int fcntl (int fd,int cmd,... /* int arg */);

The FCNTL function has the following 5 functions:

(1) Copy an existing file descriptor (CMD=F_DUPFD or f_dupfd_cloexec).

(2) Gets/sets the file descriptor flag (CMD=F_GETFD or F_SETFD).

(3) Gets/sets the file status flag (CMD=F_GETFL or F_SETFL).

(4) Get/Set asynchronous I/O ownership (Cmd=f_getown or F_setown).

(5) Get/Set record lock (CMD=F_GETLK, f_setlk or f_setlkw).

F_DUPFD: Copy file descriptor fd. The new file descriptor is returned as a function value. It is not yet open in each descriptor greater than or equal to the 3rd one

The minimum value for each value in the parameter value (taken as an integer value). The new file descriptor shares the same file table entry as the FD. But the new file descriptor has its own

A set of file descriptor flags whose fd_cloexec file descriptor flags are cleared (this means that the file descriptor remains valid at exec).

F_dupfd_cloexec: Copies the file descriptor, sets the value of the Fd_cloexec file descriptor flag associated with the new file descriptor, and returns

The new file descriptor.

F_GETFD: The file descriptor flag corresponding to FD is returned as a function value, and only one file descriptor flag Fd_cloexec is currently defined.

F_SETFD: Set file descriptor flags for FD. The new flag value is set by the 3rd parameter (taken as an integer value).

F_GETFL: The file status flag corresponding to FD is returned as a function value.

F_SETFL: Sets the file status flag to the value of the 3rd parameter (taken as an integer value). Several flags that can be changed are: O_append, o_no

Nblock, O_sync, O_dsync, O_rsync, O_fsync and O_async.

F_getown: Gets the process ID or process group ID of the current receive Sigio and Sigurg signals.

F_setown: Sets the process ID or process group ID that receives the Sigio and Sigurg signals. The positive arg specifies a process ID, and a negative arg indicates equal to

A process group ID for absolute value of ARG.

The return value of the FCNTL is related to the command. If there is an error, all commands return 1, and if successful, some other value is returned. F_DUPFD returns a new

File descriptors, F_GETFD and F_GETFL return the corresponding flags, F_getown return a positive process ID or a negative process group ID.

IV. IOCTL functions

The IOCTL system call is usually used to control the device, and control operations that cannot be performed with other functions can be performed with the IOCTL, which is:

#include <unistd.h><sys/ioctl.h>int ioctl (int fd,int request,...);

The IOCTL is used to control the properties of special device files, the first parameter FD must be an open file descriptor, the third parameter is generally

Char *ARGP, which differs depending on the second parameter request. The parameter request determines whether the parameter ARGP is passing data to the IOCTL or from the IO

CTL gets the data.

V. READV and WRITEV functions

The READV function reads data from a file descriptor into a fragmented memory block, which is a decentralized read, and the Writev function adds multiple scattered memory data

Write to the file descriptor, which is the central write. The prototype of the function is:

#include <sys/uio.h>ssize_t readv (int fd,conststruct iovec *iov,int  count); ssize_t Writev (int fd,conststruct iovec *iov,int count);

The FD parameter in the function is the target file descriptor to be manipulated. The type of the IOV parameter is an array of IOVEC structures that describe a block of memory. Parameter

The count is the length of the Iov array, that is, how much of the block of memory data needs to be read from FD or written to FD. The READV and WRITEV functions return read/

Number of bytes written to FD, failure returns 1 and set errno.

Vi.. Sendfile function

The Sendfile function passes data directly between two file descriptors (operating entirely in the kernel), thus avoiding kernel buffers and user slow

The data copy between the flushing zones is highly efficient and is known as a 0 copy. The prototype of this function is:

#include <sys/sendfile.h>ssize_t sendfile (int out_fd,int in_fd,off_t *offset,size_ t count);

The IN_FD parameter in the function is the file descriptor of the content to be read, and the OUT_FD parameter is the file descriptor for the content to be written. The offset parameter specifies the read

Where the file stream begins to read, and if it is empty, the default starting position of the read-in file stream is used. The count parameter is specified in the file descriptor IN_FD

The number of bytes transferred between and OUT_FD. The Sendfile function returns the number of bytes transferred when it succeeds, and the failure returns 1 and sets the errno. IN_FD must be a

A file descriptor that supports similar mmap functions, that is, it must point to the real file, not the socket and the pipe, and the out_fd must be a soc

Ket Therefore, the Sendfile function is almost exclusively designed to transfer files over the network.

Vii. Mmap and Munmap functions

The Mmap function is used to request a memory space, which can either be used as shared memory for interprocess communication or directly mapped to a file

into them. The Munmap function frees the memory space created by Mmap. The function prototypes are:

#include <sys/mman.h>void* MMAP (void *start,size_t length,int prot,int Flags,int  fd,off_t offset); int munmap (void *start,size_t length);

The start parameter in the function allows the user to use a specific address as the starting address for the memory, and if it is set to NULL, the system automatically

Assign an address. The length parameter specifies how long the memory segment is. The prot parameter is used to set the access permissions for the memory segment, which can take a bitwise of the following values

Or: (1) Prot_read: Memory segment readable. (2) Prot_write: Memory segment writable. (3) Prot_exec: Memory segment executable. (4) PRO

T_none: Memory segments cannot be accessed.

The flags parameter controls the behavior of the program after the contents of the memory segment is modified, and it can be set to a bitwise OR of certain values (where map_shared and map_private

is mutually exclusive and cannot be specified at the same time). Common values for the flags parameter are: map_shared, Map_private, map_anonymous, map_fixed, MA

P_hugetlb. The FD parameter is the file descriptor corresponding to the mapped file, which is usually obtained by the open system call. The offset parameter is set from the file

Start mapping (for situations where you do not need to read the entire file).

When the Mmap function succeeds, it returns a pointer to the area of the target memory, and fails returns map_failed ((void*)-1) and sets the errno. Munmap function

Returns 0 on success, 1 for failure and sets errno.

Viii. Splice function

The splice function is used to move data between two file descriptors and is also a 0 copy operation. The prototype of this function is:

#include <fcntl.h>ssize_t splice (int fd_in,loff_t *off_in,intint flags);

The fd_in parameter in the function is the file descriptor of the data to be entered, and if fd_in is a pipe file descriptor, then the OFF_IN parameter must be set

Null if FD_IN is not a pipe file descriptor, then off_in indicates where to start reading data from the input data stream, at this point if off_in

is set to NULL, it is read in from the current offset of the input data stream, and if off_in is not NULL, it indicates the specific offset position. Fd_o

The Ut/off_out parameter has the same meaning as fd_in/off_in, but is used to output the data stream. The len parameter specifies the length of the moving data; the Flags parameter controls

How the data is moved, it can be set to the bitwise OR of some values, the common values are: Splice_f_move, Splice_f_nonblock, splice_f_

More, Splice_f_gift. When using the splice function, fd_in and fd_out must have at least one pipe file descriptor. The splice function is called as

Returns the number of bytes moved, it may return 0, indicating that no data needs to be moved, which occurs when the data is read from the pipeline (fd_in is a pipe file

Descriptor) and the pipeline is not written to any data. Splice returns 1 when the function fails and sets the errno.

Nine, tee function

The tee function replicates data between two pipe file descriptors and is also a 0 copy operation. It does not consume data, so the data on the source file descriptor is still

Can then be used for subsequent read operations. The function prototype is:

#include <fcntl.h>ssize_t tee (int fd_in,intint. flags);

The function's parameters have the same meaning as splice (but both fd_in and fd_out must be pipe file descriptors). The tee function succeeds when returned in two text

The number of bytes of data copied between the pieces descriptor. Returning 0 means that no data is copied. When tee fails, it returns 1 and sets errno.

File I/O functions in Linux

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.