As a programmer who has worked for many years, do you still lack a thorough understanding of processes and threads? Do you use process management in your work? Do you know how many ways to communicate between processes?

The following issues will be discussed in this paper:

① Introduction to processes and threads

② Interprocess communication mode

③ Introduction to process management in Swoole

Introduction to processes and threads

Start with processes and threads. A process is the smallest unit of operating system resource allocation and an instance of program execution. While the program is running, the system creates a process, allocates resources to it, and then puts the process in its ready queue so that when the process scheduler selects it, it allocates CPU time slices.

The states of a process are: New state, ready state, running state, blocked state, exit state, between states can be converted: ready -> Running, running -> ready, running -> blocked, blocked -> Ready

① Newly created State Indicates the state of the newly created process. When creating a process, the process first applies for a blank process control block (PCB), and fills the PCB with information for controlling and managing the process. Then allocate resources for the process to run; Finally, the process is put into the ready state and inserted into the ready queue.

② Ready state A ready state refers to a process that is waiting to be executed, has the execution qualification, but has no execution permission. Qualified to execute means that all necessary resources have been allocated except for the CPU. No execute permission is granted because the CPU has not been obtained.

③ Running state Running state process refers to the process has obtained the CPU, both the execution qualification, and the execution power, is in the running state. In a uniprocessor (or CPU) system, only one process can be running at a time. In a multiprocessor system, more than one process may be running at the same time.

(4) Blocked State The blocked state means that the process is blocked and cannot continue to run temporarily. The process may be blocked due to I/O requests, time slices running out, or some errors.

⑤ Exit state means that the process stops running. The reasons for a process to exit are as follows: the program completes execution, calls exit functions, encounters an error, receives a termination signal, or the process is killed by the operating system. When a process exits, the operating system clears the PCB of the process and returns the PCB space to the system. A process that enters the terminated state cannot be executed again, but the operating system retains a record that holds the status code and timing statistics for collection by other processes. Once the other process has finished extracting its information, the operating system will delete its process, that is, its PCB zero, and return the blank PCB to the system.

Threads are the smallest unit of CPU scheduling and a limited system resource. A process can be composed of multiple threads that share all of the process’s resources with each thread having its own stack and local variables. Threads are scheduled independently by the CPU, allowing multiple threads to run simultaneously in a multi-CPU environment. Similarly, multithreading allows concurrent operations, with one thread assigned to each request.

Threads are similar to processes in that a process can run multiple threads, and multiple threads can share data. It’s just that front-of-thread switching consumes less CPU than process switching.

Unlike the process heap of similar process are Shared by multiple threads and method of area resources, but each thread has its own program counter, the virtual machine and the local method stack, so the system in one thread, or switch between individual threads, burden is much smaller than the process, also because of this, thread, also known as a lightweight process.

What’s the difference between a process and a thread?

Essential difference: Processes are the basic unit of operating system resource allocation, while threads are the basic unit of CPU task scheduling and execution.

Space and resources: Processes are independent of each other, and resources can be shared between threads within a unified process. Threads in different processes are independent of each other.

Switching overhead: processes have their own independent code segments and data space (program context). Switching between processes needs to save context, register and other data, which will have a large overhead. Threads in the same process share code segments and data space, each thread has its own independent run stack and program counter (PC), switching between threads is less overhead.

Relationship: A process crash in protected mode has no impact on other processes, but a thread crash will kill the entire process.

Execution sequence: a process has its own entry, sequential execution sequence, and program exit. However, threads cannot be executed independently and must be dependent on the application, which provides control over the execution of multiple threads, both of which can be executed concurrently.

Ii. Interprocess communication

Before introducing communication between processes, let me introduce the concepts of user mode and kernel mode.

A process is in user mode when it executes its own code, and kernel mode when it is stuck in kernel code because of a system call. The kernel code executed uses the kernel stack of the current process, and each process has its own kernel stack.

When a user runs a program, the process created by the program starts running its own code in user mode. To perform operations such as file operations and network data sending, you must use system calls such as write and Send. These system calls call the kernel code. The process will enter the kernel address space to execute kernel code to complete the corresponding operation, and the kernel-mode process will return to user mode after the execution. In this way, user-mode programs can not operate the kernel address space at will, which has a certain security protection function to ensure that the address space between processes will not conflict with each other, and the operation of one process will not modify the data in the address space of another process.

There are three common ways for a process to switch from user to kernel mode: system calls (such as fork calls), exceptions (such as page missing exceptions), and peripheral interrupts.

Next, InterProcess Communication, or IPC, stands for InterProcess Communication. Different processes can communicate and exchange data with each other. Processes communicate with each other in the following ways: pipes (including nameless and named pipes), message queues, semaphores, shared memory, sockets, and Streams.

(1) Pipe: pipe is divided into unknown pipe and named pipe, unknown pipe is one-way, only one-way communication is allowed. If two-way communication is required, two one-way pipes need to be opened. Named pipes are pipes that exist in a file system directory. Pipe files are simply tokens in the file system and do not take up space on disk. In use, space is opened in memory as a channel for data interaction between two processes.

② Message queue: Message queue is a linked list of messages stored in the kernel and identified by message queue identifier. The message queue overcomes the disadvantages of little signal transmission, pipe carrying only plain byte stream and limited buffer size.

③ Semaphore: A semaphore is a counter that can be used to control access to a shared resource by multiple processes. It is often used as a locking mechanism to prevent other processes from accessing a shared resource while one process is accessing it. Therefore, it is mainly used as a means of synchronization between processes and between different threads within the same process.

(4) Shared memory: Shared memory maps a segment of memory that can be accessed by other processes. This segment of shared memory is created by one process but can be accessed by multiple processes. Shared memory is the fastest IPC method and is specifically designed for the low efficiency of other interprocess communication methods. It is often used in conjunction with other communication mechanisms, such as semaphores, to achieve synchronization and communication between processes.

(5) SocketSocket is also an inter-process communication mechanism. Different from other communication mechanisms, SocketSocket can be used for inter-process communication.

Introduction to process management in Swoole

Let’s use the Process module in Swoole to deepen our understanding of processes. The swoole_process class is used to create a child process in Swoole with the following constructor prototype:

$function is a callback function that is executed after the child process is successfully created

The $redirect_STdin_stdout parameter redirects the standard input and output of the child process

$pipe_type is the pipe type

You can refer to the Swoole official documentation for specific parameter meanings. Next, we will create child processes in a process and conduct inter-process communication.

Run PHP process.php on the command line and the result is as follows:

This is an example of processes communicating through pipes, creating child processes, and setting up callback functions.

Event::add adds the pipe file descriptor $process-> PIPE to the Event loop. The first line of output hello World is output by the callback function, while read: aaaAAA is the data read from the pipe during the event loop.

There are other ways of interprocess communication in Swoole, which ARE not listed here. The above content, if there are mistakes, welcome to correct!