Architecture and operating system pickups

Part1. Architectural foundation

1. Von Neumann architecture

  1. The data and instructions processed by the computer are all represented in binary numbers
  2. Sequential execution program
    1. In the process of computer operation, the program to be executed and the data to be processed are first stored in the main memory (memory). When the computer executes the program, it will automatically and sequentially take out instructions from the main memory and execute them one by one. This concept is called sequential execution program.
  3. Computer hardware consists of arithmetic unit, controller, memory, input device and output device.

2. In-machine representation of data

Binary representation

  1. Number of machines

    1. Since the symbols and numbers in the computer are the same, they must be represented by a string of binary numbers, so the positive and negative signs must also be represented by 0 and 1.
    2. With the highest bit 0 for positive, 1 for negative, the digital representation of the positive and negative signs in the machine is called “machine number”, and the corresponding external machine number with positive and negative signs is called “truth value”, a truth value represented as a binary string of machine number process is called coding.
  2. Source code A source code is the sign bit plus the absolute value of the truth value, that is, the first bit represents the symbol and the remaining bits represent the value, for example if it is 8-bit binary: [+1] = 0000 0001 [-1] = 1000 0001 the first bit is a sign bit, so the value range of the 8-bit binary is: [1111 1111, 0111, 1111] namely [-127, +127] source code is the most easily understood and calculated expression of human brain.

  3. The expression method of inverse code is: the inverse of a positive number is itself, the inverse of a negative number is based on its original code, the symbol bit is unchanged, the rest of each bit is inverse. [+1] = Source code :[0000 0001] = Inverse code :[0000 0001] [-1] = Source code :[1000 0001] = Inverse code :[1111 1110] It can be seen that if an inverse code represents a negative number, the human brain cannot intuitively see its value, and usually needs to convert it to the original code for calculation.

  4. Complement code the complement code is expressed as follows: the complement of a positive number is itself, and the complement of a negative number is based on its original code, with the sign bits unchanged, the rest bits reversed, and finally +1. (i.e. +1 on the basis of inverse code) [+1] = source code :[0000 0001] = Complement code :[0000 0001] [-1] = Source code :[1000 0001] = Complement code :[1111 1111] It is also often necessary to convert to source code and calculate its value.

  5. Fixed point and floating point numbers Fixed point numbers are numbers with a fixed decimal point. There is no specific decimal bit in a computer; the position of the decimal point is the default by convention. Generally fixed after the lowest number of machines, or fixed after the sign bit. The former is called a fixed-point pure integer and the latter a fixed-point pure decimal. The representation of fixed point number is simple and intuitive, but the range of numerical representation is too small, and it is easy to produce overflow during operation.

    A floating point number is a number where the decimal position can be changed. In order to enlarge the range of numerical representation and prevent overflow, the floating-point representation is adopted. Floating-point notation is similar to scientific notation in decimal notation.

    In computers, floating point numbers are usually divided into order code and mantissa two parts to represent, the order code is generally expressed by the complement fixed integer, mantissa is generally expressed by the complement or the original code fixed decimal. In order to ensure no loss of significant digits, the mantissa is formatted, which is usually referred to as scientific counting method, that is, the highest bit of the mantissa is guaranteed to be 1, and the actual value is adjusted through the step code. Rank symbol represents the sign bit of the exponent, rank code represents the power, number symbol represents the sign bit of the mantissa, and mantissa represents the small value after formatting.

    N = mantissa x radix order code (exponent)Copy the code

Bit, Byte, Word

Bit: The smallest unit of data in an electronic computer, each of which can only be 0 or 1.

Byte: A basic unit of storage space, consisting of eight binary bytes. One Byte can store one English letter or half of a Chinese character. In other words, one Chinese character occupies two bytes of storage space.

Word: composed of a number of bytes, the number of bits of the Word is called the Word length, different grades of machine has different Word length. For example, on an 8-bit machine, one word is equal to one byte. If it is a 16-bit machine, then its 1 word is made up of 2 bytes, and the word length is 16 bits. A word is the unit of computer data processing and operation.

Byte order

Byte order (byte order) refers to the storage order of data that occupies more than one byte type in memory. There are two types of byte order: small endian and big endian.

Little Endian: Low-byte data is stored at the low-memory address, and high-byte data is stored at the high-memory address. Big Endian: High-byte data is stored at a low-byte address, and low-byte data is stored at a high-byte address.

X86 based PCS are small-endian, while some embedded platforms are big-endian. All network protocols also transmit data in big-endian mode, which is sometimes referred to as network endian.

For example, the number 0x12345678 is stored in the following order on the two different byte order cpus:

Big Endian (Big Endian) low address high -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- > + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + 12 34 56 78 | | | | | + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + Little Endian (Little Endian) low high addresses -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- > + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + | 78 | | to 56 34 | | 12 + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - +Copy the code

The storage order of the union union is that all members are stored from the lower address. Using this feature, we can determine whether the CPU reads and writes the memory in little-Endian mode or big-Endian mode. Sample code:

union test {
    short value;
    char str[sizeof(short)];
}example

void main(a)
{
    example.value = 0x0102;
    if (sizeof(short) = =2) {
        if (example.str[0] = =1 && example.str[1] = =2) {
            printf("Big endian order");
        } else if (example.str[0] = =2 && example.str[1] = =1) {
            printf("Little endian order");        
        } else {
            printf("Results unknown"); }}}Copy the code

Byte alignment

Memory space in the modern computers are divided according to the byte, theoretically seems to access to any type of variables can be from any address, but the reality is when they visit a specific type variables are often in a particular memory address access, which requires all kinds of data according to certain rules in space arrangement, Instead of sequentially discharging one after the other, that’s alignment.

Why byte alignment?
  • The fundamental reason is efficiency. Byte alignment improves the speed of accessing data.
  • Some platforms can only access certain types of data at certain addresses

For example, some platforms read data from even address every time. For an int variable, if stored from even address unit, it takes only one read cycle to read the variable, but if stored from odd address unit, it takes two read cycles to read the variable.

Principles of byte alignment
  • Alignment rules for data members: Structs or unions of data members where the first data member is placedoffsetFor example, if an int is 4 bytes on a 32-bit machine, it will be stored from the address of 4 bytes.
  • Struct as members: If a struct has some struct members, the struct members are stored from an integer multiple of their internal largest element size. (struct aThe entitiesstruct b.bAre there inchar,int,doubleAnd so on, thenbYou should start with multiples of 8.)
  • The total size of the structure, which is just the total size of the structuresizeofThe result must be an integer multiple of the largest internal member, and the less than one must be made up.

Part2. Operating system basics

1. Services provided by the OPERATING system

Five functions of the operating system:

  • Job management
  • File management
  • Storage management
  • I/O device management
  • Process and processor management

2. Interrupt and system call

interrupt

Interrupt: During the execution of a computer program, something special occurs that causes the CPU to suspend the execution of the program and switch to the program that handles the event. There are three types of interrupts:

  1. The interruption caused by computer hardware abnormality or failure is called internal abnormal interruption;
  2. The interrupt caused by the execution of the interrupt instruction in the program is called a soft interrupt (this is also the interrupt related to the system call we will describe)
  3. An interrupt caused by a request from an external device is called an external interrupt.

In short, the understanding of interrupts is the handling of some particular thing.

One concept closely related to interrupts is the interrupt handler. When an interrupt occurs, the system needs to handle the interrupt. The handling of these interrupts is carried out by special functions in the operating system kernel.

Another concept closely related to interrupts is interrupt priority, which describes the level of interrupts that the processor can receive while an interrupt is being processed. Interrupt priority indicates how urgent the interrupt needs to be handled. Each interrupt has a priority. When the processor processes an interrupt, only interrupts with a higher priority can be accepted and processed by the processor. Interrupts with a lower priority than the interrupt currently being processed will be ignored. Interrupt priority:

Machine Error > Clock > Disk > Network Devices > Terminals > Software InterruptionCopy the code

When software interrupts are found, all other interrupts can occur and be handled; However, when disk interrupts occur, only clock interrupts and machine error interrupts can be handled.

The system calls

Processes execute at two levels on the system: the user level (user state) and the core level (system state).

Generally, programs are executed in user mode. However, when programs need to use services provided by the operating system, such as opening a device, creating files, or reading and writing files, they need to invoke service requests to the operating system. This is called system call.

Linux system has a special function library to provide these access to request operating system services. This function library contains the interface of external services provided by the operating system. When a process makes system calls, its running state will change from user state to core state. However, the process itself is not actually doing anything at this time, this time the kernel is doing the corresponding operation, to complete the process request.

The relationship between system calls and interrupts is that when a process makes a system call request, a software interrupt is generated. After the software interrupt is generated, the system will handle the soft interrupt, and the process will be in the core state.

Differences between user state and core state:

  1. User processes can access their own instructions and data, but not kernel instructions and data (or other processes’ instructions and data). However, kernel processes can access kernel and user addresses.
  2. Some machine instructions are privileged instructions, and executing privileged instructions in user mode causes errors.

Instead of running as a collection of processes parallel to user processes, the kernel runs for user processes.


More dry articles

Blog:www.qiuxuewei.com
Wechat Official Account:@ The way developers grow

A public account with no chicken soup and only dry goods