A face questions: Tell me about the difference between process and thread

https://www.cnblogs.com/zhehan54/p/6130030.html

 

Before understanding the process and thread concept of choice to have a certain feel for concurrency, if a server within the same time can only serve one client, then there are other clients Shadeng words, it shows poor performance is expected to be scolded customers Xiang come out, so came into concurrent programming, network programming concurrency problem must be considered. There are several ways to achieve concurrency: for example, multi-process, multi-threaded, IO multiplexing.

multi-Progress

Process is the basic unit of allocation of resources (CPU, memory, etc.), it is an example of when the program is executed. The system will create programs that run when a process, and allocate resources to it, and then put the process into the process ready queue, process scheduler will select it when it allocates CPU time, the program began to really run.

Linux system function fork()to create a child process parent process, so, in a process can receive a copy upon request from a new client let a child process to handle, the parent process only responsible for monitoring the arrival of the request, then let it create a child process to deal with, so that we can achieve concurrent processing.

# -*- coding:utf-8 -*-
import os

print('当前进程:%s 启动中 ....' % os.getpid()) pid = os.fork() if pid == 0:    print('子进程:%s,父进程是:%s' % (os.getpid(), os.getppid())) else:    print('进程:%s 创建了子进程:%s' % (os.getpid(),pid ))

Output:

当前进程:27223 启动中 ....
进程:27223 创建了子进程:27224
子进程:27224,父进程是:27223

fork function returns two results, because the data the operating system will copy the current process again, and then the program is divided into two processes continue to run behind the code, return the fork in the parent and the child, the child returns the value of pid 0 is always returned in the parent process is the process the child process id.

Multithreading

When the thread is the smallest unit of program execution, it is a process flow of execution, is the basic unit of CPU scheduling and dispatch, a process can be made up of many threads, all of the resources shared among threads process, each thread has its own stack and local variables. Thread scheduling executed by the CPU independent, in a multi-CPU environment allows multiple threads to run simultaneously. Multithreading may be implemented similarly concurrent operations, each request is assigned a thread to handle.

Threads and processes each have pros and cons and what difference it?

  • The process is the smallest unit of resource allocation, the thread is the smallest unit of program execution.

  • Process has its own separate address space, each started a process, the system will allocate address space for it, set up a data table to maintain the code segment, stack and data segments, this operation is very expensive. The thread is a shared process data, using the same address space, so the CPU changes than the process takes a much smaller thread, while creating a thread overhead is much smaller than the process.

  • More convenient communication between threads, the thread in the same process share global variables, static variables and other data, and communication between processes carried out in a manner (IPC) communication. But how to handle the synchronization and mutual exclusion is the difficulty of writing multithreaded programs.

  • But the program more robust multi-process, multi-threaded program as long as there is a thread dies, the whole process is dead, too, and a process dies the other will not have impact on the process, because the process has its own separate address space.

Foreigner's words said so - "Unix Network Programming":

  • fork is expensive. Memory is copied from the parent to the child, all descriptors are duplicated in the child, and so on. Current implementations use a technique called copy-on-write, which avoids a copy of the parent’s data space to the child until the child needs its own copy. But, regardless of this optimization, fork is expensive.

  • IPC is required to pass information between the parent and child after the fork. Passing information from the parent to the child before the fork is easy, since the child starts with a copy of the parent’s data space and with a copy of all the parent’s descriptors. But, returning information from the child to the parent takes more work.

  • Threads help with both problems. Threads are sometimes called lightweight processes since a thread is “lighter weight” than a process. That is, thread creation can be 10–100 times faster than process creation.

  • All threads within a process share the same global memory. This makes the sharing of information easy between the threads, but along with this simplicity comes the problem

Guess you like

Origin www.cnblogs.com/yunleijava/p/11545513.html