Operating system final review notes (continuously updated..)

1. Basic concepts of operating systems

1.1 Operating system concept

Control and manage the hardware and software resources of the entire computer system. Reasonably organize and schedule computer work and resources. A collection of programs that provides convenient interfaces and environments for users and other software.

1.2 Characteristics of operating systems

Features: concurrent, shared, virtual, asynchronous

Concurrency: Two or more events occurring within the same time interval . The system has the ability to process and schedule multiple programs to execute simultaneously. Concurrency in the operating system is achieved through time sharing. Note: Concurrency refers to one time period, and parallel refers to the same period. Parallelism refers to a system with simultaneous execution or operations (hardware support: multiple pipelines or multiple processors). For single-machine processing, parallel macro programs are running at the same time, and micro programs are executed alternately. Multi-programs take turns occupying the CPU and execute alternately.

Important test points: A single-core CPU can only execute one program at the same time, and each program can only be executed concurrently. A multi-core CPU can execute multiple programs at the same time, and multiple programs can be executed in parallel.

Sharing: Mutually exclusive sharing mode. For example, printers and tapes can only be accessed by one process at a time . This kind of resource is called: critical resource or exclusive resource.

Simultaneous access mode: Allow multiple processes to access resources within a period of time. Typical representatives: disk devices, files written by re-entry code.

Virtual: A physical entity becomes several logical counterparts. This technology is called virtual technology. Virtual processor: Using multi-program concurrency, each end user feels like there are multiple processors. Time division multiplexing technology.

Virtual storage: Convert physical storage into virtual storage and logically expand storage usage. Space division multiplexing technology.

You can also virtualize one IO device into multiple logical IO devices, and allow each user to occupy one logical IO device.

Asynchronous: Multi-programming stops and goes, and the process moves forward at an unpredictable speed.

1.3 Operating environment of the operating system

Program running: The process of program running is the process of executing the machine instructions of the CPU one by one.

Operating system operating mechanism:

There are two types of programs executed by the CPU: operating system kernel programs and user-programmed programs.

Kernel:

Clock management: The operating system provides users with standard time, manages processes based on the clock, and implements process switching.

Interrupt mechanism: The original intention is to improve CPU utilization in a multi-programming environment. Protect and restore information of interrupted threads, and transfer control to related programs.

Primitive: at the lowest level of the system, closest to the hardware. The operation is atomic and can only be completed in one go. Data structure and processing for system control.

Interrupts and exceptions:

In order to switch between the core state and the user state, an interrupt mechanism is introduced. Kernel mode can execute privileged instructions that user mode cannot execute. The access management command is used in the user mode to convert the user mode into the core mode, so the access management command is not a privileged command.

Interrupts are the only way for the operating system kernel to regain access to the CPU.

Interrupt (external interrupt): Events from outside the CPU instructions occur. I/O interrupt: Input and output have been completed. Clock interrupt: The fixed time slice has expired, let the processor handle it.

Exception (internal interrupt): an event originating from the CPU executing instructions. Illegal opcode, division by zero, address out of bounds, arithmetic overflow. Trap command: Set by the user. After executing the trap, the user state is converted to the core state. Exceptions cannot be masked.

System call: The user calls some sub-functions provided by the operating system in the program. Device function: Complete device request or release, device startup and other functions. File management: Complete the functions of reading, writing, creating and deleting files. Process control: complete the creation, cancellation, blocking and wake-up functions of the process. Process communication: Complete the message passing and signal passing functions between processes. Memory management: Complete functions such as memory allocation, recycling, and obtaining the size and starting address of the memory area occupied by the job.

User mode and kernel mode: When in the kernel mode, it means that the kernel program is running at this time, and privileged instructions can be executed at this time.

When in user mode, it means that the application is being executed at this time, and only non-privileged instructions can be run at this time.

There is a register in the CPU called the program status word register (psw), which has a binary bit, 1 represents the kernel state, and 0 represents the user state.

Alias: kernel state = core state = tube state. User state = eye state.

Kernel mode -> user mode. Execute a privileged instruction - modify the psw flag to user mode, which means that the operating system actively gives up the CPU usage rights.

User mode -> kernel mode. Triggered by an interrupt, the hardware automatically completes the abnormal process. Triggering the interrupt signal means that the operating system forcibly regains the right to use the CPU.

1.4 Big Kernel and Micro Kernel

Large kernel: Centralizes the main functional modules of the operating system to provide high-performance system services. Advantages: Sharing information between various management modules can effectively utilize each other's effective features, which has huge performance advantages. Disadvantages: The hierarchical interaction relationships are complex, hierarchical interfaces are difficult to define, and the boundaries between layers are blurred.

Microkernel: With the development of computer architecture, the operating system provides more and more services, and the interface forms become more and more complex. Keep the most basic functions in the kernel and transfer functions that do not need to be executed in the core mode to the user mode to reduce the complexity of the kernel design. Advantages: Effectively separates the kernel and services, services and services, making the interfaces between them clearer and greatly reducing maintenance costs. Each part can be optimized and evolved independently. Disadvantages: Performance issues, requiring frequent switching between user mode and kernel mode.

2. Process

2.1 Concept and characteristics of process

The concept of process:

A process is a process of execution of a program. A process is an activity that occurs when a program and its data are executed sequentially on a processor. A process is a program with independent functions that runs on a data collection and is an independent unit of resource allocation and scheduling. It is to better describe and control the concurrent execution of programs and realize the concurrency and sharing of the operating system.

Process control block PCB: In order to better describe the basic situation and running status of the process, and then control and manage the process. The PCB is the only sign of the existence of the process.

Process characteristics:

Dynamic, concurrency, independence, asynchronous, structural.

Dynamic: The process has a life cycle, including creation, activity, suspension, termination and other processes.

Concurrency: Multiple processes exist in memory at the same time, allowing programs to execute concurrently.

Independence: Processes can independently obtain resources and accept scheduling.

Asynchronous: Processes restrict each other and advance at an unpredictable speed. The operating system is configured with a corresponding process synchronization mechanism.

Structural: Each process is configured with a PCB for description.

2.2 Process status and transitions

Status: creation state, ready state, running state, blocking state, end state.

Creation state: The process is being created and has not yet entered the ready state.

Ready state: The process is ready to run.

Running state: The process is running on the processor.

Blocking state: Also called waiting state, the process is waiting for a certain time and is suspended.

End state: The process is disappearing from the system (normal end or abnormal termination)

Mutual conversion:

Ready state -> Running state: The process in the ready state obtains the processor and enters the running state.

Running state -> Ready state: After the time slice of the process in the running state is used up, the processor enters the ready state.

Running state -> Blocked state: The process requests resources other than the processor. At this time, the running state enters the blocking state.

Blocked state -> Ready state: The process obtains the resources it is waiting for, such as IO resources, or the interrupt ends.

2.3 Process control

Process creation:

Assign a process identification number and apply for PCB (PCB is limited). Allocate resources to the process and allocate necessary memory space for programs and data and user stacks. Initialize PCB, including initialization flag information, initialization processor status information, initialization processor control information, and setting process priority. If the process ready queue can accept the new process, the process enters the ready state.

Termination of process:

End classification: Normal end: The process's task has been completed and is ready to exit. Abnormal end: The process is running, and some abnormal events occur, causing the program to be unable to continue running. External intervention: The process terminates at the request of the outside world.

End process: According to the identifier of the terminated process, retrieve the PCB and read the process status. If the process is in the running state, terminate it and deprive it of the processor. Terminate child processes under the process. All resources owned by the process are returned to the parent process or the operating system. Remove the PCB from the queue.

Process blocking and waking up:

Blocking primitive execution process: Find the PCB corresponding to the identification number of the process to be blocked. If the process is in the running state, protect its site, convert its state to the blocking state, and stop running. Insert the PCB into the waiting queue for the corresponding time.

The execution process of the wake-up primitive: find the PCB corresponding to the process in the waiting queue. Remove it from the waiting queue and set its status to ready. Insert the PCB into the ready queue and wait for scheduling by the scheduler.

Process switching:

Process switching is completed in kernel mode.

Procedure: Saves the processor context, including the program counter and other registers. Update PCB information. Move the PCB of the process into the corresponding queue, such as ready, blocking, etc. queues. Select another process to execute and update its PCB. Update memory management data structures. Restore the processor context.

Organization of processes:

A process is an independent running unit and the basic unit for resource allocation and scheduling by the operating system. It consists of the following three parts:

Process control block:

Process description information: Process identifier: Flag process. User identifier: identifies the user to whom the process belongs, mainly for sharing and protecting services.

Process control and management information: Process current status: Describes process status information. Process priority: Describes the priority of the process to seize the processor. Code execution entry address. Program external memory address. Enter memory time. Processor time occupied. Semaphore usage.

Resource allocation list:

Used to describe information about the memory address space or virtual address space status, the list of open files and the input/output devices used. Code segment pointer, data segment pointer, stack segment pointer, file descriptor, keyboard, mouse.

Processor related information:

The value of each register in the processor. General register, address register, control register, flag register value, status word.

Program segment: A program code segment that can be scheduled by the process scheduler to be executed by the CPU.

Data segment: the original data processed by the program corresponding to the process or the intermediate or final results generated when the program is executed.

How the process is organized:

Linking method: The PCB is divided into multiple queues according to the process status, and the operating system has pointers to each queue.

Indexing method: Create several index tables according to different process statuses. The operating system holds pointers to various index tables.

2.4 Process communication

Shared storage: There is a shared space that can be directly accessed between communicating processes.

Low-level approach: based on data structure sharing.

Advanced mode: based on bucket sharing.

Message passing: Data exchange between processes is in units of formatted messages. Processes exchange data through the two primitives provided by the system for sending messages and receiving messages.

Direct communication method: The sending process directly sends the message to the receiving process and hangs it on the message buffer queue of the receiving process. The receiving process obtains the message from the message buffer queue.

Indirect communication method: the sending process sends the message to an intermediate entity, and the receiving process obtains the message from the intermediate entity.

Channel communication: The sending process sends a large amount of data into the write pipe in the form of a character stream, and the receiving process receives the data from the pipe. When the pipe is full, the write() system call of the writing process will be blocked, waiting for the reading process to retrieve the data. When the reading process has taken away all the data, the pipe becomes empty, and the read() system call of the reading process will be blocked.

Channel communication is half-duplex communication and cannot be read and written at the same time. Mutual exclusion and synchronization are required to ensure the existence of the other party.

2.5 Process synchronization

Critical resources: Resources (printers, special variables, data) that only one process is allowed to use at a time.

Critical resource access process: Entry zone: Check whether the process can enter the critical zone. Critical section: Code that can access critical resources. Exit area: Clearly mark the critical area being accessed. Remainder: The rest of the code.

Mutual exclusion: indirect restriction relationship, when one process accesses critical resources, other processes cannot access it.

Synchronization: direct restriction relationship, establishing multiple processes to complete a certain task, cooperating with each other, and communicating and synchronizing with each other.

Mutual exclusion follows 4 principles:

Idle let-in: When the critical section is idle, a process that requests to enter the critical section can be allowed to enter the critical section.

Wait if busy: After an existing process enters the critical section, other processes trying to enter the critical section must wait.

Limited waiting: For a process that requests access to the critical section, it enters the critical section within a limited time.

Give up and wait: When the process cannot enter the critical section, the processor should be released immediately.

The basic methods to realize mutual exclusion of critical sections: software implementation method and hardware implementation method.

Software implementation method:

Single flag method (using semaphores to achieve synchronization): Set the public integer variable turn to indicate the process number that is allowed to enter the critical section. If turn is 0, the P0 process is allowed to enter the critical section; if turn is 1, the P1 process is allowed to enter the critical section. district.

Disadvantages: It may not be free to enter (for example, turn is 0, but P0 does not need resources and P1 needs resources). If it is busy, wait. 

The double-flag method checks first (using semaphores to achieve mutual exclusion): before the program enters the critical resource, it first checks whether the critical resource has been accessed, and can only enter if it is free.

Disadvantages: Two processes may enter the critical section at the same time (detecting that the critical section is not used at the same time), and wait if busy.

Check after double flag: first set your own flag to indicate that you want to enter, check the other party's flag, if the other party also wants to enter, then wait, otherwise enter.

Disadvantages: Both parties may give in to each other, leading to hunger.

Peterson's algorithm: To prevent two processes from waiting indefinitely, a flag bit is added based on Algorithm 3 to prevent starvation.

Advantages: Solve the problem of hunger.

Hardware implementation method:

Interrupt masking method: Mask interrupts and turn off interrupts.

Disadvantages: Limits the processor's ability to alternately execute programs.

Hardware instruction method: After reading the specified flag, set the flag to true.

Signal amount:

Shaped semaphore: wait: resource-1, signal=resource+1. Disadvantages: Failure to follow the right-waiting mechanism will result in a busy waiting state.

Record semaphore: requires a value representing the number of resources, and a process linked list to connect all processes waiting for the resource.

There are also the use of semaphores to achieve synchronization, the use of semaphores to achieve mutual exclusion, and the use of semaphores to achieve predecessor relationships.

Monitor: A software module composed of a set of data and operations on this set of data defined on this set of data. This set of operations can initialize and change the data and synchronization process in the monitor.

3. Thread

Basic concepts of threads:

Reduce the time and space overhead of concurrent execution of programs and improve the concurrency performance of the operating system.

After the introduction of threads, the process only serves as the allocation unit of system resources, and the thread serves as the allocation unit of the processor.

Comparison of threads and processes:

Scheduling: The process is the basic unit of independent scheduling, and the thread is the basic unit of resources. Switching between different threads causes process switching.

Own resources: Process is the basic unit of resource allocation.

Concurrency: After the introduction of threads, processes can be executed concurrently, and multiple threads can also be executed concurrently, improving system throughput.

System overhead: Thread switching of the same process is less expensive than process switching.

Address space and other resources: The address spaces of processes are independent of each other. Process resources (including address space) are shared between threads of the same process . Threads of a process are not visible to other processes.

Communication: Inter-process communication requires the assistance of process synchronization and mutual exclusion to ensure data consistency. Threads can communicate directly by reading/writing process program segments.

Thread attributes: Does not own system resources, has unique identifier and thread control block. Different threads can execute the same program. When the same service program is called by different users, the operating system creates different threads for it. Threads in the same process share all resources of the process. A thread is an independent scheduling unit of the processor. Threads also have life cycles, such as blocked, ready, running and other states. In a multi-CPU computer, each thread can occupy a different CPU. Each thread has a thread ID, thread control block (TCB). The overhead of switching threads in the same process is small, but the overhead of switching processes is large. Due to the shared memory address space, inter-thread communication within the same process does not even require system intervention.

4. Deadlock

Deadlock definition: Each process waits for each other's resources, causing each process to be blocked and unable to move forward.

Causes of deadlock: competition for system resources; illegal process advancement sequence.

Necessary conditions for deadlock to occur:

Mutually exclusive condition: Processes have exclusive control over allocated resources. (Only one process can use resources, other processes cannot use them at the same time)

Inevitability condition: Resources obtained by a process cannot be forcibly taken away by other processes before they are used. (If a process obtains a resource, other processes cannot deprive it of it)

Request and hold conditions: A process holds at least one resource and makes a new resource request, but the resource is already occupied by other processes. At this time, the process is blocked, but the resources it has obtained remain. (The resources occupied by oneself are not released, and new resources are requested)

Loop waiting condition: You wait for me to release, I wait for you to release.

Deadlock handling strategy:

Deadlock prevention: breaking one of the four necessary conditions. All resources are requested at once, resources are deprived, and allocated in sequence. Disadvantages: low efficiency and inflexibility.

Destroy the mutual exclusion condition (general resources are exclusive, this is more difficult to destroy). Destroy inalienable conditions and release already occupied resources. Destroy the request and maintain the state, and apply for all the required resources at once (so that the process can obtain the required resources at once). Destroy the loop waiting condition and use the sequential resource method to recommend processes sequentially.

Avoid deadlock: Use some method to prevent the system from entering an unsafe state when resources are dynamically allocated. Look for possible safe sequences and detect whether allocating resources will deadlock.

System security status, after allocating resources in a certain way, will it lead to deadlock? If it is deadlock, it is unsafe.

Banker's Algorithm: Calculate different allocation methods of current resources to see whether it will enter an unsafe state.

Detection and release of deadlock: allow process deadlock, promptly determine whether it is deadlock, and release it.

Resource allocation diagram: circles are processes and boxes are resources. The process to resource is called the request edge, and the resource to process is the allocation edge. Find the process that satisfies the allocation in the resource allocation graph, and eliminate its request edges and allocations. If all edges are eliminable, there is no deadlock.

Deadlock relief: Resource deprivation method, suspend some deadlock processes (eventually resume), and allocate resources to other processes. The process cancellation method forcibly cancels (directly kills without restoring) some or even all deadlocked processes and deprives resources. The process rollback method allows one or more processes to roll back to a point where deadlock can be avoided.

5. Processor Scheduling

The concept of scheduling: reasonable allocation of processors to processes.

Scheduling levels:

Job scheduling (advanced scheduling): Select jobs from external storage and send them to memory. Each job is only loaded once and loaded once.

Memory scheduling (intermediate scheduling): Transfer processes that are not running temporarily to external memory and put them into a suspended state. Or transfer a process that already has running conditions into the memory and change its status to ready.

Analysis: The suspended state is to transfer the process to external memory to wait; in the blocked state, the process image is still in the memory.

Process scheduling (low-level scheduling): Select a process from the ready queue according to a certain strategy or method and assign the processor to it.

The three are connected: job scheduling prepares for process activities, process scheduling makes processes normally active, and intermediate scheduling suspends processes that are temporarily unable to run.

Process scheduling method:

Non-deprivation scheduling : If you want to allocate the processor to a higher-priority process, you must wait for the process currently occupying the processor to release the processor before allocating the processor to the higher-priority process.

Advantages: Suitable for batch processing systems. Disadvantages: Not suitable for time-sharing systems.

Deprivation scheduling method : If a higher-level process requests a processor, the executing process is suspended and the processor is allocated to the higher-level process.

Advantages: Improve system throughput and response efficiency.

Basic principles of scheduling: CPU utilization (the CPU is as busy as possible); system throughput (the number of jobs completed by the CPU per unit time); turnaround time (the time from job submission to job completion); waiting time (jobs waiting for the processor) time); response time (the time from the user submitting the request to the first response).

Processor scheduling algorithm:

First come, first served algorithm: processors are allocated to the first come first.

Advantages: Good for long jobs.

Disadvantages: Short work can lead to hunger.

Short job priority algorithm: Prioritize the process with the shortest running time.

Priority: Short average wait times and turnaround times.

Disadvantages: It is not good for long-term work and can easily lead to starvation during long-term work.

Priority scheduling algorithm: The one with higher priority is executed first.

It can be divided into: deprived type and non-deprived type.

Priority can be divided into: static priority and dynamic priority. System priority is higher than user, and interactive priority is higher than non-interactive. I/O priority is higher than calculation.

High response ratio scheduling algorithm: The one with the highest response ratio is executed first.

Response ratio = (waiting time + required service time) / required service time.

The waiting time is the same, the shorter the required service time, the higher the response ratio (favoring short jobs). The required service time is the same. The longer the waiting time, the higher the response ratio (to prevent starvation).

For long jobs, as the waiting time increases, it can also have a higher response ratio.

Features: Does not cause starvation, non-preemptive algorithm.

Time slice rotation algorithm: Ready processes are queued in order of arrival, occupy the processor within the time slice, and release the processor when the time slice arrives.

Time slice selection is very important and is affected by factors such as the number of processes and system processing capabilities.

Features: Non-preemptive, will not cause hunger.

Multi-level feedback queue scheduling algorithm: Set up multiple ready queues, set different priorities for each queue, and the priority decreases. The time slice in each queue is different, and the time slice decreases. Each queue is queued on a first-come, first-served basis. If the specified time is not completed, it will be placed in the next level queue. Only when the high-level queue is empty can the low-level queue be scheduled.

Features: The phenomenon of hunger occurs, which is preemptive.

6. Virtual memory management

Locality principle:

Temporal locality: After an instruction is executed or data is accessed, it may be executed or accessed again shortly after. (Reason: There are a large number of loop operations in the program).

Spatial locality: Once a program accesses a memory location, nearby memory locations will also be accessed soon after. (Reason: Instructions are usually stored and executed sequentially, and data are generally stored in the form of arrays, etc.).

The definition and characteristics of virtual memory: Based on the principle of locality, part of the program is loaded into the memory, and part is left in the external memory. When needed, the contents of the external memory are transferred into the memory. Characteristics: Multiplexity (when the job is running, it is transferred into the memory multiple times to run), commutability (the job does not have to reside in the memory all the time, and the job is allowed to be swapped in and out during the running process), virtuality (logically Expand the memory capacity so that the visible memory capacity is greater than the actual memory capacity).

The maximum capacity of virtual memory is determined by the CPU addressing range. The actual capacity of virtual memory is determined by min (sum of memory + external storage capacity, CPU addressing range).

The page table includes: page number, physical block number, status bit (whether the page is transferred into memory), access field (this page has been accessed within a period of time), modification bit (records whether this page has been modified), external storage Address (indicates the location of the page in external memory).

Page replacement algorithm:

Optimal replacement algorithm (OPT): Select pages that are never used or have not been visited for the longest time for elimination.

First-in-first-out page replacement algorithm (FIFO): eliminate the first entered page.

Most Recently Unused (LRU) replacement algorithm: Select the page that has not been accessed for the longest time for elimination. Each page is set with an access field to identify the time since it was last accessed.

Clock (CLOCK) replacement algorithm: rotates like a clock. Each page is set with a usage bit. If it encounters an unused page, it will swap out the page and use the position 0. If it encounters a used one, it will set the usage position to zero. Then scan the next one.

Advantages: Performance is close to the best replacement algorithm. Disadvantages: Complex implementation and high overhead.

Improved CLOCK algorithm: Modify bits are added based on access bits. Scanning process: Step 1: Scan the buffer and first select pages that have not been used or modified to swap out. Step 2: If not found, look for unused but modified pages to swap out. For each frame passed, position 0 will be used. If it is not found yet, the pointer will return to the starting location and step 1 will be repeated.

Allocation strategy: The so-called global replacement means that busy processes can "borrow" physical blocks from idle processes. Partial replacement means that physical blocks can only be obtained from physical blocks reserved by the system. Global replacement can "borrow" a wider range (idle process + system).

Fixed allocation and partial replacement: Each process is allocated a fixed number of physical blocks, and the page is replaced when a page is missing.

Variable allocation global replacement: the process allocates certain physical blocks, and the system reserves certain idle physical blocks. If a page is missing, the system will

Variable allocation partial replacement: dynamically allocate physical blocks according to the page fault situation of the process.

Resident set: The set of physical page frames allocated to a process is the resident set of the process.

Jitter: The page that was just swapped out (to disk) needs to be swapped into memory again. The reason is: the number of allocated physical page frames is insufficient or the replacement algorithm is improper.

Working set: The set of pages that a process needs to access within a certain period of time.

——————————————————————————————————————

1. File system topic

Question 1: [2012 Unified Examination Real Questions] The maximum capacity of a certain file system space is 4TB (1TB=2^40 B), with disk blocks as the basic allocation unit. Disk block size is 1KB. The file control block (FCB) contains a 512B index table area. please answer the following question.

1) Assuming that the index table area only uses a direct index structure, and the index table area stores the disk block number occupied by the file, how many bytes does the block number occupy in the index table entry at least? What is the maximum supported length of a single file in bytes?

2) Assume that the index table area adopts the following structure: Bytes 0 to 7 use the <start block number, number of blocks> format to represent the continuous storage space pre-allocated when the file is created, of which the starting block number occupies 6B and the number of blocks occupies 2B ;The remaining 504 bytes use a direct index structure, and one index item occupies 6B. What is the maximum length of a single file that can be supported? In order to maximize the length of a single file, please indicate the reasonable values ​​of the number of bytes occupied by the starting block number and the number of blocks and explain the reasons.

Answer: (1) The total capacity is 2^42B, and the disk block size is 2^10B, so there are 2^32 disk blocks in total. Each disk block can be represented by a 32-bit binary number (for example, 0000 0000 0000 0000 0000 0000 0000 0010 represents the second disk block). Q: How many bytes does the block number occupy? Because each byte is 8 bits, it is 32/8=4B, so it occupies 4 bytes. (You can use 4 bytes to represent a disk block)

Now there is a 512B index table area. Using 512B/4B=128, we find that there are 128 index table entries (because 4 bytes represent a disk block, and 4 bytes are equal to 1 index table entry). Each index table The item represents a disk block , and each disk block is 1KB in size, so the maximum length of a single file is 128x1KB=128KB.

(2) 6B = 48 bits. 2B = 16 bits, 16 bits represents the number of consecutive memory blocks = 2^16, and because each memory block is 1KB = 2^10B, the pre-allocated continuous memory space is 2^26B.

Because 1 index entry occupies 6B, 504 bytes = 84 index entries. From the reasoning in question 1, we can know that 1 index entry = 1 disk block, 84x1KB = 84KB.

As can be seen from the beginning of question 2, the space size represented by the index table can = pre-allocated storage space + direct index storage space. So the maximum supported length of a single file is 2^26B+84KB.

Under the framework of question 2, the index table structure remains unchanged , that is, the first 8 bytes represent pre-allocated space, so I can only make an issue here. Because according to question 1, the system capacity is 2^32 disk blocks. So we can let the number of consecutive allocated blocks occupy 32 bits, that is, 4 bytes, so that a maximum of 4TB of continuous blocks can be allocated as pre-allocated space. The starting block number also occupies 4 bytes, which can just address 2^32 disk blocks. A perfect distribution method.

Question 2: [2016 Unified Examination Real Questions] A certain disk file system uses link allocation to organize files, and the cluster size is 4KB. Each directory entry of the directory file includes the file name and the first cluster number of the file, and other cluster numbers are stored in the file allocation table FAT.
1) Assume that the directory tree is as shown in the figure below, and the cluster numbers and sequences occupied by each file are as shown in the table below, where dir and dir1 are directories, and file1 and file2 are user files. Please give the contents of all directory files.
2) If each entry of FAT only stores the cluster number and occupies 2B, what is the maximum length of FAT in bytes? What is the maximum file length supported by this file system?
3) The system implements file management through directory files and FAT. Access by name, indicating which FAT entry the two cluster numbers 106 and 108 of file1 are stored in.


4) Assuming that only the FAT and dir directory files have been read into the memory, if the 5000th byte of the file dir/dir1/file1 needs to be read into the memory, which clusters should be accessed?

Answer: (1) You can see it this way. There are only two directories in the question, namely dir and dir1. The directory entry of dir is dir1 (no need to consider the next level), and the directory entries of dir1 are file1 and file2. By definition: Each directory entry of a directory file includes the file name and the first cluster number of the file, so the directory name of dir is: dir1 48. The directory name of dir1 is: file1 100 file2 200.

(2) 2B=16 bits. Any cluster can be represented by a cluster number composed of 16-bit binary numbers, so a total of 2^16 clusters can be represented. And because 1 entry corresponds to 1 cluster, there are 2^ in total. There are 16 table entries. Since one table entry is 2B in size, the maximum length of FAT = number of table entries x table entry size = 2^16x2 is 2^17B = 128KB.

Maximum file length = number of clusters x size of each cluster. It is known that there are 2^16 clusters, and the size of each cluster is 4KB=2^12B. So the maximum file size that can be represented is: 2^28B=256MB.

(3) As can be seen from the first question, the first directory entry of the dri1 directory file is file1 100. The 100 here is the first cluster number of file1. It needs to be connected to the next two cluster numbers 106 and 108 through pointers. The directory entry of file1 will first point to the entry in the FAT corresponding to the first cluster number (as shown below) , which is the 100th entry, because the structure of each entry in the FAT table is: entry + next Cluster number, so the next cluster number corresponding to cluster number 100 is 106. Entry 106 can be found through cluster number 106, and cluster number 108 is stored in entry 106. From this, all cluster numbers in the file1 file can be found. So the answer is: the cluster number 106 of file1 is stored in FAT entry 100, and the cluster number 108 is stored in FAT entry 106.

(4) As we know from the question, the dir directory has been read into the memory. Next, dir1 needs to be read into the memory, so the cluster with cluster number 48 needs to be accessed. Next, file1 needs to be read into the memory. Because the size of each cluster is 4KB = 4096B, the system knows that the directory entry of file1 can only access a maximum of 4096 bytes, so it will go to the 100th entry of FAT to find it, so it will Access the cluster with cluster number 106 (this cluster is the second cluster, the corresponding memory is 4097~8192 bytes, and the 5000th byte is within this range).

Question 3: File F consists of 200 records, and the records are numbered starting from 1. After the user opens the file, he wants to insert a record in the memory into file F as its 30th record. Please answer the following questions and explain your reasons.

(1) If the file system adopts continuous allocation mode, each disk block stores a record, and there is enough free disk space before and after the storage area of ​​file F, then how many times at least do the disk blocks need to be accessed to complete the above insertion operation? The file control block of F What changes will occur to the content? (2) If the file system adopts the link allocation method, and each disk block stores a record and a link pointer, how many times does the disk block need to be accessed to complete the above insertion operation? If the size of each disk block is 1 KB , 4 bytes of which store the link pointer, what is the maximum file length supported by this file system?

 Answer: (1) To clarify the meaning of the question, file F has been written on the disk. There are 200 pieces of data in total. There is enough space before and after, so the data can be moved. It should be noted here that data is inserted , not modified. First of all, you need to know: reading and writing files require access to the disk once .

The current operation is to read the first data first, then write 1 bit forward (write to the 0th bit), then read the second data, and write to the position of the first data... This is repeated 29 times in total. Then write the data you want to insert at the location where the 29th data was originally located (due to the previous forwarding operation, there are 29 numbers in front of the number, so the number is the 30th piece of data, which is in line with the meaning of the question) .

A total of 29 reads and writes + 1 write were performed, so the number of disk accesses was 29x2+1=59.

(2) 31 disk accesses are required (29+2). The first 29 times are easier to understand. We need to read the link pointer of each node from the first node at the head of the linked list. Because the link pointer points to the subsequent node, we read it a total of 29 times and found the 29th one. Node.

Then you need to generate a node Node, let the link pointer of Node point to the original 30th node (the 31st node after insertion), then write the Node to the disk (write once), and finally modify the 29th node The link pointer of a node (written once) so that it points to Node.

So the total is 31 times.

Question 4: A certain file system uses index nodes to store file attributes and address information, and the cluster size is 4KB. Each file index node occupies 64B and has 11 address items, including 8 direct address items, 1 each of first-level, second-level and third-level indirect address items. Each address item is 4B in length. please answer the following question.

(1) What is the maximum file length that the file system can support? (Just give the calculation expression.)

(2) The file system uses 1M (1M = 2^20B) clusters to store file index nodes and 512M clusters to store file data. If the size of an image file is 5600B, how many image files can the file system store at most? 

(3) If the size of file F1 is 6KB and the size of file F2 is 40KB, does the file system take the same time to obtain the cluster number of the last cluster of F1 and F2? Why?

Answer: (1) A direct address entry points to a cluster where data is stored. A first-level indirect address item points to a cluster. Address items are stored in this cluster, and each address item points to a direct address item. A second-level indirect address item also points to a cluster. There are address items stored in this cluster, but each address item points to a first-level address item.

The approximate relationship is: the third-level indirect address item contains the second-level indirect address item, the first-level indirect address item contains the direct indirect address item , and 1 direct indirect address item points to 1 cluster containing data.

So the reasoning relationship is like this: each cluster size is 4KB, and each address item is 4B in length, so a cluster can contain 1024 address items. Let’s start with the third-level indirect address items. One third-level indirect address item = 1024 second-level indirect address items = 1024x1024 first-level indirect address items = 1024x1024x1024 direct indirect address items.

In the same way, 1 second-level indirect address item = 1024x1024 direct indirect address items. 1 first-level indirect address item = 1024 direct indirect address items.

Calculation idea: Convert all first-level, second-level, and third-level indirect address items into direct address items, and then multiply by the size of the cluster (because 1 direct address item corresponds to 1 cluster storing data)

Calculation formula: (8+1024+1024x1024+1024x1024x1024)x4KB.

(2) The number of index nodes that can be stored in a cluster: 4KB/64B=2^12B/2^6B=2^6. The number of clusters storing file nodes is 1M = 2^20B. So the total is 2^26=64M index nodes.

Cluster is the smallest unit of space allocated by the file system. An image file of 5600B cannot occupy 2 clusters, but it is still counted as 2 clusters. Because 512M clusters are used to store file data, 256M image files can be stored.

But it should be noted here that the title is talking about image files. It is a file. A file must correspond to a file index node . Now there are only 64M index nodes, but there are 256M image files, so the files that can be stored Must be limited by the number of index nodes.

So only 64M image files can be stored.

(3) First of all, please note that f1 and f2 are both files, which are indexed by file index nodes. The general order of indexing is: direct address item index -> first-level address item index -> second-level address item index .. Indexed in the order of .

Because there are 8 direct address item indexes in the question, each direct address item index corresponds to a cluster that stores data. The critical value from the direct address item index to the first-level indirect address index is 8x4KB=32KB, that is, when the index file size is greater than 32KB You need to use indirect indexes.

f1 can be found through the direct address index, while f2 needs to be found through the first-level indirect address index. Therefore, the search of f2 will be one level deeper than that of f1, so the time will be different.

2. Memory management topic

Question 1: [2009 Unified Examination Real Questions] In the request paging management system, assume that the page table content of a process is as shown in the following table.

The page size is 4KB, the memory access time is 100ns, the fast table (TLB) access time is 10ns, the average time to process a page fault is 10^8ns (including the time to update the TLB and page table), the process The resident set size of is fixed to 2, and the least recently used replacement algorithm (LRU) and local elimination strategy are adopted. Assuming that the TLB is initially empty, the TLB is accessed first during address translation. If the TLB misses, the page table is accessed again (ignoring the TLB update time after accessing the page table). If the valid bit is 0, it means that the page is not in the memory, and a page fault interrupt is generated. After the page fault interrupt is processed, it returns to the instruction that generated the page fault interrupt and is executed again. There are virtual address access sequences 2362H, 1565H 25A5H, please ask:

1) How long does it take to access the above three virtual addresses in sequence? Give the calculation process.

2) Based on the above access sequence, what is the physical address of virtual address 1565H?

To solve this problem, you need the following prerequisite knowledge : 1. The page table is stored in memory, which stores the mapping relationship between virtual addresses and physical addresses . The leaf frame number is found through the page number. Finally , the leaf frame number and the offset address within the page must be Splice to get the physical address , and then access the memory again.

2. The page frame is the basic unit for storing data in the memory; 3. The page number is an identifier in the logical address space, and its function is to map the logical address to the physical address;

4. The virtual address consists of the page number (high bits) and the offset within the page (low bits). The number of bits in the offset within the page (n) is related to the size of the page (n is 2 to the nth power) .

5.2362H, 1565H, 25A5H are all hexadecimal numbers, and each number represents 4 digits. 6. Fast tables are generally stored in hardware and are caches. 7. The search sequence is: fast table -> page table -> external memory. The fast table is initially empty.

Answer: (1) Because the page size is 4KB = 2^12B, according to point 4 of the prerequisite knowledge, the last 12 digits are the page offset, and the first 4 digits are the page number.

Right: 2362H = 0010 0011 0110 0010B, so the offset within the page is 0011 0110 0010, and the page number is 0010=2. First, the cache table is checked, which takes 10 ns. Because the page table is initially empty, it is a miss (it will be loaded when the memory is accessed below); then the memory (page table) is accessed, which takes 100 ns. When the page number is 2, the valid bit is 1, which is a hit; After hitting, you can get the page frame number, concatenate the page frame number with the page offset, get the physical address, and re-access the memory, which takes 100ns. Overhead: 210ns.

Right: According to the above analysis, 1565H has a page number of 1 and an offset of 565H within the page. First, search the fast table, which takes 10ns. The item with page number 1 is not loaded and is a miss. Accessing the memory (page table) takes 100ns. When the page number is 1, the valid bit is 0, which is a miss. As a last resort, page fault interrupts are required to access external memory, which costs 10^8ns (the title says it includes the time to load memory and TLB). After obtaining the page frame number, it is concatenated with the offset address within the page to obtain the physical address and re-access the memory, which costs 100ns. Overhead (210+10^8) ns.

Right: 25A5H According to the above analysis, the page number is 2, and the page offset is 5A5H. First, search the fast table, which takes 10ns. The item with page number 2 is loaded for the first time, so it hits; after getting the page frame number, it is spliced ​​with the page offset, and then accesses the memory, which takes 100ns. Overhead 110ns.

(2) First of all, you have to read the question. The resident set size of the process is fixed at 2 , so only 2 pages can reside in the memory at most, so the page table can only have 2 page table entries , because the page table It turns out that there are already page table entries No. 0 and No. 2. When virtual address 1565H was accessed earlier, the page table entry No. 1 was also loaded into the page table. At this time, a page table entry needs to be eliminated. Because page table entry No. 2 is accessed once and page table entry No. 0 is accessed 0 times, page table entry No. 0 is eliminated. Page frame 101H of page table entry No. 0 becomes the corresponding page frame of page table entry No. 1. .

Next, follow the above analysis for 1565H. The page number is 1, the offset within the page is 565H, and the page frame is 101H. Physical address: page frame + page offset = 101565H.

Question 2: [2010 Unified Examination Real Question] Assume that the logical address space and physical address space of a certain computer are both 64KB, and are addressed in bytes. If a process requires up to 6 pages (Page) of data storage space, and the page size is 1KB, the operating system uses a fixed allocation and partial replacement strategy to allocate 4 page frames (Page Frame) to the process. See the table below. Before loading time 260, the access status of the process is also shown in the table below (the access bit is the use bit).

When the process reaches time 260, to access the data at the logical address 17ACH, please answer the following questions:

(1) What is the page number corresponding to this logical address?

(2) If the first-in-first-out (FIFO) replacement algorithm is used, what is the physical address corresponding to the logical address? The calculation process is required. If the clock replacement algorithm is used, what is the physical address corresponding to the logical address? The calculation process is required. Assume that the pointer for searching the next page moves in a clockwise direction and currently points to page frame No. 2. The schematic diagram is as follows.

Answer: (1) Because the page size is 1KB, the last 10 bits represent the page offset, 17CAH=000101 11 1100 1010B, the page number is 5, and the page offset is 11 1100 1010.

(2) First in, first out: 0->3->2->1, replace number 0, so the page frame number becomes 7=0111, physical address = page frame number + page offset = 01 1111 1100 1010 = 1FCA H. (Be careful here, be sure to convert the page frame number and page offset into binary before splicing)

First, review the clock algorithm. The access bit will be used. When choosing whether to swap out a page, it will look at whether the access bit is 1. If it is 1, the page will not be swapped out, but the access bit will be set to 0 (cleared to 0). ), then check the next page, and then swap it out until the page whose first access bit is not 0 is checked.

At time 260, the access bits of page numbers 0, 1, 2, and 3 are all 1. Starting from page 2, access is clockwise, 2->1->0->3. Since these access bits are all 1, so will be set to 0 after access, so the page number whose access bit is 0 for the first access is 2. At this time, page frame number 2 is assigned page number 5, and physical address = page frame number + page offset = 00 1011 1100 1010 = 0BCA H.

Question 3: [2012 Unified Examination Real Questions] The local page replacement strategy of a request paging system is as follows: start scanning from time 0, scan the resident set every 5 time units (scan time is ignored) and have not been visited in this round The page frame will be recycled by the system and placed at the end of the free page frame chain, and the content will not be cleared before the next allocation. When a page fault occurs, if the page has been used before and is still in the free page frame list, it is put back into the resident set of the process; otherwise, a page frame is taken out from the head of the free page frame list.

Ignoring the impact of other processes and system overhead, the process resident set is initially empty. Currently, the page frame numbers in the system's free page frame linked list are 32, 15, 21, and 41. The <virtual page numbers, access times> that process P accesses in sequence are: <1, 1>, <3, 2>, <0, 4>, <0, 6>, <1, 11>, <0, 13> ,<2,14>. please answer the following question:

1) When accessing <0, 4>, what is the corresponding page frame number?

2) When accessing <1,11>, what is the corresponding page frame number? Explain the reason.

3) Access <2, 14>H size, what is the corresponding page frame number? Explain the reason.

4) Is this method suitable for programs with good temporal locality? Explain the reasons.

answer:

(1) Corresponding page frame: 21. (2) Corresponding page frame: 32. (3) Corresponding page frame: 41. (4) Suitable. If the time locality of the program is better, the chance of retrieval from the free page frame list is greater, and the advantages of this strategy are more obvious.

Below is the reasoning process and the answers are highlighted in red . It is expressed in the form of <virtual page number, access time, page frame number>. For example, <0, 4, 21> means that the virtual page number is 0, the access time is 4, and the allocated page frame number is 21.

Initially, the page frame numbers of free pages are: 32, 15, 21, 41. The order of allocation is from left to right .

First round: 1~5 Time: <1, 1, 32>, <3, 2, 15>, <0, 4, 21 >

scanning.

At this time, the page frame number of the free page is: 41.

Second round: Time 6~10: <0, 6, 21>

scanning. Because virtual page numbers 1 and 3 did not appear in this round, page frames 32 and 15 were recycled and placed at the end of the free page frame chain (note that the title said that they were placed at the end of the free page frame chain ) . Because virtual page number 0 appeared again, The page frame number has not been recycled and is still 21.

At this time, the page frame numbers of the free pages are: 41, 32, 15 .

Third round: 11~15: <1, 11, 32 >, <0, 13, 21>, <2, 14, 41 >

The reason why <1, 11, 32> is because the title says: If the page has been used before and is still in the free list, it will be put back into the resident set of the process, because page number 1 was used in the first round. The second round does not appear and enters the free page. At this time, the third round has not yet ended and is being scanned and cleared (the title says: the content will not be cleared before the next allocation), so it has not been cleared, and page frame No. 32 is used.

The reason why <2, 14, 41> is because the title says: Otherwise (meaning that the page number has not been used), take out a page frame from the head of the free page frame list . According to the sorting at the end of the second round, the link head is page frame No. 41.

Question 4: A certain computer system is byte-addressed and uses the paging storage management method of the secondary page table. The virtual address format is as follows:

please answer the following question:

1) How many bytes are the sizes of page and page frame? What is the size of the virtual address space of a process in pages? 2) Assuming that the page directory entry and page table entry both occupy 4 bytes, how many pages do the page directory and page table of the process occupy in total? Required to write down the calculation process. 3) If the virtual addresses accessed in a certain instruction cycle are 0100 0000H and 0111 2048H, how many secondary page tables are accessed during address translation? Ask for reasons.

To answer this question, you need the following prerequisite knowledge : 1. The page directory number is used to index the page directory table. Each item in the page directory table corresponds to a page table starting address . 2. Page table index, used to index the page table. Each item in the page table corresponds to the starting address of a page .

answer:

(1) The page frame and page sizes are both 4KB (reason: the page frame is the basic unit of physical memory, and the page is the basic unit of virtual memory, and the page and page frame sizes are equal).

The virtual address space size is 2^20 pages (reason: the virtual address space size is 2^10 x 2^10 x 2^12 = 2^32B, and each page size is 2^12B, so the address space size is 2^32/2^ 12=2^20).

(2) Answer page 1025. The page directory number is 10 digits, which can represent 2^10 page directories. One page directory is 4B, so the page directory size is: 2^10 x 2^2 = 2^12B. One page is also 2^12B, so the page directory occupies 1 Page.

According to prerequisite knowledge 1, you must first know that the page directory table contains the page table . The page directory number is 10 bits, and the page table index is 10 bits, so the number of page tables is 2^10 x 2^10 = 2^20, and the total size of the page table is 2^20 x 2^2 = 2^22 B, and then Divided by the size of one page 2^22 / 2^12 = 2^10=1024 pages.

Total: 1+1024=1025 pages.

(3) By simply accessing a secondary page table, we can calculate that the high 10 bits of 0100 0000H are 4 when converted to decimal, and the high 10 bits of 0111 2048H are also 4 when converted to decimal. (According to prerequisite knowledge 1, the page directory number is used to index the page directory table. Each item in the page directory table corresponds to the starting address of a page table . For example, if the page directory number is 4, it can be found in the page directory table. The page table corresponding to item 4 , so the same page table is accessed )

(1)4x1024+2x4=4096+8=5004

Virtual address: 0001 0000 1000 0000 0001 0000 0000 1000 = 10801008H

Page catalog number: 042H

Page number: 001H

Physical address: 

00301H= 0100 0011 0000 0001=4301H

(2) The virtual addresses are not necessarily continuous, but the physical addresses must be continuous.

(3) Row traversal

3. Synchronization and mutual exclusion

To solve this problem, you first need to have the following prerequisite knowledge:

1.P operation is used to request a resource or enter a critical area. When performing the P operation, the value of the semaphore is decremented by 1. If the value minus 1 is greater than or equal to 0, execution can continue.

2. The V operation is used to release a resource or leave a critical resource. When performing a V operation, the value of the semaphore is increased by 1. If other processes or threads are blocked due to the P operation, the system will select a blocked process or thread to wake up and allow it to continue execution.

3.Semaohore is the English word for semaphore .

[2011 Unified Examination Real Questions] A bank provides 1 service window and 10 seats for customers to use while waiting. When a customer arrives at the bank, if there is an empty seat, he or she will go to the number-taking machine to get a number and wait for the number to be called. Only one customer is allowed to use the number-taking machine at a time. When the salesperson is free, he selects a customer by calling his number and serves him. The activity process of customers and salespersons is described as follows.

Please add necessary semaphore and P, V operations or wait(), signal() operations to achieve mutual exclusion and synchronization in the above process. It is required to write out the complete process, explain the meaning of the semaphore and assign an initial value.

answer:

Semaphore mutex1=1; //The number-taking machines are mutually exclusive

Semaphore empty=10; //Number of empty seats, initially 10.

Semaphore full=0; //The number of occupied seats, initially 0.

Semaphore mutex2=1; //Window mutual exclusion

Cobegin {
        process 顾客 i {
                P(empty);
                P(mutex1);
                    从取号机获取一个号码;
                V(mutex1);
                V(full);
                    等待叫号;      
                P(mutex2);
                    获得服务
        }
        process 营业员 {
                while(TRUE){
                        P(full);
                            叫号;
                        V(mutex2);
                            为客户服务;
                        V(empty);                      
                }
        }
}coend

Guess you like

Origin blog.csdn.net/RuanFun/article/details/135439498