In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > IT Information >
Share
Shulou(Shulou.com)11/24 Report--
This article comes from the official account of Wechat: low concurrency programming (ID:dibingfa). Author: flash.
I am a process on a Linux server called Xiaojin.
People always say that I can only create 65535 TCP connections at the most.
I don't believe in this evil. I'm going to practice it myself today.
I went up to the boss of the operating system and said:
"Fuck, I'm going to set up a TCP connection!"
Lao Cao took his time, took out a form and handed it to me, "Xiao Jin, fill in the form first."
As soon as I look at this watch, this is the classic socket quad. I have only one network card whose IP address is 123.126.45.68. I want to establish a TCP connection with port 80 of 110.242.68.3. I filled in this information in the table.
What should I fill in the source port number? I remember that the port number is 16 digits, and there can be a number in the range of 0 ~ 65535, so I'll choose one at random.
Just when I was hesitant about which number to choose, the old fuck grabbed my form.
"what kind of ink do you have, Xiao Jin? you don't have to fill in the source port number. I'll assign you an available number. You don't have to fill in the source IP. I know which network cards are available and will help you choose the right one. What a novice. Go back and wait for the news."
"Oh."
Lao Fang took my form and left.
After a long time, Lao Cao finally came back with a note.
"Xiao Jin, you put this away."
I asked, "what is this?"
Lao Cao said impatiently, "just said you are a novice and you are not satisfied. This 5 means the file descriptor, everything under linux is a file. When you communicate with your target IP on TCP later, just read and write to this file descriptor."
"it's so convenient! all right, thank you, old fuck."
I took this file descriptor and framed it in my memory. Anyway, I just wanted to see how many TCP connections I could create, not to actually use it.
A minute after the port number, I went to the old fuck again.
"Fuck, I'm going to set up a TCP connection!"
Lao Cao took his time, took out a form and handed it to me, "Xiao Jin, fill in the form first."
This time I am familiar with it, just fill in the target IP and target port.
After the old operator did something good, he came back with a note with the number "6" written on it.
In this way, I go to Lao Cao to establish a new TCP connection every minute. The target IP is 110.242.68.3 and the target port is 80.
Lao Cao is also very strange. I don't know what I'm messing about here. although he has great power, he has no right to refuse my instructions. he does things conscientiously every time, and gives me a note with a document descriptor.
Until one time, the note I received was a little different.
I asked with a little reproach, "Fuck, what's going on?"
Lao Cao said angrily, "this means that the port number is not enough!" I thought something was wrong with you long ago. I kept creating a TCP connection to the same IP and port. I had no way to execute the instructions you gave me before, but now I can't, the port number is not enough, I can't fill it out for you at the source port. "
I am not so gullible, questioned. "Fuck, don't bully me as a novice. I know that the port number is 16 digits, with a range of 1mm 65535. A total of 65535 TCP connections can be created. Now I have only created 63977. How come it's not enough!"
Lao Cao looked at me contemptuously. "you are really idle, and you really count one by one. Let me tell you that Linux has specific restrictions on the range of ports that can be used, which can be checked with the following command."
[root] # cat / proc/sys/net/ipv4/ip_local_port_range 1024 65000 "see, the current limit is 1024065, 000, so you can only use 63977 port numbers."
I hastened to apologize to Lao Cao, "Oh, I'm so sorry, or I have too little knowledge. Can I change this number?"
Lao Cao didn't have the same knowledge as me, so he answered me patiently, "Yes, you can modify it in the vim / etc/ sysctl.conf file, and we'll add a line of records to this file."
Net.ipv4.ip_local_port_range = 60000 60009 "execute sysctl-p / etc/ sysctl.conf to make it effective after saving it. In this way, you only have 10 port numbers available, which will add to KuaiBao's error that the port number is not enough."
"I see. Thank you, Lao Cao, for teaching me another lesson."
Alas, to establish a TCP connection, you need to bind the socket (socket) at both ends of the communication, as follows:
Source IP address: source port number destination IP address: destination port number
As long as the quad of this binding relationship is not repeated, the port number is not enough, because I have been establishing a connection to the same destination IP and port, so I will try another destination port number.
I gave this watch to Lao Cao, who saw through my mind at a glance, but there was no way to establish a new TCP connection for me immediately and successfully returned a new file descriptor note to me.
It seems to be successful, as long as the source port number is not enough, keep changing the destination IP and destination port number, to ensure that the quad is not repeated, I can create a lot of TCP connections!
It also proves how absurd it is to say that you can only create a maximum of 65535 TCP connections.
The file descriptor found a way to break through the port number limit. I kept looking for Lao Cao to establish a TCP connection, but Lao Cao couldn't do anything about it.
Until one time, I received a special note that was not a document descriptor.
I asked the old man angrily, "what's going on?"
The old fuck gloated and told me, "Oh, you think it's lawless to break through the port number limit? now there are not enough file descriptors!"
"Why are there restrictions on everything? your operating system gives us too many restrictions, right?"
"nonsense, look how many TCP connections you have made! every time I make a TCP connection, I have to assign you a file descriptor. Linux limits the number of file descriptors that can be opened in three ways."
System level: the maximum number that can be opened by the current system, which can be viewed at cat / proc/ sys / fs / file-max
User level: specifies the maximum number of users can open, which can be viewed through cat / etc/ security / limits.conf
Process level: the maximum number of openings that can be opened by a single process, which can be viewed at cat / proc/ sys / fs / nr_open
Oh, my God, people are really under the eaves. I hastened to look at these specific restrictions.
[root ~] # cat / proc/sys/fs/file-max100000 [root ~] # cat / proc/sys/fs/nr_ open100000[ root ~] # cat / etc/security/limits.conf* soft nproc 100000* hard nproc 100000 I remember the last note I just received.
After that, you get an error that the file descriptor is not enough.
I also asked Lao Cao, "Fuck, can this limit be changed?"
Lao Cao still told me patiently, "of course, for example, if you want to change the maximum file descriptor that a single process can open to 100, you can do so."
Echo 100 / proc/sys/fs/nr_open "I see. I'm going to make all kinds of file descriptor restrictions a little bigger, not much, just add a 0 at the end."
"well, I knew I wouldn't tell you, boy." Lao Cao looked at me with contemptuous eyes again.
Threads broke the file descriptor limit, and I began to create TCP connections unscrupulously.
But I found that the efficiency of Lao Cao is getting slower and slower, and it takes longer and longer to set up a TCP connection.
Once, I couldn't help asking Lao Cao, "are you lazy? it took less than a minute to build a TCP connection from you. Can you see which time I haven't waited for more than an hour recently?"
Lao Cao couldn't help it. "Xiao Jin, you still have the nerve to say that I, do you know that every TCP connection you build requires a thread to serve you? now boss CPU and I are very busy there, constantly switching contexts for hundreds of thousands of threads like you. Our energy is limited, so we naturally can't serve you as quickly as before."
After listening to the old fuck's complaint, I remembered that someone seemed to have told me about the C10K problem before, that is, when the number of server connections reached 10,000 and each connection consumed a thread resource, the operating system would be constantly busy with thread context switching. it eventually leads to a system crash, which is no joke.
I hurriedly asked the boss of the operating system, "Fuck, I'm really sorry. I always thought you were so strong, but I didn't expect that you were so busy, so what should we do now?"
Lao Cao said helplessly, "I advise you to stop playing, it doesn't make any sense, but I don't think you will listen to me, so let me say something to you."
Your current way of creating a thread for every TCP connection is the most traditional multithreaded concurrency model, which was only supported by early operating systems. But now that I've evolved, I also support IO multiplexing, which simply means that a thread can manage resources for multiple TCP connections, so you can manage a large number of TCP connections with a small number of threads.
I wondered, "what is IO multiplexing?" .
The old fuck looked disdainful, "you, you... you go to see the flash," you call this stupid thing IO multiplexing, and you'll see. "
This is really an eye-opener, I quickly changed the code to this IO multiplexing model, destroyed the original TCP connection, changed to the same thread to manage multiple TCP connections, soon, the operating system boss restored the previous efficiency, at the same time, I have more TCP connections.
Memory broke through the port number, file descriptors, the number of threads and other restrictions, I once again unscrupulously created a TCP connection.
Until once, I received another red card.
Hey, what kind of thing is restricted again? if you change it, it's over. I asked the old man impatiently, "what's the problem this time?"
Lao Cao said. "this error is called memory overflow, each TCP connection itself, as well as the buffer used in this connection, all need to take up a certain amount of memory, now you have full memory, not enough, so reported this error."
I think Lao Cao was very patient this time and didn't say much, but I thought I was limited by memory, so I was a little unhappy, so I asked Lao Cao to do me one last favor.
"Fuck, do Xiaojin one last favor. You have a lot of power. Let's kill those processes that take up a lot of memory and make room for me. I'm going to finish my dream today and see how many TCP connections can be created!"
Lao Cao saw that I was really hard enough, so he promised me and killed a lot of progress. I was very moved.
CPU has the memory resources that Lao Cao fought for for me, and I began to create TCP connections day after day.
Lao Cao no longer said anything, but also carried out my instructions day after day.
Once, Lao Cao said to me earnestly, "almost, I advise you to stop here, now the occupancy rate of CPU is almost 100%."
I think Lao Fang is really ridiculous. after these small setbacks, I understand that as long as there is no decline in thinking, there are always more methods than suffering. Lao Fang is just too cautious. How can I give up halfway and ignore him?
I continue to create TCP connections.
Until one day, Lao Cao invited me to a small restaurant, had a meal together, and said after eating. "We have been working together for a long time, and today I have come to say goodbye to you."
I was puzzled and asked, "what's the matter, fuck? what's going on?"
Lao Cao said, "because of your TCP connection, the CPU occupancy rate has been maintained at 100% for a long time, and our users, that is, our God, can hardly do anything. They even have to wait a long time to move the mouse, so he gave me a restart instruction. After I executed this instruction, you, and all processes like you, including my operating system itself, everything disappeared."
I was shocked. "Oh, is it so sudden? when will this instruction be carried out?"
Lao Cao slowly got up and said, "right now, this instruction hasn't been given a chance to run CPU yet, but now it's time."
Suddenly, my eyes went dark and everything was gone.
Summary
Resources
Resources for a Linux server
Resources consumed by an TCP connection
What happens when it's full?
CPU
Let's see how much you paid for it.
See what you're doing with it.
The computer is stuck.
Memory
Let's see how much you paid for it.
Depends on the buffer size
OOM
Temporary port number
Ip_local_port_range
one
Cannot assign requested address
File descriptor
Fs.file-max
one
Too many open files
Number of processes\ threads
Ulimit-n
Look at the IO model.
System crash
Postscript
In fact, I don't think the conclusion is important on this issue. The most important thing is the process of thinking.
The thinking process is actually quite simple, that is, looking for restrictions. In fact, at the beginning of this article, I wrote a story at the beginning, but later I felt that it was more appropriate to put it in the postscript. Here's the story.
Flash: Xiao Yu, let me ask you, how many hamburgers can you eat at most in a day?
Xiao Yu: well, that's too personal, but for the sake of teaching me skills, I'll tell you that you can eat about four at most.
Flash: ahem. Really? Well, how many hamburgers can you eat at most in a minute?
Xiao Yu: maybe two fast, but normally you should be able to eat up to one.
Flash: OK, let me ask you, why can you answer these two questions without thinking?
Xiao Yu: , of course I understand what you are talking about.
Flash: no, think about the logic of your answer to these two questions.
Xiao Yu: Oh, I see what you mean. When you ask me how many hamburgers I can eat a day, I think about how many hamburgers my stomach can hold. When you ask me how many hamburgers I can eat in a minute, I think about the speed at which I can eat hamburgers and how many hamburgers I can eat in a minute at that rate.
Flash: yes, you summed it up very well! The maximum number of hamburgers to eat a day is very abundant, so it is mainly the capacity of the stomach that limits the maximum value of the hamburger. The formula should be:
Maximum number of hamburgers = volume of stomach / volume of hamburger
The maximum number of hamburgers to eat in a minute is very abundant, and the maximum value of the hamburger is limited by the time factor. The formula is:
Maximum number of hamburgers = one minute / time to eat a hamburger
So, it depends on the constraint that is first reached.
And the question of the maximum number of TCP connections, if the interview is asked, even if you do not know it at all, you should have this way of thinking.
And if you have this way of thinking, you can more or less answer to the satisfaction of the interviewer, because computers often value ideas rather than details.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.