Soumen Santra 1
Threads
Basic
SOUMEN SANTRA
MCA, M.Tech, SCJP, MCP
5/13/2020
Soumen Santra 2
Single vs. Multithreaded Processes
THREADS : Basic
5/13/2020
Soumen Santra 3
Thread Usage: Multitasking
5/13/2020
Soumen Santra 4
Thread : Features
Block for long waits.
Use many more CPU cycles.
Respond to asynchronous events.
Parallel performance.
5/13/2020
Soumen Santra 5
User-define Threads &
Daemon Threads
5/13/2020
Soumen Santra 6
Processes vs. Threads
5/13/2020
Soumen Santra 7
Threads vs. Processes
• Each thread executes separately
• Threads in the same process share resources
• No protection among threads!!
5/13/2020
Soumen Santra 8
Property
Processes created
with fork
Threads of a process Ordinary function calls
variables
get copies of all
variables
share global variables share global variables
IDs get new process IDs
share the same process ID
but have unique thread ID
share the same
process ID (and thread
ID)
Communicatio
n
Must explicitly
communicate,
e.g.pipes
or use small integer
return value
May communicate with return
value
or shared variables
if done carefully
May communicate with
return value
or shared variables
(don't have to be
careful)
Parallelism
(one CPU)
Concurrent Concurrent Sequential
Parallelism
(multiple
CPUs)
May be executed
simultaneously
Kernel threads may be
executed simultaneously
Sequential
Threads vs Processes
5/13/2020
Soumen Santra 9
 Resource Sharing
 Reacting fast.
 Economic Feasible.
 Utilization of Architectures or Platform.
THREADS : Benefits
5/13/2020
Soumen Santra 10
User Define Threads
 Thread management done by user-level threads library maps to kernel thread.
Examples
- Windows 95/98/NT/2000
- Linux
- POSIX Pthreads
- Mach C-threads
- Solaris threads
THREADS : Types
Daemon Threads
 Many user-level threads mapped to single kernel thread.
Examples:
 Solaris Green Threads
 GNU Portable Threads
- Windows 95/98/NT/2000
- Tru64 UNIX
- BeOS
- Linux
5/13/2020
11
Daemon Threads vs. User Threads
 Kernel/Daemon Thread
 Make blocking I/O calls without blocking the entire process.
 Perform concurrently on multiple processors
 User-level Thread
 Fast context switch
 Customized scheduling & synchronizing
5/13/2020 11Soumen Santra
Kernel Threads
 kernel_thread(): to create a kernel thread.
 Example of kernel process:
 Process 0 (swapper), the ancestor of all processes.
 Process 1 (init) for initialization.
 Others: keventd, kswapd, kflushd (also bdflush), kupdated…
Here ends with ‘d’ which means daemon and ‘k’
means kernel.
5/13/2020 12Soumen Santra
Soumen Santra 13
Multithreading Concept
One-to-One
Many-to-One
Many-to-Many
THREADS : Mapping
5/13/2020
Soumen Santra 14
System calls
 fork() : Used to create Child Threads.
 exec() : Used to execute Child Threads.
Thread cancellation
 Terminating a thread before job finished.
 Two general approaches:
 Asynchronous cancellation terminates the target thread
immediately. Not always follow due to creation of orphan or
zombie process.
 Deferred cancellation allows the target thread periodically
from its child.
THREADS : Issues
5/13/2020
Soumen Santra 15
 It is used in UNIX systems to notify a process has performed.
 Generated by specific event.
 Delivered to a process.
 Options:
 Delivers the signal to the thread to which the signal applies.
 Delivers the signal to every threads.
 Delivers the signal to certain threads.
 Assign a particular thread to receive all signals.
5/13/2020
THREADS : Signal handling
Soumen Santra 16
 Generate a number of threads in a pool where they wait to yield a work.
 Advantages:
 Faster to service a request with an existing thread than create a new
thread.
 Allows the number of threads in the process within the size of the pool.
5/13/2020
THREADS : pools
Soumen Santra 17
Pthreads
 A POSIX Application Programming Interface.
 It is used to creation and synchronization of thread.
 Standardized as IEEE 1003.1c.
 API specifies behavior of the thread library, implementation is up to
development of the library.
 Use in UNIX operating systems (Solaris, Linux, Mac OS X).
Windows Threads
 Use as one-to-one mapping.
 Each thread context contains
 A thread id (TID)
 Set of Register
 Use Separate user and kernel stacks.
 Private data store
THREADS : Implementations
5/13/2020
Soumen Santra 18
Threads in Linux
 Refers as tasks.
 call clone() system to creation of thread.
 It allows a child task to share the address space with the parent task.
Threads in Java
 Created by:
 Inherited Thread class.
 Implements the Runnable interface.
 It managed by the JVM.
THREADS : Lifecycle
5/13/2020
Processes : Lightweight
 Process: It is an instance of a program in execution.
 Lightweight process (LWP): used for multithreaded applications.
 Share resources: address space, open files, etc
 Associate with each other.
 Examples of LWP: Linux Threads, IBM’s Next Generation
Posix Threading Package (NGPT)
5/13/2020 19Soumen Santra
Process : Identification
 Process descriptor pointers: 32-bit
 Process ID (PID): 16-bit
In Linux each process (LWP) has different PID.
Programmers think threads in the same group
having common PID.
Thread group: Collection of LWPs.
The PID of the first LWP is PID of the group.
TGID (Thread Group ID) is acting as process
descriptor using getpid() system call.
5/13/2020 20Soumen Santra
Processes : Creation
 Resources are duplicated for parent process.
 Slow and inefficient process.
 Mechanisms to solve this problem
Copy on Write: Both read the same physical
pages.
Lightweight process: Both share per-process
kernel data structures..
5/13/2020 21Soumen Santra
System Calls
 clone(func, args, flags, child_stack,parent_tid,
child_tid): creating lightweight process
A wrapper function in C library
Uses clone() system call
 fork(): discuss earlier
 vfork(): share the memory address space
 do_fork() function
5/13/2020 22Soumen Santra
System calls : Organized of Processes
 TASK_RUNNING
 TASK_STOPPED,
 EXIT_ZOMBIE,
 EXIT_DEAD
 TASK_INTERRUPTABLE,
 TASK_UNINTERRUPTABLE
 Two kinds of sleeping processes
Exclusive process
Nonexclusive process
5/13/2020 23Soumen Santra
THANK YOU
5/13/2020 Soumen Santra 24

Threads Basic : Features, Types & Implementation

  • 1.
    Soumen Santra 1 Threads Basic SOUMENSANTRA MCA, M.Tech, SCJP, MCP 5/13/2020
  • 2.
    Soumen Santra 2 Singlevs. Multithreaded Processes THREADS : Basic 5/13/2020
  • 3.
    Soumen Santra 3 ThreadUsage: Multitasking 5/13/2020
  • 4.
    Soumen Santra 4 Thread: Features Block for long waits. Use many more CPU cycles. Respond to asynchronous events. Parallel performance. 5/13/2020
  • 5.
    Soumen Santra 5 User-defineThreads & Daemon Threads 5/13/2020
  • 6.
    Soumen Santra 6 Processesvs. Threads 5/13/2020
  • 7.
    Soumen Santra 7 Threadsvs. Processes • Each thread executes separately • Threads in the same process share resources • No protection among threads!! 5/13/2020
  • 8.
    Soumen Santra 8 Property Processescreated with fork Threads of a process Ordinary function calls variables get copies of all variables share global variables share global variables IDs get new process IDs share the same process ID but have unique thread ID share the same process ID (and thread ID) Communicatio n Must explicitly communicate, e.g.pipes or use small integer return value May communicate with return value or shared variables if done carefully May communicate with return value or shared variables (don't have to be careful) Parallelism (one CPU) Concurrent Concurrent Sequential Parallelism (multiple CPUs) May be executed simultaneously Kernel threads may be executed simultaneously Sequential Threads vs Processes 5/13/2020
  • 9.
    Soumen Santra 9 Resource Sharing  Reacting fast.  Economic Feasible.  Utilization of Architectures or Platform. THREADS : Benefits 5/13/2020
  • 10.
    Soumen Santra 10 UserDefine Threads  Thread management done by user-level threads library maps to kernel thread. Examples - Windows 95/98/NT/2000 - Linux - POSIX Pthreads - Mach C-threads - Solaris threads THREADS : Types Daemon Threads  Many user-level threads mapped to single kernel thread. Examples:  Solaris Green Threads  GNU Portable Threads - Windows 95/98/NT/2000 - Tru64 UNIX - BeOS - Linux 5/13/2020
  • 11.
    11 Daemon Threads vs.User Threads  Kernel/Daemon Thread  Make blocking I/O calls without blocking the entire process.  Perform concurrently on multiple processors  User-level Thread  Fast context switch  Customized scheduling & synchronizing 5/13/2020 11Soumen Santra
  • 12.
    Kernel Threads  kernel_thread():to create a kernel thread.  Example of kernel process:  Process 0 (swapper), the ancestor of all processes.  Process 1 (init) for initialization.  Others: keventd, kswapd, kflushd (also bdflush), kupdated… Here ends with ‘d’ which means daemon and ‘k’ means kernel. 5/13/2020 12Soumen Santra
  • 13.
    Soumen Santra 13 MultithreadingConcept One-to-One Many-to-One Many-to-Many THREADS : Mapping 5/13/2020
  • 14.
    Soumen Santra 14 Systemcalls  fork() : Used to create Child Threads.  exec() : Used to execute Child Threads. Thread cancellation  Terminating a thread before job finished.  Two general approaches:  Asynchronous cancellation terminates the target thread immediately. Not always follow due to creation of orphan or zombie process.  Deferred cancellation allows the target thread periodically from its child. THREADS : Issues 5/13/2020
  • 15.
    Soumen Santra 15 It is used in UNIX systems to notify a process has performed.  Generated by specific event.  Delivered to a process.  Options:  Delivers the signal to the thread to which the signal applies.  Delivers the signal to every threads.  Delivers the signal to certain threads.  Assign a particular thread to receive all signals. 5/13/2020 THREADS : Signal handling
  • 16.
    Soumen Santra 16 Generate a number of threads in a pool where they wait to yield a work.  Advantages:  Faster to service a request with an existing thread than create a new thread.  Allows the number of threads in the process within the size of the pool. 5/13/2020 THREADS : pools
  • 17.
    Soumen Santra 17 Pthreads A POSIX Application Programming Interface.  It is used to creation and synchronization of thread.  Standardized as IEEE 1003.1c.  API specifies behavior of the thread library, implementation is up to development of the library.  Use in UNIX operating systems (Solaris, Linux, Mac OS X). Windows Threads  Use as one-to-one mapping.  Each thread context contains  A thread id (TID)  Set of Register  Use Separate user and kernel stacks.  Private data store THREADS : Implementations 5/13/2020
  • 18.
    Soumen Santra 18 Threadsin Linux  Refers as tasks.  call clone() system to creation of thread.  It allows a child task to share the address space with the parent task. Threads in Java  Created by:  Inherited Thread class.  Implements the Runnable interface.  It managed by the JVM. THREADS : Lifecycle 5/13/2020
  • 19.
    Processes : Lightweight Process: It is an instance of a program in execution.  Lightweight process (LWP): used for multithreaded applications.  Share resources: address space, open files, etc  Associate with each other.  Examples of LWP: Linux Threads, IBM’s Next Generation Posix Threading Package (NGPT) 5/13/2020 19Soumen Santra
  • 20.
    Process : Identification Process descriptor pointers: 32-bit  Process ID (PID): 16-bit In Linux each process (LWP) has different PID. Programmers think threads in the same group having common PID. Thread group: Collection of LWPs. The PID of the first LWP is PID of the group. TGID (Thread Group ID) is acting as process descriptor using getpid() system call. 5/13/2020 20Soumen Santra
  • 21.
    Processes : Creation Resources are duplicated for parent process.  Slow and inefficient process.  Mechanisms to solve this problem Copy on Write: Both read the same physical pages. Lightweight process: Both share per-process kernel data structures.. 5/13/2020 21Soumen Santra
  • 22.
    System Calls  clone(func,args, flags, child_stack,parent_tid, child_tid): creating lightweight process A wrapper function in C library Uses clone() system call  fork(): discuss earlier  vfork(): share the memory address space  do_fork() function 5/13/2020 22Soumen Santra
  • 23.
    System calls :Organized of Processes  TASK_RUNNING  TASK_STOPPED,  EXIT_ZOMBIE,  EXIT_DEAD  TASK_INTERRUPTABLE,  TASK_UNINTERRUPTABLE  Two kinds of sleeping processes Exclusive process Nonexclusive process 5/13/2020 23Soumen Santra
  • 24.