Entire book memory allocation Is the key point. IO and CPU work together It is difficult. Process Synchronization It is difficult.

process

Differences between processes and threads

  • Process is the resource allocation unit, and thread is the calling unit
  • Threads share process resources, shared address space, and exclusive stack space

Method of process scheduling

FCFS (first come, first serve), priority, time slice rotation, multi-level feedback

Process state transition

Ready, Execute, Block

  • The time slice is exhausted, and the process runs from execution to be ready (instead of blocking)
  • It must be defect Resources for continued implementation
  • The process must From Ready Can be converted to execution status

Process Synchronization

The purpose of process synchronization is When the process that currently accesses critical resources is switched to another process by the system scheduling, ensure that critical resources are not accessed by other processes.

Methods: Semaphore, off interrupt, hardware instruction

memory allocation

First see memory allocation Four words, you can know what this is about.
What is memory used for? To run a job, it must be loaded into memory. There are two problems:

  • Is the operation to be loaded in whole, discrete, discrete, all or only the part needed at the beginning?
  • Whether the memory is fixed or dynamically changeable (partitions can be split and merged)

Based on these two problems, there are different memory allocation algorithms.

Fixed partition allocation

Memory is divided into N blocks. Each block can be different in size, but it is fixed and cannot be changed.

Dynamic partition allocation

The dynamic is that the size of memory blocks can be changed.

At first, the memory is not partitioned. task Divide as big as needed

But when the job is recycled, a free block will be left in this place. Over time, there will be many free blocks in the whole memory. You can use a Free partition linked list Connect these spare partitions. When a new job is loaded into memory, this involves Idle partition allocation algorithm Problems.

  • First adaptation , by address, from the beginning
  • Next adaptation , by address, starting from the last allocated free partition address
  • Optimal adaptation , select the smallest one that can be installed according to the partition size.
  • Worst adaptation , select the largest by partition size

Page/Segment Allocation

We have been wrestling with this memory partition.

Paging/segmentation is for jobs, which are divided into many blocks.

The difference between page and segment is

The page size is fixed and consistent with the physical block size of memory (memory is divided into equal physical blocks) It is automatically divided by memory management and cannot be seen by users.

The size of the segment is divided into blocks of different sizes according to the programmer's wishes. Segments are logical units of information.


Another problem: when I transfer discrete job blocks into memory, how do I know which job block is in which memory block. This is it. Page/Segment Table Role of.

The page table is the job Logical address And memory Physical address Mapping of.

For example, the number of logical address bits of a 32-bit machine is 32. The number of physical address bits is determined by the real memory size.

Difference between page table and segment table one

 Page Table

Page Table

 Segment table

Segment table

Why is the segment table a two-dimensional structure?

Because the segment number corresponds to two elements (segment length and base address)

The page only corresponds to one element (block number)

Request paging allocation (virtual memory technology)

Since the job is divided into discrete blocks, according to the principle of program locality, we do not need to transfer all the blocks of the job into memory at one time. The memory stomach is very small, so I can't make allowances for it. So I only assign a certain number of physical blocks to you for an assignment.

Consider the following questions( These five questions are very important for understanding the whole process ):

  • Is the number of physical blocks in the allocation job fixed or dynamic? ( Page allocation algorithm )
  • Which pages of the job should be transferred to memory? ( Page call in algorithm )
  • If the physical block assigned to the job is full, and now it needs to call in a page, who should be replaced? ( Page replacement algorithm )
  • If the physical block allocated to the job is free, where is a page of the job transferred to memory? ( Mapping relationship between logical address and physical address )
  • What if the contents of the page in memory are modified? ( Data consistency )

These four questions are right Cache allocation It is the same to face. Just Page/physical block change into Cache block (The cache block size is not necessarily equal to the physical block size.). So let's start from the commonness and make special memories of different places.

TLB (block table), page table, cache, memory

  • Cache is stored in cache, and page table is stored in memory
  • The page table is the mapping of page numbers and block numbers of jobs and memory. TLB is a block mapping between memory and cache.
  • Difference between cache allocation and memory allocation:

    • Cache content is a copy of memory, Data consistency needs to be saved The memory content is transferred from the disk (job location), which can be inconsistent. When it is replaced, it can be written to the disk, No need for special data consistency
    • Generally, there are fewer physical blocks allocated to jobs, so Memory allocation is usually directly connected without explanation , and The mapping relationship between cache and memory is complex

Page allocation algorithm

For memory allocation , which can be fixed, that is, a fixed number of physical blocks are allocated to the job at the beginning. In this way, when the physical blocks in the memory are full, the contents of one of these physical blocks need to be replaced. I.e Fixed distribution

It can also be dynamic and can be divided into two situations:

  • At the beginning, no physical block is allocated to the job. The memory maintains a linked list of free physical blocks. If the job page needs to enter the memory, you can directly select an empty physical block from the linked list to load it. If the whole memory of the system is full, call up a page of other processes. I.e Dynamic allocation, global replacement
  • At the beginning, a certain number of physical blocks are allocated, and the memory also maintains a linked list of free physical blocks. The job page needs to be stored in memory. If your physical block is full, replace it first. If the job frequently sends page faults and interrupts, and frequently replaces its own physical block, the system will not be able to see it anymore, so it will allocate one from the link of the free physical block. If the job does not send interrupts, its physical blocks can be reduced appropriately. I.e Dynamic allocation, local replacement

For memory allocation, we usually use Fixed distribution Algorithm.

For the cache, because the cache size is very small, all jobs share the same cache space, that is Dynamic allocation, global replacement


These three ways are very similar to the way of education.

The first is to give your child a fixed monthly allowance. If you want to buy something else when you run out of money, you can sell some of your own things. I won't give you more.

The second is obvious indulgence. I will give you money if you want to buy anything. If the family has no money, Dad will spend less money on cigarettes.

The third one is to give you a fixed amount at the beginning. If you want to buy other things when you run out, you have to sell some of your own things. But if the parents send the child to buy things frequently, they will add more money. If you find that the child basically doesn't make money by selling things, you can give him less money.

Page call in algorithm

At first, the cache/memory is free. You can judge at the beginning to call in some pages/cache data blocks that may be used later, that is Pre paging policy

You can also not call in at the beginning, wait for missing pages/cache misses, and then call in. I.e Request transfer in policy

Mapping relationship

I had a hard time learning this.

The problem it solves is that when a data block is to be transferred to the cache, it cannot have a free location, so it can sit down directly rule For example, if the array block is the third member of a group in memory, it must sit in the third group of cache. Even if the first group has free seats, you can't go there.

This mapping rule also determines that when the cache is full, it can only be replaced Specify a member of the group If the specified group has multiple members, continue to select replacement according to the page replacement algorithm.

The rules are as follows

There are M rows in the cache. All rows are grouped into N groups.

The main memory should also be grouped, and the number of members in each group is N.

A data block in main memory corresponds to a group in cache


If the cache is only divided into one group, it is directly connected.

The cache is divided into M groups (N=M), that is, all connected.

Otherwise, it is group connected.


Why do we have such a complex mapping relationship?

How simple is my memory allocation. The given set of physical blocks is free, and you have to specify that I can only enter specific groups after going in directly to finish the work. Is there something wrong with you?

The reason is because The page table is in memory, and the memory space is large. The TLB is stored in the cache, and people have little money. The smaller your TLB block table, the better

What is the relationship between TLB table size and mapping?

The structure of a TLB table is similar to that of a page table, that is, the page number is changed into data block content. That is, the structure of each line can be simple as follows:

Data block number+data block content

The data block number marks which data block of the memory. (Special note: the size of the data block is not necessarily the same as the size of the physical block, so the exact name of the block number here should be Marker bit )

The mapping relationship can help us reduce the length of this tag bit, because the mapping relationship itself can provide some information.

For example, there are 16 rows of cache, divided into 8 groups with 2 rows each. The main memory is divided into 32 groups with 8 members in each group.

It can be seen that the main memory originally has 32 × 8=2 ^ 8, that is, the data block number needs 8 bits. The third block of main memory must enter the third group (3% 8=3)

In this way, the block number can be divided into two parts: the remaining part (5 digits) and the group number (3 digits)

among The group number can be seen directly from the cache location of the data block, so it is unnecessary to store it in the tag bit (This group number information is provided by the mapping relationship.). Therefore, the tag bit only needs to store the remaining part. The marker bit length is reduced from 8 bits to 5 bits.

It can also be seen that, The more groups, the smaller the marker bit (Fully connected mapping has the least number of markers). But the problem is Conflict prone For example, if the main memory block numbers are 3 and 11, the same group will be preempted.

Page replacement algorithm

Whether it is fixed allocation or dynamic, when all physical blocks are occupied and new pages must be brought in, some people must be eliminated.

When we study this part, the textbook talks about page replacement, that is, what happens in memory allocation, but actually cache allocation also uses this algorithm (a group has multiple members). If the topic is examined carefully, it will be more comprehensive.

There are optimal replacement, FIFO, LRU (least recently used algorithm), and Clock algorithms. The specific details will be expanded separately later.

Data consistency

This is not a problem specific to cache allocation. Memory allocation only defaults to Postscript method

Because cache is a copy of some memory blocks. The data in these two places must be consistent.

So when the data in the cache is modified (write operation), what should we do in the memory?
  • Misses

    • Read to the cache first, and modify the data in the cache
    • Modify the data in memory first, and then read it to the cache
  • Hit:

    • Cache and memory are modified simultaneously( Complete writing )
    • Only the cache data is modified. When the data block is replaced, the data in the memory is written back( Write back )

It can be seen that the second and third methods are to keep the cache and memory data consistent at all times. But first and fourth can increase speed.

summary

Memory allocation is a fixed allocation+local replacement+direct connection mapping+page replacement+write back data consistency algorithm

Cache allocation is dynamic allocation+global replacement+multiple mapping relationships+page replacement for members of the specified mapping group+multiple data consistency algorithms

If you can understand the above process, you should have a similar understanding of memory allocation and cache allocation.

file management

Hard link and soft link

These two concepts are really forgotten. I am helpless.

  • Soft links, such as creating a shortcut on Windows. When the original program is uninstalled, the shortcut remains, but when you open it, you will be prompted that "the application cannot be found". The content of the directory entry of the soft link file is only the path name of the target file, and the directory entry will be identified as "link".
  • Hard link: The directory contents of the hard link file and the target file are the same inode (index allocation) or the same FCB (implicit link allocation). When deleting a file, the reference count of the file will decrease by 1. Only the file

Physical allocation of files on disk

Directory entry, inode node, FCB

There are many operating system terms, and they all look very powerful.

The inode node and FCB are both ways to describe directory entries. The directory entry is the information describing the file (including Name and location on disk )。

The inode node is the FCB that removes the file name information , other information is combined to create a new name.

Continuous distribution

The files are divided into equal sized disk blocks one by one, and then stored on the disk one by one. FCB records the actual block number and size.

Explicit link assignment

In addition to the file content, the disk block is divided into a small area as the pointer area to point to the next disk block. FCB only needs to record the starting block number.

Implicit link assignment

The pointer information is placed in a FAT table. A table is a one-dimensional structure. The row number of the table represents the physical block number, and the content of the row of the table represents the physical block number of the next link.

FCB only needs to record the starting physical block number, and then query the FAT table.

Index Allocation

Each file establishes an index table, which is stored on disk. The contents of the index table are all the physical blocks of the file on the disk.

The FCB records the physical block number of the index table.

IO system

Program interrupt

When the CPU reads the disk data, the disk output speed is slow, and the CPU read speed is fast. The disk has a data register, which can only store one byte.

When the disk outputs, the CPU is not busy waiting, but when the data register is full, it is called the CPU (initiate an interrupt request).

DMA

On the principle of program interruption, add DMA controller. At the beginning, the CPU tells the DMA the number of bytes to read. Every time the data register of the disk is full, DMA request is directly initiated Transport data to memory (This process needs to compete with CPU for bus cycle). Until the specified number of bytes has been read, the DMA controller initiates an interrupt request to let the CPU handle the subsequent events.

Reference article

https://zhuanlan.zhihu.com/p/23755202

Last modification: March 25, 2019
Do you like my article?
Don't forget to praise or appreciate, let me know that you accompany me on the way of creation.