laitimes

Explain the world's operating system

author:graceli

Explain the world's operating system

This article by the table case drawon, Yunjing founder based on years of experience, from the origin of the operating system, application classification, design classification, and resource use perspective of the operating system macro elaboration, by reading this content, you will have a more systematic, macroscopic, deeper understanding of the operating system. Each of us uses different operating systems at high frequency every day, and it is necessary to have a basic understanding and understanding of the operating system from macro and in-depth simplicity.

Front and back office systems

The front and back office systems are the original antiquity of the operating system. Before the birth of an operating system, the basic computer system was the front-end and back-end system architecture model.

Concept of front-end and back-office systems

Before the emergence of no operating system, the system is the front and back office system, for example, most embedded systems are now used MCU can run simple C language programs of the single-chip microcomputer when running naked, we think of the front and back office system, the following figure introduces the model of the front and back office system:

Explain the world's operating system

From the figure, we can see that there are the following characteristics

The application is an infinite loop, generally in the main function, an infinite loop will be written, this loop will have a certain delay, and the loop will be executed every once in a while. The corresponding function is called in the loop to complete the corresponding operation, which can be seen as the background behavior (background) of the background. An infinite loop scan is a poll.

The background is always running, and this Main function does not stop.

When there is an urgent task to be handled, the MCU provides an interrupt mechanism, and each interrupt vector is hooked up with an interrupt handling function for emergency processing. This behavior of interrupt handlers is called foreground. When an emergency occurs, the interrupt handling mechanism catches it immediately. Emergency events, on the other hand, are generally external IO triggers, and the trigger behaviors include high level to low trigger, low level to high trigger, and rising or falling edge triggering. There is also an internal timing trigger, etc.

The following is the model of the front-end and back-end systems

{
    void main () {
        for(;;) {
            InitSystem();
            InitUartIsr(OnUartDataReceived);
            for(;;) {
                LED0 = ON;
                DelayMillisecs(500);
                LED1 = OFF;
                DelayMillisecs(500);
            }
        }
    } 
    void OnUartDataReceived(byte[] buffer, int count) {
        // doing something.
    }
}           

Advantages of front-end and back-office systems

The mechanism of the front and back office system is relatively simple, and the requirements for programmers are relatively low. As long as you learn a little bit of C, you can do something like light up the LEDs. Lamp. It is possible to learn the front-end and back-end systems based on the single-chip microcomputer.

The following recommends some single-chip microcomputer books, you can use these books to learn the front and back office systems.

Explain the world's operating system

The advantages of the front and back office systems are as follows:

  • Low cost, under normal circumstances, a few cents, or even a few dollars can buy a single-chip microcomputer.
  • The demand is very large, as long as the system with some simple control logic, most of them use simple MCU, running a front-end and back-end system internally, as follows we give a distribution pyramid diagram of the operating system.
Explain the world's operating system
  • Coding for design and development is relatively simple. Generally, embedded engineers complete the development and design of front-end and back-end systems.

Disadvantages of front-end and back-office systems

  • The application scenario is relatively simple, control simple peripheral computers and simple calculations. For large and complex logic or interactions, front-end and back-end systems are difficult to handle. There are also some more powerful masters, using the front and back office system can complete the tasks that the operating system can complete, and even the operating system is difficult to complete (small is useful).
  • Resources are difficult to schedule systematically, and the CPU of the front and back office systems has always been busy in a circle, and resources are difficult to be scheduled systematically.
  • When developing a system, the code is highly coupled, you have me, I have you. In the front and back office systems, each software module added to a system may affect the previous function. Therefore, if you want to play the system of the front and back office systems well, you must dominate the overall situation and understand the program logic of others.

operating system

Basic concepts

The system software (or collection of programs) that controls and manages various hardware and software resources inside the computer system and effectively organizes the operation of multiple programs is the interface between the user and the computer.

The following shares above, the mainstream operating system

Windows operating system

Explain the world's operating system

MAC OS OPERATING SYSTEM

Explain the world's operating system

Chome OS operating system

Explain the world's operating system

Mobile IOS, Android, Windows Phone (past tense)

Explain the world's operating system

The operating system determines the way the system interacts, as well as the ecology, everyone has their own favorite operating system.

Operating system classification

Classified by family

The following is the family history of Unic:

Explain the world's operating system

As you can see from the diagram above, Unix (Unics) is actually the ancestor of most operating systems. Many operating systems evolved from Unix.

Unix family evolution diagram

Explain the world's operating system

As can be seen from the figure above:

  • At present, the very famous Apple operating system in the PC field, MAC OS, and the mobile operating system IOS are all rewritten from the Unix system.
  • Google's Android operating system, ChromeOS, is modified from Linux.
  • Ubuntu is the community open source version of Linux.
  • In the automotive system, the microkernel system QNX, which is said to be a strong implementation, comes from the Unix system.

Categorized by kernel type

Explain the world's operating system

Microkernel/macrokernel concept

The core functions of the operating system, task scheduling, memory and device abstraction and management. Then, for our convenience, it was integrated into system services, drivers, file systems and the like.

The programs we usually run, each running for tens of milliseconds, rotates back and forth, so that we look as if these programs are running "at the same time". The reason why applications can be scheduled by the operating system through time slices is because for the CPU, ordinary applications and the kernel of the operating system run at different privilege levels, which we call rings. Applications run on Ring 3, while kernels run on Ring 0.

With the development of technology, the operating system has become more and more complex, and there are more and more things in the kernel. People have also begun to consider whether the original architecture should be changed to improve the performance and stability of the operating system, mainly to streamline the kernel to reduce the complexity of development, and to isolate various programs as much as possible to ensure that the crash of one program will not implicate other programs.

The hotly discussed microkernel in the 80s is such an architecture.

Several kernel architectures

Explain the world's operating system

Theoretical design and practical engineering are all compromises. So there is the emergence of hybrid kernels, combining the different advantages of macrokernels and microkernels, and compromise design in two schemes. OS X and Windows fall into this category.

Advantages of microkernels

The microkernel considers preserving the most basic functions of the operating system, namely task scheduling, memory, and device abstraction and management, in the kernel of the operating system. All other functions are moved out of the kernel, implemented in user mode, and served to other applications in C/S mode.

The benefits brought by the microkernel are mainly stability and real-time, that is, the number of modules in the kernel is less, the structure is more streamlined and optimized, the programs and drivers that can affect the kernel are also reduced, and the stability is improved; In addition, real-time performance is reduced after the core is streamlined, and the delay of response is reduced. However, it is not that streamlining will lead to performance improvement, the microkernel makes only the most critical part of the kernel, and other modules and system functions are all run in user-mode space as independent modules, and the cost of communication is increased after the function is dispersed. However, the characteristics of the microkernel operating system are especially suitable for the control of industrial control systems, and the design is simple, and there are many applications in small systems. In addition, many real-time operating systems are designed using microkernel architecture.

Summarize a few words

  • Worse is better.
    • In the computer field, well-designed products end up failing.
    • Like Unix won Multics.
    • Lisp (General Purpose High-Level Computer Programming Language) is not as popular as C.
    • OSI's vision was ultimately accomplished by the TCP/IP protocol.
    • I believe that the domestic so-called real cloud operating system is ultimately completed by Yunjing - a new generation of cloud operating system (just kidding).
    • Microsoft's WPF, the MVVM design pattern is used vividly, and the design is so pure that it has not become popular.
  • ......
  • The macro kernel is a gorgeous palace.
  • The microkernel is a delicate villa.

Why doesn't Linux use a microkernel or a mixed kernel model?

Theoretical perfection problems are met with various compromises in practice. Because what you make is to be deployed to a real-life production environment. It's not about a piece that only looks perfect in the lab. The high modularity of the microkernel naturally comes at a cost, which is to increase the redundancy of code interaction and the loss of efficiency, which is a big problem.

Linus can write all these messy things alone, write them correctly over and over again, and run stably without bugs, while we, the scum, can't do it, can only rely on the protection mode to prevent hundreds of engineers from writing out the piece of garbage, can't move the blue screen, they are weak, but they question the practice of genius, and knowing that they are weak, but also imitating the practice of genius, are the manifestations of not recognizing reality.

Linux itself was initially implemented as a side-by-side project for Linus. The Monolithic Kernel, on the other hand, is more convenient than the Micro Kernel from an implementation point of view because it does not need to deal with message queues, etc.

Linus is not interested in microkernels, which everyone knows, and as long as he is there, the kernel will not consider microkernelization. He's a pragmatist, and he famously said: Talk is cheap. Show me the code。

Linus: "Gaah. Guys, this whole ARM thing is a fcking pain in the ass." Boosted DeviceTree. (Off topic, *domineering). This is the flamboyant and free-spirited side of Linus. Take a closer look.

Categorized by real-time

According to real-time, the operating system is divided into hard real-time and soft real-time. So what advantages are hard real-time and soft real-time, in fact, it is measured by the response time of the interruption.

Measure real-time:

  • The appropriate time for the interruption.
  • The corresponding time of the interrupt == Maximum time to turn off the interrupt + Time to protect the internal registers of the CPU + Execution time to enter the interrupt service function + Time to start executing the first instruction of the interrupt service routine (ISR).
  • The task switching time is the time from the current task is suspended to the time when the task to be switched starts running.
  • When a hard real-time operating system is faced with varying loads, from minimum to worst, time requirements must be met deterministically. It has nothing to do with the CPU being powerful, and the time must be deterministic.
  • A representative of a real-time operating system
    • Linux is the representative of real-time operating systems
    • Vxworks (Wind River) is the representative of hard real-time operating systems.
  • In the following table, a comparison of the real-time performance of real-time operating systems is provided here
  • WxWorksuCOS-IIRT-Linux2QNX6MACosXWindowsLinux-GP Hardware Platform MC6800033MHz-48660MHz-48633MHz-4866 Task Switching 3.8us< 9us Unknown 12.57us Interrupt Response < 3us< 7.5us25us7.54us

Basic knowledge related to programming in the operating system

Explain the world's operating system

The basic concept of process threading

  • Concurrency and isolation.
  • Context of Execution
  • The basic unit of execution and scheduling: thread
  • Resource ownership: process
  • A process is a container for resources that contains (one or more) threads. The basic unit of kernel scheduling is a thread (not exactly), not a process.
  • Threads in the same process share resources (address space, open files, signal handlers, etc.), but registers, stacks, PC pointers, etc. are not shared.

What exactly is the difference between a process and a thread? In fact, the thread is the basic unit of scheduling and execution, and the final code is executed in the thread. A process, on the other hand, is a container for resources, including one or more threads. Threads under the same process share resources.

The difference is shown in the following figure:

Explain the world's operating system

Linux thread process concept

Explain the world's operating system

Linux threads are user-level, i.e. there are no threads in the kernel.

  • All thread management is performed at the application layer.
  • The kernel doesn't care and is actually unaware of the thread's presence.

The concept of threading processes in Windows

Explain the world's operating system

As can be seen from the figure above, Windows and Linux obviously adopt different concepts.

Windows threads are kernel-level.

  • Windows is an example of these concepts.
  • The kernel maintains the context of threads and processes.
  • Scheduling actually runs on a thread-based basis.

Interprocess communication

With threads, process isolation, a communication mechanism between thread processes is required to ensure collaboration to complete tasks and share access data.

Communication between Windows processes

  • File mapping
  • Shared memory
  • Anonymous pipes (single-item, write at one end, read at the other)
  • Name the pipe.
  • Dynamic link libraries
  • Remote procedure calls (within or across machines)
  • UDS(Unix Domain Socket)
  • Windows-based messaging mechanism ...

Interprocess communication in Linux

  • Pipe, as well as famous pipes
  • Signal
  • Message queue (Message queue)
  • Shared memory (most efficient)
  • Semaphore
  • The main role is a means of synchronization (UDS) between processes, as well as between different threads of the same process (UDS) socket sockets

Although the above Window and Linux use different methods and conceptually different ways of communicating between processes, in fact, their basic principles are similar.

Inter-thread communication

  • Share data structures. Shared memory
  • Event delivery
  • Message Queuing
  • Email (ucosII)

Thread synchronization

Thread synchronization, that is, when a thread is operating on memory or peripherals, other threads cannot operate on this memory address or peripheral, until the thread completes the operation, other threads can operate, and other threads are in a waiting state, there are many ways to achieve thread synchronization, as follows.

  • Semaphore is generally used.
  • High-level languages such as Java are designed with this in mind, such as the synchronized keyword, wait, and notify methods.
  • Mutex can be used.
  • Critical section object.

Semaphores and mutexes

Semaphore (semaphore, or semaphore)

Take, for example, the operation of a parking lot. For simplicity's sake, suppose the parking lot has only three parking spaces, and all three spaces are empty at first. At this time, if five cars come at the same time, the janitor allows three of them to enter directly, and then lowers the barrier, and the remaining cars must wait at the entrance, and the subsequent cars also have to wait at the entrance. At this time, a car left the parking lot, and after the janitor learned about it, he opened the car barrier, put in the outside one, and if he left two more, he could put in two more, and so on. In the microscopic world, in the computer world, such as accessing hard disk space and reading data, resources are often limited. This mechanism can be used for effective coordinated control of access to resources.

Mutex (mutex)

Only one thread can enter a special semaphore at a time. The performance will be better than the semaphore. For some special application scenarios, only one thread can access at a time, and other threads can continue to run after the thread exits. For example, IO peripherals for operating systems, printers, real-life public toilets, and so on.