1. Thread/process management
Job Scheduling:
1. First-Come service: scheduling is performed based on the task arrival time, and first-come-first-served scheduling.
2. Minimum job priority: give priority to the jobs with the shortest time required.
3. Priority: jobs with higher priority are executed.
4. Top response rate: jobs with high response rate are prioritized. Response Ratio = (Waiting Time + computing time)/computing time.
Process Scheduling:
1. Service First: Execute the process that first enters the ready queue.
2. Minimum priority: give priority to the process that takes the shortest time.
3. Highest Response Ratio: Processes with high response priority. Response Ratio = (Waiting Time + requesting service time)/requesting service time.
4. Priority: process with a higher priority is executed.
5. time slice rotation: allocates time slice to the process according to the first-in-first-out rule. After the time slice ends, the next process will be executed no matter whether or not it is completed.
6 multi-level feedback queue: Multiple ready queues. Each queue has different priorities, time slice is also different, and the time slice with a higher priority is short. The process first enters the first queue. If the execution is not completed in the specified time slice, the process enters the second queue, and so on.
The cause of the deadlock is as follows:
(1) Insufficient system resources.
(2) The order in which the process is promoted is inappropriate.
(3) improper resource allocation.
Four Conditions for deadlock:
(1) mutex condition: A resource can only be used by one process at a time.
(2) request and retention conditions: when a process is blocked by requesting resources, it will not release the obtained resources.
(3) Non-deprivation condition: the resources obtained by the process cannot be forcibly deprived before they are used.
(4) Cyclic waiting condition: a cyclic waiting resource relationship is formed between several processes that are connected at the beginning and end.
Avoid deadlocks:
The prevention of deadlocks is to prevent the occurrence of deadlocks by breaking the conditions for creation, but this method damages the system's concurrency and concurrency.
The first three conditions of a deadlock are necessary conditions for the deadlock to be generated. That is to say, the conditions that must be met for the deadlock to be generated, instead of the three conditions, the deadlock must be generated, so long as the fourth condition is avoided logically, the deadlock can be avoided.
To avoid deadlocks, the first three conditions are allowed, but a reasonable resource allocation algorithm is used to ensure that the closed process chain of loop wait will never be formed to avoid deadlocks. This method supports parallel execution of multiple processes. To avoid deadlocks, the system dynamically determines whether to allocate a resource to the requested process.
Deadlock Prevention: The specific practice is to destroy one of the four necessary conditions for deadlock generation.
Deadlock Avoidance: bankers algorithm.
Processes and threads
Processes are the basic unit of resource allocation, while threads are the basic unit of system scheduling.
The programs we write usually run as threads. A process can be considered as a general term that includes a series of threads and resources. A process includes at least one thread (main thread, generated when you enter the main function). You can create other threads or not.
What resources are shared by threads of the same process, and what resources are exclusive to each thread?
Shared resources include
1. Heap: Because the heap is opened up in the process space, it is shared by nature. (On a 16-bit platform, global heap and local heap are divided. Partial heap is exclusive)
2. Global variables: they are irrelevant to a specific function, so they are also irrelevant to a specific thread and therefore shared.
3. static variables: Although local variables are declared in a function in code, they are stored in the same location as global variables. BSS and. data segment, which is shared.
4. File and other public resources: This is shared and the threads that use these public resources must be synchronized. Windows provides several resource synchronization methods, including signal, critical section, event, and mutex.
Exclusive resources include
ID of the thread, a set of register values (copies are stored in the thread), the stack of the thread itself, the scheduling priority and policy, the signal shielding word, the errno variable, and the private data of the thread
Ii. Memory Management
Paging: divides the logical address space of a process into several equal parts, called pages or pages. Correspondingly, the memory space is divided into several physical blocks, and the page and block size are equal. You can place any page of your program in any part of the memory to implement discrete allocation.
Address structure: page number + page address. Use the page table to find the physical address (address ing ).
Fast table (TLB): caches page tables to accelerate address conversion.
Segment: divides the user program address space into several segments of varying sizes. Each segment can define a set of relatively complete logical information. During storage allocation, segments are measured in segments. segments and segments can be connected in the memory and are allocated discretely.
Mainly to meet user programming needs: 1) convenient programming. 2) information sharing: when sharing programs and data, it is based on the logical unit of information, and the logical unit of information is segment. 3) Information Protection: protects the logical unit of information. 4) dynamic growth: segmentation can effectively solve the dynamic growth of data. 5) Dynamic Link: you can link the segments to be run to the memory for running.
Address structure: Segment number + segment address. Implement address ing through field tables.
The paging system can effectively use the memory, while the segmentation system can meet user needs.
Segment page: This is a combination of the segmentation and paging principles. That is, the user program is divided into several segments, and each segment is divided into several pages. Each segment has a segment name. Address structure: Segment number + intra-segment page number + intra-page address. Address ing is implemented using the field table and page table.
Program Locality Principle:
1) Time locality. If a command in the program is executed, the command may be executed again soon. If a data is accessed, the data may be accessed again soon. A typical reason is that a large number of cyclic operations exist in the program.
2) spatial locality. Once a program accesses a certain storage unit, the nearby storage unit will be accessed soon, that is, the address accessed by the Program within a period of time, it may be concentrated within a certain range. A typical case is the sequential execution of the program.
Virtual Memory: allows a job to be transferred to the memory multiple times. 1) Request paging system. 2) request the segmentation system.
Page Replacement Algorithm:
1. first-in-first-out: removes the information block that first enters the cache. The phenomenon of belady occurs: the number of physical blocks allocated increases, and the number of page-missing interruptions increases.
2. Last unused: information blocks with the lowest recent usage frequency are eliminated.
3 random replacement: a random number generator is used to randomly generate an information block number and then eliminate it.
4. Optimized replacement: This method must first execute a program and then replace the following information Blocks Based on the cache replacement.
5. Best replacement: The elimination rule is to remove information blocks that will never be used in the future or that will not be used for the longest time. This method can minimize the page missing rate, but this is an ideal method. The reality cannot be achieved. It can only be used as a measure of other replacement algorithms.