operating system (OS)

1. Definition of operating system

        The operating system (Operating System, OS) refers to the control and management of the hardware and software resources of the entire computer system, and reasonably organizes and schedules the work of the computer and the allocation of resources to provide users and other software with a convenient interface and environment. It is the most basic system software in a computer system .



2. Functions and goals of the operating system

① As the manager of system resources (these resources include software, hardware, files, etc.), what functions does the operating system need to provide?

     Supplementary knowledge: A process is the execution process of a program. Before execution, the program needs to be placed in memory so that it can be processed by the CPU.



②As the interface between users and computer hardware, the operating system needs to provide simple and easy-to-use services for its upper-level users and applications. What functions do they need to implement?

Command interface : Allows users to use
programs directly Interface : Allows users to use programs indirectly
GUI : The most popular graphical user interface in modern operating systems

Easy concept:
system call = system call command = generalized instruction


online command interface example (Windows system) online command interface = interactive command interface
Step 1: win key + R
Step 2: input cmd, press Enter, open the command interpreter
Step 3: Try using the time command

Example of offline command interface (Windows system)
Offline command interface = batch command interface
Use the search function of the windows system to search for *. bat files in the C drive, and open them with Notepad - one

Program interface : such as C:\Windows\System32\user32.d1l, the programmer calls user32.d11 in the program (the calling process is a system call) to realize functions such as creating windows. Can only be used indirectly through the user program.

GUI: Graphical User Interface (Graphical User Interface)
Users can use the graphic graphical interface to operate, and no longer need to memorize complicated commands and parameters. .Example
: In the Windows operating system, to delete a file, you only need to "drag" the file to the recycle bin.


③As the level closest to the hardware, the operating system needs to implement what functions on the basis of pure hardware?

Functions and goals that need to be provided: Realize the expansion of hardware machines.
Computers without any software support become bare metal. The operating system installed on the bare metal can provide resource management functions and user-friendly service functions, and transform the bare metal into a machine with stronger functions and more convenient use

Analogy:
hardware: hammer, saw, wood, nails...
operating system: excellent craftsman
The expansion of the operating system to hardware machines: through excellent craftsmen, these simple raw materials can be organized into houses, sailboats, and Pinocchio. . .
Ordinary users can directly use the house, sailboat, and Pinocchio provided by the craftsman without caring about how these things are organized and working at the
bottom
 

 

3. Characteristics of the operating system

1. Concurrency

Concurrent: Refers to two or more events occurring within the same time interval. These events occur simultaneously macroscopically, but alternately occur microscopically.
Often confused concept - - parallel : refers to two or more events at the same - - time at the same time.

The concurrency of the operating system refers to the existence of multiple running programs in the computer system at the same time.

A single-core processor (CPU) can only execute one program at a time, so the operating system will be responsible for coordinating the alternate execution of multiple programs (these programs are executed alternately on a micro level, but macroscopically they look like they are executing at the same time)
In fact, the operating system appeared with "multiprogramming technology". Thus, operating systems and program concurrency were born together.

2. Sharing

Sharing is resource sharing, which means that the resources in the system can be shared by multiple concurrently executing processes in the memory.

The so-called "simultaneous" is often macroscopic, and microscopic, these processes may access the resource alternately (ie time-sharing sharing). Life examples: .Mutually exclusive sharing
method
: use QQ and WeChat videos. The camera can only be assigned to one of the processes at the same time.
Simultaneous sharing method: use QQ to send file A, and use WeChat to send file B at the same time. From a macro perspective, both sides are reading and sending files at the same time,
indicating that both processes are accessing hard disk resources and reading data from them. From a microscopic point of view, the two processes access the hard disk alternately.

Concurrency refers to the existence of multiple running programs in a computer system at the same time.
Sharing means that resources in the system can be shared by multiple concurrently executing processes in memory.

3. Virtual

Virtual refers to changing a physical entity into several logical counterparts. The physical entity (the former) is what actually exists, while the logical counterpart (the latter) is what the user feels.

4. Asynchronous

Asynchrony means that in a multi-programming environment, multiple programs are allowed to execute concurrently, but due to limited resources, the execution of the process is not consistent to the end, but stops and starts, moving forward at an unpredictable speed. This is the process asynchrony. .

 

4. Development and Classification of Operating Systems 


  5. The operating mechanism and architecture of the operating system

   1. Command

 2. Processor

       According to the instructions, there are two processor states, the functions are as follows

3. The functions of the two programs are as follows:

4. The kernel of the operating system

The kernel is the underlying software configured on the computer, and is the most basic and core part of the operating system.
Those programs that implement the functions of the operating system kernel are kernel programs .

Analogy:
The architectural problems of an operating system are very similar to the management problems of an enterprise.
The kernel is the management of the enterprise , responsible for - - some important work. Only management can execute privileged instructions , and ordinary employees can only
execute non- privileged instructions. The switch between the user state and the core state is equivalent to the work handover between ordinary employees and the management.
Big core: When the company is small in size when it starts up, the people in the management will be responsible for most of the things. The advantage is high efficiency; the disadvantage is that the organizational
structure is chaotic and difficult to maintain. .Microkernel : As the size of the enterprise grows larger
and larger, the management is only responsible for some of the most core tasks. The advantage is that the organizational structure is clear and easy
to maintain; the disadvantage is that the efficiency is low.

6. Interrupts and exceptions

1. The concept and function of interruption

a. When an interrupt occurs, the CPU immediately enters the core state
b. When an interrupt occurs, the currently running process is suspended, and the interrupt is processed by the operating system kernel
c. For different interrupt signals, different processing
occurs Interruption means that the operating system needs to intervene to carry out management work. Since the management work of the operating system (such as process switching,
allocating I/O devices, etc.) requires the use of privileged instructions, the CPU needs to change from the user state to the core state. Interrupts can switch the CPU from user mode to
kernel mode, allowing the operating system to gain control of the computer
. With interrupts, multiple programs can be executed concurrently.

Question: How is the switch between user mode and core mode realized?
Answer: "User mode > core mode" is realized through interrupts. And interruption is the only way. The switching of "core mode → user mode" is to set the flag bit of the program status word (PSW) to "user mode" by executing a privileged instruction

2. Classification of interrupts


   Another way to classify:

3. External interrupt processing

Step1: After executing each instruction, the CPU will check whether there is an external interrupt signal.
Step2: If an external interrupt signal is detected, it is necessary to protect the CPU environment of the interrupted process (such as program status word PSW, program counter PC, various General register)
Step3: Transfer to the corresponding interrupt handler according to the interrupt signal type
Step 4: Restore the CPU environment of the original process and exit the interrupt, return to the original process and continue to execute
 

Seven. System calls

 1. What is a system call and what does it do

      As an interface between users and computer hardware, the operating system needs to provide some simple and easy-to-use services. It mainly includes command interface and program interface. Among them, the program interface is composed of a set of system calls .
 

"System call" is an interface provided by the operating system to applications (programmers/programmers). It can be understood as a special function that can be called by applications. Applications can issue system call requests to obtain services from the operating system.

Applications request services from the operating system through system calls . All kinds of shared resources in the system are managed by the operating system, so in the user program
, all resource-related operations (such as storage allocation, I/0 operations, file management, etc.) must be done through system calls Make a service request to the operating system
, and the operating system will complete it on its behalf. This can ensure the stability and security of the system and prevent users from performing illegal operations .
 

2. The difference between system calls and library functions

3. The process behind the system call

Pass the system call parameters > Execute the trapped instruction ( user state ) → execute the corresponding service program of the system call ( core state) → return to the user program
a. The trapped instruction is executed in the user state, and an internal interrupt is triggered immediately after the trapped instruction is executed, so that the CPU Enter the core state
b. The system call request is issued in the user state, and the corresponding processing of the system call is performed in the core state
c. The trapped instruction is the only instruction that can only be executed in the user state and not in the core state

 Eight. Process

1. Definition of process

Program : is a sequence of instructions

single-program memory multi-program memory
                                                

The program segment, data segment, and PCB constitute the process entity (process image). Generally, we refer to the process entity as a process for short. For example, the so-called creation of a process is essentially the creation of the PCB in the process entity; and the cancellation of the process is essentially the cancellation of the PCB in the process entity.
Note : PCB is the only sign of the existence of a process!
From different perspectives, a process can have different definitions. The more traditional and typical definitions are: . 1. A process is an execution process
of a program . 2. A process is an activity that occurs when a program and its data are executed sequentially on a processor . 3. A process is a process in which a program with independent functions runs on a data set . It is the resource allocation and scheduling of the system - an independent unit process is the running process of the process entity , and it is an independent system for resource allocation and scheduling . unit. Note: Strictly speaking, the process entity and the process are not the same, the process entity is static , and the process is dynamic . However, unless the topic specifically examines the difference between the two, it can be considered that the process entity is the process. Therefore, we can also say that "the process consists of three parts: program segment, data segment, and PCB"




 

2. The composition of the process

A process (process entity) consists of three parts : program segment, data segment, and PCB .

3. Organization of the process

(1) Link method

(2) Index method

4. Characteristics of the process

5. Process status and transition

(1) Three basic states

(2) Process state transition

5. Process control

(1) Definition of process control

The main function of process control is to effectively manage all processes in the system. It has functions such as creating new processes, canceling existing processes, and realizing process state transitions .
Simplified understanding: Anyway, process control is to realize process state transition

(3) Realize process control

Implement process control with primitives . The characteristic of the primitive is that no interruption is allowed during execution , and it can only be completed in one go.
This uninterruptible operation is an atomic operation .
Primitives are realized by using " interrupt off command " and " interrupt on command "

Obviously, the privilege of turning off/on the interrupt command is very large, and it must be a privileged command that is only allowed to be executed in the core state (4) Related primitives of process control

A. Update the information in the PCB (such as modifying the process status flag, saving the operating environment to the PCB, and restoring the operating environment from the PCB) a. All
process control primitives must modify the process status flag
b. Deprive the CPU usage of the current running process It is necessary to save its operating environment
c. It is necessary to restore the operating environment before a process starts running
B. Insert the PCB into the appropriate queue
C. Allocate/recycle resources

6. Process communication

Process communication refers to the exchange of information between processes.
A process is a unit for allocating system resources (including memory address space) , so the memory address spaces owned by each process are independent of each other .
 

(1) Shared storage

(2) Pipeline communication

a. The pipeline can only use half-duplex communication , and only one-way transmission can be realized within a certain period of time. If two-way simultaneous communication is to be achieved ,
two pipes need to be set up.
b. Each process must access the pipeline mutually exclusive . c
. Data is written into the pipeline in the form of a character stream. When the pipeline is full , the write() system call of the writing process will be blocked, waiting for the reading process to
take the data away. When the reading process takes all the data away, the pipeline becomes empty, and the read() system call of the reading process will be blocked at this time .
d. If it is not full , reading is not allowed. If it is not read empty , no write is allowed .
e. Once the data is read, it will be discarded from the pipeline , which means that there can be at most one reading process, otherwise there may be cases of reading wrong data
.
(3) Message delivery

Data exchange between processes takes formatted messages (Message) as the unit. Processes exchange data through the two primitives "send message/receive message" provided by the operating system . (Messages are passed between processes through mailboxes)

 

  Nine. The concept of thread and multithreading model

1. What is a thread and why should it be introduced

A thread can be understood as a " lightweight process. " A thread is a basic CPU execution unit and the smallest unit of program execution flow . After the introduction of threads, not only processes
can be concurrent , but also threads within a process can be concurrent, which further improves the concurrency of the system, making it possible to process various tasks concurrently within one process (such as QQ
video, text chat, file transfer). After the thread is introduced, the process is only used as the allocation unit of system resources other than the CPU (such as printers, memory address spaces, etc. are all allocated to the process).
   (1) After the introduction of the thread mechanism, what changes

Analogy:
Going to the library to read a book.
Switch the running environment of the process : There is an unknown person who wants to use the desk, you need to take away your book, and he puts his book on the desk
Thread switching in the same process = your roommate wants to use this desk, you can Do not take away the books on the table
(2) attribute of the thread

(3) Implementation of threads

User-level threads :

Kernel-level threads , also known as kernel-supported threads

    In a system that supports both user-level threads and kernel-level threads, a combination of the two can be used: map n user-level threads to m kernel-level threads (n>=m)


the operating system can only "see" Kernel-level threads. Threads, so only kernel-level threads are the unit of processor allocation.
For example: In the model on the left, the process consists of two kernel-level threads and three user-level threads. From the user's perspective, there are three threads in this process. But even if the process
runs on a 4-core processor computer, it can only be assigned to two cores at most, and only two user threads can be executed in parallel at most.
 

2. Multi-threaded model

(1) In a system that supports user-level threads and kernel-level threads at the same time, the problem of mapping several user-level threads to several kernel-level threads leads to the problem of "multithreading model".

Many-to-one model : Multiple users and threads are mapped to one kernel-level thread. Each user process corresponds to only one kernel-level thread.
Advantages : The switching of user-level threads can be completed in user space without switching to the core state. The system overhead of thread management is small and the efficiency is high. Disadvantages: When
a user-level thread is blocked, the entire process will be blocked, and the degree of concurrency is low. high. Multiple threads cannot run in parallel on a multi-core processor
 

(2) In a system that supports user-level threads and kernel-level threads at the same time, the problem of mapping several user-level threads to several kernel-level threads leads to the problem of "multithreading model".

One-to-one model : a user and a thread are mapped to a kernel-level thread. Each user process has the same number of kernel-level threads as user-level threads.
Advantages : When a thread is blocked, other threads can continue to execute, with strong concurrency. Multiple threads can be executed in parallel on multi-core processors.
Disadvantages: A user process will occupy multiple kernel-level threads. Thread switching is completed by the operating system kernel and needs to be switched to the kernel state. Therefore, the cost of thread management is high and the overhead is high.
(3) In a system that supports both user-level threads and kernel-level threads, the problem of mapping several user-level threads to several kernel-level threads leads to the problem of "multithreading model".

Many-to-many model : n users and threads are mapped to m kernel-level threads (n>=m). Each user process corresponds to m kernel-level threads.
It overcomes the disadvantage of low concurrency in the many-to-one model, and also overcomes the disadvantage that one user process in the one-to-one model occupies too many kernel-level threads and consumes too much overhead.
 

10. Scheduling

1. The concept of processor scheduling

When there are a bunch of tasks to be processed, but due to limited resources, these things cannot be processed at the same time. This requires determining some kind of rules to determine the order in which these tasks are processed , which is the problem of "scheduling" research.
In a multi-programming system, the number of processes is often more than the number of processors, so it is impossible to process each process in parallel at the same time.
Processor scheduling is to select a process from the ready queue according to a certain algorithm and assign a processor to it to run , so as to realize the concurrent execution of processes.
(1) Three levels of scheduling---advanced scheduling

Due to the limited memory space, sometimes all the jobs submitted by the user cannot be put into the memory, so some kind of rules need to be determined to determine the order in which the jobs are loaded into the memory.
Advanced scheduling (job scheduling) . According to a certain principle, select one (or more) jobs from the jobs in the backup queue on the external memory, allocate memory and other necessary resources to them, and create corresponding processes (create PCBs) so that they (they) can get competitive processing machine rights .
Advanced scheduling is scheduling between auxiliary storage (external storage) and memory . Each job is only called in once and called out once. The corresponding PCB will be created when the job is called in , and the PCB will be canceled when the job is called out . Advanced scheduling mainly refers to the problem of calling in, because only the timing of calling in needs to be determined by the operating system, but the timing of calling out must be called out after the end of the job.
(2) Three levels of scheduling---intermediate scheduling

After the virtual storage technology is introduced, the process that cannot run temporarily can be transferred to the external memory to wait. When it has the operating conditions again and the memory is a little free, it will be transferred to the memory again.
The purpose of doing this is to improve memory utilization and system throughput .
The state of the process that is temporarily transferred to the external memory to wait is the suspended state . It is worth noting that the PCB will not be transferred to the external storage together, but will be resident in the internal memory . The PCB will record the storage location of the process data in the external memory, process status and other information, and the operating system will monitor and manage each process through the PCB in the memory. The suspended process PCB will be placed in the pending queue .
Intermediate scheduling (memory scheduling) is to decide which process in the suspended state will be reloaded into memory.
A process may be transferred out and transferred into memory many times, so the frequency of intermediate scheduling is higher than that of advanced scheduling . .
 

Supplementary knowledge : Suspended state of process and seven-state model

The status of the process that is temporarily transferred to the external memory to wait is the suspended state (suspend state, suspend).
The suspended state can be further subdivided into two states : ready suspension and blocking suspension.
Five-state model > seven-state model

Pay attention to the difference between "suspended" and "blocked". In both states, the service of the CPU cannot be obtained temporarily, but in the suspended state, the process image is transferred to the external storage, while in the blocked state, the process image is still in memory. Some operating systems will divide the ready hang and blocking hang into two hang queues, and even further subdivide the blocking hang process into multiple queues according to different blocking reasons.
 

(3) Three levels of scheduling --- low-level scheduling

The main task of low-level scheduling (process scheduling) is to select a process from the ready queue according to a certain method and strategy, and assign a processor to it. Process scheduling is the most basic scheduling in the operating system, and process scheduling must be configured in general operating systems. The frequency of process scheduling is very high, generally every tens of milliseconds.

2. Connection and comparison of three-tier scheduling

3. Timing of process scheduling

       Process scheduling (low-level scheduling) is to select a process from the ready queue to assign a processor to it according to a certain algorithm.

Problem: (1) The process cannot be scheduled and switched in the critical section of the operating system kernel program √
           (2) Processor scheduling cannot be performed when the process is in the critical section X
critical resources : resources that are only allowed to be used by one process within a time period. Processes require mutually exclusive access to critical resources.
Critical Section : The section of code that accesses critical resources.
Kernel program critical section: generally used to access some kind of kernel data structure, such as the ready queue of the process (composed of the PCB of each ready process)

4. The way of process scheduling

5. Process switching and process

The difference between "narrow process scheduling" and "process switching":
narrow process scheduling refers to selecting a process to run from the ready queue . (This process can be the process that has just been suspended for execution,
or it can be another process. In the latter case, process switching is required. )
Process switching refers to the process in which one process gives up the processor and another process occupies the processor.


Generalized process scheduling includes two steps of selecting a process and process switching.
The process of process switching is mainly completed:
(1). Saving various data of the original running process
(2). Restoring various data of the new process
(such as: program counter, program status word, various data registers, etc. Machine site information, these information are generally stored in the process control block)
Note: process switching is costly , so if the process scheduling and switching are performed too frequently, the efficiency of the entire system will inevitably be reduced ,
and the system will spend most of its time in Process switching is achieved, and the time actually used to execute the process is reduced.
 

6. Evaluation index of scheduling algorithm

(1) CPU utilization

Since the early CPUs were extremely expensive, people would want to let the CPU work as much as possible
CPU utilization : refers to the proportion of the CPU's "busy" time to the total time.

Eg: A certain computer only supports single-channel programs. A certain job needs to run on the CPU for 5 seconds at the beginning,
then print it out with a printer for 5 seconds, and then execute it for 5 seconds before it ends. During this process,
what are the CPU utilization and printer utilization?

 (2) Throughput of the system

For computers, it is hoped that they can process as many jobs as possible in as little time as possible.
Throughput : the number of jobs completed per unit time

E8: It takes 100 seconds for a computer system to process 10 jobs, what is the system throughput?     Answer: 10/100 = 0.1 job/second
 

(3) Turnaround time

For computer users, he is very concerned about how long it takes for his homework from submission to completion.
Turnaround time is the time interval between when a job is submitted to the system and when it is completed. It includes four parts: the time that the job waits for job scheduling (advanced scheduling) on ​​the external memory backup queue, the time that the process waits for process scheduling (low-level scheduling) on ​​the ready queue, the time that the process executes on the CPU, and the process waits for I/O The time the operation completed. The latter three items may occur multiple times throughout the processing of a job.

Thinking : Some jobs have a short running time and some jobs have a long running time. Therefore, in the case of the same turnaround time, jobs with different running times must give users a different feeling. Therefore, people have proposed the weighted
turnaround time .

 

 

 

Guess you like

Origin blog.csdn.net/Wang_kang1/article/details/105702013