-
字节序与union类型
字节序是什么
字节序是指在数据传输时,高位字节存储在内存中的较高位地址还是较低位地址。字节序分为大端字节序和小端字节序,两种字节序不能混用。
- 大端字节序 :高位字节保存在较低位地址的内存中
- 小端字节序 :高位字节保存在较高位地址的内存中
以
int n = 0x12345678;为例,大端字节序内存中的存储的顺序为0x12 0x34 0x56 0x78,小端字节序内存中的存储顺序为0x78 0x56 0x34 0x12。很明显可以看出大端字节序更符合人类的阅读习惯。- 常见的大端系统CPU:IBM z/Atchitecture
- 常见的小端系统CPU:intel x86
网络字节序使用的大端字节序,常用网络协议如IPv4、IPv6、TCP和UDP协议都是使用大端字节序进行数据传输的。
提供字节序转换功能的函数接口
在网络通信接口中提供了转换主机字节序和网络字节序之间的函数接口,如下所示。
uint32_t htonl(uint32_t hostlong); uint16_t htons(uint16_t hostshort); uint32_t ntohl(uint32_t netlong); uint16_t ntohs(uint16_t netshort);
有些时候,我们需要手动转换内存字节流,可以使用以下接口帮助实现指定字节序的转换。
小端字节序的函数实现如下:
// small endian inline std::uint16_t SE_GetStreamU16(const void* p) { const std::uint8_t* pTemp = static_cast<const std::uint8_t*>(p); return std::uint16_t(pTemp[0] | (pTemp[1] << 8)); } inline std::uint32_t SE_GetStreamU32(const void* p) { const std::uint8_t* pTemp = static_cast<const std::uint8_t*>(p); return std::uint32_t(pTemp[0] | (pTemp[1] << 8) | (pTemp[2] << 16) | (pTemp[3] << 24)); } inline void SE_SetStreamU16(void* p, std::uint16_t n) { std::uint8_t* pTemp = static_cast<std::uint8_t*>(p); pTemp[0] = std::uint8_t(n); pTemp[1] = std::uint8_t(n >> 8); } inline void SE_SetStreamU32(void* p, std::uint32_t n) { std::uint8_t* pTemp = static_cast<std::uint8_t*>(p); pTemp[0] = std::uint8_t(n); pTemp[1] = std::uint8_t(n >> 8); pTemp[2] = std::uint8_t(n >> 16); pTemp[3] = std::uint8_t(n >> 24); }
大端字节序的函数实现如下:
// big endian inline std::uint16_t BE_GetStreamU16(const void* p) { const std::uint8_t* pTemp = static_cast<const std::uint8_t*>(p); return std::uint16_t(pTemp[1] | pTemp[0] << 8); } inline std::uint32_t BE_GetStreamU32(const void* p) { const std::uint8_t* pTemp = static_cast<const std::uint8_t*>(p); return std::uint32_t(pTemp[3] | (pTemp[2] << 8) | (pTemp[1] << 16) | (pTemp[0] << 24)); } inline void BE_SetStreamU16(void* p, std::uint16_t n) { std::uint8_t* pTemp = static_cast<std::uint8_t*>(p); pTemp[0] = std::uint8_t(n >> 8); pTemp[1] = std::uint8_t(n); } inline void BE_SetStreamU32(void* p, std::uint32_t n) { std::uint8_t* pTemp = static_cast<std::uint8_t*>(p); pTemp[0] = std::uint8_t(n >> 24); pTemp[1] = std::uint8_t(n >> 16); pTemp[2] = std::uint8_t(n >> 8); pTemp[3] = std::uint8_t(n); }
查看当前系统的字节序
通过
lscpu命令,能得到cpu的大端和小端信息。lscpu | grep -i endian
通过常用shell命令,在大端系统中会输出0,在小端系统中会输出1。
echo -n I | od -to2 | head -n1 | cut -f2 -d" " | cut -c6
借助python进行判断。
python -c "import sys; print(sys.byteorder)" # or python -c "import sys;sys.exit(0 if sys.byteorder=='big' else 1)"
借助linux的ELF进行判断,查看第6个字节,小端系统中为1,大端系统中为2。
xxd -c 1 -l 6 /bin/bash # or hexdump -n 6 -C /bin/bash
可以使用union类型验证字节序,以下示例代码在不同的字节序下会有不同的输出。
#include <stdio.h> #include <inttypes.h> typedef union un { int32_t x; char ch[4]; } un; int main() { un u; u.ch[0] = 1; u.ch[1] = 2; u.ch[2] = 3; u.ch[3] = 4; printf("u.x=0x%x\n", u.x); return 0; }
- 大端字节序 :高位字节保存在较低位地址的内存中
-
在Python日志中输出文件名和函数名
Python内置了日志模块,在默认情况下输出的日志是不带文件名和函数名的,这样在排查问题时,遇到相似的日志就变得容易混淆,可以通过设置将输出的日志中带有文件名和函数名。参考了stackoverflow的回答,详细代码如下。
import logging log = logging.getLogger('root') LOG_FORMAT = "%(filename)s:%(lineno)s %(funcName)s() %(message)s" logging.basicConfig(format=LOG_FORMAT) log.setLevel(logging.DEBUG)
参考资料
stackoverflow -
Linux常用命令行指令 - xargs
基础用法
xargs指令默认情况下从stdin中读取信息,按照空格分或换行区分开,并且执行输入的命令(默认的命令是/bin/echo)。命令可以通过xargs的参数指定执行一次或多次,命令的执行参数为自身初始参数追加上由stdin读到的内容,命令在执行时会忽略空行。xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times with any initial-arguments followed by items read from standard input. Blank lines on the standard input are ignored.
在命令行输入
xargs命令,输入 “a b c d” 后按回车,再按ctrl+d完成本次输入,则xargs的默认echo命令会将接收到的字符串打印出来,效果如下所示。# xargs a b c d a b c d
xargs单独使用时功能比较单一,但是它与其他指令一起使用十分强大。xargs与find组合使用常见的使用方法是使用
find指令找到指定的文件,组合以xargs指令实现对指定文件的操作。find . -name "demo_" | xargs file -i
由于
xargs是使用空格作为各个参数的分隔符,如果遇到的文件名中含有空格,则并不能按照预期处理。这时,可以使用find指令的-print0将含有空格的文件名转化为字符串,再结合xargs的-0参数就能完成文件名中含有空格的处理,改进后的命令如下。find . -name "demo_" -print0 | xargs -0 file -i
xargs与grep组合使用常用命令如下,能够实现对指定规则文件的搜索匹配。
find . -name "*.log" | xargs grep error
使用
-n参数让xargs的命令重复执行如果想用
find指令完成多个匹配规则的搜索,则可以使用-n参数将xargs的内容分割成多个。
下面的命令会执行3次find命令,xargs会将接收的到的内容按照单词数为“1”分割,分3次传送给find指令。echo "*.c" "*.cpp" "*.h" | xargs -n 1 find . -name
使用
-t参数让xargs打印将要执行的命令让
xargs将指令的命令打印出来主要是方便查看命令执行情况,也方便在出现错误时进行调试。find . -name *.log | xargs -t rm -f
使用
-p参数让xargs在执行命令前询问用户增加
-p参数后,xargs在每次执行命令前会将指令打印出来并询问用户,只有用户输入 “y” 时才会真正执行该条命令。echo "*.c" "*.cpp" "*.h" | xargs -n 1 -p find . -name
使用
-I参数让xargs进行参数替换xargs默认会将读取到的内容追加在指令命令后面作为参数,使用-I参数能够控制命令参数的组合方式。
以下命令会将当前目录中的可执行文件移动到bin文件夹下。find . -executable -type f -print0 | xargs -0 -I {} mv {} ./bin -
Linux常用命令行指令 - ipcs
IPC是inter process communication的缩写,这项技术能够让进程间相互通信。
Q:每个进程都有自己的地址空间和独立的用户空间,那么进程间是如何通信的呢?
A:内核,也就是操作系统的心脏,它能够访问整个操作系统的内存。我们可以要求内核分配一块用于进程间交互的空间。几种进程间通信的方法
进程间通信的方法有很多,有些支持同机器上进程的信息交互,有些支持跨机器的进程交互。
-
管道 : pipes,管道提供了进程间交换信息的方法。
-
共享内存 : shared memory,一个进程创建一块其他进程能够访问的内存空间,多个进程可以通过共享内存进行数据交换。
-
消息队列 : message queue,消息队列是一个固定结构、有序的内存段,多个进程可以存放和取回数据。
-
信号量 : semaphores,信号量提供了多进程访问同一资源的同步机制,信号量不负责传递数据,它协调对共享资源的访问。
常用ipcs指令
列出所有的IPC设备
ipcs -a
列出所有的消息队列
ipcs -q
列出所有的信号量
ipcs -s
列出所有的共享内存
ipcs -m
获取与IPC设备信息
ipcs -q -i msq_id
列出IPC设备的限制
ipcs -l
列出IPC设备的创建者和拥有者
ipcs -m -c
列出最近使用IPC设备的进程id
ipcs -m -p
列出IPC设备的最后访问时间
ipcs -s -t
列出IPC设备的当前使用状态
ipcs -u
-
管道 : pipes,管道提供了进程间交换信息的方法。
-
使用gdb调试多线程程序
查看当前线程信息
将进程中的各个线程信息显示出来
(gdb) info threads
切换到指定进程
(gdb) thread tid
向指定的线程发送自定的指令
(gdb) thread apply tid/all args
常用的指定是查看所有线程的调用堆栈
thread apply all bt,这个指令与pstack命令有些相似。gdb默认会自动捕捉新产生线程
会在产生一个新的线程时会显示出LWP的字样提示用户,LWP = light weight process
可以设置gdb是否提示线程相关的事件(gdb) set print thread-events on/off (gdb) show print thread-events
为指定的线程设置断点
含有多线程的程序,可以为单独的线程设置断点
(gdb) break linespec thread tid
任何时候当你的程序在GDB模式下停止的时候,包括当前调试线程的所有线程都会停下来,不会对继续对当前进程造成更改。这时你可以在线程间进行切换,查看整个进程的执行状况。
Whenever your program stops under GDB for any reason, all threads of execution stop, not just the current thread. This allows you to examine the overall state of the program, including switching between threads, without worrying that things may change underfoot.
防止gdb自动切换线程
在调试gdb程序时,在单步执行时,会出现线程间跳转切换,这样对跟踪代码执行状态十分不方便。
可以通过设置scheduler-locking让gdb在所调试的线程中运行,防止线程的自动切换。(gdb) set scheduler-locking step
可以执行以下命令查看当前
scheduler-locking的设置(gdb) show scheduler-locking
scheduler-locking有三种模式-
off任何线程在任何时候都能执行
-
on只有当前线程能够执行
-
step为单步执行优化的模式,比较适合一般的调试
Set the scheduler locking mode. If it is off, then there is no locking and any thread may run at any time. If on, then only the current thread may run when the inferior is resumed. The step mode optimizes for single-stepping. It stops other threads from "seizing the prompt" by preempting the current thread while you are stepping. Other threads will only rarely (or never) get a chance to run when you step. They are more likely to run when you `next' over a function call, and they are completely free to run when you use commands like `continue', `until', or `finish'. However, unless another thread hits a breakpoint during its timeslice, they will never steal the GDB prompt away from the thread that you are debugging.
-
-
在gdb调试时忽略系统信号(signal)
在gdb调试程序时,默认情况下gdb在收到信号时会中断程序的运行,并将收到的信号显示出来。这时,可以选择输入
c(continue)让程序继续运行。如果程序会重复收到这信号,会非常影响调试效率。可以通过配置忽略指定的系统信号。查看当前系统信号的处理信息的指令如下。
(gdb) info signal
以调试网络程序为例,进程会经常收到
SIGPIPE消息,对于网络进程的SIGPIPE消息在程序中会由自身处理,可以使用以下指令让gdb不再提示SIGPIPE信号。(gdb) handle SIGPIPE nostop noprint
其中,
nostop表示在收到信号时不再中断程序的运行,noprint表示在收到信号时不再将收到的信号打印到gdb调试界面,这两个参数可以分开使用。参考资料: gnu gdb manual
(全文完)
-
C++11新特性:基于范围的for循环
在C++11中,介绍了一种新的for循环写法,基于范围的for循环。
与旧的for循环类似,也是用来对遍历容器中的所有元素,新的写法更加简洁方便。可以使用auto自动适配容器中的内容。为了遍历容器时提高效率,在访问容器内部时可以声明为元素的引用,进而避免不必要的对象拷贝。示例代码如下(代码节选自 cppreferance)std::vector<int> v = {0, 1, 2, 3, 4, 5}; for (const int& i : v) // access by const reference { std::cout << i << ' '; } std::cout << '\n'; for (auto i : v) // access by value, the type of i is int { std::cout << i << ' '; } std::cout << '\n'; for (int n : {0, 1, 2, 3, 4, 5}) // the initializer may be a braced-init-list { std::cout << n << ' '; } std::cout << '\n'; int a[] = {0, 1, 2, 3, 4, 5}; for (int n : a) // the initializer may be an array { std::cout << n << ' '; } std::cout << '\n'; for (int n : a) { std::cout << 1 << ' '; // the loop variable need not be used } std::cout << '\n';
注意
std::map的范围for循环遍历的写法std::map中存放的是key-value pair键值对,所以在遍历的时候每次拿到的是一个pair,在访问其中的元素时,需要注意书写方法。示例代码如下。std::map<int, std::string> mapDemo; for (auto& kvp : mapDemo) { std::cout << kvp.first; std::cout << " - "; std::cout << kvp.second; std::cout << std::endl; }
-
Linux常用命令行指令 - scp
scp命令代表的是 secure copy,与cp命令的本地拷贝十分相似,主要区别就在于scp的路径可以是一个远程机器的路径。使用scp进行文件拷贝时,文件的传输时加密的。cp source_path dest_path
从远程机器向本地拷贝
以下命令会把
host主机上的src_path文件拷贝到本地dest_path目录中。scp user@host:src_path dest_path
从本地向远程机器拷贝
以下命令会把本地
src_path的文件拷贝打host主机上的dest_path路径上。scp src_path user@host:dest_path
拷贝目录下的所有文件
可以使用
-r参数完成目录的递归拷贝,r代表recursive递归。scp -r user@host:/src_path dest_path
在拷贝时限制带宽
可以使用
-l参数限制传输的带宽,l代表limit限制,限制带宽的单位是kbps。
命令会在将拷贝传输的速度限制为 100kbps。scp -l 100 src_path dest_path
-
C++11新特性:自动类型推导
关键字
auto用于自动类型推导auto x=0; // int auto c='a'; // char auto d=0.5; // double
自动类型推导主要用于声明比较复杂的变量,或者变量是在泛型编程(模板)中自动生成出来的。
vector<int> vecNumbers; vector<int>::const_iterator it = vecNumbers.begin(); auto it = vecNumbers.begin();
auto类型推导并不会指定const和volatile属性如果需要限定声明的变量为
const和volatile属性,需要额外提供相应的关键字限定。因为编译器并不能推断出声明的变量是否具有常值属性和易失属性。volatile auto num=5; // volatile int auto volatile const var=true; //const valatile bool auto const ch='a'; // const char
关键字
decltype用于获取一个对量的类型使用
decltype存储一个对象或表达式的类型,decltype可以看做是auto的补充。vector<int> vecNumbers; typedef decltype(vecNumbers.begin()) Iterator; Iterator it2;
函数返回值类型推导
在C++11中可以使用
auto和decltype完成函数返回类型的推导。
旧的函数声明格式return_type func_name(param_type param, ...);
新的函数声明格式示例如下,以下函数接收bool作为函数的参数,并返回bool类型的结果。auto func_name(bool bParam)->decltype(bParam) { return bParam; }
函数返回值类型推导在模板泛型编程中非常有用,模板函数能够定义一个通用的返回类型,返回值类型会根据
decltype自动推导。
新的函数声明格式更易于维护,因为返回值并没有作为硬编码写入源文件中,但这样做可能会牺牲代码的可读性。template <class T> auto get_end(vector<T>& v) -> decltype(v.end()) { return v.end(); } vector<int> vecNumbers; auto it = get_end(vecNumbers); // returns iterator const vector<int> vecNumbers2; auto it2 = get_enm(vecNumbers2); // returns const_iterator
-
Linux下的FIFO、pipe、unix domain socket漫谈
在做Linux开发时,经常会接触
管道、AF_UNIX等相关词汇,为了弄清他们之间的关系,查阅了一些资料,将结果整理并记录下来。 -
Linux下使用gdb的调试技巧(2)
本文重点介绍在gdb模式下的常用命令。
-
使用命令行修改Linux系统的时区
系统时区目录 系统支持时区的配置文件存放在
/usr/share/zoneinfo/目录下,可以进入到该目录下查看和搜索目标时区的相关配置文件。时区配置文件 Linux系统的当前时区配置文件存放在
/etc/localtime, 可以将时区配置文件拷贝到该位置下,也可以建立一个软连接指向系统时区目录中的配置文件。操作示例 以将系统的时区改为中国时间为例,以下操作先将旧的时区配置文件删掉,然后建立新的时区配置文件的符号链接。
rm /etc/localtime ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
完成以上操作后,可以执行
date命令查看修改效果。