Operating System Basics(2)

6. Semaphore operation

P operation: apply for resources, S=S-1, if s>=0, the process performing the P operation continues to execute; if S<0, set the process to a blocking state (because there are no available resources), and insert it into blocking queue.

V operation: release resources, S=S+1, if s>0, the process performing the V operation continues to execute; if s<=0, wake up a process from the blocking state and insert it into the ready queue (at this time because A process blocked by a P operation can continue, lacking resources), and then a process performing a V operation continues.

Classic problem:

Producer and consumer problems Three semaphores:

Mutual exclusion semaphore S0 (the right to use the warehouse independently), synchronization semaphore S1 (the number of free warehouses), and synchronization semaphore S2 (the number of goods in the warehouse).

Producer process:

produce a product S

P(S0)

P (S1)

Put the product in the warehouse

V(S2)

V(S0)

Consumer process:

P(S0)

P(S2)

take out an item

V (S1)

V(S0)

7. Deadlock

When a process is waiting for an event that can never happen, a deadlock occurs. If there are multiple processes in the system in a deadlock state, it will cause a system deadlock.

Four necessary conditions for deadlock to occur:

Resource Mutual Exclusion

Each process holds resources and waits for other resources

The system cannot deprive a process of resources

The process resource graph is a loop.

After the deadlock occurs, the solution is to break the four conditions. There are the following methods:

Deadlock prevention: adopt a certain strategy to limit the requests of concurrent processes for resources, destroy one of the four conditions caused by deadlock, so that the system does not meet the conditions of deadlock at any time.

Deadlock avoidance: The banker's algorithm is generally used to avoid it. The banker's algorithm is to calculate in advance a resource allocation method that will not deadlock before allocating resources. Otherwise, no resources are allocated, which is equivalent to borrowing. Money, when considered in advance, can avoid deadlocks.

Deadlock detection: Deadlock is allowed to occur, but the system runs a program to detect deadlock regularly. If a deadlock is detected in the system, try to remove it.

Deadlock release: that is, the release method after the deadlock occurs, such as forcibly depriving resources, canceling the process, etc.

Deadlock calculation problem : There is a process in the system, and each process requires R resources, so the maximum number of resources for which deadlock occurs is n*(R-1). The minimum number of resources for which deadlock does not occur is n(R-1)+1.

8. Storage Management

page storage management

Divide the process space into pages, assuming that the size of each page is 4K, and also divide the physical space of the system into physical blocks (page detection numbers) of 4K size, so that the logic that needs to be run each time Pages are loaded into physical blocks, and other pages that need to be run can be loaded after running, so that processes can be run in batches without having to load the entire logical space into physical memory.

Advantages: high utilization, small fragmentation (only in the last page), simple allocation and management.

Disadvantage: Increased system overhead and may produce jitter.

page replacement algorithm

Sometimes, the process space is divided into 100 pages, and the system memory has only 10 physical blocks, which cannot all be allocated, so the pages to be executed need to be allocated first, and then eliminated according to the algorithm, so that 100 pages can be executed according to The sequence is called into the physical block and executed.

A page fault indicates that the page to be executed is not in the physical memory block, and needs to be loaded into the memory from the outside, which will increase the execution time. Therefore, the more pages there are, the lower the system efficiency.

Optimal algorithm: OPT, a theoretical algorithm, cannot be implemented. It is the best efficiency calculation after the process is executed, which is used to compare the gap between other algorithms. The principle is to select the page that will not be accessed within the longest time in the future to be replaced, which can ensure that the future execution will be accessed immediately.

First-in, first-out algorithm: FO, the pages that are loaded into the memory first are replaced and eliminated first, which will cause jitter. That is, the more pages are allocated, the higher the page fault rate may be (that is, the lower the efficiency). The page fault is calculated as follows:

Least recently used: LU, in the recent past, during the execution of the process, the pages that were least used in the past were replaced and eliminated. According to the principle of locality, this method is efficient and does not produce jitter.

fast watch

It is a small-capacity associative memory, composed of fast memory, accessed according to content, fast, and can guarantee parallel search according to content from hardware, and is generally used to store the page numbers of the few active pages that are currently most frequently accessed.

The fast table is to store the page table in the Cache; the slow table is to store the page table in the memory.

Therefore, the slow table needs to access the memory twice to fetch the page, while the fast table needs to access the cache once and the memory once, so it is fast.

segmented storage management

The process space is divided into segments, and each segment also has segment numbers and intra-segment stops. Different from page storage, each segment has different physical sizes, and segments are segmented according to the logical whole.

Address representation: (segment number, offset within the segment): The offset within the segment cannot exceed the segment length corresponding to the segment number, otherwise an out-of-bounds error occurs, and the real memory address corresponding to this address should be:

The base stop corresponding to the segment number + the offset within the segment.

Segmented page storage management

The process space is segmented first, and then paging. The specific schematic diagram and advantages and disadvantages are as follows:

Advantages: small space waste, easy storage sharing, and dynamic connection.

Cons: Increased complexity and overhead due to increased management software, slower execution

Advantages: The program logic is complete, and the modifications do not affect each other

Disadvantages: low memory utilization, large waste of memory fragmentation

Guess you like

Origin blog.csdn.net/flysh05/article/details/124142226