Caozuoxitong - process management 4 (thread)

Operating Systems - Process Management (thread)

  1. Thread
  • The basic concept of threads

    A thread is smaller than the process, the basic unit capable of independent operation, the process can be better than the threads execute in parallel to improve the speed of the program, take full advantage of multiprocessor superiority. Reference thread mainly to improve the processing efficiency of the system, reducing processor idle time and CPU time due to the protection of on-site waste schedule.

    A thread is the smallest unit of the process operation is performed , i.e., the basic unit of processor performs scheduling. In the introduction of the thread operating system, you can switch threads within a process.

    Process is the basic unit of resource allocation, all the resources of all threads in the same process of sharing the process. A thread is the basic unit allocated to the processor, the real processor is running on a thread.

    Linux system does not distinguish between processes and threads, they all use the same method described, using the same scheduling and management strategies.

  • State of the thread and conversion operations

    Thread only three basic states: execution, blocking and ready . For the three state of the thread, the thread to transform the existing state of five operations.

    • Derived (Spawn)

      Thread derived in the process can also be re-spawn a thread. Users can derive its own thread through the related system calls. Linux system, the library function clonc () and create_thread () are used to derive different threads of execution mode.

      Give birth to a new breed has a corresponding thread pointer data structures and variables, such as variables and pointers register context in this thread and stack registers. The new derived thread is placed in the ready queue.

    • Schedule (Schedule)

      Select a thread enters execution ready state.

    • Blocking (Block)

      If a thread needs to wait for an event occurs during execution, it was blocked. When the obstruction, register context, the program counter, and stack pointer are guaranteed.

    • Activation (Unblock)

      If the thread is blocked waiting for an event occurs, the thread into the ready queue is activated.

    • End (Finish)

      End of a thread execution, the register context and its contents will be released stack.

  • The benefits of the introduction of the thread

    • Easy scheduling. Since the thread only as a basic unit of independent scheduling, multiple threads in the same process of resource sharing process, so the process is easy to switch.
    • Improve the efficiency of the system. Can be effectively achieved through concurrent threads, processes can create multiple threads execute different parts of the same program.
    • Creating a thread is less than the cost to create a process to create a fast speed.
  • Multi-threaded

    Thread is divided into user-level threads and kernel-level threads (also called core-level thread).

    • User-level threads ULT (the Thread the User Level) . It is established by the user of the application by the user application for scheduling and management. The operating system kernel is not aware of the existence of these threads, only to manage the process. So this has nothing to do with the kernel threads.

      ULT the following advantages:

      Time and memory (1) the application is much less than the overhead of the thread switch kernel-level threads.

      Scheduling algorithm (2) nothing to do with the thread scheduling algorithms operating system.

      (3) because regardless of the operating system kernel, it is suitable for any operating system.

      Disadvantages are:

      (1) In a typical operating system, there are many system requests are being blocked with, so when a thread executes a system request can not help but this thread is blocked, the process all threads are blocked.

      (2) In the system of the method, because each process can only by a thread running in the CPU , and therefore can not take advantage of multiple processors.

    • Kernel-level threads KLT (Kernel Level the Thread) . Kernel-level threads to create, manage and schedule all the threads by the operating system kernel is complete. An application can write multithreaded program by the way, when it is submitted to the time a multi-threaded operating system execution, the kernel creates a process and a thread for it, the thread can create a new thread. Operating system kernel will provide the appropriate system calls and application program interface for the application, so that users can program to create, execute and revocation threads. Windows NT is such.

      Kernel-level threads advantages:

      (1) a plurality of core threads may be scheduled in a process, so as to run in parallel on multiple processors, thereby increasing the efficiency of the system.

      (2) a thread is blocked, other threads can still run.

      (3) the kernel itself can be run in a thread way .

      Disadvantages:

      Since the thread scheduler running in kernel mode and the user program running in kernel mode, so the same process to go through the thread switching mode from user mode to the kernel, then switching from kernel mode to user mode two mode. Thus it gave birth to user-level threads and kernel-level threads combination model .

  • Thread Linux system

    Linux kernel-level threads also called system-level threads. Linux supports kernel-level threads and user-level threads. Most operating systems define the data structure for a single thread, using a separate thread management. The Linux thread is defined as "execution context" , it's really just another execution context of the process of it, and enjoy the same process of representation, management and scheduling. Defined execution context may refer to the following

    Application user space, system calls into the kernel space. This time the process user space to pass many variables, values of the parameters to the kernel, kernel mode when running but also keep some register values, variables, and other user processes. The so-called "process context", can be seen as that set of variables and register values and the prevailing environment of user process parameters passed to the kernel and the kernel to be saved and so on.

    So what is the kernel space and user space it? Their main difference is a difference between the two can access to resources.

    Kernel space has the highest authority, have access to all resources. The user can access only limited space authority, can not directly access memory and other hardware devices, must be transferred to the kernel through system privileges to access these resources. The process can either run in user space, and can run in kernel space. The process is running in user space, is called user mode process, and into kernel space of time, known as the kernel mode processes.

    To go to the kernel mode from user mode, need a system call to complete.

    For example, when we view the contents of the file, you need to call several times to complete the system: first call open () to open the file, and then call read () reads the contents of the file, and call write () to write the contents of the standard output, and finally call close () to close the file.

    In this process, CPU context switch occurs, the process is as follows:
    1, save the original instruction of the CPU register bit user mode
    2, in order to perform the code kernel mode, the CPU registers need to be updated to the new location of kernel mode instruction.
    3, jump to the kernel mode running kernel tasks.
    4, when the end of a system call, CPU registers need to restore previously saved user mode, and then switch to the user space, continue to run the process.

    So, once the process of system calls, in fact, is twice the CPU context switch occurs . (User mode - kernel mode - user mode)


    This article refers to the text of the article know almost from: a text to let you know CPU context switch

Guess you like

Origin www.cnblogs.com/lunar-ubuntu/p/12233519.html