What is multi-threaded and multi-process

Source: Internet
Author: User
Tags message queue posix semaphore thread advantage

Threads and processes are now more fashionable in the concept of computers, what is multi-threading, what is multi-process? This article gives you a detailed introduction, hope to enhance your understanding of contemporary computer technology, there is no place, but also to the master to correct. Process (English: Process, Chinese translation course, Taiwan translation itinerary) is the entity on the computer that has run the program. The process itself does not run, it is a container for threads. The program itself is only a collection of instructions, and the process is the actual operation of the program (those instructions). Several processes may be related to the same program, and each process can run independently (in sequential) or unsynchronized (parallel) mode. Process is the basic operating unit of today's time-sharing system

Threads (English: Thread, translated from Taiwan), terminology in operating system technology is the smallest unit in which the operating system is able to perform operational scheduling. It is covered in the process, a thread refers to a single sequential control flow in the process, one process can be concurrent with multiple threads, and each thread performs different tasks in parallel. In Unix System V and SunOS are also known as lightweight processes (lightweight processes), but lightweight processes refer to kernel threads (kernel thread) more, while the user thread is called a thread.

Threads are the basic unit of independent Dispatch and dispatch. A thread can be a kernel thread that is scheduled by the operating system kernel, such as a Win32 thread, or a user thread that is self-dispatched by the user process, such as Linux portable thread; or by the kernel and the user process, such as Windows 7 threads, for mixed scheduling.

Multiple threads in the same process share all of the system resources in the process, such as virtual address space, file descriptors, signal processing, and so on. But multiple threads in the same process have their own calling stack (call stack), their own register environment (register context), their own thread-local storage (thread-local storage).

A process can have many threads, and each thread performs different tasks in parallel.

The benefits of using multithreaded programming on multicore or multi-CPU, or on CPUs that support hyper-threading, are obvious, which increases the execution throughput of the program. On a single CPU single-core computer, using multithreading technology, can also be responsible for IO processing in the process, human-computer interaction and the standing blocked part of the dense computing part of the execution, write a dedicated workhorse thread to perform dense computation, thus improving the execution efficiency of the program

A process is the smallest unit of resource allocation, and a thread is the smallest unit of CPU scheduling. The difference between a thread and a process is that the child process and the parent process have different code and data spaces, while multiple threads share the data space, and each thread has its own execution stack and program counter for its execution context. Multithreading is mainly to save CPU time, play use, depending on the situation. The running of a thread requires the use of the computer's memory resources and CPU.

Multi-process: a process is an execution activity on a computer by a program. When you run a program, you start a process. Obviously, the program is Dead (static), and the process is alive (dynamic). Processes can be divided into system processes and user processes. The process used to complete the various functions of the operating system is the system process, which is the operating system itself; All user-initiated processes are user processes. A process is the unit in which the operating system allocates resources. The process is also refined to a thread, which means that there are several smaller units under a process that can run independently. At the same time, if two or more processes are allowed to run in the same computer system, this is multitasking. Modern operating systems are almost always multitasking operating systems that can manage multiple processes at the same time. The benefits of multitasking are obvious, such as you can listen to the Internet while listening to MP3, and even print the downloaded documents without interfering with each other. So here is the question of parallelism, as the saying goes, one cannot use two, this is the same for computers, in principle a CPU can only be assigned to a process in order to run the process. We usually use a computer with only one CPU, that is, only one heart, to make it multitasking, running multiple processes at the same time, you must use concurrency technology. The implementation of concurrency technology is quite complex, the most easy to understand is the "time slice rotation process scheduling algorithm", its ideas are briefly described as follows: Under the management of the operating system, all running processes take turns using the CPU, each process allows the CPU time is very short (such as 10 milliseconds), so that users do not feel The CPU is in turn serving multiple processes as if all the processes were running uninterrupted. But there is actually only one process that occupies the CPU at any one time. If a computer has multiple CPUs, the situation is different, if the number of processes is less than the number of CPUs, then different processes can be assigned to different CPUs to run, so that multiple processes are really running concurrently, which is parallel. However, if the number of processes is greater than the number of CPUs, concurrency technology is still required. CPU allocation is done in threads, a process may be composed of multiple threads, and the situation is more complicated, but in short, the following relationships are:

Number of bus <= CPUs: running in parallel

Number of bus threads > number of CPUs: concurrently running

The efficiency of parallel operation is obviously higher than that of concurrency, so multitasking is more efficient in multi-CPU computers. However, if you run only one process (thread) in a multi-CPU computer, you will not be able to take advantage of multiple CPUs. This involves multitasking operating systems, where the rationale for multitasking operating systems (such as Windows) is that the operating system allocates the CPU's time slices to multiple threads, each of which is done within the time slice specified by the operating system (note that multiple threads here are part of different processes). The operating system constantly switches from one thread execution to another, and so on, on the macro level, it seems as if multiple threads are executing together. Because these threads belong to different processes, it seems to us as if multiple processes are executing simultaneously, thus enabling multitasking

Multithreading: In computer programming, a basic concept is to control multiple tasks at the same time. Many programming problems require that the program be able to stop the work at hand, and instead deal with some other problems before returning to the main process. This can be achieved in a variety of ways. At the very beginning, programmers who mastered the low-level language of the machine wrote some "interrupt service routines", and the pauses of the main process were implemented by hardware-level interrupts. Although this is a useful method, the programming is difficult to transplant, resulting in another category of costly problems. Interruptions are necessary for tasks that are very real-time. But for many other problems, it only requires dividing the problem into a separate running program fragment, so that the whole program can respond to the user's request more quickly.

At the very beginning, a thread is just a tool to allocate processing time for a single processor. But if the operating system itself supports multiple processors, then each thread can be assigned to a different processor, really into the "parallel operation" state. One of the most valuable features of multithreaded operations from a programming language perspective is that programmers don't have to worry about how many processors are being used. The program is logically divided into several threads, and if the machine itself has multiple processors installed, the program will run faster without any special tuning. According to the previous discussion, you may feel that threading is very simple. But you have to pay attention to one problem: sharing resources! If multiple threads are running at the same time, and they try to access the same resources, they will encounter a problem. For example, two threads cannot send information to a printer at the same time. To solve this problem, for those resources that can be shared (such as printers), they must enter a lock during use. So a thread can lock the resource and, after completing its task, unlock (release) the lock so that other threads can then use the same resource.

Multithreading is to accomplish many tasks synchronously, not to improve the efficiency of the operation, but to improve the efficiency of the use of resources to improve the efficiency of the system. Threads are implemented at the same time when multiple tasks need to be completed.

An application that employs multithreaded technology can make better use of system resources. Its main advantage is to make full use of the idle time slice of the CPU, can respond to the user's request with as little time as possible, make the overall running efficiency of the process be improved greatly, and enhance the flexibility of the application. More importantly, because all threads of the same process share the same memory, there is no need for a special data transfer mechanism, no need to establish a shared storage or shared files, so that the coordination between different tasks and operations, data interaction, resource allocation and other issues more easily resolved.

interprocess communication (ipc,inter-process communication), which is a technique or method that transmits data or signals between at least two processes or threads. A process is the smallest unit of resources allocated by a computer system. Each process has its own part of a separate system resource that is isolated from each other. Inter-process communication is achieved in order to enable different processes to access resources and coordinate work with each other. These processes can run on the same computer or on different computers that are connected to the network.

Interprocess communication technologies include messaging, synchronization, shared memory, and remote procedure calls. IPC is a standard UNIX communication mechanism.

Reasons to use IPC:

Information sharing

Accelerate

Modular;

Convenient And

Private right separation.

The main IPC method

Method provider (operating system or other environment)

File most operating systems

Signal most operating systems

Sockets most operating systems

Message Queuing (En:message queue) most operating systems

Pipeline (En:pipe) for all POSIX systems, Windows.

Named pipe (en:named pipe) for all POSIX systems, Windows.

Semaphore (En:semaphore) for all POSIX systems, Windows.

Shared memory for all POSIX systems, Windows.

Message passing (En:message passing) is used for MPI specifications, Java RMI, CORBA, MSMQ, Mailslot, and others.

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.