Parallel processing: fork (fork) and Threads (thread)

Source: Internet
Author: User


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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.