To talk about unix / linux for file operations in these functions,These functions for linux basically meet our requirements in any document,
First talk about the open function
In linux you want to operate on the file must first open the file and then the corresponding processing,Why should we do it? That is because the process and not directly manipulate files between,To process the file first to use the system call between processes and documents connected to a resume operations,This connection becomes a file descriptor,Called by the process leading to the kernel as a kernel pipeline,Build relationships between processes and files from。
Understanding of the specific file descriptor can be understood by its use,Non-negative integer file descriptor (file descriptor) is usually a small。It identifies the kernel file with a particular process is accessible。When the kernel to open a file or create a new Friends of the file,He sent back a file descriptor (non-negative integer)。You can use it when reading and writing files。
A brief look at this function open
1 2 3 4 5 6 7 8 9 |
目标 打开一个文件,让进程可以对文件进行操作。 头文件 #include 函数原型 int fd = open(char *name, int how) 参数 name // 文件名 how // 打开模式 O_RDONLY,O_WEONLY,orO_REWR // 返回值 int fd // 返回的文件描述符(非负整数) -1 // 遇到错误 |
If the file is opened successfully go back on the value of a positive integer,This value is the file descriptor,Since file descriptor is to establish a connection between the files and processes,Then the return value of type int is what effect? In fact, the int return value is the file descriptor is used to identify this process sweater and files connected。If you open multiple files,They are different file descriptor,If a file is opened multiple processes at the same time they file descriptor is different。This is the uniqueness of the file descriptor,And other functions must be performed on a file by file descriptor。
Using open this function allows us to open a file,Speak processes and files associated with them。But when the file is opened successfully whether to allow it to open again? This situation occurred in the case of multiple processes accessing a file,unix / linux does not prohibit a file simultaneously access multiple processes,If disabled, then the multi-user mode, many commands will not be used。
Original article written by LogStudio:R0uter's Blog » unix / linux programming study notes- Open file descriptors and function
Reproduced Please keep the source and description link:https://www.logcg.com/archives/527.html