
Linux
cwdben
Do not go gentle into that good night
展开
-
c++11 实现设计一个多线程打印程序,第i个线程只打印i-1数字
#include <thread>#include <iostream>#include <mutex>#include <condition_variable>#include <vector>#include <functional>using namespace std;mutex print_mutex;condition_variable cond;string s = "";int flag = 0;原创 2022-03-09 22:12:57 · 1180 阅读 · 0 评论 -
基于select的回射服务器
server#include <sys/types.h>#include <sys/socket.h>#include <stdio.h>#include <netinet/in.h>#include <sys/time.h>#include <sys/ioctl.h>#include <unistd.h>#include <arpa/inet.h>#include <string.h&g原创 2022-01-17 21:37:45 · 709 阅读 · 0 评论 -
调试coredump程序
写一个产生coredump的程序#include <iostream>using namespace std;int main(int argc, char const *argv[]){ int*p; p=nullptr; cout<<*p<<endl; return 0;}编译g++ -g -o coredump coredump.cpp运行发生段错误。找到core的日志ubuntu下core文件处理cwd@cw原创 2022-01-09 19:58:49 · 1419 阅读 · 1 评论 -
使用tcpdump观察tcp的三次握手,四次挥手,以及http的keep-alive机制
测试方法:服务器使用自己写的,架设在阿里云服务器上。打开两个终端,分别运行cwd@cwd:~$ sudo tcpdump dst 116.62.35.197 and tcpcwd@cwd:~$ sudo tcpdump src 116.62.35.197 and tcp测试一:使用postman发送有keep-alive标志的get请求一次,观察报文情况。测试结果:客户端发向服务器报文:cwd@cwd:~$ sudo tcpdump dst 116.62.35.197 and tcptc原创 2021-12-26 14:42:09 · 1833 阅读 · 0 评论 -
解决webbench运行时卡住的问题
问题描述:使用webbench进行压力测试的时候,在baidu上一般来说正常。在自己的webserver上测试,有时会卡住而没有返回值。在修改为长连接的时候尤其会发生。如下图必须要ctrl+c终止。github上也有这样的问题长连接问题 · Issue #31 · linyacool/WebServer解决思路:找到可能被卡住的地方测试时修改了部分源码进行打印,看每次1000个子进程中被阻塞了的进程数判断。1:代码健壮性增加:从while (s = Socket(host, port)原创 2021-12-23 22:06:25 · 1014 阅读 · 0 评论 -
修改webbench支持长连接
概述我们知道HTTP协议采用“请求-应答”模式,当使用普通模式,即非KeepAlive模式时,每个请求/应答客户和服务器都要新建一个连接,完成之后立即断开连接(HTTP协议为无连接的协议);当使用Keep-Alive模式(又称持久连接、连接重用)时,Keep-Alive功能使客户端到服务器端的连接持续有效,当出现对服务器的后继请求时,Keep-Alive功能避免了建立或者重新建立连接。http 1.0中默认是关闭的,需要在http头加入"Connection: Keep-Alive",才能启用Keep-原创 2021-12-23 20:35:40 · 531 阅读 · 0 评论 -
webbench详解与源码注释
webbench总共就是三个文件,makefile,socket.c,webbench.cmakefile完成编译,sudo make install安装到 /usr/local/bin目录下面。其实 make install命令就是把可执行文件拷贝到/bin目录下面。INSTALL(1) User Commands INSTALL(1)NAME install - copy files and原创 2021-12-23 20:18:24 · 1020 阅读 · 0 评论 -
linux中install/make install的时候发生了什么?
核心:install就是把可执行文件拷贝到bin目录下。参考man installINSTALL(1) User Commands INSTALL(1)NAME install - copy files and set attributesSYNOPSIS install [OPTION]... [-T] SOURCE DEST install [OPTION]...原创 2021-12-19 14:43:34 · 590 阅读 · 0 评论 -
基于c++11新特性的safe_call wapper
今天调试项目,进入了一个公司写的源代码去看,其中写了一个wapper层。核心代码如下:std::map<std::thread::id, std::exception> exceptions;std::map<std::thread::id, bool> exceptions_read;template<typename class_type, typename ret_type, typename ...arg_type>inline ret_type saf原创 2021-12-15 21:50:08 · 763 阅读 · 0 评论 -
python与c++程序通过mqtt通信
mqtt安装与基本测试#安装mosquitto mqtt#引入仓库sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa#更新源sudo apt-get update#安装mosquittosudo apt install mosquitto#安装客户端sudo apt-get install mosquitto-clients######测试#一个终端开启sub mqttmosquitto_sub -h localhost原创 2021-12-11 15:18:47 · 3124 阅读 · 1 评论 -
Linux--UIO机制
kernel解释前言对于许多类型的设备,创建 Linux 内核驱动程序是大材小用。真正需要的只是某种处理中断并提供对设备内存空间的访问的方法。控制设备的逻辑不一定必须在内核中,因为设备不需要利用内核提供的任何其他资源。像这样的一类常见设备是用于工业I / O卡。为了解决这种情况,设计了用户空间 I/O 系统 (UIO)。对于典型的工业I/O卡,只需要一个非常小的内核模块。驱动程序的主要部分将在用户空间中运行。这简化了开发,并降低了内核模块中出现严重错误的风险。请注意,UIO 不是通用驱动程序接口。已原创 2021-11-25 14:01:01 · 4395 阅读 · 0 评论 -
malloc分配内存的实现中brk和mmap的区别
malloc函数族: #include <stdlib.h> void *malloc(size_t size); void free(void *ptr); void *calloc(size_t nmemb, size_t size); void *realloc(void *ptr, size_t size); void *reallocarray(void *ptr, size_t nmemb, siz原创 2021-11-08 21:08:09 · 2648 阅读 · 0 评论 -
《Linux设备驱动程序》(第三版)的shortprint模块程序注释
/* * A version of the "short" driver which drives a parallel printer directly, * with a lot of simplifying assumptions. * * Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet * Copyright (C) 2001 O'Reilly & Associates * * The source code i原创 2021-09-12 19:50:33 · 258 阅读 · 0 评论 -
隔离并绑定线程到cpu使内核线程独占cpu
隔离cpu1.运行htop,观察各个cpu负载情况:2.修改/etc/default/grubGRUB_CMDLINE_LINUX=“isolcpus=2”更新sudo update-grub重启3.htop查看负载分布[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nd1ad04M-1630635890292)(/home/imc/Pictures/htop2.png)]cpu的隔离没有问题。(上述两次htop运行在ethercat启动,ec_ep3e.k原创 2021-09-03 10:31:21 · 2662 阅读 · 0 评论 -
linux内核模块与用户程序共享内存
使用remap_pfn_range在内核层共享内存,完成mmp回调函数的编写。默认动态分配设备号,允许以命令行方式指定设备号使用cdev_add注册字符设备使用device_create创建设备文件"/dev/demo0","/dev/demo1"部分参考了这个博客编译模块#make#sudo insmod sharedmem.ko#cd /dev#ls有demo0,demo1测试程序编译#gcc user.c -o user切换到root用户#su root#./user.原创 2021-08-25 20:58:58 · 987 阅读 · 0 评论 -
linux 内核重要数据结构 file_operations、file、inode
标记化结构初始化方法内核中结构体的赋值常使用标准C的标记化结构初始化方法,这在之前的用户层代码并不常见。static struct file_operations fops ={ .owner = THIS_MODULE, .open = hello_dev_open, .release = hello_dev_release, .read = hello_dev_read, .write = hello_dev_writ原创 2021-08-25 11:28:39 · 650 阅读 · 0 评论 -
利用shm_open的共享内存在无亲缘进程间通信例程
头文件定义储存在共享内存的结构体,以及一些宏定义#include <unistd.h>#include <stdint.h>struct shm_ect_data { /* struct stored in shared memory */ int32_t targetPosition; //电机的目标位置 int32_t targetVelocity; //电机当前运行速度 int8_t opModeSet; //电机运行模原创 2021-05-14 16:26:47 · 322 阅读 · 0 评论 -
Linux下使用信号处理,终端执行ctrl+c后的SIGINT信号
很多时候我们写的程序是一个一直运行的,调试的时候,我们直接按ctrl+c终止程序,那么在while后面的一些close函数的调用,在没有设置中断函数的时候,是不会被执行的。错误例子#include <iostream>#include <unistd.h>using namespace std;int main(){ while(1){ //sleep(1); } printf("closing\n");}编译运行,ct原创 2021-05-13 20:01:46 · 2745 阅读 · 0 评论 -
使用sendmsg/recvmsg在进程之间传递文件描述符
需要注意一点,传递一个文件描述符,并不是传递一个文件描述符的值,而是要在接收进程中创建一个新的文件描述符,并且该文件描述符和发送进程中被传递的文件描述符,指向同一个文件。例程#include <sys/socket.h>#include <fcntl.h>#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <assert.h>#include原创 2021-05-13 19:18:14 · 1255 阅读 · 0 评论 -
基于共享内存和信号触发的聊天室服务器方案(包括多进程处理)
源码部分借鉴自游双的《Linux高性能服务器编程》完整源码参见github-chatroom1.全局配置client_data#define BUFFER_SIZE 64#define USER_LIMIT 5#define FD_LIMIT 65535#define MAX_EVENT_NUMBER 1024#define PROCESS_LIMIT 65535int sig_pipefd[2];bool stop_child = false; //子进程的标志位struct cl原创 2021-05-12 17:12:27 · 336 阅读 · 0 评论 -
关于epoll_create的size,以及创建出的句柄epollfd是否需要close探讨
sizeepoll_creat的函数圆形如下:int epoll_create(int size);其中,size的含义值得琢磨。首先参考官方给出的资料:gun库里的注释/* Creates an epoll instance. Returns an fd for the new instance. The "size" parameter is a hint specifying the number of file descriptors to be associated wit原创 2021-05-11 22:11:05 · 953 阅读 · 0 评论 -
基于poll的聊天室程序
源码部分借鉴自游双的《Linux高性能服务器编程》客户端#include <sys/socket.h>#include <sys/types.h>#include <fcntl.h>#include <unistd.h>#include <assert.h>#include<stdio.h>#include <stdlib.h>#include <netinet/in.h>#include &原创 2021-05-10 08:58:02 · 341 阅读 · 1 评论 -
epoll的ET模式和LT模式的例程与分析
例程来自游双的《Linux高性能服务器编程》#include <sys/socket.h>#include <sys/types.h>#include <sys/epoll.h>#include <fcntl.h>#include <unistd.h>#include <assert.h>#include<stdio.h>#include <stdlib.h>#include <neti原创 2021-05-06 19:12:59 · 222 阅读 · 0 评论 -
aio_read()的例程详解
man手册AIO(7) Linux Programmer's Manual AIO(7)NAME aio - POSIX asynchronous I/O overviewDESCRIPTION The POSIX asynchronous I/O (AIO) interface allows applications to ini‐ tiate one or more I/O原创 2021-05-05 21:59:36 · 1890 阅读 · 0 评论 -
syslog()的使用和例程
man手册命令行man openlog即可查看;写的非常详细,看完其实就懂了。NAME closelog, openlog, syslog, vsyslog - send messages to the system loggerSYNOPSIS #include <syslog.h> void openlog(const char *ident, int option, int facility); void syslog(in原创 2021-05-05 09:35:30 · 1843 阅读 · 0 评论 -
c++手写线程池
#ifndef THREADPOOL_H#define THREADPOOL_H#include <mutex>#include <condition_variable>#include <queue>#include <thread>#include <functional>class ThreadPool {public: //explicit声明显示类构造函数 //make_shared 创建Pool以及其的原创 2021-04-10 14:20:54 · 520 阅读 · 1 评论 -
Epoll写的一个tcp回射服务器
运行环境:Linux/* include fig01 */#include "unp.h"#include <limits.h> /* for OPEN_MAX */#include <sys/epoll.h>#define INET_ADDRSTRLEN 16#define OPEN_MAX FOPEN_MAX#define NOTDEFintmain(int argc, char **argv){ int listenfd, connfd;原创 2021-04-09 08:21:50 · 157 阅读 · 0 评论 -
bind:address already in use 端口已占用解决办法
lsof -i:9877root@cwd:/home/cwd/code/unpv13e/tcpcliserv# lsof -i:9877COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEtcpserv01 14189 root 3u IPv4 91150 0t0 TCP *:9877 (LISTEN)root@cwd:/home/cwd/code/unpv13e/tcpcliserv# kill -9 14189原创 2021-04-01 17:08:55 · 3494 阅读 · 0 评论 -
编译linux实时内核‘error: ‘-mindirect-branch’ and ‘-fcf-protection’ are not compatible’等几个问题
Kernel does not support PIC mode 解决办法:https://blog.youkuaiyun.com/jasonLee_lijiaqi/article/details/84651138./include/linux/types.h:17:9: error: unknown type name ‘__kernel_ino_t’ typedef __kernel_ino_t in...原创 2020-04-03 20:54:55 · 4529 阅读 · 1 评论