Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of linux process communication shared memory?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "what is the principle of shared memory for linux process communication". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the principle of linux process communication shared memory"?

1 there is a global structure data, each time you need a piece of shared memory (shmget), take a structure from it and record the relevant information.

Struct shmid_ds {

/ / permission related

Struct ipc_perm shm_perm; / * operation perms * /

/ / size of shared memory

Int shm_segsz; / * size of segment (bytes) * /

Time_t shm_atime; / * last attach time * /

Time_t shm_dtime; / * last detach time * /

Time_t shm_ctime; / * last change time * /

/ / the process of creating the structure

Unsigned short shm_cpid; / * pid of creator * /

Unsigned short shm_lpid; / * pid of last operator * /

/ / the number of processes currently using the shared memory

Short shm_nattch; / * no. Of current attaches * /

/ * the following are private * /

/ / number of pages of shared memory

Unsigned short shm_npages; / * size of segment (pages) * /

/ / pointer to shared physical memory

Unsigned long * shm_pages; / * array of ptrs to frames-> SHMMAX * /

/ / process information using the shared memory

Struct vm_area_struct * attaches; / * descriptors for attaches * /

}

2 input the id returned by shmget when calling shmat. Shmat finds the corresponding shmid_ds structure according to id. Create a new vm_area_struct structure. The start and end addresses are calculated based on the information in the shmid_ds, that is, the size of the user's request. Then insert the vm_area_struct into the avl tree that manages the vm_area_struct in the process. And save some context information to the page table item. Use in shm_swap_in when broken pages are missing.

Shm_sgn = shmd- > vm_pte + ((shmd- > vm_offset > > PAGE_SHIFT) vm_start; tmp

< shmd->

Vm_end; tmp + = PAGE_SIZE

Shm_sgn + = (1 vm_task,tmp)

Page_middle = pmd_alloc (page_dir,tmp)

If (! page_middle)

Return-ENOMEM

Page_table = pte_alloc (page_middle,tmp)

If (! page_table)

Return-ENOMEM

Pte_val (* page_table) = shm_sgn

}

A page break is triggered when a process accesses an address in the shared memory range.

Void do_no_page (struct vm_area_struct * vma, unsigned long address)

Int write_access)

{

Pte_t * page_table

Pte_t entry

Unsigned long page

/ / obtain the address of the page entry corresponding to address in the process page table

Page_table = get_empty_pgtable (vma- > vm_task,address)

/ / if the assignment fails, it returns.

If (! page_table)

Return

Entry = * page_table

/ / the mapping from virtual address to physical address has been established, and return

If (pte_present (entry))

Return

/ / No mapping has been established yet

If (! pte_none (entry)) {

Do_swap_page (vma, address, page_table, entry, write_access)

Return

}

.

}

Do_swap_page is called in a page fault break.

Static inline void do_swap_page (struct vm_area_struct * vma, unsigned long address)

Pte_t * page_table, pte_t entry, int write_access)

{

Pte_t page

If (! vma- > vm_ops | |! vma- > vm_ops- > swapin) {

Swap_in (vma, page_table, pte_val (entry), write_access)

Return

}

Page = vma- > vm_ops- > swapin (vma, address-vma- > vm_start + vma- > vm_offset, pte_val (entry))

If (pte_val (* page_table)! = pte_val (entry)) {

Free_page (pte_page (page))

Return

}

If (mem_ map [map _ NR (pte_page (page))] > 1 & &! (vma- > vm_flags & VM_SHARED))

Page = pte_wrprotect (page)

+ + vma- > vm_task- > mm- > rss

+ + vma- > vm_task- > mm- > maj_flt

/ / write to the physical address

* page_table = page

Return

}

Where vma- > vm_ops- > swapin corresponds to the shm_swap_in of shm.c

Pte_val (pte) = shp- > shm_ pages [IDX]

/ / No physical memory has been allocated yet

If (! pte_present (pte)) {

/ / allocate physical memory

Unsigned long page = get_free_page (GFP_KERNEL)

...

/ / record the physical address

Shp- > shm_ pages [IDX] = pte_val (pte)

}

Mem_ map [map _ NR (pte_page (pte))] + +

Return pte_modify (pte, shmd- > vm_page_prot)

Assign if the physical address has not been assigned, otherwise directly range the address that has already been assigned. The last sentence of the do_swap_page function writes the physical address to the page table entry of the process. There will be no page breakage next time.

Similarly, when other processes share the block of memory, the process is similar if the address in the range is accessed. The process accesses an address, a page fault occurs, and then it goes into the do_swap_page function, and then to shm_swap_in. It is found that the shared memory has been mapped to the physical address at this time. Finally, rewrite your own page table item. Because each process corresponds to the same piece of memory, it will perceive each other and communicate with each other during the operation.

At this point, I believe you have a deeper understanding of "what is the principle of linux process communication shared memory". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report