<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Rxsi Blog</title>
    <description></description>
    <link>https://rxsi.github.io/</link>
    <atom:link href="https://rxsi.github.io/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Tue, 07 Mar 2023 02:52:03 +0000</pubDate>
    <lastBuildDate>Tue, 07 Mar 2023 02:52:03 +0000</lastBuildDate>
    <generator>Jekyll v3.9.3</generator>
    
      <item>
        <title>Python属性查找</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#两个问题&quot; id=&quot;markdown-toc-两个问题&quot;&gt;两个问题&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;两个问题&quot;&gt;两个问题&lt;/h1&gt;
今天在公司有同事提出了一个问题：
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;FuncA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FuncA&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FuncA&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
以上代码是否会造成内存泄漏问题？

另外根据这个问题涉及到的相关特性，再思考下这个问题：
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Descr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_instance&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_val&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__get__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;owner&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;descr.__get__&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_instance&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;instance&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_val&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__set__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;descr.__set__&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_instance&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;instance&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_val&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Descr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
以上代码是否会造成内存泄漏？如果把&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__set__&lt;/code&gt;去掉呢？

</description>
        <pubDate>Tue, 17 Jan 2023 09:18:23 +0000</pubDate>
        <link>https://rxsi.github.io/2023/01/17/python_attribute_find/</link>
        <guid isPermaLink="true">https://rxsi.github.io/2023/01/17/python_attribute_find/</guid>
        
        <category>属性查找</category>
        
        <category>源码分析</category>
        
        
        <category>Python</category>
        
      </item>
    
      <item>
        <title>提升函数内部导入性能</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#为什么需要在函数内部导入&quot; id=&quot;markdown-toc-为什么需要在函数内部导入&quot;&gt;为什么需要在函数内部导入&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;为什么需要在函数内部导入&quot;&gt;为什么需要在函数内部导入&lt;/h3&gt;
一般来说，我们都是在文件的头部进行导入，常见的形式有：
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;A&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Func&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
这两种导入方式对应的底层逻辑是一致的。

如果我们导入的是模块名，那么当文件被加载时会根据模块名从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sys.modules&lt;/code&gt;中查询，如果对应的模块还未被初始化，那么首先就会进行模块的初始化。如果模块已经初始化完，那么就会把对应模块对象的引用拷贝当前的名字空间，即拷贝到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;globals()&lt;/code&gt;字典中，对应模块对象的引用计数会 + 1。后续当我们再从当前文件使用导入的模块时会直接从名字空间中查找，也就是说引用的拷贝只会发生在文件的第一次加载，因此具有较高的效率。

而如果我们导入的是模块中的类/函数/属性等，那么会先根据模块名查找到模块对象，再从模块对象的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__dict__&lt;/code&gt;数据结构中查找到目标类对象/函数对象，然后把这个类对象/函数对象的引用拷贝到当前名字空间。后续再使用导入的类/函数时也不会再次导入，因此也是具有较高的效率。

</description>
        <pubDate>Thu, 24 Nov 2022 09:34:49 +0000</pubDate>
        <link>https://rxsi.github.io/2022/11/24/python_import_in_function/</link>
        <guid isPermaLink="true">https://rxsi.github.io/2022/11/24/python_import_in_function/</guid>
        
        <category>模块导入</category>
        
        
        <category>Python</category>
        
      </item>
    
      <item>
        <title>多进程/线程读写文件问题</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#进程线程打开文件&quot; id=&quot;markdown-toc-进程线程打开文件&quot;&gt;进程/线程打开文件&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;进程线程打开文件&quot;&gt;进程/线程打开文件&lt;/h1&gt;
首先需要关注的是 linux 系统的文件系统结构，当我们使用 open() 函数打开一个文件时，底层会创建如下的文件结构：

&lt;img src=&quot;/images/io_read_write_file/open_one_file.png&quot; alt=&quot;open_one_file.png&quot; /&gt;

在每个进程的 task_struct 结构中，包含有文件描述符表，int 类型的 fd 就是这个表的下标位置。一个文件每被打开一次，系统就会为其创建一个 file 结构，用以标识连接信息，主要包含文件状态（可读、可写、可读写），当前文件的偏移量，引用计数，指向 inode 的指针等。使用 fork() 可以复制父进程的文件描述符，父子进程会共用 file 结构， file 结构会增加引用计数。inode 是对底层不同文件模式的一种抽象，以提供统一的操作方式。如下图所示：

</description>
        <pubDate>Wed, 19 Oct 2022 08:02:33 +0000</pubDate>
        <link>https://rxsi.github.io/2022/10/19/io_read_write_file/</link>
        <guid isPermaLink="true">https://rxsi.github.io/2022/10/19/io_read_write_file/</guid>
        
        <category>文件读写</category>
        
        
        <category>IO技术</category>
        
      </item>
    
      <item>
        <title>bind源码解析</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#bind-系统调用&quot; id=&quot;markdown-toc-bind-系统调用&quot;&gt;bind 系统调用&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#__sys_bind&quot; id=&quot;markdown-toc-__sys_bind&quot;&gt;__sys_bind&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;bind-系统调用&quot;&gt;bind 系统调用&lt;/h2&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bind&lt;/code&gt;函数在用户层的函数签名如下：
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sockfd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sockaddr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;myaddr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;socklen_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addrlen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt;/*
int sockfd：通过socket()函数申请的套接字
const struct sockaddr *myaddr：本地端地址信息结构体，这里使用的是sockaddr_in结构体去转换为sockaddr结构体类型。其中端口号如果绑定为0，则意味着让系统帮我们自动分配一个，一般来说我们会主动分配一个确定的端口号，只有在发起connect时才会让系统替我们自动分配
socklen_t addrlen：结构体的长度，通过sizeof()计算即可

返回值：成功返回0，失败返回-1
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
对应的系统函数是：
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;SYSCALL_DEFINE3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sockaddr&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;umyaddr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addrlen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__sys_bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;umyaddr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addrlen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h2 id=&quot;__sys_bind&quot;&gt;__sys_bind&lt;/h2&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__sys_bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sockaddr&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;umyaddr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addrlen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sockaddr_storage&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fput_needed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;sock&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sockfd_lookup_light&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fput_needed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 根据fd找到socket结构体&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c1&quot;&gt;// .....&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sock&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ops&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                              &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sockaddr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                              &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addrlen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 调用的是&amp;amp;inet_stream_ops中的inet_bind函数&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;fput_light&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sock&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fput_needed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

</description>
        <pubDate>Fri, 23 Sep 2022 09:38:33 +0000</pubDate>
        <link>https://rxsi.github.io/2022/09/23/tcp_bind/</link>
        <guid isPermaLink="true">https://rxsi.github.io/2022/09/23/tcp_bind/</guid>
        
        <category>源码分析</category>
        
        
        <category>TCP</category>
        
      </item>
    
      <item>
        <title>握手&amp;挥手源码解析:第四次挥手</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#第四次挥手&quot; id=&quot;markdown-toc-第四次挥手&quot;&gt;第四次挥手&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;第四次挥手&quot;&gt;第四次挥手&lt;/h2&gt;
此时的客户端正处于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FIN_WAIT_2&lt;/code&gt;状态（有维持时间上限），在接收到了服务端的第三次挥手后，就会进入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TIME_WAIT&lt;/code&gt;状态

</description>
        <pubDate>Mon, 19 Sep 2022 11:57:37 +0000</pubDate>
        <link>https://rxsi.github.io/2022/09/19/tcp_wave_4/</link>
        <guid isPermaLink="true">https://rxsi.github.io/2022/09/19/tcp_wave_4/</guid>
        
        <category>四次挥手</category>
        
        <category>源码分析</category>
        
        
        <category>TCP</category>
        
      </item>
    
      <item>
        <title>握手&amp;挥手源码解析:第三次挥手</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#第三次挥手&quot; id=&quot;markdown-toc-第三次挥手&quot;&gt;第三次挥手&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;第三次挥手&quot;&gt;第三次挥手&lt;/h2&gt;
服务端的第三次挥手是在调用了 close/shutdown 函数之后发出的，这里的逻辑过程是和第一次挥手类似的，借助 tcp_close_state 函数使 TCP 的状态由&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CLOSE_WAIT&lt;/code&gt;转变为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LAST_ACK&lt;/code&gt;状态，并发出&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FIN&lt;/code&gt;报文。

</description>
        <pubDate>Sat, 17 Sep 2022 12:20:12 +0000</pubDate>
        <link>https://rxsi.github.io/2022/09/17/tcp_wave_3/</link>
        <guid isPermaLink="true">https://rxsi.github.io/2022/09/17/tcp_wave_3/</guid>
        
        <category>四次挥手</category>
        
        <category>源码分析</category>
        
        
        <category>TCP</category>
        
      </item>
    
      <item>
        <title>握手&amp;挥手源码解析:第二次挥手</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#第二次挥手&quot; id=&quot;markdown-toc-第二次挥手&quot;&gt;第二次挥手&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#tcp_fin&quot; id=&quot;markdown-toc-tcp_fin&quot;&gt;tcp_fin&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;第二次挥手&quot;&gt;第二次挥手&lt;/h2&gt;
&lt;h3 id=&quot;tcp_fin&quot;&gt;tcp_fin&lt;/h3&gt;
在服务端进入稳定状态之后，处理数据包的顶层函数为 tcp_rcv_established ，其中该函数根据一些条件划分为了快处理和慢处理两种方式，再通过 tcp_data_queue 放入队列，当该数据包是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FIN&lt;/code&gt;包，那么会进入 tcp_fin 函数：

</description>
        <pubDate>Wed, 14 Sep 2022 12:40:10 +0000</pubDate>
        <link>https://rxsi.github.io/2022/09/14/tcp_wave_2/</link>
        <guid isPermaLink="true">https://rxsi.github.io/2022/09/14/tcp_wave_2/</guid>
        
        <category>四次挥手</category>
        
        <category>源码分析</category>
        
        
        <category>TCP</category>
        
      </item>
    
      <item>
        <title>握手&amp;挥手源码解析:第一次挥手</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#第一次挥手&quot; id=&quot;markdown-toc-第一次挥手&quot;&gt;第一次挥手&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;第一次挥手&quot;&gt;第一次挥手&lt;/h2&gt;
在 TCP 中有两个函数可以关闭连接，分别是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;close&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shutdown&lt;/code&gt;

</description>
        <pubDate>Tue, 13 Sep 2022 12:33:32 +0000</pubDate>
        <link>https://rxsi.github.io/2022/09/13/tcp_wave_1/</link>
        <guid isPermaLink="true">https://rxsi.github.io/2022/09/13/tcp_wave_1/</guid>
        
        <category>四次挥手</category>
        
        <category>源码分析</category>
        
        
        <category>TCP</category>
        
      </item>
    
      <item>
        <title>握手&amp;挥手源码解析:第三次握手</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#第三次握手&quot; id=&quot;markdown-toc-第三次握手&quot;&gt;第三次握手&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#tcp_rcv_synsent_state_processy&quot; id=&quot;markdown-toc-tcp_rcv_synsent_state_processy&quot;&gt;tcp_rcv_synsent_state_processy&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;第三次握手&quot;&gt;第三次握手&lt;/h2&gt;
&lt;h3 id=&quot;tcp_rcv_synsent_state_processy&quot;&gt;tcp_rcv_synsent_state_processy&lt;/h3&gt;
当客户端接收到了第二次握手包后，就进入第三次握手判断阶段，此时客户端处于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SYN_SENT&lt;/code&gt;阶段

</description>
        <pubDate>Sat, 10 Sep 2022 11:32:44 +0000</pubDate>
        <link>https://rxsi.github.io/2022/09/10/tcp_handshake_3/</link>
        <guid isPermaLink="true">https://rxsi.github.io/2022/09/10/tcp_handshake_3/</guid>
        
        <category>三次握手</category>
        
        <category>源码分析</category>
        
        
        <category>TCP</category>
        
      </item>
    
      <item>
        <title>握手&amp;挥手源码解析:第二次握手</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#第二次握手&quot; id=&quot;markdown-toc-第二次握手&quot;&gt;第二次握手&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;第二次握手&quot;&gt;第二次握手&lt;/h2&gt;
要进入第二次握手的处理流程，服务端必须是先接收到了第一次握手包，此时的处理流程如下：

</description>
        <pubDate>Fri, 09 Sep 2022 12:02:34 +0000</pubDate>
        <link>https://rxsi.github.io/2022/09/09/tcp_handshake_2/</link>
        <guid isPermaLink="true">https://rxsi.github.io/2022/09/09/tcp_handshake_2/</guid>
        
        <category>三次握手</category>
        
        <category>源码分析</category>
        
        
        <category>TCP</category>
        
      </item>
    
  </channel>
</rss>
