Process mapping, mmap (DAY05)

Source: Internet
Author: User

First, memory management (continued) each process has its own independent 4G virtual address space. Von Neumann architecture The Harvard architecture Section Code snippet read-only data segment stack example illustrates the segment to which the data belongs. Code See MEMORY.C How do I get the process's own PID? Getpid (2) #include<sys/types.h>#include<unistd.h>pid_t Getpid (void); function: Gets the ID parameter of the process:voidreturn value: pid_t The Pidcat of the current process/proc/pid/maps initialization and assignment are not the same thing. The stack segment and code snippet data segment stack segment is a container with multiple stack frames in the stack segment. A process has only one stack segment. Each function has its own stack frame, and the function's stack frame is freed at the end of the function. The space of the variable in the function is allocated on the stack frame, and the variable space is freed. The life of the variable is over. What kinds of variables are allocated on the stack frame?  The spatial allocation of the parameters of the automatic local variables and functions in the function is in the stack frame. function static local variables and the spatial allocation of global variables in the data segment. Process but the scope of the two is different. Stackheap code Snippet data segment heap, stack life cycle either the programmer manually frees the heap space. Otherwise, at the end of the process. Second, use Mmap to map the physical address to the virtual address space of the process. Mmap (2) #include<sys/mman.h>void*mmap (void*ADDR, size_t length,intProtintflags,intfd, off_t offset); function: Maps a file or device to a memory parameter: Addr: Specifies the starting address of the mapped region. The NULL address has the kernel decision length: Specifies the size of the mapped area prot:prot_exec Pages may executed. Prot_read Pages may be READ. Prot_write Pages may written. Prot_none Pages May is accessed.flags:2 selected 1:map_shared: Updates to the mapped area are viewed by other processes, and also synchronized to the underlying file. Map_private: Updates to the mapped area are not visible to other processes. Also does not sync to the lower-level file map_anonymous: The contents of the mapped area are initialized to 0. Not related to any file. If this parameter is referred to. FD and offset are ignored. FD: File descriptor (file) offset: The file's starting position (file) return value: map_failed error errno is set to return the first address of the mapped area. intMunmap (void*addr, size_t length); function: Release the file-to-memory mapping parameter: Addr:mmap (2The return value of length: Specifies the size of the mapped area. return value:0Success-1The failure errno is set to add: Everything is file. For example, use Mmap to map a physical address to the virtual address space of a process and then manipulate the virtual address space. Code See MMAP.C Summary: First, process map snippet data segment Stack week 6th review C language Two, use Mmap to map physical addresses to the virtual address space of the process

Process mapping, mmap (DAY05)

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.