共享内存小记


Shared Memory

    Shared Memory is easily to understand, especially after grasping semaphore.

    Normally, if we execute a piece of program twice (make them running at the same time), variables with the same name will occupy different physical memory. That means if in p1(one of the running process) the system makes x++, in p2(another running process) the same-name variable x will not be affected.

    But sometimes we need that the 2 processes could influence each other for communication, then we can use shared memory.

    Similar to semaphore, we use get function to obtain an ID.

        int shmget(key_t key, size_t size, int shmflg);

    shmget() returns an ID pointing to the shared memory associated to the parameter key. The 2nd argument size indicates the memory size that will be allocated, And we usually set shmflg to 0666 | IPC_CREAT.

    After we execute the shmget() function, the memory could not be accessed yet. We should use shmat() to attach the shared memory to the calling process, so that the process could perform operations on the memory.

       void *shmat(int shmid, const void *shmaddr, int shmflg);

    shmid is the ID returned by shmget(). If we set shmaddr to (void *)0, the system will find a suitable memory to allocate. And usually we set shmflg to 0.

    The opposite one to shmat() is shmdt() -- to detach the shared memory from the calling process so that the process could not operate with it any more unless shmat() again.

    The prototype of shmdt() is :

       int shmdt(const void *shmaddr);

    Finally, we use shmctl() to remove the shared memory.

    Usually we use shmctl(shmid, IPC_RMID, 0).

 

    以下是一个小实验,我们用shm1.c来对共享内存写消息,然后用shm2.c来读取。

shm1.c:

shm2.c:

    对两段代码编譯执行:

       需要注意的是对共享内存的读写并不是原子操作,可能出现读到一半的时候另一个进程重写了共享内存,这样难免会出现错误。 关于这一点,则需要编程人员来控制。


Jason Lee

2009-11-15 p.m

### 使用 UniApp 开发小记账本应用程序 #### 项目概述 生活记账小程序通过前端 Vue 和 UniApp 设计开发,后端采用 SpringBoot 提供数据接口支持。主要功能模块包括首页展示、分类记账以及微信登录状态管理[^1]。 #### 创建新项目 首先安装 HBuilderX 或者其他 IDE 工具来创建一个新的 UniApp 项目: ```bash npm install -g @dcloudio/uni-cli uni create myAccountBookProject ``` 进入项目目录并初始化必要的配置文件。 #### 配置 App.vue 生命周期函数 为了更好地控制应用生命周期,在 `App.vue` 中定义如下几个重要钩子函数用于处理不同场景下的逻辑操作: - **onLaunch**: 当整个程序启动时调用此方法, 可以在这里做一些全局性的初始化工作. - **onShow**: 页面每次从前台切到后台再返回前台都会触发这个事件, 合适用来刷新某些实时变化的内容. - **onHide**: 对应于当用户点击 Home 键使 APP 进入后台运行模式时执行的动作. 这些设置有助于提升用户体验流畅度和响应速度[^2]. #### 数据绑定与交互实现 确保所有的业务逻辑都放置在 methods 下面,并且避免 data 属性名称同 method 名冲突以免造成不必要的错误;另外注意检查 HTML 标签内部是否存在重复属性声明尤其是集成第三方 UI 组件库 uView-ui 的时候要格外小心[^3]: ```html <template> <!-- 记录条目输入框 --> </template> <script> export default { name: 'RecordEntry', data() { return { recordType: '', amount: null, date: '' } }, methods:{ addNewRecord(){ // 添加新的财务记录... } } } </script> ``` #### 接口对接与缓存机制 对于前后端分离架构的应用来说,合理的 API 路由规划至关重要。这里推荐使用 RESTful 风格的服务端点配合 axios 发起 HTTP 请求完成增删改查等基本 CRUD 功能。与此同时引入 Redis 做为临时存储介质加速频繁访问的数据读取效率降低 MySQL 查询压力。 #### 微信开放能力接入 如果计划让这款理财工具具备社交分享特性,则需按照官方文档指引注册成为微信公众平台开发者账号获得 appid 权限认证之后才能正常使用诸如支付等功能服务。此外还可以考虑加入直播营销插件吸引更多潜在客户群体关注产品动态[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值