编程
文章平均质量分 69
耿小渣
这个作者很懒,什么都没留下…
展开
-
使用zlib实现gzip压缩
读写文件#include <stdio.h>#include <string.h>#include <unistd.h>#include <zlib.h>int main(){ int err; int len = 11; gzFile file; z_off_t pos; unsigned char uncompr[128] = {0}; int uncomprLen = 128; .原创 2021-03-23 18:39:13 · 1874 阅读 · 0 评论 -
libevent 使用实现http 服务
单线程#include <stdio.h>#include <stdlib.h>#include <string.h> //for strcat#include <unistd.h> //for getopt, fork// for struct evkeyvalq#include <event.h>#include <sys/queue.h>// for http//#include <evhttp.h原创 2021-03-21 18:45:01 · 1119 阅读 · 0 评论 -
libpcap使用
捕获数据包操作/*pcap_test.c编译方式:gcc pcap_test.c -o pcap_test -lpcap -lpthread*/#include <stdio.h>#include <stdlib.h>#include <pcap.h>#include <pthread.h>#define SNAP_LEN 65535pcap_t *handle;pcap_dumper_t* out_pcap;/*函数名称.原创 2020-08-07 11:13:45 · 959 阅读 · 0 评论 -
DPDK CAS(compare and set)操作
rte_atomic32_cmpset()称为CAS(compare and set)操作,是DPDK无锁队列实现的关键函数,代码如下:static inline intrte_atomic32_cmpset(volatile uint32_t *dst, uint32_t exp, uint32_t src){uint8_t res; asm volatile( "lock;" "cmpxchgl %[src], %[dst];" "sete %[res];" :[res] "=a.原创 2020-07-27 16:04:25 · 803 阅读 · 0 评论 -
Gcc内置原子操作__sync_系列函数简述及例程
Gcc 4.1.2版本之后,对X86或X86_64支持内置原子操作。就是说,不需要引入第三方库(如pthread)的锁保护,即可对1、2、4、8字节的数值或指针类型,进行原子加/减/与/或/异或等操作。有了这套内置原子操作函数,写程序方便很多。老宋根据Gcc手册中《Using the GNU Compiler Collection (GCC)》章节内容,将__sync_系列17个函数声明整理简化如下:type __sync_fetch_and_add (type *ptr, type value, ..转载 2020-07-27 14:39:51 · 870 阅读 · 0 评论 -
gdb调试程序no symbol table is loaded.Use the "file" command 和 value optimized out 错误
一、原因我们有时候用gdb调试程序时会出现no symbol table is loaded.Use the "file" command 错误,出现这种错误的原因是程序在编译时没有加-g 选项.value optimized out 错误原因是gcc 优化导致的我们只需加上-O0选项禁止强制优化。二、解决方法1、如果是我们写的makefile 只需在gcc 之后增加-g选项。2、...原创 2018-07-23 18:30:57 · 2948 阅读 · 0 评论 -
进程的各种ID
pid(进程ID) UNIX进程总是会分配一个号码用于在其命名空间中唯一的标识他们。该号码被称为进程ID号,简称PID。用fork或者clone产生的每个进程都由内核自动的分配一个新的唯一的PID值。#include <sys/types.h>#include <unistd.h>pid_t getpid( void ); ppid(父进程ID) ...原创 2020-04-14 15:43:42 · 1924 阅读 · 0 评论 -
多线程编程笔记
1、线程概念线程,有时被称为轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元。一个标准的线程由线程ID,当前指令指针(PC),寄存器集合和堆栈组成。另外,线程是进程中的一个实体,是被系统独立调度和分派的基本单位,线程自己不拥有系统资源,只拥有一点儿在运行中必不可少的资源,但它可与同属一个进程的其它线程共享进程所拥有的全部资源。一个线程可以创建和撤消另一个线程...原创 2020-04-10 17:01:09 · 241 阅读 · 0 评论 -
发生段错误后程序不崩溃
一个段错误的例子:#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){ char *tmp = NULL; strcpy(tmp, "hello"); return 0;}执行结果加上段错误处理函数:#include <stdi...原创 2020-01-13 17:46:24 · 1142 阅读 · 0 评论 -
C语言调用python脚本
.Python脚本,名称为py_add.py def add(a=1,b=1): print('Function of python called!') print('a = ',a) print('b = ',b) print('a + b = ',a+b)C语言代码callpython.c#include <Python.h>#i...转载 2019-07-24 16:10:35 · 4255 阅读 · 0 评论 -
一个简单的进程池
//半同步/半异步进程池的实现#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <stdio.h>#include <stdlib.h>#include &l...原创 2019-01-07 14:43:33 · 206 阅读 · 0 评论 -
linux c 程序异常退出时打印堆栈调用信息
先来了解三个函数#include <execinfo.h> int backtrace(void **buffer, int size); char **backtrace_symbols(void *const *buffer, int size); void backtrace_symbols_fd(void *const *buffer, int size, in...原创 2018-12-12 16:38:52 · 6950 阅读 · 0 评论 -
posix消息队列和systemV消息队列
一、概述: 消息队列可认为是一个消息链表。有足够写权限的线程可往队列中放置消息,有足够读权限的 线程可从队列中取走消息。posix消息队列和systemV消息队列主要如下差异: 1、一般来说posix的接口要比systemV的简单,但是systemV的可已移植性更好几乎所有的unix系统都支持。 2、对posix消息队列的读总是返回最高优先级的最早消息,对systemV消息队列的读...原创 2018-06-01 18:33:36 · 2615 阅读 · 0 评论 -
一个简单的爬取小说的python程序
第一个python程序,记录一下各种坑。程序主要功能是爬取读书笔记网站(http://www.mossiella.com/)上的小说下载保存为txt,这里拿《明朝那些事》为例,网址为http://www.mossiella.com/xiandaiwenxue/mingchaonaxieshier/如果想要爬取别的小说,只需要更改如下的变量即可:初始化函数def __init__(self): ...原创 2018-07-03 11:35:57 · 1650 阅读 · 0 评论 -
readelf nm objdump 命令详解
讲解上面三条命令之前要先了解一下ELF格式文件https://blog.csdn.net/u014608280/article/details/81983055一、readelf readelf命令可以查看ELF文件的详细信息。选项-a--all 显示全部信息,等价于 -h -l -S -s -r -d -V -A -I. -h--file-header 显示elf文件开始...原创 2018-08-22 18:45:01 · 4877 阅读 · 0 评论 -
深入理解堆与栈
大多数操作系统会将内存空间分为内核空间和用户空间,而每个进程的内存空间又有如下的“默认”区域。 1、栈:栈用于维护函数调用的上下文,离开栈函数调用就会无法实现。栈通常在用户空间的最高地址处分配,通常有数兆字节。 2、堆:堆用来容纳应用程序动态分配的内存区域,我们使用malloc 或者new分配内存时,得到的内存来自堆里。堆通常存于栈的下方(低地址方向),堆一般比栈大很多,...原创 2018-08-30 18:00:48 · 38515 阅读 · 1 评论 -
目标文件
目标文件:编译器编译源代码后生成的文件叫做目标文件。(也就是我们所说的windows下的.obj和Linux 下的.o)从广义上来看,目标文件和可执行文件的格式其实几乎是一样的,所以我们可以广义的将目标文件与可执行文件看成是一种类型的文件。在linux下,我们可以统称为ELF文件。ELF文件包括如下表中四类: file 命令我们可以通过file命令来查看文件的类型...原创 2018-08-23 16:26:00 · 1523 阅读 · 1 评论 -
预处理,编译,汇编和链接
当我们gcc 编译一个程序的时候有没有想过到底发生了什么?1、我们有一个hello.c文件,用最简单的命令gcc hello.c 生成 a.out 这个过程可以分为四个步骤:预处理(Prepressing)、编译(Compilation)、汇编(Assembly)和链接(Linking)。一、预编译 1.将所有的#define删除,并展开所有的宏定义; 2.处理所有的预编译指令...原创 2018-08-23 18:03:07 · 570 阅读 · 0 评论 -
gcc -finstrument-functions 追踪函数调用,获取程序的执行流程
我们在阅读源码的时候经常会碰到很多回调函数,而单步调试又比较麻烦,所以我们可以用gcc 的-finstrument-functions 选项打印函数调用栈。 如果我们在编译、链接的时候在gcc加上-finstrument-functions,gcc会自动在函数的入口调用 __cyg_profile_func_enter函数,在函数出口调用__cyg_profile_func_exi...原创 2018-09-14 16:51:20 · 5104 阅读 · 0 评论 -
条件变量
#include <stdio.h>#include <stdlib.h>#include <pthread.h>#include <unistd.h> static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;static pthread_cond_t cond = PTHREAD_...转载 2018-10-30 15:42:24 · 121 阅读 · 0 评论 -
linux c 清屏功能实现
一、调用system("clear")函数二、printf("\x1b[H\x1b[2J")#include<stdio.h>#include<stdlib.h>#include<unistd.h>int main(){ printf("hello !\n"); printf("hello !\n"); print原创 2018-11-28 14:41:51 · 4223 阅读 · 0 评论 -
字符串处理常用函数
截取字符串的一定的长度在解各种数据包中是非常常用的,怎么实现截取取决于数据包的数据类型。 1、截取两个字节长度: 定义为宏:define pntoh16(p) ((uint16_t) \ ((uint16_t)*((const uint8_t *)(p)+0)&amp;amp;lt;&amp;amp;lt;8| \ ...原创 2018-05-31 11:00:45 · 804 阅读 · 0 评论