Use of open read write fcntl lseek and other functions:

Source: Internet
Author: User

System Call: the so-called system call refers to a set of special interfaces provided by the operating system to the user. The user program can use this set of special interfaces to obtain the services provided by the kernel. For example, the kernel can create processes through system calls related to process control to implement process scheduling and process management.

 

Why can a user program enjoy the services provided by the kernel only through system calls? In linux, to better protect the kernel space, the program space is divided into kernel space and user space (kernel state and user State ). They run at different levels and are logically isolated from each other. Therefore, user programs cannot access kernel data or access kernel functions, they can only operate user space data and access user space functions.

 

However, in some special cases, a user space process needs to obtain system services (a program that calls the kernel space), and the system calls require that the user process enter the specific location of the kernel space. During system calling, the program running space needs to enter the kernel space from the user space.

 

Linux system calls (only about 250). These system calls can be divided into process control, inter-process communication, file system control, system control, storage management, and network management according to the functional logic, socket control, user management.

 

 

User Programming Interface API

System calls do not directly interact with programmers. They only provide a soft interrupt mechanism to provide requests to the kernel to obtain the kernel service interfaces. In practical applications, APIs are usually called as user programming interfaces. Not all API functions correspond to one system call. When there is an API function, several system calls are required to work together to complete the function. There are even some API functions that do not need to be called by the system (so what they do is not the services provided by the kernel ).

 

 

In Linux, User Programming Interface APIs follow the POSIX standard, the most popular application programming interface standard in UNIX.

 

 

System Command: The system command is a higher level than the API. In fact, it is an executable program, and the user programming interface API is referenced internally to implement corresponding functions.

 

Linux Files and file descriptors:

In Linux, operations on directories and devices are equivalent to operations on files. Therefore, the processing of different devices by the system is greatly simplified. Linux Files include common files, directory files, connection files, and device files.

 

 

For Linux, all operations on devices and files are performed using file descriptors.

 

 

 

File I/O operations without Caching: (open close write read sleek)

 

 

Without caching, each function calls only one function in the system.

 

(1) Description of the open close function:

 

The open function is used to open and create files. When opening or creating a file, you can specify the attributes of the file and various parameters of the user's permissions.

 

Close to close an open file. When a process is terminated, all opened files are automatically closed by the kernel. You can see that many programs use this function without explicitly closing a file.

 

Int open (char * pathname, flag, int perms );

Patname: the file name that opens the file contains the directory name.

Flags: file opening method. O_RDONLY, O_WRONLY, O_RDWR, O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC, O_APPEND

Perms: The deposit permission for the opened file, which is in octal notation.

If O_EXCL uses O_CREAT as a file, an error message is returned. It can be used to determine whether a file exists.

If the O_TRUNC file already exists and is successfully opened as read-only or write-only, the original data in the file will be deleted.

O_APPEND opens a file by adding a file. When the file is opened, the file Pointer Points to the end of the file.

 

Int close (int fd );

 

Fd file description method, return value 0 Success 1 error.

 

Ssize _ t read (int fd, void * buf, size_t count)

Fd: file descriptor

Buf: Specify the buffer for reading data from the memory.

Count: the number of bytes read.

Returned value: Successful: number of bytes read: 0: the value has reached the end of the file-1 error.

 

Ssize_t write (int fd, void * Buf, count)

FD: file descriptor

Buf: Specifies the buffer for data written to the storage.

Count: the number of bytes read.

Returned value: successful: the number of bytes written-1 error.

 

Off_t lseek (int fd, off_t offset, int whence)

FD: file descriptor

Offset: the offset, the distance to be moved for each read/write operation. The unit is the number of bytes. It can be positive or negative (forward or backward)

 

Whence: the current position of seek_set is the beginning of the file, and the new position is the offset size.

The current position of seek_cur is the file pointer, and the new position is the current position plus the offset.

The current position of seek_end is the end of the file, and the new position is the file size plus the offset.

 

 

Open Close write read lseek allows you to open, close, read, and write files.

Related Article

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.