Operating System interview: Program, process, thread
Note: There are many errors and vulnerabilities in the operating system of the "Interview Guide". I have rewritten the relevant books and opinions for your reference.
1. Programs, processes, and threads
1. programs and processes.
A process consists of two parts: 1) the operating system is used to manage the kernel objects of the process. The kernel object is also used by the system to store statistics about processes. 2) address space. It contains the code and data of all executable modules or DLL modules. It also contains the space allocated by the dynamic memory. Such as thread stack and heap allocation space.
|
Definition |
Usage of system running resources |
Program |
A set of computer commands, which are stored on disks as files. A program is a passive entity. In a multi-Program System, it cannot run independently and cannot be concurrently executed with other programs. |
Do not use [the program cannot apply for system resources, be scheduled by the system, or be used as an independent operating unit. Therefore, it does not occupy system running resources ]. |
Process |
It is usually defined as an instance of a running program, which is an execution activity of a program in its own address space. Definition: A process is a running process of a process entity (including a program segment, a related data segment, and a process control block PCB). It is an independent unit for the system to allocate and schedule resources. |
Use [process is the unit for resource application, scheduling, and independent operation. Therefore, it uses the running resources in the system .] |
2. Process and thread
If the operating systemPurpose of introducing a processIsImprove concurrent program execution to improve resource utilization and system throughput. In the operating systemThread IntroductionToReduce the time-space overhead in the Process of concurrent executionSo that the operating system can execute well concurrently.
A process defines an execution environment, including its own private address space, a handle table, and a secure environment. A thread is a control flow with its own call stack, it records its execution history.
A thread consists of two parts: 1) The Kernel Object of the thread, which is used by the operating system to manage the thread. The kernel object is also used by the system to store thread statistics. 2) thread stack, which is used to maintain all parameters and local variables required by the thread for code execution. When a thread is created, the system creates a thread kernel object. The kernel object of this thread is not the thread itself, but a small data structure used by the operating system to manage the thread. The thread kernel object can be considered as a small data structure consisting of statistics about threads.
The process and thread are compared as follows:
Comparison |
Process |
Thread |
Dynamic |
Not lively (only the thread container) |
Lively |
Address Space |
Independent virtual address space assigned by the system (for 32-bit processes, this address space is 4 GB) |
Run the code in the address space of the process. The thread has only one kernel object and one stack, and few records are retained. Therefore, the memory required is very small. Because the thread requires less overhead than the process |
Scheduling |
Only the basic unit of Resource Allocation |
Basic Unit of independent scheduling and dispatching |
Concurrency |
Only inter-process concurrency (traditional OS) |
Inter-process and inter-thread concurrency |
Possess resources |
Basic Unit of resource ownership |
Basically no resources |
System overhead |
Large overhead for creation, cancellation, and switching |
Saves only a small number of registers, with low overhead. |
3. Process Synchronization
The main task of Process Synchronization is to coordinate multiple processes in the execution order, so that the concurrent processes can effectively share resources and cooperate with each other, so that the execution of the program is reproducible.
Principles of synchronization:
(1) idle access;
(2) wait when you are busy (ensure mutually exclusive access to the critical section );
(3) Limited wait (limited represents a limited time to avoid waiting );
(4) Let the right Wait (when the process cannot enter its critical section, release the processor to avoid being busy ).
Due to process synchronization, a series of typical synchronization problems are "producer-consumer", "philosophers eat", and "readers-writers.
4. How is inter-process communication implemented?
Process communication refers to the communication between processes.Information exchange(If the Information volume is small, it is a state or number, and if the information volume is large, it is thousands of bytes ). Therefore, for processes that use semaphores for mutual exclusion and synchronization, it is classified as low-level communication because the amount of information it exchanges is small.
The so-called advanced process communication refers to a communication mode in which users can use a group of communication commands provided by the operating system to transmit large amounts of data. The operating system hides the Implementation Details of Process Communication. Or, the communication process is transparent to users.
Advanced communication mechanisms can be categorized into three categories:
(1) shared storage system (the shared storage area in the memory); in practice, the corresponding "Clipboard" (the clipboard is actually a memory area maintained and managed by the System) for example: press Ctrl + C for the word process and press Ctrl + V for the PPT process to complete the communication between the word process and the PPT process, copy the data to the clipboard. When the clipboard is pasted, the data is retrieved and displayed in the PPT window.
(2) Message Passing System (data exchange between processes is measured in message units. In today's most popular microkernel operating systems, communication between microkernels and servers, message transmission is adopted without exception. Application Example: Mailslot is designed based on the broadcast communication system and uses connectionless and unreliable data transmission. The mail slot is a one-way communication mechanism. The server process that creates the mail slot reads data and the client process that opens the mail slot writes data.
(3) pipeline Communication System (pipeline: A shared file that connects read and write processes to achieve communication between them (pipe files, similar to FIFO queues, are written by a process, )). In practice, pipelines are divided into anonymous pipelines and named pipelines. An anonymous pipeline is an unnamed one-way pipeline that transmits data between a parent process and a child process. An anonymous pipeline can only implement communication between two processes on the local machine, rather than cross-network communication. The named pipe not only enables communication between two processes on the local machine, but also supports communication between two processes across networks.
|
Communication between two processes on the same machine |
Cross-Network Communication |
Clipboard clipboard |
Yes |
No |
Anonymous pipeline Pipe |
Yes |
No |
Named Pipe (Point-to-Point single communication, large data volume) namedpipe |
Yes |
Yes |
Mail slot (one-to-many, small data size, less than 424 bytes) Mailslot |
Yes |
Yes |
The following communication methods are provided under Win32:
1) clipboard: a common method in the 16-bit era. It is supported in the cwnd class.
2) COM/DCOM: exchange data between processes through the proxy stubs of the com system, but it can only transmit data when calling interface functions, data can be transferred between different hosts through DCOM.
3) Dynamic Data Exchange (DDE): a frequently used method in the 16-bit era.
4) file mapping: A new method provided by the 32-bit system to share memory.
5) mailslots: A new method provided in the 32-bit system. It can exchange data between different hosts, which can be divided into the server and the customer. Both parties can exchange data through it, in Win9x, only mail slot customers are supported.
6) pipes: pipelines, which are divided into unknown pipelines: data exchange between parent and child processes; famous pipelines: data exchange between different hosts, divided into servers and clients, in Win9x, only famous MPs queue customers are supported.
7) RPC: Remote Process calls are rarely used for two reasons: complicated and not fully compatible with RPC in UNIX systems. However, the call to COM/DCOM is based on rpc.
8) Windows Sockets: a network interface that can exchange data between different hosts, divided into the server side and the customer side.
9) wm_copydata: Send the wm_copydata message and put the data in the parameter to pass the data to other processes.
5. Thread Synchronization
The classification and comparison are as follows:
|
Kernel Object/ Non-Kernel Object |
Description |
Disadvantages |
Applicable |
Key code segment (critical section) criticalsection |
Non-kernel objects, working onObjects in user mode |
Control thread concurrency from the perspective of program code |
1. Because the timeout value cannot be set while waiting for the key code segment to enter, it is easy to enter the deadlock status. 2. It cannot be used across processes. |
Synchronization between threads in a single process (fast synchronization) |
Event object |
Kernel Object |
The most basic of all kernel objects. |
Slow speed (thread synchronization compared to user mode) |
Synchronization between threads of multiple processes |
Mutex |
Kernel Object |
Exclusive access to a resource |
Semaphores Semaphore |
Kernel Object |
Use counters to control program access to a shared resource |