Operating System Principles Chapter 4 Threads

Operating System Principles for Undergraduate Students Learning Record
Learning Record Family Bucket

4.1 What is a thread

4.1.1 Introduction reason

performance:

Operating process system overhead is high

Unix lightweight process (fork)

application:

Requirements for concurrent execution of process code

Example: PPTedit (type, spell check, save)

hardware:

multi-core processor

Speed ​​up the running of the process

Thread thread is called in Windows

Process process

4.1.2 Threads

thread:

The basic unit of execution that can run on a CPU

A piece of code within a process can be created as a thread

Process is still the basic unit of resource allocation

4.1.3 Processes and Threads

process thread
the code process contains threads A thread is a piece of code in a process
resource Process is the basic unit of resource allocation Threads do not own resources and share the resources of processes
scheduling A thread switch within the same process does not cause a process switch Thread is the basic scheduling unit
to switch Heavyweight context switching, expensive Lightweight switching, low cost
life cycle Withdrawal at the beginning of this year will cause all its threads to be destroyed Thread cancellation does not affect the process

Threads are attached to processes

If it is communication between threads of two different processes, the process must also switch

4.1.4 Thread structure

code and data: from process

Various resources: from process

Thread control block: TCBThread Control Block

​ Each thread has its own ID, thread ID

4.1.5 Advantages of threads

High responsiveness :

​ Thread creation overhead is small

​ Example: web server

Resource sharing : threads in a process can share process resources

Economy :

​ Thread creation, context switching than process blocks

​:Solaris Creating a thread is 30 times faster than a process, and thread switching is 5 times faster than process switching

Application of MP architecture: threads of a process run in parallel on different processors

4.1.7 Linux threads

Create threads through the clone() system call, but the clone function needs to pass in parameters

Use the term "task" task to indicate processes and threads, generally not "threads"

User Threads: PThreadsThread Library

Multi-threaded matrix operation, some operating systems support multi-threaded operations, and some do not support multi-threaded operations

4.2 Multithreading model

4.2.1 User threads

User thread: a thread managed by the user thread library process

  • ​ The kernel cannot see user threads
  • ​ User threads are created and scheduled in user space without kernel intervention
  • ​ Applied to traditional operating systems that only support processes

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-hgF7kcDf-1641211985938) (E:\Documents and PPT\Junior Course Study\Operating System\Pictures\Fourth Chapter\User Threads.png)]

4.2.2 Kernel threads

Kernel thread: a thread managed by the kernel

  • Requires kernel support
  • Thread scheduling is done by the kernel
  • Created and revoked by the kernel

example:

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-yvU40sEz-1641211985941) (E:\Documents and PPT\Junior Course Study\Operating System\Pictures\Fourth chapter\kernel-thread.png)]

4.2.3 Multithreading Model

many-to-one model

  • Operating systems that do not support kernel threads: the kernel only has processes

  • Kernel sees only one process: multiple threads cannot run in parallel on multiple processors

  • User threads in a process are managed by the process itself:

    In-process thread switching does not cause process switching

    A system call from one thread will cause the entire process to block

one-to-one model

  • For operating systems that support threads:

    User threads are mapped to kernel threads

    The operating system manages these threads

  • Good concurrency: multiple threads can run in parallel on multiple processors

  • Kernel overhead is high

Windows generally adopts this multithreading model

many-to-many model

Multiple user threads are mapped to an equal or smaller number of kernel threads <=

  • Both concurrency and efficiency
  • increased complexity

two-level model

Similar to M: M

4.3 Thread library

Provides an API for programmers to create and manage threads

Two modes:

User Libraries (User Threads)

Kernel library (kernel thread)

Guess you like

Origin blog.csdn.net/weixin_45788387/article/details/122291826