Operating system facebook collection

1. Concurrency and parallelism

Concurrency: When there are multiple threads operating, if the system has only one CPU, only one thread executes in each time period, and other threads hang

Parallelism: When the system has more than one CPU, the operations of threads may not be concurrent. Each CPU can be assigned to a thread, and threads can proceed concurrently.

Concurrency is the sequential execution of multiple events, and parallelism is the simultaneous execution of multiple events.

2. Multi-thread and multi-process

Multi-process : Refers to the computer executing multiple processes (multiple software) at the same time. A process is related to resources and is an independent unit for resource allocation and scheduling by the system.

process entity

It is composed of program segment, related data segment and PCB (program control block, a data structure specially set up to enable the programs participating in concurrent execution to run independently, and it is the unique identifier of the existence of the process).

Classification of processes

There are system processes and user processes. The processes used to complete various functions of the system are called system processes, and all processes started by users are user processes.

Multithreading : Multithreading is an execution model, including many-to-one, one-to-one, and many-to-many models.

Example :

Multithreading Executes multiple threads at the same time. For example, use a browser to download, listen to songs, watch videos, and browse web pages at the same time

Multiprocessing executes multiple programs at the same time. For example, colleagues run WeChat, QQ, and various browsers.

pros and cons multi-Progress Multithreading
advantage Simple programming and debugging, high reliability Fast creation, destruction and switching, small memory and resource occupation
shortcoming Create, destroy, and switch slowly, and occupy a lot of memory and resources Complicated programming and debugging, poor reliability

2.1 What should be paid attention to when using multi-threading

All resources of the process are shared between threads. When multiple threads operate on the same variable, the result may be incorrect.

Therefore, special attention should be paid to the issue of thread safety .

Usually there are many ways to ensure thread safety

  • use thread lock

  • Use a serial queue

  • Use thread-safe classes

  • Use semaphores or runloops to make asynchronous look like synchronous execution

  • Note that tasks may themselves be asynchronous

3. The difference between thread and process

A process is the smallest unit of resource allocation, and a thread is the smallest unit of CPU scheduling

Process is the basic unit of resource (CPU, memory, etc.) allocation, and it is an instance of program execution. When the program is running, the system will create a process, allocate resources for it, and then put the process into the process ready queue. When the process scheduler selects it, it will allocate CPU time for it, and the program starts to run.

A thread is the smallest unit of program execution. It is an execution flow of a process and the basic unit of CPU scheduling and dispatch. A process can be composed of many threads, and all resources of the process are shared between threads. Each thread has its own stack. and local variables. Threads are independently scheduled for execution by the CPU. In a multi-CPU environment, multiple threads are allowed to run at the same time. Similarly, multi-threading can also implement concurrent operations, and each request is allocated a thread to process.

The difference between process and thread

  • A process is the basic unit of resource allocation, and a thread is the smallest unit of program execution; each process can execute multiple threads, and a thread is the smallest unit of operation performed by a process;

  • Threads are lightweight processes that consume relatively little resources and switching costs, but are not conducive to resource management and protection;

  • Processes are independent, but individual threads are not necessarily. Threads in the same process are very likely to affect each other.

  • A process has its own address space, and a thread has a shared address space

  • Threads belonging to the same process, the heap is shared, and the stack is private.

  • All threads belonging to the same process have the same address space.

The number of threads a process can create is determined by the available virtual space and the size of the thread's stack

4. Big and small endian storage

Big-endian mode: refers to the high byte of the data, which is stored in the low address of the memory, and the low byte of the data is stored in the high address of the memory;

Little endian mode: refers to the high byte of the data, which is stored in the high address of the memory, and the low byte of the data is stored in the low address of the memory.

Advantages and disadvantages:

Advantages of big-endian mode: the sign bit is in the first byte of the memory of the data represented, which is convenient for quickly judging the positive and negative and size of the data (we usually read the order) Advantages of little-endian mode:

  1. The low address of the memory stores the low byte, so there is no need to adjust the content of the byte when converting the data (Note: For example, when converting 4 bytes of int to 2 bytes of short, the int data is stored directly Just give the first two bytes to short, because the first two bytes are just the lowest two bytes, which conforms to the conversion logic);

  2. When the CPU does numerical calculations, it fetches data from the memory in order from low to high for calculation until the highest sign bit is refreshed at the end. This operation method will be more efficient.

Judgment:

Method 1: By coercing multi-byte data into single-byte data, and then detecting by judging whether the initial storage location is the high byte or low byte of the data

Method 2: Use the storage order of the union (all members share the same memory) to detect that all members are stored from the low address - the array is from low to high

 

5. What is the communication method between processes? Communication method between threads?

5.1, between processes

  1. Pipeline (pipe): slow speed, limited capacity, only parent-child process communication; (the communication method is anonymous, so it can only be used for inter-process communication with kinship)

    Parent-child process: Create another completely independent process based on one process

  2. Named pipe (FIFO): Any process can communicate, but the speed is slow; (provide a path name associated with it)

  3. Message Queue (MessageQueue): the capacity is limited by the system;

  4. Semaphore: It cannot transmit complex messages, does not have data exchange function, and can only be synchronized. It is used to manage critical resources (counter, PV operation)

    Prevent multiple programs from accessing a shared resource at the same time and cause a series of problems. At any time, only one execution thread can access the critical area of ​​​​the code

  5. Shared memory (SharedMemory): It is easy to control the capacity, fast (no corresponding mutual exclusion mechanism is provided) but synchronized

  6. Socket (Socket): An inter-process communication mechanism that can be used for process communication between different processes.

  7. Signal (sinal): A signal is a more complex communication method used to notify the receiving process that an event has occurred.

5.1.4 Critical Sections, Critical Resources

Critical resource: A resource that is only allowed to be used by one process at a time.

Critical section: A segment of program code that accesses critical resources.

5.2, between threads

Mainly used for thread synchronization (no communication mechanism for data exchange like in process communication)

  1. Lock mechanism: including mutex, condition variable, read-write lock

    • Mutexes provide a means of preventing data structures from being modified concurrently in an exclusive manner.

    • Read-write locks allow multiple threads to read shared data at the same time, while writing operations are mutually exclusive.

    • A condition variable can atomically block a process until a certain condition is true. The testing of the condition is done under the protection of a mutex. Condition variables are always used with mutexes.

  2. Semaphore mechanism: including unnamed thread semaphore and named thread semaphore

  3. Signal mechanism: similar to signal processing between processes

5.2.1. Thread has several states, how to switch?

New New state: Create a new thread object;

Runnable runnable state: waiting for the operating system to allocate resources (such as CPU), waiting for IO connection, running state;

Blocked is blocked:

Waiting waiting state:

Timed Waiting Timed waiting state:

Terminated terminated:

6. What are the states of the process? How did you convert it?

Creation state: the process is being created, but has not yet reached the ready state

Ready state: The process is ready to run, that is, the process has obtained all the required resources except the processor, and can run once the processor resource is obtained

Running: the process is running on the processor

Blocking state: Also known as waiting state, the process is waiting for a certain event and suspends operation, such as waiting for a certain resource to be available or waiting for the IO operation to complete. Process cannot run even if the processor is idle

Final state: The process is disappearing from the system, it may be that the process terminated normally or exited for other reasons

6.1 What are the scheduling

  1. Job Scheduling--Advanced Scheduling

    It is used to decide which jobs in the backup queue on the external storage are transferred into the memory, and are in the ready queue of the memory, ready to be executed.

  2. Process scheduling--low-level scheduling

    Determines which process in the ready queue will get the processor

  3. Exchange Scheduling--Intermediate Scheduling

    The purpose is to improve memory utilization and system throughput

6.2. Reasons for process scheduling

  1. Executing process completes

  2. Executing processes are suspended due to I/O requests or other events.

  3. time slice complete

  4. A certain primitive operation is performed during process communication or synchronization, such as P operation (wait operation) blocking

  5. high priority entry

6.3 Scheduling method

1. Non-deprivation method (non-preemption method) - first come, first served, short job

Once occupying CPU, until complete or block

Do not use real-time tasks, do not use short jobs; used in batch processing systems

2. Deprivation mode (preemption mode) ——short job, high response ratio priority

Under certain circumstances, a process can be deprived of a processor

The principles of preemption are: short job (process) priority principle, time slice principle, and priority principle.

7. Process status and replacement

7.1. Three-state model:

Running state: A state in which a process occupies the processor and is running. The process has acquired the CPU and its program is executing. In a uniprocessor system, only one process is executing; in a multiprocessor system, multiple processes are executing.

Ready (ready) state: the process has the conditions to run, waiting for the system to allocate a processor to run the state. When the process has been allocated all the necessary resources except the CPU, it can be executed immediately as long as the CPU is obtained again. The state of the process at this time is called the ready state. There may be multiple processes in the ready state in a system, and they are usually arranged in a queue, called the ready queue.

Wait (wait) state: also known as blocking state or sleep state, refers to the state that the process does not have the conditions to run and is waiting for a certain time to complete. Also known as waiting or sleeping state, a process is waiting for a certain event to occur (such as requesting I/O and waiting for I/O to complete, etc.) and temporarily stops running. At this time, even if the processor is assigned to the process, it cannot run, so it is called The process is blocked.

The specific reasons for the process state transition are as follows:
Running state → waiting state: waiting to use resources; such as waiting for peripheral transmission; waiting for manual intervention.
Waiting state→ready state: resources are satisfied; if peripheral transmission ends; manual intervention is completed.
Running state→ready state: the running time slice is up; there is a higher priority process.
Ready state—→running state: Select a ready process when the CPU is idle.

7.2. Five-state model

The five-state model adds a new state (new) and an end state (exit) on the basis of the three-state model.

New state: Corresponding to the state when the process was created, it has not yet entered the ready queue. Creating a process requires two steps: 1. Allocate the required resources and establish the necessary management information for the new process. 2. Set the process to the ready state and wait to be scheduled for execution.

Termination state: refers to the state that a process is in when it completes a task and reaches a normal end point, or terminates abnormally due to an insurmountable error, or is terminated by the operating system and a process with termination rights. A process in the terminated state will no longer be scheduled for execution, will be revoked by the system in the next step, and will eventually disappear from the system. Terminating a process requires two steps: 1. Wait for the operating system or related processes to deal with the aftermath (such as extracting information). 2. Then the occupied resources are recovered and deleted by the system.

The specific reasons for the process state transition are as follows:
NULL→New state: Execute a program and create a child process.
New state→ready state: when the operating system has completed the necessary operations for process creation, and the current system performance and virtual memory capacity are allowed.
Running state → terminated state: When a process reaches a natural end point, or an insurmountable error occurs, or is terminated by the operating system, or terminated by other processes with termination rights.
Running state→ready state: the running time slice is up; there is a higher priority process.
Running state → waiting state: waiting to use resources; such as waiting for peripheral transmission; waiting for manual intervention.
Ready state → Terminated state: Not shown in the state transition diagram, but some operating systems allow the parent process to terminate the child process.
Waiting state → terminated state: Not shown in the state transition diagram, but some operating systems allow the parent process to terminate the child process.
Termination state → NULL: Complete the aftermath operation.

7.3. Seven-state model

The seven-state model adds ready suspend and blocked suspend to the five-state model.

Suspended ready state: The process is ready to run, but it is currently in the external memory and can only be scheduled for execution if it is swapped into the memory.

Suspended waiting state: Indicates that the process is waiting for an event to occur and is in external memory.

The specific reasons for the process state transition are as follows:
Waiting state→suspended waiting state: The operating system can decide to switch out the waiting state process to become suspended waiting state according to the current resource status and performance requirements.
Suspended waiting state→suspended ready state: After the event that causes the process to wait occurs, the corresponding suspended waiting state process will be converted to the suspended ready state.
Suspended ready state→ready state: When there is no ready state process in the memory, or the suspended ready state process has a higher priority than the ready state process, the system will convert the suspended ready state process to the ready state.
Ready state→suspended ready state: The operating system can also decide to swap out the process in the ready state to become the suspended ready state according to the current resource status and performance requirements.
Suspended waiting state→waiting state: When a process is waiting for an event, it does not need to be loaded into memory in principle. However, this state change is possible in the following case. Happens when a process exits, there is a large chunk of free space in main memory, and a pending process has a higher priority and the operating system has learned that the event that caused it to block is about to end. This state changes.
Running state→suspended ready state: When the waiting event of a process with a higher priority in the suspended waiting state ends, it needs to preempt the CPU, and at this time the main memory space is insufficient, which may cause the running process to turn into a suspended state. from the ready state. In addition, the process in the running state can also suspend itself.
New state→suspended ready state: Considering the current resource status and performance requirements of the system, it can be decided that the newly created process will be swapped out to become the suspended ready state.

8. Characteristics of suspended processes

  • The process cannot be executed immediately

  • A suspended process may wait for an event, but the event is independent of the suspension condition, and the end of the event does not make the process eligible for execution. (The process becomes suspended and ready after waiting for the event to end)

  • A process enters a suspended state because the operating system, a parent process, or the process itself prevents it from running.

  • The command to end the suspended state of a process can only be issued by the operating system or the parent process.

8.1, the cause of the process hang

  1. End User Request. When an end user finds a suspicious problem during the running of his program, he hopes to suspend his program to stop it. That is, the execution of the process is suspended; if the user process is in the ready state but not executed at this time, the process will not accept scheduling temporarily, so that the user can study its execution or modify the program. We call this quiescent state the "suspended state".

  2. request from the parent process. Sometimes the parent process wants to suspend one of its child processes in order to examine and modify the child process, or to coordinate activities among the child processes.

  3. The need for load regulation. When the workload in the real-time system is heavy and may affect the control of real-time tasks, the system can suspend some unimportant processes to ensure the normal operation of the system.

  4. operating system needs. The operating system sometimes wishes to suspend certain processes in order to check resource usage while running or for accounting purposes.

  5. swap needs. In order to alleviate the situation of memory shortage, the process in the blocked state in the memory is switched to the external memory.

9. Thread synchronization method

  1. Semaphore: Allows multiple threads to access the same resource at one time, but needs to control the maximum number of threads that access this resource at one time;

  2. Mutex: It is actually a special case of semaphore, allowing only one thread to access the same resource at the same time;

  3. Signal (event): The notification operation is used to ensure multi-thread synchronization, and it is also convenient to realize the comparison operation of multi-thread priority.

10. Deadlock and conditions

(Deadlock in concurrency) In two or more concurrent processes, if each process holds a certain resource and waits for the other process to release the resource it currently holds, before changing this state, it cannot send a request to the resource. forward. This situation is called a deadlock .

Four conditions for deadlock to occur:

  • Mutual exclusion: a resource can only be used by one process at a time

  • Request and hold conditions: When a process is blocked due to requesting resources, it keeps its own resources

  • Non-alienable conditions: The resources obtained by the process cannot be forcibly deprived before they are fully used

  • Circular waiting condition: Several processes form a head-to-tail ring waiting resource relationship before

10.1. The basic method to solve deadlock:

deadlock prevention, deadlock avoidance, deadlock detection, deadlock resolution

  • Deadlock prevention: ensure that at least one of the four necessary conditions for deadlock to occur does not hold

  • Avoid deadlock: Dynamically detect the resource allocation status to ensure that the circular waiting condition is not established, so that the system is in a safe state

  • Resolving deadlocks: including process termination and resource preemption

    • choose a victim

    • rollback

    • Starvation (the more you roll back, the less likely you are to continue as a victim)

  • Avoid deadlocks:

    • Destruction of mutual exclusion conditions: There is no way to destroy this condition. We use locks to make them mutually exclusive of critical resources.

    • Destroy request and hold conditions: apply for all resources at once

    • Destruction does not deprive resources: when a thread occupying some resources further applies for other resources, if the application cannot be obtained, it can actively release the resources it occupies.

    • Destruction of circular waiting conditions: prevent by applying for resources on demand.

11. Kernel mode and user mode, privileged and non-privileged

Kernel state: When a task (process) executes a system call and falls into execution in the kernel code, we call the process in the kernel running state.

User mode: User programs run in user mode, and the operating system runs in kernel mode.

Privileged instructions: instructions that can only be used partially by the operating system kernel and are not allowed to be used directly by users. Such as I/O command, set terminal mask command, clear memory, build storage protection, set clock command.

Unprivileged Instructions: All programs can be used directly.

System state (core state, special state, management state): Execute all instructions. User state (normal state, target state): Execute non-privileged instructions.

11.1 Conversion from user mode to kernel mode

interrupt implementation

For conversion, to perform stack conversion, the user mode registers need to be saved, and the contents of these registers will be restored when the kernel mode returns to the user mode, which is relatively expensive and time-consuming.

a. System call

This is a way for a user-mode process to actively request to switch to the kernel mode. The user-mode process requests to use the service program provided by the operating system to complete the work through the system call. For example, fork() in the previous example actually executes a system that creates a new process. transfer. The core of the system call mechanism is realized by using an interrupt specially opened by the operating system for users, such as the int 80h interrupt of Linux.

b. Abnormal

When the CPU is executing a program running in user mode, some unknowable exceptions occur, which will trigger the current running process to switch to the kernel-related program that handles the exception, and also transfer to the kernel mode, such as Page fault exception.

c. Interrupts from peripherals

When the peripheral device completes the operation requested by the user, it will send a corresponding interrupt signal to the CPU. At this time, the CPU will suspend the execution of the next instruction to be executed and turn to execute the processing program corresponding to the interrupt signal. If the previously executed instruction is The program in the user mode, then the conversion process naturally occurs from the user mode to the kernel mode. For example, when the hard disk reading and writing operation is completed, the system will switch to the interrupt handler for hard disk reading and writing to perform subsequent operations.

11.2 Kernel mode

(1) I/O command, set terminal mask command, clear memory, build storage protection, set clock command.

(2) Interrupt, exception, trap, such as page fault interrupt, etc.

(3) Process (thread) management

(4) System calls, such as calling device drivers

(5) Conversion of user memory address (logic ---> physical mapping)

12. What is an operating system

  • Operating System (OS for short) is a program that manages computer hardware and software resources, and is the core and foundation of a computer system;

  • An operating system is essentially a software program that runs on a computer;

  • The operating system provides users with an interface to interact with the system;

  • The operating system is divided into a kernel and a shell (we can understand the shell as an application program surrounding the kernel, and the kernel is a program that can operate the hardware).

13. Memory management method?

  • Block-based: memory is divided into several fixed-size blocks, and each block contains only one process. If the program needs a small part of the space, but allocates a large space, it will lead to memory waste, which is often called "fragmentation".

  • Page-based: The storage is divided into page-by-page of equal size. Compared with the block-based division, the page is smaller, which improves memory utilization and reduces fragmentation.

  • Segment: Although paging management improves memory utilization, the pages in paging management do not actually have any practical significance. Segment management divides the main memory into segments, and the space of each segment is much smaller than that of a page.

  • Segment page: The segment page management mechanism combines the advantages of segment management and page management. To put it simply, the segment page management mechanism is to divide the main memory into several segments first, and each segment is divided into several pages, that is to say, in the segment page management mechanism, the inter-segment and the inside of the segment are all discrete.

14. Orphan process and zombie process?

Orphan process : A parent process exits, and one or more of its child processes are still running, then those child processes will become orphan processes. Orphan processes will be adopted by the init process (process number 1), and the init process will complete the state collection work for them.

Zombie process : A process uses fork to create a child process. If the child process exits and the parent process does not call wait or waitpid to obtain the state information of the child process, the process descriptor of the child process is still saved in the system. Such a process is called a zombie process.

The solution to the zombie process:

  • Through the semaphore mechanism. When the child process exits, a SIGCHILD signal is sent to the parent process, and the parent process handles the SIGCHILD signal. Call wait in the signal processing function to process the zombie process.

  • Fork twice, the principle is to make the child process an orphan process, so that its parent process becomes an init process, and the zombie process can be processed through the init process.

Zombie process hazard scenario:

  For example, there is a process that generates a child process periodically. This child process needs to do very little, and it exits after finishing what it should do. Therefore, the life cycle of this child process is very short, but the parent process only cares about it. Generate a new sub-process, as for the things after the sub-process exits, it will be ignored. In this way, after the system has been running for a period of time, there will be many dead processes in the system. If you use the ps command to check, you will see to many processes in state Z. Strictly speaking, the dead process is not the root of the problem, the culprit is the parent process that spawned a large number of dead processes. Therefore, when we are looking for how to eliminate a large number of zombie processes in the system, the answer is to shoot the culprit that produces a large number of zombie processes (that is, send SIGTERM or SIGKILL signals through kill). After killing the culprit process, the zombie process it generates becomes an orphan process, and these orphan processes will be taken over by the init process, and the init process will wait() these orphan processes to release the resources in the system process table occupied by them, so that, These dead orphan processes can die in peace.

15. The process from the beginning to the end of the program

Preprocessing - Compilation - Assembly - Linking

  • The .c file is preprocessed to generate the middleware .i file. This stage has two functions, one is to replace the content of the include header file, and the other is to process the macro definition.

  • .i files are compiled to generate assembly .s files

  • The assembly file of .s is generated by the assembler into the object file of .obj

  • .obj generates an exe executable program through the linker and lib (static link library), dll (dynamic link library) files.

16. The difference between interrupt and exception

Interruption refers to the response of the CPU to a certain event in the system: the CPU suspends the program being executed, and automatically transfers to execute the interrupt processing program of the event after saving the scene; after execution, it returns to the interrupt processing program of the original program. Continue at the point. Interrupts can be further divided into external interrupts and internal interrupts:

  • External interrupts - what we refer to as interrupts - refer to interrupts caused by external device events, such as usual disk interrupts, printer interrupts, etc.;

  • Internal interrupts—that is, exceptions—refers to interrupts caused by internal events of the CPU, such as program errors (illegal instructions, address out-of-bounds). Internal interrupt (trap) is also translated as "capture" or "trapped".

==Exception== is caused by the execution of the current instruction. Interrupts due to system calls are exceptions.

==Interrupt==It is caused by an event in the system, which has nothing to do with the current instruction.

The same point: both are a reaction of the CPU to something happening in the system. Difference: interrupts are caused by external factors, and exceptions are caused by the CPU itself.

17. Concepts of sectors, blocks, pages, and clusters

The smallest unit of communication between the operating system and the disk is the disk block.

(1) What is a cluster? What is the difference between block and block?

    It is called a cluster in a file system such as NTFS under Windows; it is called a block in a file system such as Ext4 under Linux. Each cluster or block may include 2n sectors.

(2) Why do disk blocks exist?

Easy to read: Since the number of sectors is relatively small, it is difficult to address the large number, so the operating system combines adjacent sectors together to form a block, and then performs overall operations on the block.

  Separation of dependence on the underlying layer: The operating system ignores the design of the underlying physical storage structure. By virtualizing the concept of a disk block, a block is considered to be the smallest unit in the system.

(3) The relationship between blocks and pages

The operating system often communicates with two storage devices, memory and hard disk, similar to the concept of "block", which requires a virtual basic unit. Therefore, with memory operations, the concept of a virtual page is used as the smallest unit. Dealing with the hard disk is to take the block as the smallest unit.

Guess you like

Origin blog.csdn.net/slave_of_life/article/details/130490849#comments_26326493