Modern Operating Systems (Medium)

Chapter 3 Memory Management

overview

Memory (RAM) is an important resource in a computer that needs to be carefully managed.

After years of exploration, people have proposed the concept of a memory hierarchy, that is, in this system, the computer has several megabytes (MB) of fast, expensive and volatile cache (cache), and several gigabytes (GB) ) speed and affordable, equally volatile memory, and several terabytes (TB) of low-speed, cheap, non-volatile disk storage, plus removable storage such as DVD and USB. It is the job of the operating system to abstract this storage hierarchy into a useful model and to manage this abstraction.

The part of the operating system that manages the hierarchical memory hierarchy is called the memory manager. Its task is to effectively manage memory, that is, record which memory is in use and which memory is free; allocate memory for the process when it needs it, and release the memory after the process is used up.

Address Space and Switching Technology

  1. address space

In the original system, there was no abstraction of memory, and the direct use of physical addresses for storage caused serious problems: if user programs could address every byte of memory, they could easily corrupt the operating system, thereby Stop the operating system. It is difficult to run multiple programs at the same time, because it is easy to overwrite data using physical addresses.

To ensure that multiple applications exist in memory at the same time and do not affect each other, two problems must be solved: protection and relocation. A good solution is to create a memory abstraction: the address space. An address space creates an abstract memory for programs. An address space is a set of addresses that a process can use to address memory. Each process has its own address space, and this address space is independent of the address spaces of other processes (except in special cases where you want to share).

2. Memory overload

When the memory required by all processes to be executed is greater than the physical memory of the computer, all processes cannot be kept in memory all the time. This situation is called memory overload. To solve this problem, there are two ways.

Swapping technology: that is, transfer a process into the memory completely, make the process run for a period of time, and then save it back to the disk. Idle processes are mostly stored on disk, so they don't take up memory.

Virtual memory: Allows a program to run with only a portion of it loaded into memory.

Virtual Memory

A process shares the CPU and main memory with other processes, but the main memory space is limited. When multiple processes are running at the same time, the memory will not be enough. At this time, we introduced the concept of virtual memory, which is an abstract computer memory management technology for main memory.
The basic idea of ​​virtual memory is: each program has its own address space, this space is divided into multiple blocks, each block is called a page or page, and each page has a continuous address range. These pages are mapped to physical memory, but not all pages have to be in memory to run a program. When a program references a portion of the address space in physical memory, the necessary mapping is performed immediately by the hardware. When a program refers to a part of the address space that is not in physical memory, the operating system is responsible for loading the missing part into physical memory and re-executing the failed instruction.
Advantages and disadvantages of the virtual memory mechanism:
Advantages: It can make up for the lack of physical memory size; improve the reflection speed to a certain extent; reduce the reading of physical memory to protect memory and prolong the service life of memory; it can execute larger memory in smaller available memory user program; more programs can be accommodated in memory for concurrent execution; it does not have to affect the program structure during programming (compared with overlay technology)

Disadvantages: occupy a certain amount of physical hard disk space; increase the read and write of the hard disk; improper settings will affect the stability and speed of the whole machine

virtual memory and physical memory

  1. The address used by the user when programming is called virtual address or logical address, and the corresponding storage space is called virtual memory space or logical address space

    The access address of computer physical memory is called real address or physical address, and its corresponding storage space is called physical storage space or main memory space

  2. Physical memory: In the application, the real existence is the capacity of the memory stick inserted in the memory slot of the motherboard. Essentially, physical memory is the window in which code and data operate.

    Virtual memory: Make the program think that it has continuous available memory (a continuous and complete address space), but in fact, it is usually divided into multiple physical memory fragments, and some are temporarily stored on external disk storage. Data exchange takes place when required.

    If the random access memory (RAM) required by the computer to run programs or operations is insufficient, Windows will use virtual memory to compensate, that is, take out a part of the hard disk space to use as memory. This part of the space is called virtual memory, and virtual memory is stored on the hard disk. The form of existence on the Internet is the page file PAGEFILE.SYS. It combines your computer's RAM with temporary space on your hard drive. Moving data into the paging file frees up RAM so work can be done.

    If your computer slows down due to lack of available RAM, try to compensate by increasing virtual memory. However, the rate at which the computer reads data from RAM is faster than the rate at which it reads data from the hard disk, so expanding the capacity of RAM (additional memory sticks) is the best choice.

memory paging mechanism

Actually stored in physical memory (on disk), read page by page at runtime

Basic idea: The address space (virtual address space) of the user program is divided into several fixed-size areas called "pages". Correspondingly, the memory space is divided into several physical blocks, and the size of the page and the block is equal. Any page of the user program can be placed in any block of the memory, realizing discrete allocation.
Logical address: The system divides the logical space of the program into several pages of the same size, called logical pages or pages. Each logical page of the program is numbered sequentially starting from 0, which is called logical page number or relative page number. Each page starts addressing from 0, which is called the page address. The logical address in the program consists of two parts: the page number P and the displacement W in the page.
If a logical address is given as A and the page size is L, then the page number P=INT[A/L], and the page address W=A MOD L

Page table: In the paging system, each page of a process is allowed to be discretely stored in any physical block of memory. In order to find the physical block corresponding to each page in memory, the system creates a page table for each process , used to record the correspondence between process logical pages and memory physical pages. The function of the page table is to realize the address mapping from the page number to the physical block number. How many pages are there in the address space, how many rows are registered in the page table, and the page fault interrupts are arranged in the order of logical pages: pages in the process linear address
space It does not need to be resident in memory. When executing an instruction, if it is found that the page he wants to access is not in memory (that is, the existence bit is 0), then the execution of the instruction will be stopped, and an exception that the page does not exist will be generated, and the corresponding fault handling The program can troubleshoot by loading the page from the external memory, and then the instruction that caused the exception can continue to execute without generating an exception.
The difference between paging and segmentation
A segment is a logical unit of information, which is divided according to the needs of the user, so the segment is visible to the user; a page is the physical unit of information, which is divided for the convenience of managing the main memory, and is for the user is transparent.
The size of a segment is not fixed and is determined by the function it completes; the size of a page is fixed and is determined by the system. A
segment provides users with a two-dimensional address space; a page provides users with a one-dimensional address space.
A segment is a logical unit of information for easy storage Protection and sharing of information, page protection and sharing are limited.
Page Replacement Algorithm
During the running of the program, sometimes the page to be accessed is not in the memory, but needs to be transferred into the memory. However, the memory has no free space to store pages. To ensure the normal operation of the program, the system must transfer a page of programs or data from the memory to the disk swap area. At this time, a certain algorithm is needed to determine which page needs to be transferred. This algorithm is often referred to as a "page replacement algorithm".

1. Optimal permutation algorithm (OPT) (ideal permutation algorithm)

Implementation principle: Each time, pages that will not be accessed for a long time in the future or that will never be used in the future are selected for elimination. Remove pages that will never be needed from main memory; if no such page exists, select the page that will not be accessed for the longest time. Because the selected eliminated pages will never be used in the future, or pages that will not be accessed for the longest time, it can ensure the lowest page fault rate. That is, evicted pages are pages that will never be used in the future or will not be accessed for the longest period of time.

Advantages: The best replacement algorithm can guarantee the lowest page fault rate

Disadvantages: The optimal replacement algorithm is an idealized algorithm with better performance, but it cannot be realized in practice (it is impossible to predict which of several pages in a process will not be accessed for the longest time)

2. Recently Not Used Algorithm (NRU)

Implementation principle: In order to enable the operating system to collect useful statistics, in most computers with virtual memory, the system sets two status bits for each page. The access bit R bit is set when the page is accessed (read or write); the modification bit M bit is set when the page (ie modified page) is written. Use the R and M bits to construct a simple page replacement algorithm: when a process is started, both bits of all its pages are set to 0 by the operating system, and the R bit is periodically (e.g., at each clock interrupt) ) is cleared to distinguish pages that have not been accessed recently from pages that have been accessed.

When a page fault occurs, the operating system examines all pages and classifies them into 4 categories according to their current R and M bit values:

Class 0: not accessed, not modified 00

Category 1: not accessed, modified 01

Category 2: Accessed, not modified 10

Category 3: has been accessed, has been modified11

The NRU algorithm randomly selects a page from the non-empty class with the smallest class number to eliminate it. Implicit in this algorithm is that it is better to evict a modified page that has not been accessed in the last clock tick (typically about 20ms) than to evict a "clean" page that is frequently used.

Advantages: easy to understand and can be effectively implemented, although its performance is not the best, but it is enough

3. First-in-first-out replacement algorithm (FIFO)

Implementation principle: When a page needs to be eliminated, the page that resides in the main memory for the longest time is always selected for elimination, that is, the page that enters the main memory first is eliminated first. The reason is that the pages that were first transferred into main memory are most likely to be no longer used. That is, the pages that enter memory the earliest are eliminated first.

Advantages: the first-in-first-out algorithm is simple to implement and is the most intuitive algorithm

Disadvantages: The performance of first-in-first-out is the worst, because it does not conform to the usage rules of the usual pages, so there are few practical applications

4. The second page replacement algorithm

Implementation principle: The FIFO algorithm may replace frequently used pages. To avoid this problem, a simple modification is made to the algorithm: check the R bit of the oldest page. If the R bit is 0, the page is old and unused, and can be replaced immediately; if it is 1, the R bit is cleared to 0, and the page is placed at the end of the linked list, and its loading time is modified so that It loads as if it was just loaded, and continues searching.

5. Clock page replacement algorithm

Implementation principle: Although the second chance algorithm is a relatively reasonable algorithm, it often needs to move pages in the linked list, which reduces efficiency and is not very necessary. A better approach is to keep all pages in a circular linked list like a clock face, with a hand pointing to the oldest page. When a page fault occurs, the algorithm first checks the page pointed by the pointer, if its R bit is 0, the page is eliminated, and a new page is inserted into this position, and then the pointer is moved forward by one position; if the R bit is 1, it is Clear the R bit and advance the hands one position, repeating the process until a page with an R bit of 0 is found.

6. Least Recently Unused Replacement Algorithm (LRU)

Implementation principle: Use the principle of locality to infer future behavior based on the past page access history of a job during execution. It assumes that pages that have not been visited in the past may not be visited again in the immediate future. Therefore, the essence of this algorithm is: when a page needs to be eliminated, the page that has not been used for the longest time in the most recent period is always selected to be eliminated. That is, pages that have not been visited for the longest time recently are eliminated.

Memory Segmentation Mechanism

The basic idea of ​​segment management is: divide the program into segments according to the content or process (function) relationship, and each segment has its own name (number).

In the eyes of programmers, the program is divided into many segments, and each segment has different characteristics. Applicable to different fields. Each segment starts at address 0 of that segment. That is to say, the address of the place where the main program is stored should start from zero, and the address of the place where variables are stored should also start from zero, and the same is true for other areas. Each area in the user program has its own characteristics. For example, the part of the main program should be read-only, the area where variables are located should be writable, the function library should be linkable or not, and the stack should only be Unidirectional increase. These requirements are certainly not guaranteed if the entire program is put together. Therefore, the program should be saved in segments, and these segments have their own characteristics.

Since it is segmented, how is the address defined? Or base address + offset. It's just that the base address here is no longer the starting location of this program, but the starting address of this section of the program. This base address is placed in the segment table.
Every time the CPU executes an instruction involving an address, it will check the process segment table in the PCB to determine the physical address. This table is actually an LDT table, and there is a register LDTR register dedicated to storing the address of the table. So far the memory is ready to use. Because the address has already been set.

memory partition

1. Fixed partition

When the operating system is initialized, the memory is divided into n partitions with the same size. However, when the program is running, the memory requirements vary. If this method is used, it will inevitably cause a lot of waste.

2. Variable partition

The basic idea of ​​variable partition is to establish the allocated partition table and free partition table. The allocated partition table records the memory that has been used, and indicates which program used this memory. The starting address and length are How many. The free partition table records the free area in the memory, including the start address and length. At this time, if there is a memory request, allocate memory for this request according to the requested memory size and the size of the free partition on the free partition table, and update these two tables at the same time; if any process is finished running, also update these two tables. The advantage of this is that you can allocate a large block of memory to programs that require large memory, and allocate small memory to programs that require small memory, improving memory utilization.

3. Three adaptation methods for variable partitions

For example, there is a request that requires 40K memory, and there are many memory areas larger than 40K in the free partition table, which one should be selected for allocation?

First adaptation: As the name implies, it is to allocate the first memory that meets the request. The advantage of this is: fast

Next adaptation: When allocating, continue to search from the end of the last scan, and allocate from the first free space that meets the requirements

Best fit: Look through all the free memory blocks, and allocate the memory closest to 40K and larger than 40K to it. The advantage of this is that it can improve memory usage

Worst fit: Look through all free memory blocks, and allocate the largest block of memory to it, and the memory block must be larger than 40K. The advantage of this is that the remaining memory blocks are relatively uniform

buffer overflow

Buffer overflow means that when the computer fills the buffer with data beyond the capacity of the buffer itself, the overflowed data is overwritten on the legal data.
The harm has the following two points:
the program crashes, resulting in a denial of service; jumps and executes a piece of malicious code
3. The main reason for buffer overflow is that the program does not check user input carefully.

Chapter 4 File System

File system

The file system is the method and data structure used by the operating system to specify files on storage devices (commonly disks, and also NAND Flash-based solid-state drives) or partitions; that is, the method of organizing files on storage devices. The software organization responsible for managing and storing file information in the operating system is called a file management system, or file system for short.

The file system consists of three parts: the interface of the file system, the software collection for object manipulation and management, objects and attributes. From a system point of view, a file system is a system that organizes and allocates the space of a file storage device, is responsible for file storage, and protects and retrieves stored files. Specifically, it is responsible for creating files for users, storing, reading, modifying, and dumping files, controlling file access, and revoking files when users no longer use them.

In a computer, a file system is a system for naming files and placing logical storage and retrieval of files.

1. File system type

ext2: The file system commonly used in early Linux

ext3: an upgraded version of ext2, with logging function

RAMFS: In-memory file system, fast

iso9660: disc or disc image

NFS: Network File System, invented by SUN, mainly used for remote file sharing

MS-DOS: MS-DOS file system

FAT: The file system used by the Windows XP operating system

NTFS: The file system adopted by the Windows NT/XP operating system

  1. The maximum hard disk capacity supported by various file systems

NTFS format (windows partition, Linux can also be used): the maximum supported partition is 2TB, and the maximum file size is 2TB

FAT32 format (windows partition, Linux can also be used): the maximum supported partition is 128GB, and the maximum file size is 4GB

Ext2 format: maximum partition size is 4TB, maximum file size is 1TB

Ext3 format: maximum partition size is 16TB, maximum file size is 2TB

EXT4 format: the maximum partition size is 1EB, the maximum file size is 16TB

document

A file is a logical unit of information that creates a process. A disk generally contains thousands or even millions of files, and each file is independent of other files. The only difference is that the files model the disk, not the RAM.

Processes can read existing files and create new ones as needed. Information stored in files must be persistent and not be affected by process creation and termination.

1. File Naming

The UNIX file system distinguishes between upper and lower case letters, while the DOS file system does not.

2. File structure method

Byte sequence: An unstructured byte sequence. In fact, the operating system does not know or care about the contents of the file. What the operating system sees are bytes, and any meaning of the file content is only interpreted by the user program. Both UNIX and Windows take this approach.

Sequence of records: A file is a sequence of fixed length, each record has its internal structure. The central idea behind treating a file as a sequence of records is that a read operation returns a record, and a write operation overwrites or appends a record.

Tree: In this structure the file consists of a tree, each record does not have to be the same length, there is a key field at a fixed position of the record. The tree is sorted by key field, allowing for fast lookups on specific keys.

3. File structure classification

The logical structure of the file mainly includes: (1) continuous structure (2) multiple structure (3) transposition structure (4) sequential structure

The physical storage of files mainly includes: (1) sequence structure (2) link structure (3) index structure

The directory structure of the file mainly includes: (1) first-level directory structure (2) second-level directory structure (3) tree structure (4) acyclic graph

4. File access

Sequential access: In these systems, a process can read all the bytes or records of a file sequentially from the beginning, but cannot skip certain content, nor can it read out of order. Sequential access to the file is possible to return to the starting point, and the file can be read as many times as needed.

Random Access Files: When a file is stored on disk, the bytes or records in the file can be read out of order, or the records can be accessed by key instead of position, and the bytes or records can be read in any order

There are two ways to indicate where to start reading a file. One is that each read operation gives the position to start reading the file. The other is to use a special seek operation to set the current position, and after the seek operation, start reading files sequentially from this current position. UNIX and Windows use the latter method.

5. File properties

insert image description here

6. File operation

The purpose of using files is to store information and facilitate later retrieval. For storage and retrieval, different systems provide different operations. Here are some of the most common system calls related to files:

Create: Creates a file without any data. The purpose of this call is to indicate that the file is about to be created and to set some attributes of the file.

Delete: When a file is no longer needed, it must be deleted to free up disk space. Any filesystem always has a system call to delete files.

Open: The file must be opened before it can be used. The purpose of the open call is to load the file attributes and disk address table into the memory, so as to facilitate the quick access of subsequent calls.

Close: After the access, file attributes and disk addresses are no longer needed. At this time, the file should be closed to release the internal table space. Many systems limit the number of open files a process can have to encourage users to close files they no longer use. The disk is written in blocks, and when a file is closed, the last block of the file is written, even if the block is not full.

Read: Read data in the file. Generally, the data read is from the current position of the file. The caller must specify how much data needs to be read and provide a buffer to store the data.

Write: Write data to the file, and the write operation generally starts from the current position of the file. If the current position is the end of the file, the file length is incremented. If the current position is in the middle of the file, the existing data is overwritten and lost forever.

Append: This call is a restricted form of write, it can only add data at the end of the file. If the system only provides a minimal set of system calls, there is usually no append. Many systems provide multiple implementations of the same operation, and these systems sometimes have an append call.

Seek: For random access files, to specify where to start getting data, the usual method is to use the seek system call to point the current position pointer to a specific position in the file. After the seek call ends, you can start reading and writing data from this position.

get attributes: The process often needs to read file attributes. For example, the make program in UNIX is often used to manage software development projects consisting of multiple source files. When make is called, it will check the modification time of all source files and target files to achieve minimal compilation, so that all files are the latest version. To achieve this, it is necessary to find some attributes of the file, namely the modification time.

set attributes: Some attributes can be set by the user, even after the file is created, and the set attributes system call is used to implement this function. Protected mode information is a typical example, and most flags also belong to this type of attribute.

Rename: Users often want to change the name of an existing file, and the rename system call is used for this purpose. Strictly speaking, the rename system call is not necessary, because the same purpose can be achieved by first copying the file into a new file and then deleting the original file.

Table of contents

File systems usually provide directories or folders for recording the location of files, and in many systems directories are themselves files.

1. Level 1 directory system

The simplest form of a directory system is to contain all files in a single directory. This is sometimes called the root directory, but since there is only one directory, its name is not important. Such systems were common in early personal computers, in part because there was only one user. Interestingly, the world's first supercomputer, the CDC 6600, also had only one directory for all files, even though the machine was being used by many users at the same time. This decision is undoubtedly to simplify the software design.

2. Hierarchical directory system

Hierarchy (i.e. a directory tree). In this way, many directories can be used to group files in a natural way.

3. Path name

When organizing a file system with directory trees, there needs to be some way of specifying file names. There are two commonly used methods. In the first, each file is given an absolute pathname consisting of the path from the root directory to the file.

Another way to specify filenames is to use relative pathnames. It is often used with working directory. Users can specify a directory as the current working directory. At this time, all pathnames that do not start with the root directory are relative to the working directory.

4. Directory operations

The system calls for managing directories vary more from system to system than the system calls for managing files. To understand what these system calls are and how they work, an example (taken from UNIX) is given below.

Create: Create a directory. The contents of the directory are empty except for the directory entries "." and "...". Directory items "." and "..." are automatically placed in the directory by the system.

Delete: delete the directory. Only empty directories can be deleted. A directory containing only directory entries "." and "..." is considered to be an empty directory, and these two directory entries cannot normally be deleted.

Opendir: The contents of the directory can be read. For example, to list all the files in a directory, the program must first open the directory and then read the filenames of all the files in it. Same as opening and reading files, before reading a directory, the directory must be opened.

Closedir: After reading the directory, the directory should be closed to release the internal table space.

Readdir: The system call readdir returns the next directory entry of the open directory. Previously, the read system call was also used to read the directory, but this method has a disadvantage: the programmer must understand and deal with the internal structure of the directory. In contrast, readdir always returns a directory entry in the standard format, regardless of the directory structure used.

Rename: Directories and files are similar in many ways. Files can be renamed, as can directories.

Link: Link technology allows the same file to appear in multiple directories. This system call specifies an existing file and a pathname, and establishes a link from the file to the name indicated by the path. This way, the same file can appear in multiple directories. This type of link increases the count of the file's i-node counter (records the number of directory entries containing the file), and is sometimes called a hard link.

Unlink: Delete directory entry. If the unlinked file is only present in one directory (the usual case), it is removed from the file system. If it occurs in more than one directory, only the link for the specified pathname is removed, leaving links for other pathnames. In UNIX, the system call (discussed earlier) used to delete files is actually unlink.

5. File system performance

Common ways to optimize file system performance are:

Caching: The most common technique for reducing the number of disk accesses is block caching or buffer caching. A cache refers to a series of blocks that logically belong to disk but are actually kept in memory for performance reasons.

Block Read Ahead: Attempts to write blocks into the cache ahead of time before they are needed, thereby increasing the hit rate. Of course, the block read-ahead strategy only applies to files that are actually read sequentially. For random-access files, read-ahead has no effect.

Reduce disk arm movement: Put blocks that may be accessed sequentially together, preferably on the same cylinder, thereby reducing the number of disk arm movements.

Chapter 5 Input/Output

In addition to providing abstractions (for example, processes, address spaces, and files), the operating system controls all of the computer's I/O (input/output) devices. The operating system must send commands to the device, catch interrupts, and handle various errors from the device. It should also provide a simple and easy-to-use interface between the device and the rest of the system. If possible, this interface should be the same for all devices, which is called device independence. The code of the I/O part is an important part of the entire operating system.

I/O device principle

1. I/O equipment

I/O devices can be roughly divided into two categories: block devices and character devices.

Block devices store information in fixed-size blocks, each with its own address. Typically block sizes are between 512 bytes and 65536 bytes. All transfers are in units of one or more complete (consecutive) blocks. The basic characteristic of a block device is that each block can be read and written independently of other blocks. Hard disks, Blu-ray discs, and USB sticks are the most common block devices.

Another type of I/O device is the character device. A character device sends or receives a stream of characters in units of characters, regardless of any block structure. Character devices are not addressable and do not have any seek operations. Printers, network interfaces, mice (used as pointing devices), and most devices other than disks can be considered character devices.

2. Device Controller

I/O devices generally consist of two parts, mechanical parts and electronic parts. These two parts can often be treated separately to provide a more modular and general design. The electronic part is called a device controller or adapter, and on a personal computer it often takes the form of a chip on the motherboard, or a printed circuit board that plugs into a (PCI) expansion slot. The mechanical part is the device itself.

The controller's task is to convert the serial bit stream into byte blocks and perform necessary error correction. The byte block is usually first assembled bit by bit in a buffer inside the controller, and after the checksum is checked and the byte block is proven error-free, it is copied to main memory.

3. Direct memory access DMA

Direct memory access is a mechanism for transferring data quickly. Data can be transferred from an adapter card to memory, from memory to an adapter card, or from one piece of memory to another. The importance of DMA technology is that it does not require the participation of the CPU when using it for data transfer. Another advantage of using DMA to transfer data is that the data is transferred directly between the source address and the destination address without an intermediary.

4. Interruption

In computer science, interruption means that the computer CPU learns something, suspends the program being executed, and then executes the program that handles the event, and continues to execute the previous program after the program is executed. The whole process is called interrupt processing, or interrupt for short, and the events that cause this process are called interrupt events. Interrupts are the key to concurrent execution of computers and are fundamental to the operation of operating systems.

Classification: Interrupts are classified by event source, which can be divided into external interrupts and internal interrupts. Interrupt events from outside the CPU are called external interrupts, and those from inside the CPU are called internal interrupts.

Further subdivided, external interrupts can also be divided into maskable interrupts and non-maskable interrupts, while internal interrupts can be divided into soft interrupts and exceptions according to whether the event is normal.

I/O software principle

1. A key concept in designing I/O software is device independence. It means that it should be possible to write a program that can access any I/O device without specifying the device in advance.

Closely related to device independence is the goal of uniform naming. A file or a device name should be a simple string or an integer, it should not be device dependent. In UNIX systems, all storage disks can be integrated into the file system hierarchy in any way, so the user does not need to know which name corresponds to which device.

2. Another important issue for I/O software is error handling. In general, errors should be handled as close to the hardware as possible. When the controller detects a read error, it should try to correct the error itself if it can handle it. If the controller can't handle it, then the device driver should handle it, maybe just reread the piece of data once and it's correct.

3. Another key issue is synchronous and asynchronous transmission. Most physical I/O is asynchronous. After the CPU initiates the transfer, it goes to do other work until an interrupt occurs.

4. Another problem with I/O software is buffering. After data leaves a device, it usually does not go directly to its final destination. In order to alleviate the contradiction between the speed mismatch between the CPU and the I/O device and improve the parallelism between them, in modern computer systems, almost all I/O devices use buffers when exchanging data with the CPU.

5. The problem of shared equipment and exclusive equipment. Some I/O devices (such as disks) can be used by multiple users at the same time. Multiple users opening files on the same disk at the same time will not cause any problems. Other devices, such as tape drives, must be used exclusively by a single user, and cannot be owned by another user until that user is done using them. Having two or more users randomly write interspersed blocks of data to the same tape is not guaranteed to work. The introduction of exclusive (non-shared) devices also introduces various problems, such as deadlocks.

I/O implementation
I/O can be implemented in three fundamentally different ways: program-controlled I/O; interrupt-driven I/O; DMA I/O

1. Program control I/O

The simplest form of I/O is to let the CPU do all the work, an approach called program-controlled I/O.

Example: print a string
insert image description here
First, the data is copied to kernel space. The operating system then enters a closed loop, outputting one character at a time. The CPU is constantly polling the device to see if it is ready to receive another character. This behavior is often called polling or busy waiting.

Program-controlled I/O is very simple but has the disadvantage that it takes up all of the CPU's time until all I/O is complete.

3 2. Interrupt control I/O

This way of allowing the CPU to do something else while waiting for the printer to become ready is to use interrupts to do a context switch and run other processes.

An obvious disadvantage of interrupt-driven I/O is that interrupts happen on every character. Interrupts take time, so this approach will waste a certain amount of CPU time.

3. I/O using DMA

The idea is to have the DMA controller feed the printer one character at a time without bothering the CPU. Essentially, the DMA controller does all the work instead of the main CPU. This strategy requires special hardware (DMA controller), but frees the CPU to do other work during I/O.

DMA's big success is reducing the number of interrupts from printing each character once to printing each buffer.

I/O software layer

I/O software is usually organized into four levels. Each layer has a well-defined function to perform and a well-defined interface to adjacent layers. Features and interfaces vary from system to system.

insert image description here

1. Interrupt handler

While program-controlled I/O is occasionally beneficial, hardware interrupts are unpleasant and unavoidable for most I/O. It should be hidden deep within the operating system so that the rest of the system has as little contact with it as possible. The best way to hide them is to block the driver that initiates an I/O operation until the I/O operation completes and generates an interrupt.

2. Device Drivers

Every I/O device connected to a computer requires some device-specific code to control it. Such code is called a device driver and is typically written by the device's manufacturer and delivered with the device. Because each operating system requires its own drivers, device manufacturers typically provide drivers for several popular operating systems.

3. Device-independent I/O software

The basic function is: execute the common I/O function of all devices, and provide a unified interface to the user layer software.

4. I/O software in user space

Although most of the I/O software resides inside the operating system, a small portion resides in user space, including libraries that are linked with user programs, and even programs that run entirely outside of the kernel.

disk

1. Disk

Disks are equally fast to read and write, which makes them suitable as secondary storage (for paging, file systems, etc.).

Disks are organized into cylinders, each cylinder containing a number of tracks equal to the number of heads stacked vertically. The track is divided into several sectors. There are about 832 sectors per track on a floppy disk, and the number of sectors on each track on a hard disk can be as many as several hundred. The number of heads is about 1 16.

2. Disk classification

(1) IDE disk: Features are low price, strong compatibility, high cost performance, slow data transmission, does not support hot swap, etc.

(2) SCSI disk: high transmission rate, good read and write performance, stable operation, and multiple devices can be connected. It can support hot swapping and takes up less CPU, but the price is relatively expensive, and it is generally used on workstations or servers.

(3) SATA disk: simple structure, support hot swap

(4)USB

3. Disk partition:

Easy to manage and use; conducive to data security; save time looking for files

4. Disk formatting

Refers to formatting a partition into a different file system

5. Disk arm scheduling algorithm

How long it takes to read or write a disk block. This time is determined by three factors:

Seek time (the time it takes to move the disk arm to the proper cylinder); rotational latency (the time it takes to wait for the proper sector to spin under the head); actual data transfer time

For most disks, the seek time is dominant compared to the other two times, so reducing the average seek time can substantially improve system performance.

Algorithm: first come first serve; shortest seek first; elevator algorithm

6. Stable memory

Stable memory uses a pair of identical disks, with corresponding blocks working together to form an error-free block. When there are no errors the corresponding blocks are the same on both drives, and reading either will give the same result. In order to achieve this goal, the following three operations are defined: stable write; stable read; crash recovery

clock

Clocks, also known as timers, are critical to the operation of any multiprogramming system for a variety of reasons. The clock is responsible for maintaining time and preventing one process from monopolizing the CPU, among other functions.

keyboard & mouse

Every general-purpose computer comes with a keyboard and a monitor (and usually a mouse) that allow people to interact with it. Although the keyboard and monitor are technically separate devices, they work closely together. On a mainframe, there are often many remote users, each with a device consisting of a keyboard and a monitor connected together as a unit. These devices have historically been called terminals.

1. Keyboard

The numbers in the I/O port are the key numbers, called scan codes, not ASCII codes. The keyboard has no more than 128 keys, so only 7 bits are required to represent the key number. Bit 8 is set to 0 when the key is pressed and bit 8 is set to 1 when the key is released. It is the driver's job to keep track of the state of each key (pressed or up). So, all the hardware does is give interrupts for keys being pressed and released, and the software does the rest.

Although the keyboard and the monitor are logically separate devices, many users are used to seeing the characters they just type appear on the screen. This process is called echoing.

2. Mouse

Trackball mouse: The mouse has a rubber ball inside that protrudes through a round hole in the bottom of the mouse, and the rubber ball rotates when the mouse is moved over a rough surface. As the rubber ball rotates, it rubs against two rubber rollers placed on mutually perpendicular rollers. East-west motion causes the rollers to rotate parallel to the y-axis, and north-south motion causes the rollers to rotate parallel to the x-axis.

Optical mouse: Equipped with one or more light-emitting diodes and photodetectors at its bottom. Modern optical mice have image processing chips in them and take successive low-resolution pictures of what lies beneath them, looking for changes from image to image.

When the mouse moves a certain minimum distance in any direction, or a button is pressed or released, a message is sent to the computer.

Guess you like

Origin blog.csdn.net/fjj2397194209/article/details/131350168