最全的Linux教程,Linux从入门到精通
======================
-
linux从入门到精通(第2版)
-
Linux系统移植
-
Linux驱动开发入门与实战
-
LINUX 系统移植 第2版
-
Linux开源网络全栈详解 从DPDK到OpenFlow
第一份《Linux从入门到精通》466页
====================
内容简介
====
本书是获得了很多读者好评的Linux经典畅销书**《Linux从入门到精通》的第2版**。本书第1版出版后曾经多次印刷,并被51CTO读书频道评为“最受读者喜爱的原创IT技术图书奖”。本书第﹖版以最新的Ubuntu 12.04为版本,循序渐进地向读者介绍了Linux 的基础应用、系统管理、网络应用、娱乐和办公、程序开发、服务器配置、系统安全等。本书附带1张光盘,内容为本书配套多媒体教学视频。另外,本书还为读者提供了大量的Linux学习资料和Ubuntu安装镜像文件,供读者免费下载。
本书适合广大Linux初中级用户、开源软件爱好者和大专院校的学生阅读,同时也非常适合准备从事Linux平台开发的各类人员。
需要《Linux入门到精通》、《linux系统移植》、《Linux驱动开发入门实战》、《Linux开源网络全栈》电子书籍及教程的工程师朋友们劳烦您转发+评论
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
自己测试运行结果:
root@chenwr-pc:/home/workspace/test# gcc tmp.c -o run
root@chenwr-pc:/home/workspace/test# strace ./run
execve(“./run”, [“./run”], [/* 22 vars */]) = 0
brk(0) = 0x1ffa000
access(“/etc/ld.so.nohwcap”, F_OK) = -1 ENOENT (No such file or directory)
access(“/etc/ld.so.preload”, R_OK) = -1 ENOENT (No such file or directory)
open(“/etc/ld.so.cache”, O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=106932, …}) = 0
mmap(NULL, 106932, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fcab05de000
close(3) = 0
access(“/etc/ld.so.nohwcap”, F_OK) = -1 ENOENT (No such file or directory)
open(“/lib/x86_64-linux-gnu/libc.so.6”, O_RDONLY|O_CLOEXEC) = 3
read(3, “\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P \2\0\0\0\0\0”…, 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1857312, …}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcab05dd000
mmap(NULL, 3965632, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fcab000e000
mprotect(0x7fcab01cc000, 2097152, PROT_NONE) = 0
mmap(0x7fcab03cc000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1be000) = 0x7fcab03cc000
mmap(0x7fcab03d2000, 17088, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fcab03d2000
close(3) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcab05db000
arch_prctl(ARCH_SET_FS, 0x7fcab05db740) = 0
mprotect(0x7fcab03cc000, 16384, PROT_READ) = 0
mprotect(0x600000, 4096, PROT_READ) = 0
mprotect(0x7fcab05f9000, 4096, PROT_READ) = 0
munmap(0x7fcab05de000, 106932) = 0
open(“/tmp/file_mmap”, O_RDWR|O_CREAT, 0600) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=14, …}) = 0
mmap(NULL, 14, PROT_READ|PROT_WRITE, MAP_SHARED, 3, 0) = 0x7fcab05f8000
close(3) = 0
munmap(0x7fcab05f8000, 14) = 0
exit_group(0) = ?
+++ exited with 0 +++
该文件已经变成大写。
root@chenwr-pc:/tmp# cat file_mmap
WWW.BAIDU.COM
网上该demo的说明:
open(“/tmp/file_mmap”, O_RDWR|O_CREAT, 0600) = 3 //open,返回fd=3
fstat64(3, {st_mode=S_IFREG|0644, st_size=18, …}) = 0 //fstat, 即文件大小18
mmap2(NULL, 18, PROT_READ|PROT_WRITE, MAP_SHARED, 3, 0) = 0xb7867000 //mmap文件fd=3
close(3) = 0 //close文件fd=3
munmap(0xb7867000, 18)= 0 //munmap,移除0xb7867000这里的内存映射
这里mmap的addr是0(NULL),offset是18,并不是一个内存页的整数倍,即有4078bytes(4kb-18)内存空间被闲置浪费了。
(2)demo2 read的方式
#include <stdio.h>
#include <ctype.h>
#include <sys/mman.h> /mmap munmap/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int fd, len;
char *buf;
char *fname = “/tmp/file_mmap”;
ssize_t ret;
struct stat sb;
fd = open(fname, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR);
if (fd == -1)
{
perror(“open”);
return 1;
}
if (fstat(fd, &sb) == -1)
{
perror(“stat”);
return 1;
}
buf = malloc(sb.st_size);
if (buf == NULL)
{
perror(“malloc”);
return 1;
}
ret = read(fd, buf, sb.st_size);
for (len = 0; len < sb.st_size; ++len)
{
buf[len] = toupper(buf[len]);
/putchar(buf[len]);/
}
lseek(fd, 0, SEEK_SET);
ret = write(fd, buf, sb.st_size);
if (ret == -1)
{
perror(“error”);
return 1;
}
if (close(fd) == -1)
{
perror(“close”);
return 1;
}
free(buf);
return 0;
}
自己测试运行的结果:
root@chenwr-pc:/home/workspace/test# strace ./run
execve(“./run”, [“./run”], [/* 22 vars */]) = 0
brk(0) = 0x13ac000
access(“/etc/ld.so.nohwcap”, F_OK) = -1 ENOENT (No such file or directory)
access(“/etc/ld.so.preload”, R_OK) = -1 ENOENT (No such file or directory)
open(“/etc/ld.so.cache”, O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=106932, …}) = 0
mmap(NULL, 106932, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fb98f1d7000
close(3) = 0
access(“/etc/ld.so.nohwcap”, F_OK) = -1 ENOENT (No such file or directory)
open(“/lib/x86_64-linux-gnu/libc.so.6”, O_RDONLY|O_CLOEXEC) = 3
read(3, “\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P \2\0\0\0\0\0”…, 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=1857312, …}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fb98f1d6000
mmap(NULL, 3965632, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fb98ec07000
mprotect(0x7fb98edc5000, 2097152, PROT_NONE) = 0
mmap(0x7fb98efc5000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1be000) = 0x7fb98efc5000
mmap(0x7fb98efcb000, 17088, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fb98efcb000
close(3) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fb98f1d4000
arch_prctl(ARCH_SET_FS, 0x7fb98f1d4740) = 0
mprotect(0x7fb98efc5000, 16384, PROT_READ) = 0
mprotect(0x600000, 4096, PROT_READ) = 0
mprotect(0x7fb98f1f2000, 4096, PROT_READ) = 0
munmap(0x7fb98f1d7000, 106932) = 0
open(“/tmp/file_mmap”, O_RDWR|O_CREAT, 0600) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=14, …}) = 0
brk(0) = 0x13ac000
brk(0x13cd000) = 0x13cd000
read(3, “www.baidu.com\n”, 14) = 14
lseek(3, 0, SEEK_SET) = 0
write(3, “WWW.BAIDU.COM\n”, 14) = 14
close(3) = 0
exit_group(0) = ?
+++ exited with 0 +++
网上该demo的说明:
open(“/tmp/file_mmap”, O_RDWR|O_CREAT, 0600) = 3 //open, fd=3
fstat64(3, {st_mode=S_IFREG|0644, st_size=18, …}) = 0 //fstat, 其中文件大小18
brk(0) = 0x9845000 //brk, 返回当前中断点
brk(0x9866000) = 0x9866000 //malloc分配内存,堆当前最后地址
read(3, “www.perfgeeks.com\n”, 18)= 18 //read
lseek(3, 0, SEEK_SET) = 0 //lseek
write(3, “WWW.PERFGEEKS.COM\n”, 18) = 18 //write
close(3) = 0
这里通过read()读取文件内容,toupper()后,调用write()写回文件。因为文件太小,体现不出read()/write()的缺点:频繁访问大文件,需要多个lseek()来确定位置。每次编辑read()/write(),在物理内存中的双份数据。 当然,不可以忽略创建与维护mmap()数据结构的成本。需要注意:并没有具体测试mmap vs read/write,即不能一语断言谁孰谁劣,具体应用场景具体评测分析。 你只是要记住:mmap内存映射文件之后,操作内存即是操作文件,可以省去不少系统内核调用(lseek, read, write)。
五、自己写的demo
#include <stdio.h>
#include <ctype.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#define INT64U unsigned long long
#define MSG_ERR 1
#define MSG_WARN 2
#define MSG_INFO 3
#define MSG_DBG 4
#define MSG_NOR 5
#define MSG_HEAD (“libfat->”)
#define PRTMSG(level, fmt, args…)
do {
if (level <= MSG_NOR) {
if (level <= MSG_NOR) {
printf(“%s, %s, line %d: " fmt,FILE,FUNCTION,LINE, ##args);
} else {
printf(”%s:" fmt, MSG_HEAD, ##args);
}
}
} while(0)
typedef unsigned char BOOLEAN;
typedef unsigned char INT8U;
typedef unsigned int INT16U;
typedef unsigned long INT32U;
typedef signed char INT8S;
typedef signed int INT16S;
typedef signed long INT32S;
char *filename = “./lt00001”;
//char *filename = “/mnt/sdisk/video/lt00004”;
char *data = “1111111111
2222222222
3333333333
4444444444”;
INT32S data_len = 40;//单次写入的数据长度
struct timeval t_start, t_end;
struct stat file_info;
long cost_time = 0;
int write_num = 1000;
INT32S mmap_write(INT32S fd, INT64U offset, void *data, INT32S data_len)
{
char *buf = NULL;
if (fstat(fd, &file_info) == -1) {
perror(“fstat”);
PRTMSG(MSG_ERR, “[cwr] Get file info failed\n”);
return -1;
}
buf = mmap(0, file_info.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (buf == MAP_FAILED) {
perror(“mmap”);
PRTMSG(MSG_ERR, “[cwr] mmap failed\n”);
return -1;
}
//offset = (INT64U)((order)*sizeof(FAT_FILE_LIST_T));
memcpy(buf+offset, data, data_len);
if (munmap(buf, file_info.st_size) == -1) {
perror(“munmap”);
PRTMSG(MSG_ERR, “[cwr] munmap failed\n”);
return -1;
}
return data_len;
}
int write_test()
{
int fd, ret, i, data_size;
INT64U ret64, offset;
int ret_len = 0;
time_t starttime, endtime;
fd = open(filename, O_RDWR);
if (fd < 0) {
printf(“[cwr] open file faild\n”);
}
gettimeofday(&t_start, NULL);
for (i=0; i<write_num; i++) {
offset = i*data_len;
ret64 = lseek64(fd, offset, SEEK_SET);
if (ret64 == -1LL) {
printf(“lseek data fail\n”);
return -1;
}
ret_len = write(fd, data, data_len);
if (ret_len != data_len) {
printf(“[cwr] count = %d; write error\n”, i);
close(fd);
return -1;
}
}
gettimeofday(&t_end, NULL);
printf(“[cwr] test end, count = %d\n”, i);
close(fd);
return 0;
}
int mmap_write_test()
{
int fd, ret, i, data_size;
INT64U ret64, offset;
int ret_len = 0;
fd = open(filename, O_RDWR);
if (fd < 0) {
printf(“[cwr] open file faild\n”);
}
gettimeofday(&t_start, NULL);
for (i=0; i<write_num; i++) {
offset = i*data_len;
ret_len = mmap_write(fd, offset, data, data_len);
if (ret_len != data_len) {
printf(“[cwr] count = %d; mmap write error\n”, i);
close(fd);
return -1;
}
}
gettimeofday(&t_end, NULL);
printf(“[cwr] mmap write test end, count = %d\n”, i);
close(fd);
return 0;
}
void main()
{
int ret;
memset(&file_info, 0, sizeof(file_info));
#if 1
ret = write_test();
if (ret != 0) {
printf(“[cwr] write_test failed\n”);
}
#endif
#if 0
ret = mmap_write_test();
if (ret != 0) {
printf(“[cwr] mmap_write_test failed\n”);
}
#endif
cost_time = t_end.tv_usec - t_start.tv_usec;
printf(“Start time: %ld us\n”, t_start.tv_usec);
printf(“End time: %ld us\n”, t_end.tv_usec);
printf(“Cost time: %ld us\n”, cost_time);
while(1) {
sleep(1);
}
}
运行结果:
write的方式获取的时间
使用mmap的方式去操作
后续不映射整个文件的空间大小,而是映射要写入数据的长度40字节
buf = mmap(0, 40, PROT_READ|PROT_WRITE, MAP_SHARED, fd, offset41024)
为何测试mmap效率并没有更高效。
六、参考资料
先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前在阿里
深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Linux运维全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
何学起的朋友。**
[外链图片转存中…(img-sr0GmpCB-1715115062664)]
[外链图片转存中…(img-8wDdiChN-1715115062664)]
[外链图片转存中…(img-gv4Ew27L-1715115062665)]
[外链图片转存中…(img-EI1v3jAj-1715115062665)]
[外链图片转存中…(img-UR7nCEy3-1715115062665)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新