mmap相关API

概述 mmap()调用进程的虚拟进程空间中一段新的内存映射。 1 2 3 #include <sys/mman.h> void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset); mmap(2) — Linux manual page 用途 变更的可见性 映射类型 文件 匿名 私有 根据文件内容初始化内存 内存分配 共享 内存映射I/O,进程间共享内存(IPC) 进程间共享内存(IPC) 文件映射 创建步骤 1 2 3 4 5 6 //1. 打开使用的文件 fd = open(argv[1], O_RDONLY); //2.获取文件信息,文件大小 fstat(fd, &sb); //3. 生成内存映射 addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); 相关议题 文件权限与内存保护位prot 1 2 3 fd = open(argv[1], O_RDONLY); ... addr = mmap(NULL, sb.st_size, PROT_WRITE, MAP_SHARED, fd, 0); mmap抛出 errno=13 Permission denied错误 边界情况 增加一个异常捕获的处理 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 void signal_handler(int no){ switch(no){ case 11: printf("get SIGSEGV\n"); break; case 7: printf("get SIGBUS\n"); break; default: printf("get %d\n", no); break; } exit(-1); } 1 2 # ls -l dax.file -rw-r--r--. 1 root root 7 Jul 18 17:51 dax.file SIGSEGV 访问超过mmap映射范围 ...

2020-07-22 · 5 min · 893 words · Garlic Space

centos7 升级 glibc && gcc

环境 cenos7(X86_64) 为了验证 mmap()一些功能需要升级glibc版本, 顺便把gcc一起升级一下 升级gcc 预安装包 安装过程中需要makeinfo, 先安装下texi2html, texinfo 1 yum install texi2html texinfo 安装gcc 1 2 3 4 5 6 7 8 wget https://ftp.gnu.org/gnu/gcc/gcc-10.1.0/gcc-10.1.0.tar.gz tar zxvf gcc-10.1.0.tar.gz cd gcc-10.1.0/ mkdir build cd build /configure --enable-languages=c,c++ --disable-multilib make && make install 验证 1 2 3 4 5 6 7 8 9 # gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=... Target: x86_64-pc-linux-gnu Configured with: ../configure --enable-languages=c,c++ --disable-multilib Thread model: posix Supported LTO compression algorithms: zlib gcc version 10.1.0 (GCC) 升级glibc 预安装包 我的机器上是要求安装最新的make, 网上有需要升级ld, 的下载安装binutils就可以了 1 2 3 4 5 6 7 8 wget https://ftp.gnu.org/pub/gnu/make/make-4.3.tar.gz tar zxvf make-4.3.tar.gz cd make-4.3 mkdir build ./configure make && make install cp /usr/bin/make make.backup ln /usr/local/bin/make /usr/bin/make 安装glibc 1 2 3 4 5 6 7 8 wget http://ftp.gnu.org/gnu/glibc/glibc-2.31.tar.gz tar -xvf glibc-2.31.tar.gz mkdir glibc-2.31/build cd glibc-2.31/build ../configure --prefix=/usr --with-headers=/usr/include --with-binutils=/usr/bin make make install 问题 make install 报错 1 2 3 4 5 6 7 /usr/bin/perl scripts/test-installation.pl /tmp/glibc-2.31/build/ /usr/bin/ld: cannot find -lnss_test2 ... LD_SO=ld-linux-x86-64.so.2 CC="gcc -B/usr/bin/" /usr/bin/perl scripts/test-installation.pl /tmp/glibc-2.31/build/ /usr/bin/ld: /lib/../lib64/libnss_nis.so: undefined reference to '_nsl_default_nss@GLIBC_PRIVATE' 可以从上面脚本信息看到是 scripts/test-installation.pl 有报错, 进去看一下, 主目录下的 Makefile ...

2020-07-18 · 3 min · 527 words · Garlic Space

cenos7+openssh 修改SSH服务端口

环境: cenos7(X86_64) + openssh ssh端口配置 修改端口 例如8888 文件 /etc/ssh/sshd_config 1 2 #Port 22 Port 8888 重启服务 1 2 systemctl restart sshd systemctl status sshd 防火墙配置: 我这里配置比较简单仅zone配置了一个public 1 firewall-cmd --permanent --zone=public --add-port=8888/tcp 重启服务 1 2 systemctl restart firewalld systemctl status firewalld

2020-07-13 · 1 min · 40 words · Garlic Space

LeetCode – Path Sum Solution

题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. Note: A leaf is a node with no children. Example: Given the below binary tree and sum = 22, 1 2 3 4 5 6 7 5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1 return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22. ...

2020-07-12 · 2 min · 250 words · Garlic Space

LeetCode – Symmetric Tree Solution

题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 2 3 4 5 1 / \ 2 2 / \ / \ 3 4 4 3 But the following [1,2,2,null,3,null,3] is not: 1 2 3 4 5 1 / \ 2 2 \ \ 3 3 Follow up: Solve it both recursively and iteratively. ...

2020-07-12 · 2 min · 279 words · Garlic Space

Linux slob/slab/slub

概述 Linux初始化通过bootmem/memblock引导内存分配进行内存管理,支持buddy system完成相关初始化后,将物理内存分配的功能转交给buddy system,buddy system是以page为单位分配方式。 对于内核要经常创建的对象, 如task_struct,fs_struct, mm_struct 通常会放到高速缓存中,保留基本结构,从而可以重复使用他们, 这里需要用到slab分配器。 slab分配器 通过 buddy system 申请空闲页 将申请到页处理为更小的分配单元,为其他子系统提供缓冲区存放内核对象 缓存经常使用的对象,释放后保存器初始状态,再次分配对象速度会很快 充分利用硬件高速缓存 历史发展 1991 Initial K&R allocator 1996 SLAB allocator 2003 SLOB allocator 2004 NUMA SLAB 2007 SLUB allocator 2008 SLOB mulitlist 2011 SLUB fastpath rework 2013 Common slab code 2014 SLUBification of SLAB … 常用的分配器 SLOB: K&R allocator (1991-1999) SLAB: Solaris type allocator (1999-2008) SLUB: Unqueued allocator (2008-today SLOB 简单的空闲对象列表管理 遍历列表查找合适的空间,没有的话申请向伙伴系统申请page增加堆栈大小. 碎片化严重 优化: 按照不同大小多个链表,减少碎片。 原理图 ...

2020-07-11 · 8 min · 1562 words · Garlic Space

为什么x86无法生存-Why x86 won’t survive

前不久, 评估CEO 库克在其全球开发者大宣布,Mac笔记本以及个人电脑将会改用苹果自家的ARM架构处理器。这将是自2006年从PowerPC处理器改用英特尔x86处理器后,又一次CPU架构的调整。下面这篇文章是Medium推送的,不过文章贬低X86中提到的漏洞, 我查了资料其实部分ARM也是存在的。 不过确实是代表了一类观点。 苹果的去INTEL,让我想起了去IOE. 原文链接 Why x86 won’t survive X86是Intel公司1978年发明的一种微处理架构,他应用于大多数笔记本。他被认为是高效的, 可靠的, 直到前一段时间, 2018年发现的一系列漏洞 x86是由Intel创建的微体系结构(是一套开发处理器的指令集架构),最早在1978年面市(Intel 8086)。它为大多数笔记本电脑所采用,并且可能为您现在使用的笔记本电脑使用的就是这一系列的CPU。 直到几个月前,它才被认为是强大,有效和可靠的。 仅在2018年发现的x86体系结构中存在的漏洞和漏洞利用的简短历史记录: Meltdown, Spectre, SMT/Hyper threading found to be a security threat. 好像不是很多? 实际不是这样的。这3个漏洞是最近历史上发现的最主要的漏洞,几乎影响了所有笔记本电脑,台式机和服务器内核。 虽然已针对前两个实施了修复程序,但主要缺点是性能命中率高达15%。 在修复Spectre和Meltdown后不久,SMT/Hyper 已显示出使Intel和AMD CPU上的预测执行缺陷变得更糟。 具有古老而混乱的指令集的x86开始显示它的年龄,并且无法做它曾经可以做的事情。 什么是Speculative Execution?为什么要有它? 这就是ARM的用武之地。ARM出现于1990年(wiki上显示是1983年艾康电脑公司开始设计),是通常用于移动电话的台式机处理器的更新且更轻量的替代品。 如今,大多数智能手机都使用基于ARM的处理器,例如高通的Snapdragon系列,麒麟和苹果的定制7nm A系列处理器。 它们重量轻,功能强大且效率极高。 这些处理器比几年前的某些高端游戏台式机更好,但是它们有一个主要缺点-这就是为什么它们从未在笔记本电脑中使用过的原因。 应用程序兼容性。 几乎每个应用程序都是专门为x86开发的,没有考虑ARM的余地。 但是,这种情况开始有所改变。 谷歌正在帮助高通和微软将其浏览器引入ARM架构的Windows设备。 这是一个缓慢的开始,但这标志着x86结束的开始。 Google 已经看到ARM的发展空间, 苹果和微软也如此,如果他们开始致力于做好ARM兼容性, 那么大多数开发者将效仿并为此架构发布自己的软件, 随着兼容性的增加, 由于速度, 可靠性,安全性和价格原因,更多的用户切换到ARM, 由于X86的明显的漏洞, 更多的人将选择ARM. 当然,这种情况不会在一到两年内发生,但最终会发生。 这将是x86的终结,如果没有它,我们可能会更好。

2020-06-29 · 1 min · 59 words · Garlic Space

通过alternatives进行多版本间切换

上周在学习单点登录安装CAS Overlay Template要使用指定版本,当时机器上安装了多个版本的jdk使用alternatives命令进行了切换. 如果需要在多个版本应用间切换并进行管理可以使用这个命令。 alternative 前身是 Debian Linux的一个用 Perl实现的工具 update-alternatives 后续Red Hat重写了并重新命名使用在 Red Hatand CentOS版本中. alternative统一了有多个版本应用,但在UNIX中更认可通过环境变量来设置 通常定义在 /etc/profile 或 $HOME/.profile 下面用一个例子演示一下 如果有有一个应用 em ,他的新版本 nem, 由于习惯原因, 我们更习惯敲em, 可以通过下面步骤设置一下 创建 先创建两个脚本做为代表em nem应用 1 2 3 4 5 6 [root@centosgpt alternatives]# cat em #!/bin/bash echo " This is em " [root@centosgpt alternatives]# cat nem #!/bin/bash echo " This is nem " 生成一个alternative需要下面四个要素: alternatives --install <link> <name> <path> <priority> link : 统一应用名称,一个链接文件 name : alternative 的名称便于记忆 path : 实际版本的路径 priority :优先级 1 2 3 4 5 6 7 8 9 10 [root@centosgpt alternatives]# sudo alternatives --install /usr/bin/em uemacs /root/alternatives/em 1 [root@centosgpt alternatives]# sudo alternatives --install /usr/bin/em uemacs /root/alternatives/nem 99 [root@centosgpt alternatives]# alternatives --config uemacs There are 2 programs which provide 'uemacs'. Selection Command ----------------------------------------------- + 1 /root/alternatives/em * 2 /root/alternatives/nem 验证 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [root@centosgpt ~]# em This is nem [root@centosgpt ~]# alternatives --config uemacs There are 2 programs which provide &#039;uemacs&#039;. Selection Command ----------------------------------------------- 1 /root/alternatives/em *+ 2 /root/alternatives/nem Enter to keep the current selection[+], or type selection number: 1 [root@centosgpt ~]# em This is em 移除 alternatives --remove <name> <path> ...

2020-06-28 · 2 min · 393 words · Garlic Space

LeetCode – Maximum Depth of Binary Tree

题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Note: A leaf is a node with no children. Example: Given binary tree [3,9,20,null,null,15,7], 1 2 3 4 5 3 / \ 9 20 / \ 15 7 return its depth = 3. 解题: 深度优先搜索方式,分别找到左右子树最大的深度+1即可;广度优先搜索,逐层遍历找到最大层数返回。 ...

2020-06-23 · 2 min · 332 words · Garlic Space

LeetCode – Minimum Depth of Binary Tree

题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. Note: A leaf is a node with no children. Example: Given binary tree [3,9,20,null,null,15,7], 1 2 3 4 5 3 / \ 9 20 / \ 15 7 return its minimum depth = 2. ...

2020-06-23 · 2 min · 256 words · Garlic Space