Skip to content

Latest commit

 

History

History
123 lines (82 loc) · 3.29 KB

Thread.md

File metadata and controls

123 lines (82 loc) · 3.29 KB

Thread

Overview

Why we need thread inside the process?

  • Application
  • Cost
  • Performance

If without thread?

  • Single service process
    • lower performance
    • sequencial programming (i.e. simple)
  • Finite state machine

Three ways to construct a server (Modern OS)

Model Feature
Threads Parallelism, blocking system calls
Single-threaded process No parallelism, blocking system calls
Finite-state machine Parallelism, nonblocking system calls, interrupts

Basic Concept

Process: the Recources owner

Thread: a instance of a process, the scheduling unit of CPU

  • sometimes called light-weight process

Thread Attribute

  • has status
  • need to store context while not running
    • registers (e.g. program counter, etc.)
  • has its own stack and stack pointer

    return value of function is in the stack

  • share process address space and other resources

OS concepts threads

Implementation

  • User level thread
  • Kernel level thread
    • Implement by modifying kernel or OS
  • Mix these two together
    • Creating thread in user space and scheduling in kernel space
    • Many user threads share (run on) a kernel thread
    • e.g. Solaris

User Level Thread

  • Pros and Cons
    • Pros
      • It can run on any OS (just need the library)
    • Cons
      • Most of the system call is blocking, thus if kernel block the "process" then all the thread the process has will be blocked too. (because in the aspect of OS, there is only one process)

POSIX Pthreads

POSIX stands for Portable Operating System Interface

Thread call Description
Pthread_create Create a new thread
Pthread_exit Terminate the calling thread
Pthread_join Wait for a specific thread to exit
Pthread_yield Release the CPU to let another thread run
Pthread_attr_init Create and initialize a thread's attribute structure
Pthread_attr_destroy Remove a thread's attribute structure

Blocking System Call Management

  • Modify system to non-blocking
  • Reimplement the I/O library system call function

Pros and Cons of User Level Thread

Kernel don't know the existence of threads

TBD

Kernel Level Thread

Example in real world

Linux Thread

Linux does not distinguish between processes and threads - It uses the more generic term "tasks"

Check out the task_struct

  • fork()
  • clone()

Windows Thread Library

Windows thread

Conclusion of Thread and Process

Process is the "owner of resources" (e.g. Memory); Thread is the deploy unit of CPU

Resources

Book

Operating System Concepts 9ed.