Linux system programming-system call I/O operations (File Operations), linux system calls

Source: Internet
Author: User

Linux system programming-system call I/O operations (File Operations), linux system calls
File descriptor

In the Linux world, all devices are files.. We can call the I/O function (I: input, input; O: output, output) in the system to perform operations (open (), close () on the file (), write (), read ).


When an existing file is opened or a new file is created, the system (kernel) returns a file descriptor used to specify the opened file.This file descriptor is equivalent to the number of the opened file. The file descriptor is a non-negative integer and is the File Identifier. operating on this file descriptor is equivalent to operating the file specified by this descriptor..


After the program runs (each process), there is a file descriptor table. The device file for standard input, standard output, and standard error output is opened, the corresponding file descriptors 0, 1, and 2 are recorded in the table.After the program runs, the three file descriptors are opened by default..

# Define STDIN_FILENO 0 // standard input file descriptor

# Define STDOUT_FILENO 1 // standard output file descriptor

# Define STDERR_FILENO 2 // file descriptor with a standard error


When other files are opened after the program runs,The system returns the minimum available file descriptor in the file descriptor table.And the file descriptor is recorded in the table. In Linux, a process can only open up to NR_OPEN_DEFAULT (that is, 1024) files, so when the file is no longer in use, you should call the close () function to close the file in time.


Common I/0 Functions

Required header file:

# Include <sys/types. h>

# Include <sys/stat. h>

# Include <fcntl. h>

# Include <unistd. h>


Int open (const char * pathname, int flags, mode_t mode );

Function:

Open the file. If the file does not exist, create it.

Parameters:

Pathname: file path and file name.

Flags: indicates the behavior of opening a file. For example, if the file is opened in read-only mode (O_RDONLY, the first one is a letter rather than data), it is opened in read/write or new file mode (O_RDWR | O_CREAT.


Mode: this parameter is valid only when the file does not exist. It indicates the permission of the specified file when the file is created (for details about the File Permission, click this link ).


Return Value:

Success: The opened file descriptor is returned successfully.

Failed:-1


Int close (int fd );

Function:

Close opened files

Parameters:

Fd: file descriptor, return value of open ()

Return Value:

Success: 0

Failed:-1


Ssize_t write (int fd, const void * addr, size_t count );

Function:

Write a specified number of data to a file (fd)

Parameters:

Fd: file descriptor

Addr: the first data address.

Count: the length (in bytes) of the written data ),Generally, the data is written into the file as much as possible.

Return Value:

Success: the number of bytes actually written.

Failed:-1


Ssize_t read (int fd, void * addr, size_t count );

Function:

Read a specified number of data into memory (buffer zone)

Parameters:

Fd: file descriptor

Addr: memory first address

Count: number of bytes read

Return Value:

Successful: Number of actually read bytes

Failed:-1


Practical examples

Next, we use the above four systems to call the I/O function to write a program that can implement the cp function of the system command:



Use open () to open the source file, use read () to read data from the file, and use write () to write data to the target file. The sample code is as follows:

# Include <stdio. h> # include <sys/types. h> # include <sys/stat. h> # include <fcntl. h> int main (int argc, char * argv []) {if (argc = 3) & (strcmp (argv [1], argv [2])! = 0) {// ensure that there are three parameters, and the source file and the target file name cannot be the same int fd_src, fd_dest, ret; // open the source file fd_src = open (argv [1], O_RDONLY) in read-only mode; if (fd_src <0) {perror ("open argv [1]"); return-1;} // create the destination file fd_dest = open (argv [2], O_WRONLY | O_CREAT, 0755); if (fd_dest <0) {close (fd_src ); perror ("open argv [2]"); return-1 ;}do {char buf [1024] ={ 0}; // read data from source file ret = read (fd_src, buf, sizeof (buf); // write the data to the target file. Pay attention to the last parameter and the number of writes (fd_dest, buf, ret );} while (ret> 0); // close the opened file close (fd_src); close (fd_dest);} return 0 ;}

The running result is as follows:



For sample code downloading in this tutorial, click here.

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.