Is the operating system a big liar?

1
Chat

Our programs lie quietly in a corner of the hard drive, waiting expectantly to be used by the owner. It is loaded by the operating system and then enters the memory to work, to be precise: It is executed by the CPU A-Gump.

Entering the memory is our mission. If we just stay on the hard disk, then we are just a bunch of binary code. Apart from occupying the hard disk space, there is nothing effect.

But the owner seems to be particularly fond of several of these programs, like browsers, QQ, Word, and players, more than 80% of the time It’s all consumed on them.

A small tool like me, calculator, is unknown and no one cares about. Apart from sleeping on the hard drive, it is chatting with helloworld in the same directory.

Helloworld is also very sad, since the owner created it, it has only been run once, and a hello world is output on the screen! No one will care about it anymore. .

But I am even more sad. I didn’t even have the chance to run it once. I once asked helloworld curiously, what it’s like to execute in memory, this silly bastard unexpectedly Say: Nothing feels like, the code runs out soon, and my program quit.

I ignored him again, and went to find the game man in the same directory. He entered the memory to run many times and was very knowledgeable.

Unexpectedly, he said angrily: “I tell you, if you want to enter the memory execution, you must load it through the operating system, but the operating system Just a big liar!”

“Why?”

“First, he Cooperating with CPU A-Gump, it creates an illusion that makes us think that every program can use the huge space of 3G, but in fact it is only virtual!  The memory we use is actually pitiful!”

(Note: This is a 32-bit Linux system)

“Secondly, he didn’t take your program all at once. Load physical memory, but unload you eight pieces, in his terminology, called page (page), and then paging into the memory on demand, note that it is not loaded continuously, Sometimes you load this piece first, and sometimes you load the other piece first. In the end, you don’t know where the various parts of your body are in the memory. It’s absolutely painful.”

“Third, you think Runtime monopolizes the CPU, don’t dream it. The operating system allocates time slices to let us these programs. No, to be precise, the processes are executed in rotation, plus a little process scheduling algorithm, which will kick you out from time to time. CPU. Since the various processes switch very quickly, it creates an illusion for humans, as if the various programs are executing at the same time. Do you think he is a big liar? “

The game man said that he was filled with indignation, I will be suspicious, but wait patiently for the day when it runs.

2
Loading

A great day has finally arrived.

The master typed in the calculator in the command line window, and I was asleep, and I was immediately awakened by the loader, and he said he was operating The system sent it to the memory to execute it for me.

I was full of joy, waiting for the loader to load me into the memory, but after waiting for a long time, nothing happened, I couldn’t help asking him: buddy, isn’t it Do I run into memory?

The loader said: “What’s the hurry, look at how you have never seen the world, don’t you know I’m creating a virtual address space for you?”

Sure enough! I’m going to create a virtual space for me.

“Are you busy copying my code and data into this virtual address space?” I asked deliberately.

“It’s really ignorant. This is virtual address space, not real memory. How can it be possible to put code and data?”  This loader has a bad temper.

I think this loader will at least load my code into physical memory, and then create a direct mapping between virtual memory and physical memory. So wait patiently.

But this loader did not do this. In fact, besides reading some of my Header information, he did not copy my data to the physical Going to the memory, what exactly is he going to do?

I asked: “If you don’t load my code into physical memory, how can I run it?”

< /p>

He said: “Don’t worry, I have used a data structure (page table) to record the location of your code/data on the hard disk, and it will be loaded when it is actually run.”< /p>

As he said, he gave me a picture: “Did you see the page table? The green one means it has been loaded into the memory, and the yellow one means it is still on the disk. In the initial state, it is all yellow, just like you.”

(Note: For simplicity, this picture does not reflect the combination of paragraphs and pages)

This grumpy loader finished the work, and found the entry point address of the program (assuming 0x080480c0) from my code. He said that when the process is executed, it will start from From here, read the first instruction.

3
Run

I realized that although I was still lying on the hard drive, the boss of the operating system had already established a process for me. This process has its own virtual address, page table and other “advanced” data structures, and is ready to run.

Sure enough, soon after, the operating system scheduled this process to run, starting from the program entry point 0x080480c0 returned by the loader.

The boss named the CPU A-Gump to fetch the instruction at 0x080480c0 for execution, but this is a virtual address and must be converted into a physical address.

So A-Gump went to check the page table and tried to turn it into an address in physical memory, but this page table pointed to an address in the hard disk, A-Gump Immediately report: “Boss, this is a new guy, and its code is still on the hard drive!”

“Okay, start the page fault handler immediately!” It seems that the boss has become commonplace.

The page fault processing program started to execute. According to the address in the page table, I was found on the hard disk again, and I cooperated with him to take the code away.

For the first time in my life, my code was finally read into the memory. Of course, Forrest Gump had to modify the page table to reflect The data has entered the memory:

Now The content at the virtual address 0x080480c0 can be read, the address of the physical memory is located through the translation of the page table, the instruction is taken out, and it can finally be executed!

As the instructions are executed, more and more data and codes are loaded into the physical memory. Sure enough, as the game guy said, I was taken apart. Inserted into different locations of physical memory.

< p>But what Brother Game said is not right. It’s not me, it’s just an incarnation of me. This avatar is a running process, and the CPU A-Gump keeps reading and writing data. When the time slice is up, the process will be suspended and run again later.

Finally, the process ends, and the data in the memory will be cleaned up and overwritten, but I am still me, playing well and lying on the hard drive without damage.

After experiencing this run, I understand that the operating system is indeed a big liar, but it is actually not easy. The resources are very limited, and the memory is only That big, there is only one CPU A-Gump, and there are so many programs. In order to make more programs run and make more effective use of memory and CPU, I can only use a little trick.

(End)

< p>What you see is just the tip of the iceberg. For more exciting articles, please go to “Coder’s turn to 2016 article essence” or “Coder turns over the essence of the first half of 2017 article

Have thoughts Everyone to share? Contributions are welcome! My contact information: WeChat: liuxinlehan QQ: 3340792577

/section>

Excellent talents do not lack job opportunities, they only lack good opportunities that suit them. But they often don’t have the energy to find the most suitable one from the vast opportunities.

< section style="max-width: 100%;display: inline-block;word-wrap: break-word !important;box-sizing: border-box !important;">

100offer will strictly screen the talents and companies on the platform, so that the “best talents” and “best companies” will meet.

Scan the QR code below , Sign up for 100offer and talk about your expectations for your next job. Within a week, receive 5-10 good opportunities to meet your requirements!

1

< section style="display: inline-block;padding: 5px 10px;color: rgb(93, 88, 31);font-weight: bold;border-top-right-radius: 3px;border-bottom-right-radius: 3px;background-color: rgb(209, 206, 171);box-sizing: border-box;" class="135brush" data-bgless="lighten" data-brushtype="text" data-bglessp="20% "> Chat

< section style="margin-top: 15px;margin-right: 0%;margin-left: 0%;">

These programs of us lie quietly in a corner of the hard disk, waiting expectantly to be used by the owner, loaded by the operating system, and then enter the memory to work, exactly Say: It was executed by CPU A-Gump.

Entering the memory is our mission. If we just stay on the hard disk, then we are just a bunch of binary code. Apart from occupying the hard disk space, there is nothing effect.

But the owner seems to be particularly fond of several of these programs, like browsers, QQ, Word, and players, more than 80% of the time It’s all consumed on them.

A small tool like me, calculator, is unknown and no one cares about. Apart from sleeping on the hard drive, it is chatting with helloworld in the same directory.

Helloworld is also very sad, since the owner created it, it has only been run once, and a hello world is output on the screen! No one will care about it anymore. .

But I am even more sad. I didn’t even have the chance to run it once. I once asked helloworld curiously, what it’s like to execute in memory, this silly bastard unexpectedly Say: Nothing feels like, the code runs out soon, and my program quit.

I ignored him again, and went to find the game man in the same directory. He entered the memory to run many times and was very knowledgeable.

Unexpectedly, he said angrily: “I tell you, if you want to enter the memory execution, you must load it through the operating system, but the operating system Just a big liar!”

“Why?”

“First, he Cooperating with CPU A-Gump, it creates an illusion that makes us think that every program can use the huge space of 3G, but in fact it is only virtual!  The memory we use is actually pitiful!”

(Note: This is a 32-bit Linux system)

“Secondly, he didn’t take your program all at once. Load physical memory, but unload you eight pieces, in his terminology, called page (page), and then paging into the memory on demand, note that it is not loaded continuously, Sometimes you load this piece first, and sometimes you load the other piece first. In the end, you don’t know where the various parts of your body are in the memory. It’s absolutely painful.”

“Third, you think you monopolize the CPU at runtime ,Stop dreaming, The operating system allocates time slices to let these programs, no, to be precise, the processes are executed in rotation, plus a little process scheduling algorithm, to kick you out of the CPU from time to time. Since the various processes switch very quickly, it creates an illusion for humans, as if the various programs are executing at the same time. Do you think he is a big liar? “

The game man said that he was filled with indignation, I will be suspicious, but wait patiently for the day when it runs.

2
Loading

A great day has finally arrived.

The master typed in the calculator in the command line window, and I was asleep, and I was immediately awakened by the loader, and he said he was operating The system sent it to the memory to execute it for me.

I was full of joy, waiting for the loader to load me into the memory, but after waiting for a long time, nothing happened, I couldn’t help asking him: buddy, isn’t it Do I run into memory?

The loader said: “What’s the hurry, look at how you have never seen the world, don’t you know I’m creating a virtual address space for you?”

Sure enough! I’m going to create a virtual space for me.

“Are you busy copying my code and data into this virtual address space?” I asked deliberately.

“It’s really ignorant. This is virtual address space, not real memory. How can it be possible to put code and data?”  This loader has a bad temper.

I think this loader will at least load my code into physical memory, and then create a direct mapping between virtual memory and physical memory. So wait patiently.

But this loader did not do this. In fact, besides reading some of my Header information, he did not copy my data to the physical Going to the memory, what exactly is he going to do?

I asked: “If you don’t load my code into physical memory, how can I run it?”

< /p>

He said: “Don’t worry, I have used a data structure (page table) to record the location of your code/data on the hard disk, and it will be loaded when it is actually run.”< /p>

As he said, he gave me a picture: “Did you see the page table? The green one means it has been loaded into the memory, and the yellow one means it is still on the disk. In the initial state, it is all yellow, just like you.”

(Note: For simplicity, this picture does not reflect the combination of paragraphs and pages)

This grumpy loader finished the work, and found the entry point address of the program (assuming 0x080480c0) from my code. He said that when the process is executed, it will start from From here, read the first instruction.

3
Run

I realized that although I was still lying on the hard drive, the boss of the operating system had already established a process for me. This process There is a set of “advanced” data structures such as its own virtual addresses, page tables, etc., which are ready to run.

Sure enough, soon after, the operating system scheduled this process to run, starting from the program entry point 0x080480c0 returned by the loader.

The boss named the CPU A-Gump to fetch the instruction at 0x080480c0 for execution, but this is a virtual address and must be converted into a physical address.

So A-Gump went to check the page table and tried to turn it into an address in physical memory, but this page table pointed to an address in the hard disk, A-Gump Immediately report: “Boss, this is a new guy, and its code is still on the hard drive!”

“Okay, start the page fault handler immediately!” It seems that the boss has become commonplace.

The page fault processing program started to execute. According to the address in the page table, I was found on the hard disk again, and I cooperated with him to take the code away.

For the first time in my life, my code was finally read into the memory. Of course, Forrest Gump had to modify the page table to reflect The data has entered the memory:

Now The content at the virtual address 0x080480c0 can be read, the address of the physical memory is located through the translation of the page table, the instruction is taken out, and it can finally be executed!

As the instructions are executed, more and more data and codes are loaded into the physical memory. Sure enough, as the game guy said, I was taken apart. Inserted into different locations of physical memory.

< p>But what Brother Game said is not right. It’s not me, it’s just an incarnation of me. This avatar is a running process, and the CPU A-Gump keeps reading and writing data. When the time slice is up, the process will be suspended and run again later.

Finally, the process ends, and the data in the memory will be cleaned and overwritten, but I am still me, playing well and lying on the hard drive without damage.

After going through this run, I understand that the operating system is indeed a big liar, but it is actually not easy. The resources are very limited, and the memory is only那么大,CPU阿甘只有一个,程序又那么多, 为了让更多的程序运行,更有效地利用内存和CPU, 也只能施展一点骗术了。 

(完)

你看到的只是冰山一角, 更多精彩文章,请移步《码农翻身2016文章精华》或者《码农翻身2017上半年文章精华

有心得想和大家分享?欢迎投稿 !我的联系方式:微信:liuxinlehan  QQ: 3340792577

优秀人才不缺工作机会,只缺适合自己的好机会。但是他们往往没有精力从海量机会中找到最适合的那个。

100offer 会对平台上的人才和企业进行严格筛选,让「最好的人才」和「最好的公司」相遇。

扫描下方二维码,注册 100offer,谈谈你对下一份工作的期待。一周内,收到 5-10 个满足你要求的好机会!

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 5389 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.