One of the operating system interviews – programs, processes, threads

Note: There are many errors and loopholes in the operating system of “Interview Bible”. I have rewritten the relevant books and my own views for your reference.

A, procedures, processes, threads

1. Procedures and processes.

A process consists of two parts: 1) a kernel object used by the operating system to manage the process. Kernel objects are also where the system stores statistics about processes. 2) Address space. It contains the code and data for all executable modules or DLL modules. It also contains space for dynamic memory allocation. Such as thread stack and heap allocation space.

  define Use of system running resources
The program A collection of computer instructions stored as files on disk. A program is a static Entity. In a multi-program system, it cannot be run independently, let alone executed concurrently with other programs. No use [a program cannot apply for system resources, cannot be scheduled by the system, nor can it operate as an independent unit, therefore, it does not occupy system operating resources].
process Usually defined as an instance of a running program, it is an execution activity of a program in its own address space. Definition: process is the running process of process entity (including: program segment, related data segment, process control block PCB), is an independent unit of system resource allocation and scheduling. A process is a unit of resource requisition, scheduling, and independent operation; therefore, it uses running resources in the system.

2. Processes and threads

If the purpose of the operating system to introduce processes is to improve the concurrent execution of programs, to improve resource utilization and system throughput. So the purpose of introducing threads into the operating system is to reduce the time and space cost of concurrent execution of the process, so that the operating system can perform concurrent execution well.

The process defines an execution environment, including its own private address space, a handle table, and a security environment. A thread is a control flow with its own call stack, which 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. Kernel objects are also where the system stores thread statistics. 2) Thread stack, which maintains all parameters and local variables that the thread needs to execute code. When a thread is created, the system creates a thread kernel object. The thread kernel object is not the thread itself, but the smaller data structure that the operating system uses to manage threads. You can think of a thread kernel object as a small data structure composed of statistics about threads.

Processes are compared to threads as follows:

To compare process thread
Lively sex Inactive (just a container for threads) lively
Address space A separate virtual address space assigned by the system (4GB for 32-bit processes) Executes code in the address space of the process. Threads have only one kernel object and one stack, keeping very few records, and therefore require very little memory. Because threads require less overhead than processes
scheduling It is simply the basic unit of resource allocation A basic unit of independent dispatch and dispatch
concurrency Interprocess concurrency only (traditional OS) Interprocess and interthread concurrency
Has the resources The basic unit of resource ownership Basically no resources
overhead Creating, undoing, and switching costs a lot Save only a small amount of register content, low overhead.

      

3. Synchronize processes

The main task of process synchronization is to coordinate the execution sequence of multiple related processes, so that the concurrent processes can effectively share resources and cooperate with each other, so that the execution of the program has reproducibility.

The synchronization mechanism follows the following principles:

(1) Free to let in;

(2) Wait if busy (to ensure mutually exclusive access to critical sections);

(3) limited wait (limited means limited time, avoid death, etc.);

(4) let the power wait, (when the process can not enter its own critical area, should release the processor, so as not to fall into a busy state).

Due to process synchronization, there are a series of classic synchronization problems “producer-consumer” problem, “philosopher eating” problem, “reader – writer” problem.

      

4. How is inter-process communication implemented?

Process communication refers to the exchange of information between processes (as little as a state or value, as much as thousands of bytes). Therefore, the mutual exclusion and synchronization between processes using semaphores can be reduced to low-level communication due to the small amount of information exchanged.

Advanced process communication refers to a communication mode in which a user can transfer a large amount of data using a set of communication commands provided by the operating system. The operating system hides the implementation details of process communication. In other words, the communication process is transparent to the user.

Advanced communication mechanisms can be grouped into three broad categories:

(1) Shared memory system (shared storage area divided in memory); In practice, the communication mode corresponds to the “clipboard” (the clipboard is actually a memory area managed by the system maintenance). For example, press CTRL + C in the Word process and CTRL + V in the PPT process to complete the communication between the Word process and the PPT process. Data is added to the clipboard during replication. When pasting, take the data from the clipboard and display it on the PPT window.

(2) Message passing system (The data exchange between processes takes message as the unit. In today’s most popular microkernel operating system, the communication between microkernel and server adopts message passing mechanism without exception. Application Example: MailSlot is designed based on broadcast communication system. It adopts connectionless and unreliable data transmission. A post slot is a one-way communication mechanism in which the server process that creates the post slot reads data and the client process that opens the post slot writes data.

(3) Pipe communication system (pipe: shared file (similar to fifO queue, written by one process and read by another process) that connects reading and writing processes to achieve communication between them). In practice, pipes are divided into anonymous pipes and named pipes. An anonymous pipe is an unnamed, one-way pipe that transfers data between a parent process and a child process. Anonymous pipes can only communicate between two processes on the local machine, not across the network. Named pipes can realize communication between two processes not only on the local machine, but also across the network.

  Communication between two processes on the same machine Cross network communication
The Clipboard Clipboard can Can not be
Anonymous Pipe can Can not be
Namedpipe (point-to-point single communication, data volume can be large can can
Mailslot (one-to-many, less than 424 bytes) Mailslot can can

* * * *

Win32 provides the following interprocess communication modes:

1) Clipboard Clipboard: In the 16-bit era of the common way, the CWnd class provides support. 2) COM/DCOM: data exchange between processes is carried out through the proxy stub of COM system, but data can only be transmitted when the interface function is called. Data can be transmitted between different hosts through DCOM. 3) Dynamic Data Exchange (DDE) : common in the 16-bit era. 4) File Mapping: a new method provided on 32-bit systems that can be used to share memory. 5) Mailslots: Mailslots are a new way to exchange data between different hosts on 32-bit systems. They are divided into server side and client side for data exchange. Under Win9X, only Mailslots are supported. Pipes: To exchange data between parent and child processes; Named pipe: can exchange data between different hosts, divided into server side and client side, in Win9X only support named pipe client. 7) RPC: Remote procedure call, rarely used for two reasons: complex and not fully compatible with RPC on UNIX systems. But COM/DCOM calls are based on RPC. 8) Windows Sockets: Network Sockets, can exchange data between different hosts, divided into server side and client side. 9) WM_COPYDATA: Pass data to other processes by sending WM_COPYDATA message and putting data in parameters.

\

5. Synchronize threads

The synchronization modes in user mode and kernel mode are classified and compared as follows:

  Kernel objects/non-kernel objects meaning disadvantages apply
Critical code sections Non-kernel objects that work inIn user mode, indicates the object in user mode Control thread concurrency from the point of view of program code 1. Because a timeout value cannot be set while waiting for a critical code segment to enter, it is easy to enter a deadlock state. 2. It cannot be used across processes. Synchronization between threads in a single process (fast synchronization)
Event object The kernel object The most basic of all kernel objects. Slower (compared to user mode for thread synchronization) Synchronization is achieved between individual threads of multiple processes
Mutex is a Mutex object The kernel object Represents exclusive access to a resource
The signal Semaphore The kernel object Use counters to control program access to a shared resource