Linux Development Learning Diary (Part 1) (Unix Environment Advanced Programming)

zhaozj2021-02-17  51

/ * Affirmation: This learning diary is written with reference to "Unix Environmental Advanced Programming".

Since the first chapter and the second chapter mainly explains some basic knowledge of the UNIX (Linux) system and some technical standards, I will no longer waste time and energy. If you are interested, you can look at yourself.

This chapter mainly explains the related functions and usage of unbuffered I / O operations in the Linux system. Finally, for everyone to flexibly master the usage of these functions, a small amount of up-machine exercises is given.

First, we understand the approximate process of file operations from the whole: Open (Open) or create (create) files -> read (read), write (LSEEK) Location file -> Close file (close) . Then in the unbuffered I / O operation, what we use to mark our open files ------ File Descriptor.

1. File Descriptor

The file descriptor is the minimum non-negative integer of the system kernel assigned to the open file. When we open (Open) an existing file or create a new file, the kernel returns a file descriptor to the process. It is then transmitted as a parameter to R e a d, W R I t e, Lseek, and so on, the function of operating the file.

The scope of the file descriptor is limited in the header file, and the new Linux system is usually 63, that is, one process can open 64 files at the same time. At the same time, three constants of stdin_fileno, stdout_fileno, stderr_fileno, which are 0, 1, 2, respectively, represent the standard input device (generally a keyboard), respectively, and standard output devices, respectively, and standard output devices, respectively. Output device (generally also a display).

2. File operation function

1>. Open file functions

#include

#include

#include

Int open (const char Pathname, int OFLAG, ... / * MODE_T MODE * /)

Returns: If it is successful for the INT type file descriptor, if the error is - 1

Pathname: You want to open the directory where the file is located (including the file name). Oflag: Specifies the parameters of the way to open the file, can be combined by a single or "or" combination. The most mainly three: read-only (o_rdonly), written (O_Wronly), read and write (o_rdwr); there are some other control parameters such as o_append, o_creat, etc., these parameters specific meaning and usage can pass in Linux Man Open is obtained. They are all defined in the header file . Mode: is an optional parameter, which only when there is O_CREAT in the OFLAG, this parameter will appear in the Open function, which is mainly used to specify the permissions when creating a new file.

转载请注明原文地址:https://www.9cbs.com/read-30858.html

New Post(0)