Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What process knowledge should PHP programmers know?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "what process knowledge PHP programmers should know". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what process knowledge PHP programmers should know.

I. introduction of processes and threads

First, let's talk about processes and threads. A process is the smallest unit of operating system resource allocation and an example of program execution. When the program is running, the system creates a process, allocates resources to it, and puts the process in the process's ready queue, so that when the process scheduler selects it, it allocates CPU time slices.

The states of the process are as follows: new state, ready state, running state, blocking state and exit state. The states can be transferred between them: ready-> run, run-> ready, run-> block, block-> ready.

The state of the ① new state creation process when it was first created. When creating a process, the process first requests a blank process control block (that is, PCB) and fills in the PCB with the information used to control and manage the process, then allocates the resources needed for the process at run time, and finally puts the process into the ready state and inserts it into the ready queue.

② Readiness Readiness refers to a process waiting to be executed, qualified for execution, and not yet authorized to execute. Execution qualification means that all necessary resources except CPU have been allocated, and there is no execution permission because CPU has not yet been obtained.

The process of ③ running state means that the process has obtained CPU, has both execution qualification and execution power, and is in a running state. In a single processor (that is, CPU) system, only one process can be running at a time. In a multiprocessor system, multiple processes can be running at the same time.

④ blocking state means that the process will turn to blocking state when it cannot continue to run when it encounters blocking. The causes of process blocking are Istroke O request, running out of time slices, encountered some errors, and so on.

The ⑤ exit state means that the process stops running. The reasons for the process to exit state are: program execution completed, call exit function, encountered an error, received a termination signal, the process was killed by the operating system, and so on. When a process exits, the operating system clears the PCB of the process and returns the PCB space to the system. Processes that enter the terminated state can no longer be executed, but a record is still kept in the operating system, in which status codes and some timing statistics are kept for other processes to collect. Once another process has finished extracting its information, the operating system will delete its process, that is, clear its PCB and return the blank PCB to the system.

After talking about the process, the thread is the smallest unit of CPU scheduling, and the thread is also a limited system resource. A process can be composed of multiple threads, all resources of the process are shared among threads, and each thread has its own stack and local variables. Threads are independently scheduled and executed by CPU, which allows multiple threads to run at the same time in a multi-CPU environment. Similarly, multithreading can also implement concurrent operations, allocating one thread for each request.

The state of a thread is similar to that of a process. A process can run multiple threads, and multiple threads can share data. It's just that the previous thread switch consumes less CPU resources than the process switch.

Unlike a process, multiple threads of the same kind share the heap and method area resources of the process, but each thread has its own program counter, virtual machine stack and local method stack, so the system generates a thread. Or when switching between threads, the burden is much less than the process, and because of this, threads are also called lightweight processes.

What is the difference between a process and a thread?

Essential difference: process is the basic unit of operating system resource allocation, while thread is the basic unit of CPU task scheduling and execution.

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

Switching overhead: processes have their own independent code segment and data space (program context), switching between processes need to save context, registers and other data, there will be a large overhead; threads in the same process share code segments and data space, each thread has its own independent stack and program counter (PC), the cost of switching between threads is small.

Interrelationship: after a process crashes, it does not affect other processes in protected mode, but a thread crashes and the entire process dies.

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

II. Mode of inter-process communication

Before introducing communication between processes, let's introduce a concept: user state and kernel state.

When a process is in the user state when executing its own code, it is in the kernel state when the process is trapped in the kernel code because of the system call. The executed kernel code 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 by running its own code and is in user mode. If you want to perform file operations, network data transmission and other operations must be through write, send and other system calls, these system calls will call the kernel code. The process will enter the kernel address space to execute the kernel code to complete the corresponding operation, and the kernel process will return to the user state after execution. In this way, the user-mode program can not manipulate the kernel address space at will, and has a certain role of security protection 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 mode to kernel mode: system calls (such as fork calls), exceptions (such as page-missing exceptions), and peripheral interruptions.

Let's talk about interprocess communication, or IPC, which is called InterProcess Communication. Different processes can communicate with each other and exchange data. The communication methods between processes are: pipes (including unnamed pipes and named pipes), message queues, semaphores, shared memory, Socket, Streams.

① pipeline: the pipeline is divided into unnamed pipeline and named pipeline, unnamed pipeline is one-way, only one-way communication is allowed. If two-way communication is required, two one-way pipes need to be opened. A named pipe is a pipe file that exists in the file system directory. Pipe files are just markings in the file system and do not take up space on disk. When in use, open up space in memory as a channel for data exchange between the two processes.

② message queuing: message queues are stored in the kernel by a linked list of messages and identified by message queue identifiers. Message queuing overcomes the shortcomings of less signal transmission information, pipeline can only carry unformatted byte stream and limited buffer size.

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

④ shared memory: shared memory maps a piece of memory that can be accessed by other processes. This shared memory is created by one process, but can be accessed by multiple processes. Shared memory is the fastest IPC mode, and it is specially designed for other inter-process communication modes to run inefficiently. It is often used in conjunction with other communication mechanisms, such as semaphores, to achieve synchronization and communication between processes.

⑤ SocketSocket is also an inter-process communication mechanism. Unlike other communication mechanisms, it can be used for different and inter-process communication.

Introduction of process management in Swoole

Let's use the Process module in Swoole to deepen our understanding of the process. Create a child process through the swoole_process class in Swoole. The constructor prototype is as follows:

$function is a callback function, which is executed after the child process is created successfully.

The $redirect_stdin_stdout parameter can redirect the standard input and output of a child process

$pipe_type is the pipe type

For the description of the parameters, please refer to the official Swoole documentation. Next, we will create a child process in a process and communicate between processes.

Execute php process.php on the command line, and the result is as follows:

This is an example of pipeline communication between processes, creating child processes, and setting callback functions.

Event::add adds the pipe file descriptor $process- > pipe to the event loop. The hello world output in the first line is output by the callback function, while the read:aaaaaa is the data read out of the pipeline when the event loop is executed.

At this point, I believe that everyone has a deeper understanding of "what process knowledge PHP programmers should know". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report