Three concurrent programming models of IO multiplexing, multi-process and multi-threading

Source: Internet
Author: User

I/O multiplexing model

I/O multiplexing principle: Allows an application to monitor multiple I/O ports at the same time to determine if the operation on it can be performed and to achieve the purpose of reuse. Seeing an example in the book explaining the principle of I/O, I think it is very image that if a water pipe (I/O port) from 10 different places is monitored for flow (i.e., whether it is readable), then 10 people (i.e. 10 threads or 10 code) are required to do this. If a technology (such as a webcam) is used to communicate the state of the 10 pipes to a certain point, then only 1 people are required to monitor it, and a multi-way I/O multiplexing mechanism like Select or Epoll is a function of the camera, which enables multiple i/ o The status of the port is fed back to the same place, such as a particular file descriptor, so that the application simply uses the corresponding select () or epoll_wait () system call to block the focus.

The pros and cons of I/O multiplexing: Because I/O multiplexing is in the context of a single process, each logical process has access to the full address space of the process, so the overhead is much lower than the multi-process, and the disadvantage is the high complexity of programming.

The simplest way to construct a multi-process model is to use a process, like a fork function. For example, a concurrent server that accepts a client connection request in the parent process and then creates a new child process to serve each new client. Advantages
    1. Each process is independent of each other, does not affect the stability of the main program, the child process crashes does not matter;
    2. By increasing the CPU, it is easy to expand the performance;
    3. Can minimize the impact of line Cheng/unlock, greatly improve performance, even if the thread running module algorithm low efficiency is not OK;
    4. Each sub-process has a 2GB address space and related resources, and the overall performance limit is very high
Disadvantages
    1. The logic control is complex and needs to interact with the main program;
    2. Need to cross process boundaries, if there is a large amount of data transfer, it is not very good, suitable for small data transfer, dense operation
    3. Multi-process scheduling overhead is relatively large;
Multithreaded model each thread has its own thread context, including a thread ID, stack, stack pointer, program counter, general purpose register, and condition code。 All threads running in a process share the entire virtual address space of the process. Because the thread runs in a single process, it shares the entire contents of the process's virtual address space, including its code, data, heap, shared library, and open files.

The execution model of threads and processes is somewhat similar, and the declaration cycle for each process is a thread, which we call the main threads. Threads are peers, and the main thread differs from other threads in that it executes first. Advantages
    1. No cross-process boundaries are required;
    2. The program logic and control method are simple;
    3. All threads can share memory and variables directly;
    4. The total resource consumed by threading method is better than the process mode;
Disadvantages
    1. Each thread and the main program share the address space, limited by the 2GB address space;
    2. Synchronization between threads and lock control is more troublesome;
    3. The crash of a thread may affect the stability of the whole program;
    4. After reaching a certain number of threads, even if the CPU does not increase performance, such as Windows Server 2003, about 1500 threads are nearly the limit (thread stack is set to 1M), if the set thread stack is 2M, the total number of 1500 threads is not reached;
    5. Thread can improve the total performance is limited, and after the thread is more, the scheduling of the thread itself is a hassle, need to consume more CPU
The thread implementation of Linux is done outside the core, and the kernel provides the interface Do_fork () that creates the process. The kernel provides two system calls, __clone () and fork (), and eventually calls the Do_fork () kernel API with different parameters. Do_fork () provides a number of parameters, including CLONE_VM (shared memory space), CLONE_FS (shared file system Information), Clone_files (Shared file descriptor table), Clone_sighand (shared signal handle tables), and Clone_ PID (Shared process ID, only valid for the core process, that is, process # No. 0). When a multi-process is generated using a fork system call, the kernel calls Do_fork () without using any shared properties, and the process has a separate running environment. When using Pthread_create () to create a thread, all of these properties are eventually set to call __clone (), and all of these parameters are passed to the Do_fork () in the kernel, thus creating a "process" that has a shared running environment, and only the stack is independent, by __clone ( Incoming
That is, under Linux, whether it is multithreaded programming or multi-process programming, the end is to use do_fork implementation of multi-process programming, but the process is created with different parameters, resulting in a different shared environment. Linux threads exist in the kernel in the form of lightweight processes, with separate process table entries, and all creation, synchronization, and deletion operations are performed in the Pthread library. The Pthread library uses a management thread (__pthread_manager (), each process is independent and unique) to manage the creation and termination of threads, assigns thread IDs to threads, sends thread-related signals, and the primary thread Pthread_create ()) The caller passes the request information through the pipeline to the management thread.

Three concurrent programming models of IO multiplexing, multi-process and multi-threading

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.