Can the Linux operating system seize Microsoft's share in the future? Many developers are thinking about this issue. In fact, a single transaction cannot be monopolized at present, because it is not good for any transaction to be monopolized, and competition can lead to development. However, at least in the near future, the Linux operating system does not have such a condition, I personally think that the key lies in the fact that there are too many Linux operating system versions that are not uniform enough.
The Linux operating system provides the memory ing function mmap, which maps the file content to a memory segment (specifically, virtual memory). By reading and modifying this memory segment, to read and modify files, Let's first look at the mmap function declaration:
Header file:
<Unistd. h>
<Sys/mman. h>
Prototype: void * mmap (void * addr, size_t length, int prot, int flags, int fd, off_t offsize );
Returned value: if the operation succeeds, the start address of the ing area is returned. If the operation fails, MAP_FAILED (-1) is returned ).
Parameters:
Addr: Specifies the starting address of the ing, which is usually set to NULL and specified by the system.
Length: how long the file length is mapped to the memory.
Prot: Protection Method of the ing area, which can be:
PROT_EXEC: The ing area can be executed.
PROT_READ: The ing area can be read.
PROT_WRITE: The ing area can be written.
PROT_NONE: The ing area cannot be accessed.
Flags: the features of the ing area, which can be:
MAP_SHARED: write data to the ing area will be copied back to the file, and other processes that map the file can share.
MAP_PRIVATE: write operations on the ing area will generate a copy-on-write operation. modifications made to this area will not be written back to the original file.
In addition, there are several other flags that are not commonly used. For details, refer to the Linux operating system C function description.
Fd: file descriptor returned by open, representing the file to be mapped.
Offset: the offset starting from the file. It must be an integer multiple of the page size, usually 0, indicating the ing from the file header.
The following describes the steps for memory ing:
Open the file with an open system call and return the descriptor fd.
Use mmap to create a memory ing and return the ing first address pointer start.
Perform Various operations on the ing (file), display (printf), and modify (sprintf ).
Use munmap (void * start, size_t lenght) to disable memory ing.
Use the close system call to close the file fd.
Note:
When modifying a ing file, you can only modify the original length, but cannot increase the file length, because the memory has been allocated.
The mmap knowledge in the Linux operating system is introduced here. I hope it will help you learn about Linux.
- Use logs to make Linux management easier
- Operation notes: Linux Framebuffer Programming
- Detailed introduction to Linux File Types
- Command Daquan: Linux advanced command line skills
- Introduction Linux screenshot tool: import