0%

Concept

可重入函数:如果一个函数在执行过程中被中断服务程序打断,执行中断服务程序之后恢复执行,还能不妨碍之前的执行,就称该函数是可重入的

可重入函数一般用于硬件中断处理递归等应用程序中。

可重入程序/可重入子例程:在多个处理器上能被安全地多次并发调用。

与线程安全的区别:可重入函数的概念在多任务操作系统出现之前就存在了,所以该概念仅仅针对的是单线程执行过程。
一个函数可以是线程安全但非可重入的,例如,该函数每次都使用互斥量来包裹。但是,如果该函数用于中断服务程序,那么,它可能在等待第一次执行过程释放互斥量时陷入饥饿。TODO:陷入饥饿为什么就不是可重入了?

Reference

Get IP from the host name

Key function: getaddrinfo.

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
#define _POSIX_C_SOURCE 200112L

#include <stdio.h>
#include <sys/socket.h>
#include <netdb.h>
#include <sys/types.h>
#include <errno.h>
#include <strings.h>
#include <arpa/inet.h>

int main() {
struct addrinfo hints;
bzero(&hints, sizeof hints);
// hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;

struct addrinfo *res = 0;
// The following 3 hostnames are legal.
int rc = getaddrinfo("www.baidu.com", NULL, &hints, &res);
// int rc = getaddrinfo("localhost", NULL, &hints, &res);
// int rc = getaddrinfo("PC-XXX", NULL, &hints, &res); // PC-XXX is a hostname
if (rc != 0)
perror("getaddrinfo failed");

for (struct addrinfo* res_i = res; res_i != NULL; res_i = res_i->ai_next) {
if (res_i->ai_addr) {
if (res_i->ai_addr->sa_family == AF_INET) {
char ip4[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(((struct sockaddr_in*)(res_i->ai_addr))->sin_addr), ip4, INET_ADDRSTRLEN);
printf("IP: %s\n", ip4);
} else {
char ip6[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, &(((struct sockaddr_in6*)(res_i->ai_addr))->sin6_addr), ip6, INET_ADDRSTRLEN);
printf("IP: %s\n", ip6);
}
}
}

return 0;
}

预定义宏

#if defined(__linux)
#ifdef LINUX2

C标准预定义宏

  • __LINE__
  • __func__
  • __FILE__
  • NDEBUG:参考_DEBUG和NDEBUG的区别,其中,_DEBUG是Visual Studio定义的,NDEBUG是C/C++标准。

GNU C预定义宏

官方文档

  • __COUNTER__: 扩展为从0开始的连续整数值,每次在源码中出现,则加1。不同源文件的__COUNTER__互不影响。

    可以用来生成唯一的命名。
    参考链接

    1
    2
    3
    4
    5
    6
    7
    8
    #define CONCAT_IMPL(x,y) x##y
    #define CONCAT(x,y) CONCAT_IMPL(x,y)
    #define VAR(name) CONCAT(name,__COUNTER__)
    int main() {
    int VAR(myvar); // 展开为 int myvar0;
    int VAR(myvar); // 展开为 int myvar1;
    int VAR(myvar); // 展开为 int myvar2;
    }
  • program_invocation_name:参考man page

#pragma

#pragma weak

Synopsis

1
#pragma weak function-name1 [= function-name2]

#pragma weak means that even if the definition of the symbol is not found, no error will be reported.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>

// It is not an error for symbol to never be defined at all.
// Without this line, the address of "foo" will always evaluate to "true",
// so the linker will report an "undefined reference to 'foo'" error.
#pragma weak foo
// The declaration is needed.
/* extern */ void foo();

int main() {
if (foo)
foo();

return 0;
}

Reference: 123

Host site

有两个托管个人站点的网站:Github pagesreathedocs.

Github Pages

使用github pages可以搭建个人主页。

github pages简介:官方链接

github pages使用了CNAME record技术,参考:链接1链接2Custom domains in Github Pages

注:Read the Docs也是一个很好的搭建个人主页的网站。

Github Pages 站点类型

有3种类型的 Github Pages 站点(sites):project, user 和 organization 。

Project sites 连接到 github 上特定 project ,比如 Javascript library 或 recipe collection。user 或 organization sites 连接到 github.com 的特定账户。

发布 user site ,你必须创建一个你的个人账户下的一个名为 <username>.github.io 的 repository 。发布 organization site ,你必须创建一个组织所有的名为 <organization>.github.io 的 repository 。除非你使用 custom domain ,否则 user 和 organization sites 将位于 http(s)://<username>.github.iohttp(s)://<organization>.github.io

project site 的源文件存储在作为 project 的相同的 repository 中。除非使用 custom domain , 否则 project sites 将位于 http(s)://<username>.github.io/<repository>http(s)://<organization>.github.io/<repository>

有关如何自定义影响您网站的域名的更多信息,参见”About custom domains and GitHub Pages“。

每个 github 账户允许创建 1 个 user 或 organization 站点。无论是被组织还是个人所有,project 站点的个数不限制。

GitHub Pages 访问方法

参考官方文档

例如,你的project站点配置的发布源是gh-pages分支,然后在gh-pages分支上创建了一个about/contact-us.md文件,你将可以在https://<user>.github.io/<repository>/about/contact-us.html访问它。

你也可以使用Jekyll等静态站点生成器来给你的github page配置一个主题。

站点发布常见问题的解决方法

Github workflows

参考官方文档

Github个人访问令牌

Github个人访问令牌

readthedocs

Similar to the github pages.

Tutorial

配置前准备

Markdown编辑器

推荐的markdown编辑器

  • VSCode:免费。VSCode原生支持Markdown,安装一些插件可以帮助更快地编写markdown文件。
  • Typora:现在已经开始收费。

VSCode markdown插件:

  • Mardown All in One: 提供快捷键,帮助更快的编写markdown文件。
  • Markdown+Math:提供数学公式支持。
  • Markdown Preview Enhanced: 将原生markdown预览的黑色背景改成白色。
  • Markdown Preview Github Styling:提供Github风格的预览。

在线表格生成器:可以生成Markdown、Text、HTML、LaTex、MediaWiki格式的表格。

轻量级虚拟机WSL

WSL,Windows Subsystem for Linux,是Windows提供的轻量级Linux虚拟机。

安装教程:见链接

WSL默认没有启用systemctl:

启用systemctl的方法:链接

替代方法:不需要启动systemctl,因为会比较占用资源,启动也会变慢。可以使用service命令替代。

WSL默认没有安装openssl-server:

使用ssh连接到服务器时,需要服务器运行着sshd程序,否则连接不上,会出现”Connection refused“错误。

参考链接

查看openssh-server有没有安装:

1
dpkg --list | grep ssh

注:如果安装了openssh-server,执行which sshd可以看到路径。

WSL默认没有安装openssh-server,安装方法:

1
sudo apt-get install openssh-server

启动ssh:

1
sudo service ssh start

ssh-keygen

1
2
# 删除错误的 know_hosts 项
ssh-keygen -f "/home/<user>/.ssh/known_hosts" -R "ubuntu"

通过https登录到github

git push不再支持输入用户名和密码,当提示输入密码时,需要输入personal access token.

步骤1:在github上创建personal access token

步骤2:在命令行上使用personal access token

步骤3:为了避免每次都需要输入personal access token,可以将其缓存在git client上

1
gh auth login

注:使用gh命令需要先安装GitHub CLI:

1
sudo apt-get install gh

WSL常见问题

在WSL的Linux下,在.bashrc中加入:

1
2
3
4
function browser() {
explorer.exe $(wslpath -w $(realpath $1))
}
export -f browser

打开html:

1
browser index.html

静态站点生成器

以下几种静态站点生成器都可以用来搭建个人主页。如果使用除JekyII外的工具,则需要配置Github Actions以构建和发布你的站点。

mkdocs

mkdocs是一个快速的静态网页生成器。

mkdocs.yml 文件是 MkDocs 文档生成器的配置文件,其格式说明参见这里

JekyII

Jekyll 是一个静态站点生成器,内置对 GitHub Pages 的支持和简化的构建进程。

参见 About GitHub Pages and Jekyll

Hexo

Sphinx

Install Sphinx
Hosting the documentation

python

python建立个人站点

python -m http.server

Syntax

Markdown

reStructuredText

.rst

大型文件存储

图片

Postimages

  • 网站: https://postimages.org/
  • 特点:
  • 适合快速上传和生成图片链接。
  • 不需要注册,可以直接上传图片并获得链接。
  • 提供图片缩略图和直接链接。
  • 上传后图片会长期存储,适用于网站和博客图片托管。

视频

TinyPic (由 Photobucket 提供)

  • 网站: https://tinypic.com/
  • 特点:
  • 支持上传并生成快速共享链接。
  • 上传后,链接可以嵌入到社交媒体、论坛、博客中。
  • 图片和视频都支持托管,但TinyPic已关闭,现由Photobucket服务接管。

Git LFS

GitHub 提供的用于存储大型文件的服务叫 Git LFS(Git Large File Storage)。

什么是 Git LFS?

Git LFS 是一个 Git 扩展,用于处理 Git 仓库中的大型文件(如图片、视频、音频等),它通过将大型文件存储在 GitHub 的独立存储区来解决 Git 的性能瓶颈。

Git LFS 的免费额度:

GitHub 为免费账户提供一定的 LFS 存储和带宽额度:

免费存储:每个 GitHub 账户提供 1GB 的 LFS 存储。

免费带宽:每个月 1GB 的下载带宽。

如果超出免费额度,你可以选择购买更多的存储和带宽。

Git LFS 的工作原理:

文件替代:Git LFS 用一个指向大文件的指针来替代文件内容,将文件本身存储在 GitHub 提供的 LFS 存储空间中。

文件存储:实际的文件内容存储在 GitHub 的 LFS 存储中,Git 只会管理这些文件的指针。

下载/上传:当你从仓库克隆或拉取代码时,Git LFS 会自动下载大文件,反之,当你提交大文件时,Git LFS 会将其上传到 GitHub。

如何使用 Git LFS?

  1. 安装 Git LFS:
    首先需要安装 Git LFS,可以通过 Git LFS 官方网站 下载和安装。
1
git lfs install
  1. 初始化 Git LFS:
    在你的 Git 仓库中初始化 Git LFS。
1
2
3
4
5
git lfs track "*.jpg"   # 例如跟踪所有 jpg 格式的图片
git add .gitattributes # 添加 .gitattributes 文件
git add <large-file> # 添加大型文件
git commit -m "Add large file"
git push origin main # 推送到 GitHub
  1. 使用 Git LFS 进行提交:
    每次提交时,Git LFS 会自动处理大文件,并将它们上传到 GitHub LFS 存储区。
优势:

减少仓库体积:通过将大文件存储在 Git LFS 中,可以避免将大文件直接存储在 Git 仓库中,从而提高 Git 的性能。

适用于大文件:适合管理图像、视频、二进制文件等大型文件,避免 Git 克隆和拉取过程变得过于缓慢。

总结:

Git LFS 是 GitHub 提供的用于管理和存储大型文件的扩展服务,适合存储需要频繁更改或管理的大文件(如图片、音频、视频等)。

推荐书籍

  1. 多人在线游戏架构实战:基于C++的分布式游戏编程/彭放编著. –北京:机械工业出版社,2020.11 (ISBN 978-7-111-66792-6)

Options

npm root - Current installation root path.
npm root -g - The global installation root path.
npm uninstall - After executing this command, you should also remove the items in the package.json file and delete the package-lock.json file and the node_modules directory.

Reference

Where does npm install packages?
Cannot Uninstall two npm Packages

Installation

sudo apt install net-tools

Manual

man 7 socket - socket options
man 7 tcp - tcp options

Files

/proc/sys/net/ipv4/ip_local_port_range

Commands

ip

NAME

ip - show / manipulate routing, devices, policy routing and tunnels.

netstat / ss

NAME

ss - another to investigate sockets.

Reference

查看进程占用的端口

COMMANDS

ss -lnpt

OPTIONS

-n, --numberic

-l, --listening

-t, --tcp
    Note: Only established (non-listening) connections.

-a, --all
    Display both listening and non-linstening (for TCP this means established connections) sockets.

-u, --udp

-p, --process

lsof

Options:

-n inhibits the conversion of network numbers to host names for network files.

-P inhibits the conversion of port numbers to port names for network files.

-i specifies the Internet addresses

    multiple addresses (up to a limit of 100) may be specified with multiple options.

    An Internet address is specified in the form (Items in square brackets are optional.):

    [46][protocol][@hostname|hostaddr][:service|port]

    where:
    46 specifies the IP version, IPv4 or IPv6
        that applies to the following address.
        '6' may be be specified only if the UNIX
        dialect supports IPv6.  If neither '4' nor
        '6' is specified, the following address
        applies to all IP versions.
    protocol is a protocol name - TCP, UDP
    hostname is an Internet host name.  Unless a
        specific IP version is specified, open
        network files associated with host names
        of all versions will be selected.
    hostaddr is a numeric Internet IPv4 address in
        dot form; or an IPv6 numeric address in
        colon form, enclosed in brackets, if the
        UNIX dialect supports IPv6.  When an IP
        version is selected, only its numeric
        addresses may be specified.
    service is an /etc/services name - e.g., smtp -
        or a list of them.
    port is a port number, or a list of them.

-s list file size

-s p:s  exclude(^)|select protocol (p = TCP|UDP) states by name(s).

Examples:

1
2
3
lsof -i:<port>
lsof -i -P -n | grep LISTEN
lsof -nP -iTCP -sTCP:LISTEN

nc/netcat/nmap

socket

Create a TCP or a UNIX domain socket and connect to stdin/out.

Installation:

sudo apt-get install socket

电影链接

Midsomer Murders: Season 1 Episode 2 - Written in Blood

台词

Phoenix turned and fired twice, the 34 millimetre bullets spitting through the air and splintering the brickwork two inches to the left of the Russia thug’s shoulder. A moment later came the reply of the Uzi semiautomatic and Phoenix twisted round. 菲尼克斯转身开了两枪,34毫米的子弹在空中喷射,将俄罗斯暴徒肩膀左侧两英寸砖墙击碎。过一会儿,乌兹半自动冲锋枪回应了,菲尼克斯扭过身来。

Her lips, searching in the sapphire, honeyscented darkness, found his; cool, forceful, unresisting, and as the frozen waves crashed down. 当冰冷、有力、不可抗拒的浪汹涌而下的时候,她的嘴唇在蓝宝石般散发着蜜香的夜色中寻找,找到了他的嘴唇。

-Got your meeting, have you? 你要去开会,对吗?
-Yes, I’m on my way. 是的,我正在去。

He pulled her towards him, his powerful, masculine hands tearing at the silk faric of her Gaultier nightshirt.

The repetitive beat of the innercity drum.

1887, Sebastian Lyddiard, greatgrandson of Herbert Lyddiard, who served under Sir John Jervis against Napoleon. 对抗拿破仑。

The Right Honourable William Lyddiard. 威廉·利迪亚德阁下。

-Do you ever regret marrying hime, Amy? I wonder if you ever think that but for you he might still be alive? 你后悔嫁给他吗,埃米?我不知道你是否想过,如果不是因为你,他可能现在还活着?
-(Amy) No. 没有想过。
-Well I do. 我想过。

-So we come to the question of which writer we invite to address us? 所以我们打算请哪位作家来给我们做演讲呢?
-How about Jilly Cooper?
-I think he said a writer.
-Do you think we could interest Frederick Forsythe?
-I rather doubt it.
-Oh, it’s a wonderful idea, Gerald. Especially since you’re writing a thriller. I think Frederick Forsythe would be first rate! 我认为Frederick Forsythe是第一流的。
-He’d never come. He’d just pretend he didn’t have time. They always do.
-I’d like Seamus Heaney.
-The poet?
-Not another poet. That last one was such a ghastly little man.

词汇

screenplay by 剧本由……写成
millimetre 毫米
phoenix 凤凰,这里是人名,菲尼克斯
spit 吐,唾
thug 恶棍,暴徒
twist 扭,拧,捻
brickwork 砖砌体,砖墙
splinter n. 碎片,弹片; v. 碎裂。
Uzi semiautomatic 乌兹半自动冲锋枪(Uzi submachine gun)。Uzi 冲锋枪是由以色列国防军 (IDF) 上尉(后来的少校)Uziel Gal 在 1948 年阿以战争后设计的。
sapphire 蓝宝石
honey 蜂蜜,蜜
scent 香味(a pleasant natural smell);scented 散发香味的
resist 抗拒的;unresisted 不可抗拒的。
masculine 男性的。
tear 撕,撕破,撕毁;拆;泪水。
nightshirt 睡衣
scum 浮渣
scum-bag 渣子袋,这里应该是“渣男”、“人渣”的意思,骂人的话。
hum 哼声
greatgrandson 曾孙
Sir 爵士
Napoleon 拿破仑
The Right Honourable 阁下。其中,Right是副词,意思是’very’ or ‘fully’。从语法上说,The Right Honourable 是形容短语,直接用来称呼人名或者某人用来自称都是不正确的,应该与其引用的名称或名称一起使用第三人称。
address 演讲
first rate 第一流
pretend 假装
ghastly US:/ˈɡæst.li/ UK:/ˈɡɑːst.li/ 阴森的,可怕的,非常坏的
bestseller 畅销书