分类 Linux 中的文章

c/c++ max/min 4种实现方法

1.简单的宏实现 1 2 #define max(a,b) (((a) > (b)) ? (a) : (b)) #define min(a,b) (((a) < (b)) ? (a) : (b)) 2.内核的宏实现 1 2 3 4 5 6 7 8 9 10 11 12 13 14 /* safe min & max */ /* avoid ++/-- expand twice */ /* compile warning if type diff */ #define min(x, y) ({ \ typeof(x) _min1 = (x); \ typeof(y) _min2 = (y); \ (void) (&_min1 == &_min2); \ _min1 < _min2 ? _min1 : _min2; }) #define max(x, y) ({ \ typeof(x) _max1 = (x); \ typeof(y) _max2 = (y); \ (void) (&_max1 == &_max2); \ _max1 > _max2 ? _max1 : _max2; }) Linux 内核的实现是安全的,避免了 ++/– 计算多次,而且会在编……

阅读全文

线程创建 pthread_create 中自定义参数注意事项

1.函数原型 1 2 int **pthread_create**(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); 本文主要讨论最后一个参数,同时传递多个的问题 (如果只传递一个 int char 等长度小于指针的数据类型,可以直接传,然后在线程内把 (void *) 强制转换) 2.错误示例 是在一本书上看到的,也是写本文的初衷 错误原因: fds_for_new_worker 是局部变量,线程创建异步的,pthread_create 后, else if 也结束……

阅读全文

Linux fork 后 wait 获取子进程结束的状态示例

概览 使用 fork 后,可能需要获取 fork 的进程的运行状况,比如有没有异常、崩溃。 wait 在 man 中关键的描述如下: All of these system calls are used to wait for state changes in a child of the calling process, and obtain information about the child whose state has changed. A state change is considered to be: the child terminated; the child was stopped by a signal; or the child was resumed by a signal. 示例代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46……

阅读全文

Linux 最简单内核模块 Hello World 示例

注: 如果想要按照本篇实践,需要有能运行的arm开发板和对应版本的内核(如果想在Linux主机上编译运行,请参考附1) 1.在相应版本内核的driver目录下新建如下文件 其中文件代码如下: /* hello.c */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <linux/init.h>#include <linux/module.h> static int hello_init(void) { printk(KERN_INFO "[init] Can you feel me?\n"); return 0; } static void hello_exit(void) { printk(KERN_INFO "[exit] Yes.\n"); } module_init(hello_init); module_exit(hello_exit); MODULE_AUTHOR("Alan Wang <alan@wrcode.com>"); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("A simple Hello……

阅读全文

C语言结构体初始化的三种方法

示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include <stdio.h> struct student_st { char c; int score; const char *name; }; static void show_student(struct student_st *stu) { printf("c = %c, score = %d, name = %s\n", stu->c, stu->score, stu->name); } int main(void) { // method 1: 按照成员声明的顺序初始化 struct student_st s1 = {'A', 91, "Alan"}; show_student(&s1); // method 2: 指定初始化,成员顺序可以不定,Linux 内核多采用此方式 struct student_st s2 = { .name = "YunYun", .c = 'B', .score = 92, }; show_student(&s2); // method 3:……

阅读全文

Linux C ftruncate 函数清空文件注意事项(要使用 lseek 重置偏移量)

概览 之前有个要把打开的文件清空,然后重新写入的需求,但是使用 ftruncate(fd, 0)后,并没有达到效果,反而文件头部有了'\0',长度比预想的大了。 究其原因是没有使用 lseek 重置文件偏移量,是我太天真了,以为清空文件就会从头开始写入。 文档 首先 man ftruncate 看下帮助手册 1 2 3 4 5 6 7 8 9 10 11 12 13 14 NAME truncate, ftruncate - truncate a file to a specified length SYNOPSIS int truncate(const char *path, off_t length); int……

阅读全文

gcc/g++/make 编译信息带颜色输出

概览 如果编译一个项目错误警告太多,非常不好找,所以非常希望输出信息可以带有颜色。 可是 gcc 4.9.0 之前的版本并不支持,很多情况下是不能替换编译器的,比如使用交叉编译器, 也可以使用 colorgcc,但我觉得不是特别好,需要配置,如果使用 Makefile 还要更改编译器设置, 所以我自己动手写了一个,效果还可以,源码在github……

阅读全文

Linux C 网络编程 - 获取本地 ip 地址,mac,通过域名获取对应的 ip

概览 获取本地 ip 地址,mac,通过域名获取对应的 ip, 是网络编程可能遇到的比较常见的操作了,所以总结如下(封装了3个函数) 代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88……

阅读全文

windows 上的 _splitpath 函数在 linux 平台下的简单实现

概览 在做移植时, 发现了 _splitpath 在 linux 下是没有的,于是决定自己写下,也不难。 首先百科到如下内容: 声明定义 void _splitpath( const char *path, char *drive, char *dir, char *fname, char *ext ); 说明 分解路径,把你的完整路径给分割开来,就是一个对字符串进行分割的函数 参数表 1 2 3 4 5 path, Full path(完整路径) drive , Optional drive letter, followed by a colon (:)( 磁盘驱动包含:) dir, Optional directory path, including trailing……

阅读全文

Linux 最最常用命令使用示例(10个并配图)

之前一个朋友想要我教下 Linux 的使用,于是我按照命令的关联性做了一个入门级的教程 1 打开终端的方法(我演示的系统是Linux Mint) A. 点击终端图标 B. 右击桌面,在右键菜单中选择终端 C. Ctrl+alt+t 2 pwd 命令:查看当前路径 “/” 代表根目录,类似 Windows 的 C 盘。 3 cd 命令:更改当前目录 “..” 代表上一级目录,“.” 代表当前目录。 4 ls……

阅读全文

最近文章

分类

标签

其它