Parallel processing: fork (fork) and Threads (thread)
Fork (fork) is a UNIX term that, when branching a process (a running program), basically replicates it, and the two processes after the fork continue to run from the current execution point, and each process has its own memory copy (such as a variable). One process (the original one) becomes the parent process, and the other (copied) becomes a child process. If you are a science fiction fan, think of them as parallel universes (parallel universe).
The fork operation creates a branch on the timeline (timeline) and finally obtains two independent processes. Fortunately the process can determine which is the original process and which is the child process (by looking at the return value of the fork function). So what do they do differently (and if it's the same, what's the point?) )。
In a server that uses forks, each client-machine connection creates a child process using forks. The parent process continues to listen on the new connection while the child process processes the client. When the client's request ends, the child process exits. So the forked process is run in parallel, and the client does not have to wait for each other.
Because forks are a bit resource-intensive (each fork-out process requires its own memory), there is another option: Thread. Threads are lightweight processes or child processes, all of which are present in the same (real) process, sharing memory. The decline in resource consumption is accompanied by a flaw: because threads share memory, you must ensure that their variables do not conflict, which can be confusing if you modify the same content at the same time. These problems can all be attributed to synchronization issues. In modern operating systems (with the exception of Windows, which does not support forking), forks are really fast, and modern hardware can handle resource consumption better than ever. Forking is a good choice if you don't want to be bothered by synchronization problems.
Parallel processing: fork (fork) and Threads (thread)