Colderleo's blog Colderleo's blog
首页
Linux
C++
Python
前端
工具软件
mysql
索引
关于
GitHub (opens new window)

Colder Leo

热爱代码的打工人
首页
Linux
C++
Python
前端
工具软件
mysql
索引
关于
GitHub (opens new window)
  • 常见程序性能开销cost、latency延迟耗时的经验值
  • 面试常见问题
  • 静态链接-动态链接-elf详解-elfloader
  • 动态库和静态库的依赖问题
  • glibc和ld的编译调试-为某程序单独设置ld
  • dl_iterate_phdr遍历linkmap头、获取so加载地址
  • shell、bash语法和脚本模板
  • so文件查找路径
  • 逻辑地址-线性地址or虚拟地址-物理地址
  • 通过ELF读取当前进程的符号表并获取符号的版本信息
  • 虚拟内存,cache,TLB,页表
  • 用户内存空间分布和mmap
  • numa网卡绑定
  • 隔核绑核、服务器优化
  • popen底层原理和仿照实现-execl
  • tmux用法
  • ASLR机制
  • 程序后台运行、恢复前台nohup
  • 大页内存huge_page
  • 用perf查看page-fault
  • Bash设置显示全部路径
  • 查看socket fd状态,设置nonblock
    • 将socket设置为非阻塞(non-blocking)
    • 查看是否为non-blocking
    • fcntl()
  • cout输出到屏幕的过程
  • 多进程写同一文件-write原子性-log日志
  • vim用法
  • epoll用法
  • signal信号、软中断、硬中断、alarm
  • 内核模块
  • 读写锁之pthread_rwlock和内核rwlock自旋读写锁
  • systemtap
  • xargs、awk用法
  • openssl libssl.so.10.so缺失问题
  • netstat用法
  • fork函数
  • tcp延迟确认ack
  • 90.centos7上一次std-string编译错误定位
  • docker用法
  • find用法
  • dmesg
  • gcc编译用法
  • avx-sse切换惩罚
  • Centos7防火墙
  • chmod用法
  • kernel-devel安装版本
  • Linux-Centos7系统安装、网络设置、常见报错
  • linux下g++编译c++文件
  • MegaCli 安装及使用
  • mysql
  • mysql忘记密码修改方法
  • set用法
  • crontab
  • ssh传文件scp
  • ssh连接
  • tcpdump、tshark、tcpreplay用法
  • ubantu root登录以及创建新用户
  • ubuntu安装g++和gdb
  • uClibc编译失败解决方法
  • win10安装WSL open-ssh
  • yum升级git
  • 比较so文件版本-md5sum
  • 查看磁盘信息
  • 合并两个硬盘,挂载到一个文件夹下
  • 软件安装目录usr-local-src
  • 下载centos历史版本
  • sh脚本转可执行文件、加密
  • Linux
gaoliu
2022-03-14
目录

查看socket fd状态,设置nonblock

# 将socket设置为非阻塞(non-blocking)

https://blog.csdn.net/SprintfWater/article/details/9232073 (opens new window)

有一个非常有迷惑性的做法是: u_long has = 1; ioctl(m_sock, FIONBIO , &has); 这个函数会非常无耻的返回你success,但是它实际上很可能什么也没做。

正确的做法应该是使用fcntl: int flags = fcntl(m_sock, F_GETFL, 0); fcntl(m_sock, F_SETFL, flags|O_NONBLOCK);

这真是一个隐蔽的问题,折腾了我两天。线程每每停留在send()调用那里,我始终没怀疑到:用ioctl设置FIONBIO成功之后,socket竟然还是阻塞的。

# 查看是否为non-blocking

#include <fcntl.h>

int flags = fcntl(s, F_GETFL, 0);

printf("flags&O_NONBLOCK=%x, O_NONBLOCK=%x, flags=%x\n", flags&O_NONBLOCK, O_NONBLOCK, flags);
//flags&O_NONBLOCK=800, O_NONBLOCK=800, flags=802

1
2
3
4
5
6
7

# fcntl()

https://man7.org/linux/man-pages/man2/fcntl.2.html (opens new window)

FCNTL(2)                Linux Programmer's Manual               FCNTL(2)
NAME         top
       fcntl - manipulate file descriptor
SYNOPSIS         top
       #include <fcntl.h>

       int fcntl(int fd, int cmd, ... /* arg */ );
DESCRIPTION         top
       fcntl() performs one of the operations described below on the
       open file descriptor fd.  The operation is determined by cmd.

       fcntl() can take an optional third argument.  Whether or not this
       argument is required is determined by cmd.  The required argument
       type is indicated in parentheses after each cmd name (in most
       cases, the required type is int, and we identify the argument
       using the name arg), or void is specified if the argument is not
       required.

       Certain of the operations below are supported only since a
       particular Linux kernel version.  The preferred method of
       checking whether the host kernel supports a particular operation
       is to invoke fcntl() with the desired cmd value and then test
       whether the call failed with EINVAL, indicating that the kernel
       does not recognize this value.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
编辑 (opens new window)
上次更新: 2023/05/07, 17:27:54
Bash设置显示全部路径
cout输出到屏幕的过程

← Bash设置显示全部路径 cout输出到屏幕的过程→

最近更新
01
通过模板实现结构体成员拷贝-按成员名匹配
05-07
02
c++17通过模板获取类成员的个数
05-01
03
avx-sse切换惩罚
04-30
更多文章>
Theme by Vdoing | Copyright © 2019-2023 Colder Leo | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×