Linux under C Programming (a) file base

Source: Internet
Author: User
Tags file type file

Bo Master Original, reproduced please add http://www.cnblogs.com/jikexianfeng/p/5769357.html

I. Curriculum overview
1. File descriptors
2. File operations and kernel data structures
3. File Atomic operation

Two. Text
(i). File descriptors
1. Introduction to file descriptors
1). For the Linux kernel, all file opens are referenced by a file descriptor.
2). The file descriptor is a non-negative integer (returns-1 is the standard error) and corresponds to the file.
3). When an existing file is opened or a new file is created, the kernel returns a file descriptor to the process.
4). When reading and writing files, you need to pass the file descriptor as an argument to the appropriate function (Read,write) (with the Open or creat return value).
5). Code Practice Gcc-o Bin/fd_test src/fd_test.c
Problem: When (./bin/fd_test) executes code, open file fails (return value is-1), enter bin directory to execute fd_test when successful (return value is 3)?
6). Why is the return value 3?
When the program runs, the system will open 3 files, standard input 0 (Stdin_fileno), standard output 1 (stdout_fileno), standard error 2 (Stderr_fileno), which are defined in the header file <unistd.h> So the open file is counted starting at 3.
7). The scope of the file descriptor is 0~open_max. This macro value. Many systems are 63,linux 1024.
(ii) Implementation of SHELL commands
1. Custom header File
1). Write a custom header file with the CP function io.h (passed in as a file descriptor)
2) path
Include/io.h
Src/io.c
3). Compile and build the binary file
Gcc-c-iinclude src/io.c
2. Writing MCP upper-level files
1). Path
Src/mcp.c
2). Compiling
Gcc-iinclude src/io.c Src/mcp.c-o BIN/MCP
3. Writing MCAT upper-level files
1). Path
Src/mcat.c
2). Compiling
Gcc-iinclude src/io.c Src/mcp.c-o BIN/MCP

(iii). File operations and kernel data Structures
1. Five functions that manipulate file I/O
1). Open
2). Read
3). Wirte
4). Lseek
5). Close
2. Action file descriptor function
1). DUP
2). dup2
3. Change the file status function
1). FCNTL
4. The data structure of the file in the kernel (there are 3 data structures represented)
1). File descriptor Table
2). File Tables Entry
3). V node
5. File Atomic Operation


(iv). Function Description
1.open function (open file)
1). header file
#include <sys/types.h>
#include <sys/stat.h>
# Include <fcntl.h>
2). prototype
int open (const char *pathname, int flags);
int open (const char *pathname, int fl AGS, mode_t mode);
Const Char *pathname is the file path to open
int flags as read/write flag bit
O_rdonly self-read open
O_wronly write-only open
or o_rdwr read-write clock-out
O_app End is added to the end of the file each time it is written
O_creat if this file does not exist, create it, use this selection to describe the third parameter mode, use it to describe the permissions of the file
O_excl if O_creat is specified and the file exists, The error occurs. This tests whether a file exists and creates the file if it does not exist.
O_trunc If this file exists and is open for read-only or write-only success, truncate its length to 0
O_sync physical I/O operations such as write every time
O _nonblock If pathname specifies a FIFO, a special file, or a word special file, this option sets the non-blocking mode for this open operation and subsequent I/O operations for this file.
mode_t mode is an optional parameter, and only when the file is created will you choose to use the
return value: The successful return of the file descriptor, and the failure to return -1.

2.creat function (create file, default to write-only open)
1). header file
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
2). Prototypes
int creat (const char *pathname, mode_t mode);
const char *pathname is the file path to open
Permissions for the mode_t mode file
Return value: Successful return of file descriptor, failed return-1.

3.close function (close file descriptor)
1). header file
#include <unistd.h>
2). Prototypes
int close (int fd);
int FD to file descriptor
Return value: Successfully returned 0, failure returned-1.
When a process is developed, all the files it opens are automatically closed by the kernel.

4.read function (read content from file)
1). header file
#include <unistd.h>
2). Prototypes
ssize_t pread (int fildes, void *buf, size_t nbyte, off_t offset);
ssize_t Read (int fildes, void *buf, size_t nbyte);
int Fildes File Descriptor
void *buf Buffer
size_t nbyte The number of bytes required to read data
Return value: The number of bytes read successfully returned, or 0 if the end of the file has been read and the file has no content, and the failure returns-1.
3). There are several scenarios in which the number of bytes read is less than the number of bytes required to read
(1). When reading a normal file, the end of the file is reached before the required number of bytes are read.
(2). When reading from an end device, you usually read a line up.
(3). When reading from the network, the cache mechanism in the network may cause the return value to be less than the number of bytes required to read.
(4). Some record-oriented devices, such as tapes, return at most one record at a time.
(5). The process terminates due to a signal interruption (the process is being read).

5. Write function (writes content to file)
1). header file
#include <unistd.h>
2). Prototypes
ssize_t pwrite (int fildes, const void *BUF, size_t nbyte,off_t offset);
ssize_t write (int fildes, const void *buf, size_t nbyte);
int Fildes File Descriptor
const void *BUF, size_t nbyte buffer
size_t nbyte The number of bytes that need to be written to the file
Return value: The number of bytes written to successfully returned, failure returns-1.

6.lseek function (read and write pointer position when reading and writing files)
1). header file
#include <unistd.h>
#include <sys/types.h>
2). Prototypes
off_t lseek (int fd, off_t offset, int whence);
int FD File Descriptor
off_t offset File offset
int whence position
If whence is seek_set, the offset of the file is set to offset bytes from the beginning of the file.
If whence is seek_cur, the offset of the file is set to the current value plus offset, which can be either positive or negative.
If whence is seek_end, the offset of the file is set to the file length plus offset, which can be positive or negative.

8.dup and Dup2 (file descriptor Operations)
1). header file
#include <unistd.h>
2). Prototypes
int dup (int oldfd);
int dup2 (int oldfd, int newfd);
Used to copy file descriptors, change process standard input and standard output device when process communication,
Return value: Successful return of the new file descriptor, failed return-1.
3). Use dup2 standard input/output from the directed file.
(1). Writing dup2_test.c
(1). Path
Src/dup2_test.c
(2). Compiling
Gcc-o bin/dup2_test src/dup2_test.c

9.fcntl (can change the nature of the file that has been opened.)
1). header file
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
2). Prototypes
int fcntl (int fd, int cmd, .../* arg */);
int FD File Descriptor
int cmd file status setting
Return value: Success (then dependent on cmd) returns a number greater than 0, and the failure returns-1.
3). Writing MCAT upper-level files
(1). Path
Src/flag_test.c
(2). Compiling
Gcc-iinclude src/io.c Src/flag_test.c-o Bin/flag_test
(3)./bin/flag_test test.txt zhangjianqi!

10. Data structure of the file in the kernel (see./picture/data structure in the kernel. png)
An open file in the kernel has three kinds of data structure, the first is a File descriptor table, the second is a file, the third is a V node, where when a process starts, he will automatically open a file structure, the file structure is a number of FB file descriptor fields, So how to find this file through the File Descriptor field, let's look at a sketch, when a process starts, it will automatically open some file descriptor, there are 0 (standard input) 1 (standard output) 2 (standard error), and then through the file descriptor to find a data structure in the kernel FDT, The 2nd is a pointer to the file table entry, the file description file table inside the first content is a file descriptor flag, the second is a representation of the pointer, is a pointer to the file table entry, when it found the file table of FI, the file table entry also records the status flag of the file, that is, read only, write, or o_ Append status flag, and the current file offset, the file table entry also records the V Node table key Pointer, he also used to point to the next data structure v node.
V node is also stored in the file type file creator or operation time, that is, ls-l see the content, where the v node is stored in the most important content is I-node (the label of the fast device, that is, ls-i see the content), and then through the I-node to find the file on this disk .

11. File Atomic operation
1). Robust Append
When you open a file with the O_appen flag, the process adds an atomic operation to the file offset adjustment and data. Each time the kernel writes to the file, it sets the current offset of the process to the end of the file, so that it does not need to lseek to adjust the offset.
2). File creation
The O_creat and o_excl of the open function are used, and the file exists, open fails, otherwise the file is created, and the decision to make the file exists and the creation process is atomic.

Code Download: Https://git.oschina.net/jikexianfeng/LINUX.git

Linux under C Programming (a) file base

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.