What is Shared Memory?

共享内存机制详解
本文深入探讨了共享内存机制,一种使多个进程能够访问同一段内存的技术。通过介绍共享内存的基本概念、工作流程及其在UNIX System V中的实现方式,本文帮助读者理解如何在不同进程中设置共享内存,以及如何通过系统调用来管理和保护这些共享区域。

What is Shared Memory?

In the discussion of the fork() system call, we mentioned that a parent and its children have separate address spaces. While this would provide a more secured way of executing parent and children processes (because they will not interfere each other), they shared nothing and have no way to communicate with each other. A shared memory is an extra piece of memory that is attached to some address spaces for their owners to use. As a result, all of these processes share the same memory segment and have access to it. Consequently, race conditions may occur if memory accesses are not handled properly. The following figure shows two processes and their address spaces. The yellow rectangle is a shared memory attached to both address spaces and both process 1 and process 2 can have access to this shared memory as if the shared memory is part of its own address space. In some sense, the original address spaces is "extended" by attaching this shared memory.

 

Shared memory is a feature supported by UNIX System V, including Linux, SunOS and Solaris. One process must explicitly ask for an area, using a key, to be shared by other processes. This process will be called the server. All other processes, the clients, that know the shared area can access it. However, there is no protection to a shared memory and any process that knows it can access it freely. To protect a shared memory from being accessed at the same time by several processes, a synchronization protocol must be setup.

A shared memory segment is identified by a unique integer, the shared memory ID. The shared memory itself is described by a structure of type shmid_ds in header file sys/shm.h. To use this file, files sys/types.h and sys/ipc.h must be included. Therefore, your program should start with the following lines:

#include  <sys/types.h>
#include  <sys/ipc.h>
#include  <sys/shm.h>

A general scheme of using shared memory is the following:

  • For a server, it should be started before any client. The server should perform the following tasks:
    1. Ask for a shared memory with a memory key and memorize the returned shared memory ID. This is performed by system call shmget().
    2. Attach this shared memory to the server's address space with system call shmat().
    3. Initialize the shared memory, if necessary.
    4. Do something and wait for all clients' completion.
    5. Detach the shared memory with system call shmdt().
    6. Remove the shared memory with system call shmctl().
  • For the client part, the procedure is almost the same:
    1. Ask for a shared memory with the same memory key and memorize the returned shared memory ID.
    2. Attach this shared memory to the client's address space.
    3. Use the memory.
    4. Detach all shared memory segments, if necessary.
    5. Exit.

On the next few pages, we shall describe these system calls and their uses.

 
ubuntu# rtl8852be_bt ## Introduction This code is for realtek 8852BE bluetooth. I cloned original code from [Linux 5.15 Kernel LTS](https://www.kernel.org/) and change it a bit. It can now support Ubuntu 22.04 LTS(need more test). Any ideas are welcomed. ## Prerequisites ``` build-essential linux-headers bc ``` ## Usage ### 1. Check your Bluetooth Device ``` $ lsusb $ Bus 003 Device 005: ID 0bda:4853 Realtek Semiconductor Corp. Bluetooth Radio ``` ### 2. Modify/Add your USB DEVICE in btusb.c [Modify Here](https://github.com/HRex39/rtl8852be_bt/blob/5.15/btusb.c#L424) ``` /* Realtek 8852BE Bluetooth devices */ { USB_DEVICE(0x0bda, 0x4853), .driver_info = BTUSB_REALTEK | BTUSB_WIDEBAND_SPEECH }, // USB_DEVICE(0x____ , 0x____) is your ID name which shows in lsusb command ``` ### 3. Save and Build ``` #Turn off your Security Boot in BIOS make sudo make install reboot ``` ## WHAT IS LINUX? ``` WHAT IS LINUX? Linux is a clone of the operating system Unix, written from scratch by Linus Torvalds with assistance from a loosely-knit team of hackers across the Net. It aims towards POSIX and Single UNIX Specification compliance. It has all the features you would expect in a modern fully-fledged Unix, including true multitasking, virtual memory, shared libraries, demand loading, shared copy-on-write executables, proper memory management, and multistack networking including IPv4 and IPv6. It is distributed under the GNU General Public License - see the accompanying COPYING file for more details. ```蓝牙搜不到设备
09-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值