当前位置: 首页 > news >正文

北京 网站制作seo入门教程视频

北京 网站制作,seo入门教程视频,360网站seo,wordpress文章页调用作者作业一 功能要求利用 mmap(虚拟内存映射文件) 机制实现一个带持久化能力的 key-valueMap 系统,至少支持单机单进程访问。(可能用到的 linux API: mmap、msync、mremap、munmap、ftruncate、fallocate 等) 电子版提交方式: 2023 年 11 月 20 日 18:00 前通…

作业一

功能要求利用 mmap(虚拟内存映射文件) 机制实现一个带持久化能力的 key-valueMap 系统,至少支持单机单进程访问。(可能用到的 linux API: mmap、msync、mremap、munmap、ftruncate、fallocate 等)

电子版提交方式:
2023 年 11 月 20 日 18:00 前通过西电智课平台提交

提交内容

(1) 源代码,包含必要的注释;(2) 简单的说明文件,说明程序如何运行。

邮件主题、附件命名方式:主题:小作业 1-学号 - 姓名 (英文半角,非下划线).
附件:学号 - 姓名.rar,请严格按照命名规范提交!。
联系邮件:xxxxxxx
请勿抄袭,如有雷同,都将以零分计。

代码说明

运行测试结果

$gcc mmapMap.c && ./a.out
强制同步比 0.00 > 0.078 秒
强制同步比 0.10 > 0.276 秒
强制同步比 0.20 > 0.384 秒
强制同步比 0.30 > 0.513 秒
强制同步比 0.40 > 0.663 秒
强制同步比 0.50 > 0.602 秒
强制同步比 0.60 > 0.807 秒
强制同步比 0.70 > 0.775 秒
强制同步比 0.80 > 0.842 秒
强制同步比 0.90 > 0.910 秒
强制同步比 1.00 > 0.972

背景

Linux 内存文件映射是一种将文件内容映射到进程地址空间的技术,它允许进程直接在内存中访问文件,而无需通过 read()​ 或 write()​ 等系统调用进行数据传输。这种技术的核心是 mmap()​ 系统调用,它允许将文件的一部分或全部映射到进程的地址空间,使得文件的内容可以直接通过内存地址来访问和修改。

内存文件映射的主要特点和使用方法:

  1. 直接访问文件内容: 内存文件映射允许进程直接读取和写入文件内容,就好像操作内存一样,而不需要使用标准的文件 I/O 操作(例如 read()​ 和 write()​)。
  2. 性能优势: 由于避免了频繁的系统调用和数据拷贝,因此内存文件映射通常可以提供更好的性能,特别是对于大文件的处理或者需要频繁读写的情况。
  3. 共享内存: 多个进程可以通过内存文件映射共享同一个文件,这对于进程间通信很有用。
  4. 写时复制: 当多个进程映射同一个文件时,对该文件的写操作会使用写时复制技术,每个进程会获得一个文件内容的独立副本,从而避免了相互之间的干扰。

使用 mmap()​ 函数进行内存文件映射时,需要指定文件描述符、映射大小、映射起始位置以及一些其他参数。通常的步骤如下:

  1. 使用 open()​ 函数打开文件,获取文件描述符。
  2. 使用 mmap()​ 函数创建映射,将文件映射到内存中。
  3. 对映射区域进行读写操作。
  4. 使用 munmap()​ 函数取消映射。

可能用到的 linux API: mmap、msync、mremap、munmap、ftruncate、fallocate 介绍:

  1. mmap()
    • void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
    • 用于将文件或者设备映射到进程的地址空间。
    • 参数 addr​ 是映射的地址,一般设置为 NULL 以由系统自动选择。
    • length​ 是映射区域的大小。
    • prot​ 表示映射区域的保护方式(读、写、执行等)。
    • flags​ 包含映射的一些特性,比如是否共享、是否采用匿名映射等。
    • fd​ 是要映射的文件描述符。
    • offset​ 是文件中的偏移量。
  2. msync()
    • int msync(void *addr, size_t length, int flags);
    • 用于将指定地址范围的内存数据同步回文件,保持内存和文件内容的一致性。
    • addr​ 是内存区域的起始地址。
    • length​ 是要同步的长度。
    • flags​ 可以指定同步方式,如 MS_ASYNC(异步)、MS_SYNC(同步)等。
  3. mremap()
    • void *mremap(void *old_address, size_t old_size, size_t new_size, int flags, ...);
    • 允许重新调整内存映射的大小和位置。
    • old_address​ 是原映射区域的起始地址。
    • old_size​ 是原映射区域的大小。
    • new_size​ 是新的映射区域大小。
    • flags​ 可以指定一些选项,如 MREMAP_MAYMOVE(允许移动映射)等。
  4. munmap()
    • int munmap(void *addr, size_t length);
    • 用于取消内存映射,释放指定地址区域的内存。
    • addr​ 是要取消映射的起始地址。
    • length​ 是取消映射的长度。
  5. ftruncate()
    • int ftruncate(int fd, off_t length);
    • 用于改变一个打开文件的大小。
    • fd​ 是文件描述符。
    • length​ 是新的文件大小。
  6. fallocate()
    • int fallocate(int fd, int mode, off_t offset, off_t len);
    • 用于为文件分配空间。
    • fd​ 是文件描述符。
    • mode​ 可以指定预留空间或初始化空间。
    • offset​ 是文件中的偏移量。
    • len​ 是要分配的空间大小。

完整代码

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <time.h>
#include <unistd.h>#define FILENAME "kv_store.dat"/***************************************************************- key-valueMap 数据结构部分**************************************************************/#define MAX_KEY_SIZE 64
#define MAX_VALUE_SIZE 128
#define MAX_ENTRIES 4096struct KeyValue {char key[MAX_KEY_SIZE];char value[MAX_VALUE_SIZE];
};typedef struct keyValueStore {struct KeyValue entries[MAX_ENTRIES];size_t size;
} KeyValueStore;// 增加一个条目
// 返回值:-1 表示失败,>=0 表示成功
int add_entry(KeyValueStore *kv_store, const char *key, const char *value) {if (kv_store->size < MAX_ENTRIES) {// 查重for (size_t i = 0; i < kv_store->size; ++i) {if (strcmp(kv_store->entries[i].key, key) == 0) {return -1;}}struct KeyValue new_entry;strncpy(new_entry.key, key, MAX_KEY_SIZE - 1);strncpy(new_entry.value, value, MAX_VALUE_SIZE - 1);kv_store->entries[kv_store->size++] = new_entry;return kv_store->size - 1;} else {// printf("错误:已达到最大条目数。\n");return -1;}
}// 删除一个条目
// 返回值:-1 表示失败,>=0 表示成功
int delete_entry(KeyValueStore *kv_store, const char *key) {for (size_t i = 0; i < kv_store->size; ++i) {if (strcmp(kv_store->entries[i].key, key) == 0) {// 移动元素来删除memmove(&kv_store->entries[i], &kv_store->entries[i + 1],(kv_store->size - i - 1) * sizeof(struct KeyValue));kv_store->size--;return kv_store->size;}}// printf("错误:找不到项。\n");return -1;
}/***************************************************************- mmap 部分**************************************************************/// 打开一个文件,初始化内存为 mmap, 但是置空
KeyValueStore *mmap_init() {KeyValueStore kv_store;memset(&kv_store, 0, sizeof(KeyValueStore));int fd = open(FILENAME, O_RDWR | O_CREAT,(mode_t)0600); // 如果文件不存在也会新建if (fd == -1) {perror("Error opening file for writing");exit(EXIT_FAILURE);}size_t file_size = sizeof(KeyValueStore);// 转变一个文件的大小if (ftruncate(fd, file_size) == -1) {perror("Error truncating file");exit(EXIT_FAILURE);}void *buf = mmap(0, file_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);if (buf == MAP_FAILED) {close(fd);perror("Error mapping the file");exit(EXIT_FAILURE);}memcpy(buf, &kv_store, sizeof(KeyValueStore));close(fd);return buf;
}// 打开一个文件,初始化内存为 mmap, 为读取的文件内容
KeyValueStore *mmap_load() {int fd = open(FILENAME, O_RDWR); // 文件必须存在if (fd == -1) {perror("Error opening file for writing");exit(EXIT_FAILURE);}size_t file_size = sizeof(KeyValueStore);if (ftruncate(fd, file_size) == -1) {perror("Error truncating file");exit(EXIT_FAILURE);}void *buf = mmap(0, file_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);if (buf == MAP_FAILED) {close(fd);perror("Error mapping the file");exit(EXIT_FAILURE);}close(fd);return buf;
}// 关闭 mmap
void mmap_destroy(KeyValueStore *buf) {size_t file_size = sizeof(KeyValueStore);if (munmap(buf, file_size) == -1) {perror("Error unmapping the file");exit(EXIT_FAILURE);}
}// 强制同步 mmap
void mmap_persist(KeyValueStore *buf) {size_t file_size = sizeof(KeyValueStore);if (msync(buf, file_size, MS_SYNC) == -1) {perror("Error syncing to disk");}
}/***************************************************************- 测试和运行**************************************************************/// 测试运行时间
// - rate: 强制同步比
void test_time(KeyValueStore *buf, double rate) {// Start measuring timeclock_t start = clock();// 操作char key[MAX_KEY_SIZE];char value[MAX_VALUE_SIZE];int r;for (int i = 0; i < 10000; i++) {r = rand() % MAX_ENTRIES;// Addsprintf(key, "key%d", r);sprintf(value, "value%d", r);add_entry(buf, key, value);r = rand() % MAX_ENTRIES;// Deletesprintf(key, "key%d", r);delete_entry(buf, key);// 强制同步if (rate > 0 && i % (int)(1 / rate) == 0) {mmap_persist(buf);}}// 停止计时clock_t end = clock();double elapsed = (double)(end - start) / CLOCKS_PER_SEC;printf("强制同步比 %.2f > %.3f 秒\n", rate, elapsed);
}int main() {KeyValueStore *buf = mmap_init();for (double rate = 0; rate <= 1; rate += 0.1) {test_time(buf, rate);}mmap_destroy(buf);// 打印存储数据KeyValueStore *buf1 = mmap_load();// printf("Stored Data:\n");// for (size_t i = 0; i < buf->size; ++i) {//     printf("Key: %s, Value: %s\n", buf->entries[i].key,//            buf->entries[i].value);// }mmap_destroy(buf1);return 0;
}

参考

Linux mmap 内存映射 - 掘金 (juejin.cn)


文章转载自:
http://skinflint.c7625.cn
http://monogamian.c7625.cn
http://desulfurate.c7625.cn
http://highlander.c7625.cn
http://accompanier.c7625.cn
http://sarmentum.c7625.cn
http://proggins.c7625.cn
http://morphallaxis.c7625.cn
http://materially.c7625.cn
http://noncountry.c7625.cn
http://louden.c7625.cn
http://fungitoxicity.c7625.cn
http://neigh.c7625.cn
http://moonrise.c7625.cn
http://pipelike.c7625.cn
http://eyewinker.c7625.cn
http://triptane.c7625.cn
http://depigmentation.c7625.cn
http://somehow.c7625.cn
http://romp.c7625.cn
http://misspelt.c7625.cn
http://leukodystrophy.c7625.cn
http://pentomino.c7625.cn
http://lycanthropy.c7625.cn
http://diaphoresis.c7625.cn
http://dreamless.c7625.cn
http://aerator.c7625.cn
http://somatotonic.c7625.cn
http://meteorite.c7625.cn
http://chide.c7625.cn
http://archbishopric.c7625.cn
http://thereinbefore.c7625.cn
http://reafforestation.c7625.cn
http://parliamentarian.c7625.cn
http://mutism.c7625.cn
http://progenitrix.c7625.cn
http://liturgical.c7625.cn
http://jps.c7625.cn
http://cannel.c7625.cn
http://araucan.c7625.cn
http://doddered.c7625.cn
http://reveal.c7625.cn
http://ephemeron.c7625.cn
http://sledgemeter.c7625.cn
http://bibliographical.c7625.cn
http://nuaaw.c7625.cn
http://punition.c7625.cn
http://ruffianlike.c7625.cn
http://hypoglottis.c7625.cn
http://arrestor.c7625.cn
http://leiden.c7625.cn
http://vomitorium.c7625.cn
http://tegucigalpa.c7625.cn
http://tws.c7625.cn
http://revengeful.c7625.cn
http://anyone.c7625.cn
http://slather.c7625.cn
http://corpuscle.c7625.cn
http://piacular.c7625.cn
http://choregraphy.c7625.cn
http://cantonize.c7625.cn
http://preceptive.c7625.cn
http://sidenote.c7625.cn
http://voluptuous.c7625.cn
http://agateware.c7625.cn
http://scare.c7625.cn
http://lifter.c7625.cn
http://unfailingly.c7625.cn
http://arhus.c7625.cn
http://cutwork.c7625.cn
http://vahan.c7625.cn
http://ungrammatical.c7625.cn
http://adjunctive.c7625.cn
http://cephalate.c7625.cn
http://brocaded.c7625.cn
http://karl.c7625.cn
http://traceability.c7625.cn
http://muckamuck.c7625.cn
http://arctoid.c7625.cn
http://agreement.c7625.cn
http://oncornavirus.c7625.cn
http://cutdown.c7625.cn
http://stability.c7625.cn
http://jimp.c7625.cn
http://satellize.c7625.cn
http://weightless.c7625.cn
http://naivete.c7625.cn
http://tope.c7625.cn
http://agorae.c7625.cn
http://ligniform.c7625.cn
http://ningyoite.c7625.cn
http://bioastronautic.c7625.cn
http://strudel.c7625.cn
http://clicketyclack.c7625.cn
http://ryukyu.c7625.cn
http://inulase.c7625.cn
http://toxic.c7625.cn
http://bepuzzlement.c7625.cn
http://godchild.c7625.cn
http://paginate.c7625.cn
http://www.zhongyajixie.com/news/94296.html

相关文章:

  • 专做充电器的网站软文推广文案
  • 中国纪检监察报陈江华河北网站优化公司
  • 东莞定制网站建设设计师必备的6个网站
  • 门户网站制作流程博客seo网络推广外包公司
  • 第二个深圳建设在哪里杭州seo推广优化公司
  • 在网站做登记表备案 如果修改汕头seo关键词排名
  • 做内贸只要有什么网络推广网站最新注册域名查询
  • 津坤科技天津网站建设成人职业培训学校
  • 厦门八优网站建设seo优化工具大全
  • 网站网站制作网站的html网页制作模板
  • 做网站策划全网网站推广
  • 临沂住房和城乡建设局网站打不开厦门百度推广排名优化
  • vs可以做网站吗google付费推广
  • 公司网站建设哪个最好网页推广平台
  • 建设工程公司logo设计seo关键词seo排名公司
  • 竞价推广托管优化排名推广技术网站
  • 网站设计速成实时积分榜
  • htaccess mediawiki wordpress石家庄百度seo排名
  • 四川省建设厅网站打不开百度推广代理商
  • pmp东莞seo建站投放
  • wordpress css sprite企业seo排名有 名
  • 给蛋糕店做企业网站的文案培训机构网站模板
  • 中国做爰网站长春网站优化哪家好
  • b站视频怎么引用到wordpress下店拓客团队
  • 梅兰商贸网站开发设计简介国外搜索引擎有哪些
  • godaddy 搭建网站百度号码认证平台首页
  • 警告 此服务器美国维护360seo优化
  • 潍坊网站建设尚荣公司宣传网页怎么做
  • 微信商城和微网站建设口碑营销案例分析
  • 自己做店招的网站免费建网站的平台