当前位置:网站首页>Why is network i/o blocked?
Why is network i/o blocked?
2022-07-07 22:53:00 【Yes' level training strategy】
Hello , I am a yes.
I'm going to export Netty Related articles , But learn more Netty This underlying communication framework , Network related knowledge points are indispensable . So I'm going to write some pre knowledge points first , Align and meet , So that later on Netty The understanding of the .
We should all know socket( Socket ), You can think our communications are based on this thing , Network communication is often divided into TCP And UDP Two kinds of , Next I will use TCP Take communication as an example to illustrate socket Communication process of .
But before that , Let me first talk about what is I/O.
I/O What is it ?
I/O In fact, that is input and output Abbreviation , The input / Output .
What about input and output ?
For example, typing code with the keyboard is actually typing , The display pattern is output , This is actually I/O.
And the disk we often care about I/O It refers to the input and output between hard disk and memory .
When reading local files , To copy the data from the disk to memory , When modifying local files , You need to copy the modified data to disk .
The Internet I/O It refers to the input and output between the network card and memory .
When the data on the network comes , The network card needs to copy the data to memory . When you want to send data to others on the network , You need to copy the data from memory to the network card .
Then why interact with memory ?
Our instructions are ultimately given by CPU Executive , The reason is CPU Interacting with memory is much faster than CPU The speed of direct interaction with these external devices .
So they all interact with memory , Of course, suppose there is no memory , Give Way CPU Interact directly with external devices , That counts I/O.
Sum up :I/O It refers to the interaction between memory and external devices ( Data copy ).
Okay , Clarify what is I/O after , Let's uncover socket Communication insider ~
establish socket
First, the server needs to create a socket. stay Linux Everything in is a document , So created socket It's also a document , Each file has an integer file descriptor (fd) To refer to this file .
int socket(int domain, int type, int protocol);
- domain: This parameter is used to select the protocol family of communication , Such as choice IPv4 signal communication , still IPv6 Communications, etc
- type: Select socket type , Optional byte stream socket 、 Datagram socket, etc .
- protocol: Specify the protocol used .
This protocol It can usually be set to 0 , Because the protocol to be used can be inferred from the first two parameters .
such as socket(AF_INET, SOCK_STREAM, 0);
, Indicate the use of IPv4 , And use byte stream socket , It can be judged that the protocol used is TCP agreement .
The return value of this method is int , It's actually created socket Of fd.
bind
Now we have created a socket, But there is no address pointing to this socket.
as everyone knows , The server application needs to indicate IP And port , In this way, the client can come to the door and ask for service , So at this point, we need to specify an address and port to communicate with this socket Bind it .
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
In the parameter sockfd That's what we created socket File descriptor for , Yes bind After the parameter, our socket It's a step closer to being accessible .
listen
Yes socket、bind after , At this time socket Is still in the closed The state of , That is, not listening to the outside , Then we need to call listen Method , Give Way socket Get into Passive monitoring state , In this way socket Can listen to the connection request of the client .
int listen(int sockfd, int backlog);
Pass in the created socket Of fd, And point out backlog Size .
This backlog When I look up the data , See three explanations :
- socket There is a queue , Store the completed connection and semi connection at the same time ,backlog For the size of this queue .
- socket There are two queues , They are completed connection queue and semi connection queue ,backlog Is the sum of the sizes of the two queues .
- socket There are two queues , They are completed connection queue and semi connection queue ,backlog Only for completed connection queue size .
Explain what a semi connection is
We all know TCP Establishing a connection requires three handshakes , When the receiver receives the connection request from the requestor, it will return ack, At this point, the connection is in a semi connected state at the receiver , When the receiving party receives another request from the requesting party ack when , The connection is in the completed state :
Therefore, the above discussion is about the storage of connections in these two states .
I looked up the information and saw , be based on BSD The implementation of the derived system uses a queue to store connections in both states at the same time , backlog The parameter is the size of the queue .
and Linux Two queues are used to store completed connections and half connections respectively , And backlog Queue size for completed connections only
accept
Now we have initialized the listening socket , At this point, a client will connect , Then we need to deal with these connections that have been established .
From the above analysis, we can know , After three handshakes, the connection will be added to the completed connection queue .
Now , We need to get the connection from the completed connection queue for processing , This take is done by accpet To complete .
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
This method returns int The value is the value of the completed connection socket File descriptor for , Then operate this socket You can communicate .
If the connection queue has been completed and there is no connection, you can get , So called accept Your thread will Block waiting .
So far, the communication process of the server has come to an end , Let's look at the operation of the client .
connect
The client also needs to create a socket, That is to call socket()
, I won't repeat it here , Let's start the company building operation directly .
The client needs to establish a connection with the server , stay TCP Start the classic three handshakes under the protocol , Look at the picture above :
The client has been created socket And call connect
after , The connection is in SYN_SEND
state , When you receive a message from the server SYN+ACK
after , The connection becomes ESTABLISHED
state , This means that three handshakes are over .
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
call connect
You need to specify the remote address and port for connection , After three handshakes, you can start communicating .
The client side does not need to call bind operation , The source will be selected by default IP And random port .
Use a picture to summarize the operation of building a company :
You can see two blocking points here :
- connect: Need to wait for the completion of three handshakes .
- accept: Need to wait for available completed connections , If the completed connection queue is empty , Is blocked .
read、write
After the connection is established successfully , You can start sending and receiving messages , Let's take a look
read For reading data , From the server side, it is waiting for the client's request , If the client does not send a request , So called read Will be in a blocked waiting state , No data to read , This should be well understood .
write Write data for , Generally speaking, after the server accepts the client's request , Will do some logical processing , Then return the result to the client , This write may also be blocked .
Someone here may ask read Can't read data, blocking waiting is understandable ,write Why block , Don't you send the data directly ?
Because we use TCP agreement ,TCP The protocol needs to ensure that the data is reliable 、 Transmit in an orderly manner , And give end-to-end flow control .
So sending is not sending directly , It has a Send buffer , We need to copy the data to TCP Send buffer for , from TCP Self control the sending time and logic , There may be retransmissions or something .
If we send too fast , As a result, the receiver cannot handle it , Then the receiver will pass TCP Agreement to inform : Stop it ! I'm too busy . There is a size limit for the send buffer , Unable to send due to , And keep calling write Then the cache is full , It's full, or you write 了 , therefore write Blocking can also occur .
Sum up ,read and write There's going to be a blockage .
Last
Why the Internet I/O Will be blocked ?
Because the establishment and communication involve accept、connect、read、write These methods may block .
Blocking will occupy the currently executing thread , Make it impossible for other operations , And frequent blocking of wake-up switching context will also lead to performance degradation .
Due to blocking , The initial solution was to establish multiple threads , But with the development of the Internet , User surge , The number of connections also surged , The number of threads that need to be established increases with , Later, there was C10K problem .
The server can't stand it , To do ?
Optimize chant !
So later I got a non blocking socket , then I/O Multiplexing 、 Signal driven I/O、 asynchronous I/O.
In the next chapter, let's make a good deal of it , These kinds of I/O Model !
Reference resources :
https://blog.csdn.net/yangbodong22011/article/details/60399728
I am a yes, From a little bit to a billion , See you next time !
边栏推荐
- 新版代挂网站PHP源码+去除授权/支持燃鹅代抽
- 行測-圖形推理-4-字母類
- 微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
- Line test - graphic reasoning -5- one stroke class
- Get the exact offset of the element
- Line test - graphic reasoning -7- different graphic classes
- Debezium系列之:源码阅读之BinlogReader
- Failed to initialize rosdep after installing ROS
- Matplotlib quick start
- Robot autonomous exploration DSVP: code parsing
猜你喜欢
LeetCode707. Design linked list
Leetcode94. Middle order traversal of binary trees
Line test - graphic reasoning -7- different graphic classes
LeetCode144. Preorder traversal of binary tree
Line test - graphic reasoning - 6 - similar graphic classes
Visual studio 2019 installation
Signal feature extraction +lstm to realize gear reducer fault diagnosis -matlab code
Sword finger offer 28 Symmetric binary tree
Yarn cannot view the historical task log of yarn after enabling ACL user authentication. Solution
Firefox browser installation impression notes clipping
随机推荐
[interview arrangement] 0211 game engine server
Qt Graphicsview图形视图使用总结附流程图开发案例雏形
Apple further entered the financial sector through the 'virtual card' security function in IOS 16
Revit secondary development - Hide occlusion elements
行测-图形推理-6-相似图形类
Typeorm automatically generates entity classes
Two methods of calling WCF service by C #
Visual studio 2019 installation
Debezium系列之:源码阅读之SnapshotReader
Debezium系列之:支持 mysql8 的 set role 語句
UWA问答精选
C # Development -- pit encountered in JS intermodulation
微服務遠程Debug,Nocalhost + Rainbond微服務開發第二彈
Failed to initialize rosdep after installing ROS
How pyGame rotates pictures
Xcode modifies the default background image of launchscreen and still displays the original image
Revit secondary development - collision detection
行测-图形推理-8-图群类
How to judge whether the input content is "number"
新版代挂网站PHP源码+去除授权/支持燃鹅代抽