SlideShare uma empresa Scribd logo
1 de 12
Baixar para ler offline
Threads
• Overview
• Multithreading Models
• Thread Libraries
• Threading Issues
1. Overview
• A thread is a basic unit of CPU utilization, It includes a thread ID, a program
  counter, a register set, and a stack
• It shares code section, data section, and other OS resources like open files
  and signals with other threads belonging to the same process
• A traditional (or heavyweight) process has a single thread of control
• Difference between traditional single-threaded & multithreaded process




                                                                                2
                               Loganathan R, CSE, HKBKCE
1. Overview Contd…
1.1 Motivation
• A single application may be required to perform several similar
  tasks
  • Example : A web server may have several of clients concurrently accessing it
  • Tradition Solution : The server run as a single process that accepts requests and
    when it receives a request, creates a separate process to service that request
  • Process creation is time consuming and resource intensive
• The server create a separate thread to listen for client requests,
  when a request made, it create another thread to service the
  request
  • RPC servers are multithreaded a server receives a message, it services the message
    using a separate thread, which allows the server to service several concurrent
    requests.
  • Example :Java's RMI systems
• OS kernels are now multithreaded, several threads operate in the
  kernel, and each thread performs a specific task, such as managing
  devices or interrupt handling
  • Example : Linux uses a kernel thread for managing free memory
                                                                                  3
                                Loganathan R, CSE, HKBKCE
1. Overview Contd…
1.2 Benefits
• Responsiveness
   – Multithreading allow a program to continue running even if part of it is
     blocked or is performing a lengthy operation, thereby increasing
     responsiveness to the user.
   – For example a multithreaded web browser could allow user interaction in
     one thread while an image was being loaded in another thread
• Resource Sharing
   – By default, threads share the memory and the resources of the process to
     which they belong
• Economy
   – Allocating memory and resources for process creation is costly, more
     economical to create and context-switch threads
• Utilization of Multiprocessor Architectures
   – Multithreading on a multi-CPU machine increases concurrency, threads
     may be running in parallel on different processors,

                             Loganathan R, CSE, HKBKCE                      4
2. Multithreading Models
• Support for threads may be provided either at the user level, for user threads,
  or by the kernel, for kernel threads
• User Threads - Thread management done by user-level threads without kernel
  support. User thread libraries: POSIX Pthreads , Win32 threads, Java threads
• Kernel Threads - Supported and managed directly by the OS. Examples :
  Windows XP/2000, Solaris, Linux, Tru64 UNIX, Mac OS X
• The relationship between user threads and kernel threads are established in 3
  ways
2.1 Many-to-One Model
• Many user-level threads mapped to single kernel thread
• Thread management is done by the thread library in user space, so it is
  efficient
• Disadvantages
   – The entire process will block if a thread makes a blocking system call
   – Multiple threads are unable to run in parallel on multiprocessors since
       only one thread can access the kernel at a time
• Examples
   – Solaris Green Threads
   – GNU Portable Threads
                              Loganathan R, CSE, HKBKCE                        5
2. Multithreading Models                              Contd…
2.2 One -to-One Model
• Each user-level thread maps to kernel thread
• It provides more concurrency i.e. allows another thread to run when a thread
  makes a blocking system call
• Allows multiple threads to run in parallel on multiprocessors
• Disadvantages
  • Creating a user thread requires creating the corresponding kernel thread
• Examples : Windows NT/XP/2000, Linux




            Many-to-One Model                       One -to-One Model

                                 Loganathan R, CSE, HKBKCE                     6
2. Multithreading Models                                Contd…

2.3 Many-to-Many Model
• Allows many user level threads to be mapped to many kernel threads
• Allows the user and OS to create a sufficient number of user and kernel threads
• When a thread performs a blocking system call, the kernel can schedule another thread
  for execution
• Windows NT/2000 with the ThreadFiber package
Two-level Model :
•   Similar to M:M, except that it allows a user thread to be bound to kernel thread
•   Examples : IRIX, HP-UX, Tru64 UNIX, Solaris 8 and earlier




                  Many-to-Many                                              Two-level
                  Model                                                     Model



                                  Loganathan R, CSE, HKBKCE                             7
3. Thread Libraries
• A thread library provides the programmer an API for creating and managing
  threads
• Two ways of implementation
   – Provide a library entirely in user space with no kernel support - code and data
     structures for the library exist in user space
   – A kernel-level library supported directly by the operating system - code and data
     structures for the library exist in kernel space
3.1 Pthreads
• Provided as either a user or kernel-level library
• A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization
• API specifies behavior of the thread library, implementation is up to
  development of the library
• Common in UNIX operating systems (Solaris, Linux, Mac OS X)
• All Pthreads programs must include the pthread.h header file
• pthread _t t id declares the identifier t id for the thread to be created
• The pthread_attr_t attr declares the attributes for the thread
• The attributes are set in the function call pthread_attr_init(&attr)
• A separate thread is created with the pthread_creat e () function call
• pthread_join () to wait
                                Loganathan R, CSE, HKBKCE                           8
3. Thread Libraries                          Contd…

3.2 Windows XP Threads
• Threads are created in the Win32 API using the CreateThread() function with
  thread parameters
• Parameters includes security information, the size of the stack, and a flag that
  can be set to indicate if the thread is to start in a suspended state
• WaitForSingleObj ect () function for waiting
3.3 Java Threads
• Threads are the fundamental model of program execution in a Java and
  managed by the JVM
• Java threads may be created by:
  –Extending Thread class (derive Thread class and override run())
  –Implementing the Runnable interface[public interface Runnable{public abstract void run();}]
• start () method creates the new thread then allocates memory initialize it in
  JVM, and calls run() to run thread in JVM
• Join() method to wait
• Java Thread States



                                   Loganathan R, CSE, HKBKCE                               9
4. Threading Issues                              …
4.1 The fork() and exec() System Calls
•     Does fork() duplicate only the calling thread or all threads?
•     Some UNIX have Both versions
•     Exec() system call works in the same way (will replace the entire process)
•     If exec() is called immediately after forking, duplicating only the calling thread is
      appropriate
4.2 Cancellation
•     Terminating a thread before it has completed
•     Multiple threads are concurrently searching a database and one thread returns the
      result, the remaining threads might be canceled
•     A thread that is to be canceled is referred as the target thread
•     Two general approaches:
    – Asynchronous cancellation terminates the target thread immediately
    – Deferred cancellation allows the target thread to periodically check if it should be cancelled
• Difficulty in cancellation
• Canceling a thread asynchronously may not free a necessary system-wide resource (OS
  will only reclaim System resources only)
• Deferred cancellation occurs only after the target thread has checked a flag to
  determine if it should be canceled or not
• Checking whether it should be canceled at a point when it can be canceled safely is
  known as cancellation points in Pthreads.
                                         Loganathan R, CSE, HKBKCE                                     10
4. Threading Issues                          Contd……

4.3 Signal Handling
• Signals are used in UNIX systems to notify a process that an particular event has
  occurred may be received either synchronously or asynchronously.
• Signals whether synchronous(illegal memory access & Division by 0) or
  asynchronous (external event like CTL+C,& Timer expire), follow the same
  pattern:        1. Signal is generated by particular event
                    2. Signal is delivered to a process               3.Signal is handled
•   A signal handler is used to process signals
     – default signal handler that is run by the kernel when to handle the signal
     – user-defined signal handler that is called to override default action
• In single-threaded programs, signals are always delivered to a process
• In multithreaded programs:
     – Deliver the signal to the thread to which the signal applies
     – Deliver the signal to every thread in the process
     – Deliver the signal to certain threads in the process
     – Assign a specific thread to receive all signals for the process
•   Multithreaded versions of UNIX allow a thread to specify which signals it will accept
    and which it will block
•   Windows does not explicitly provide support for signals, they can be emulated using
    asynchronous procedure calls (APCs)
                                   Loganathan R, CSE, HKBKCE                                11
4. Threading Issues                                           Contd……
4.4 Thread Pools
• Create a number of threads at start & place in a pool where they wait for work
• Advantages:
  – Usually slightly faster to service a request with an existing thread than create a new thread
  – Allows the number of threads in the application(s) to be bound to the size of the pool
• The number of threads in the pool can be set based on factors like the number of CPUs
   in the system, the amount of physical memory, and the expected number of concurrent
   client requests
• Win32 API provides several functions related to thread pools
4.5 Thread Specific Data
• Allows each thread to have its own copy of data
4.6 Scheduler Activations
• Both M:M and Two-level models require communication to maintain the appropriate number of kernel
  threads allocated to the application to be adjusted dynamically
• An intermediate data structure between the user and kernel threads is placed and it is known as a
  lightweight process, or LWP                                                                                  Lightweight
• To the user-thread library, the LWP appears to be a virtual processor on which the application can
                                                                                                         LWP   Process
  schedule a user thread to run and each LWP attached to a kernal thread
• If a kernel thread blocks, the LWP blocks, the user-level thread attached to the LWP also blocks
• Communication between the user-thread library and the kernel is known as scheduler activation
• The kernel provides an application with a set of virtual processors (LWPs) for the application to
  schedule user threads onto LWP and informs an application about certain events is known as an
  upcall
• Upcalls are handled by the thread library with an upcall handler which run on a virtual processor is
  responsible to switch between threads                                                                             12

Mais conteúdo relacionado

Mais procurados

Parallel Processors (SIMD)
Parallel Processors (SIMD) Parallel Processors (SIMD)
Parallel Processors (SIMD) Ali Raza
 
Threads in Operating System | Multithreading | Interprocess Communication
Threads in Operating System | Multithreading | Interprocess CommunicationThreads in Operating System | Multithreading | Interprocess Communication
Threads in Operating System | Multithreading | Interprocess CommunicationShivam Mitra
 
30326851 -operating-system-unit-1-ppt
30326851 -operating-system-unit-1-ppt30326851 -operating-system-unit-1-ppt
30326851 -operating-system-unit-1-pptraj732723
 
Processes and Processors in Distributed Systems
Processes and Processors in Distributed SystemsProcesses and Processors in Distributed Systems
Processes and Processors in Distributed SystemsDr Sandeep Kumar Poonia
 
Unit II - 2 - Operating System - Threads
Unit II - 2 - Operating System - ThreadsUnit II - 2 - Operating System - Threads
Unit II - 2 - Operating System - Threadscscarcas
 
Operating Systems - "Chapter 4: Multithreaded Programming"
Operating Systems - "Chapter 4:  Multithreaded Programming"Operating Systems - "Chapter 4:  Multithreaded Programming"
Operating Systems - "Chapter 4: Multithreaded Programming"Ra'Fat Al-Msie'deen
 
Process management in linux
Process management in linuxProcess management in linux
Process management in linuxMazenetsolution
 
Files concepts.53
Files concepts.53Files concepts.53
Files concepts.53myrajendra
 
17 cpu scheduling and scheduling criteria
17 cpu scheduling and scheduling criteria 17 cpu scheduling and scheduling criteria
17 cpu scheduling and scheduling criteria myrajendra
 
Operating System-Threads-Galvin
Operating System-Threads-GalvinOperating System-Threads-Galvin
Operating System-Threads-GalvinSonali Chauhan
 
Multithreading
MultithreadingMultithreading
MultithreadingA B Shinde
 

Mais procurados (20)

Parallel Processors (SIMD)
Parallel Processors (SIMD) Parallel Processors (SIMD)
Parallel Processors (SIMD)
 
Os Threads
Os ThreadsOs Threads
Os Threads
 
Threads
ThreadsThreads
Threads
 
Threads ppt
Threads pptThreads ppt
Threads ppt
 
Scheduling algorithms
Scheduling algorithmsScheduling algorithms
Scheduling algorithms
 
Threads in Operating System | Multithreading | Interprocess Communication
Threads in Operating System | Multithreading | Interprocess CommunicationThreads in Operating System | Multithreading | Interprocess Communication
Threads in Operating System | Multithreading | Interprocess Communication
 
30326851 -operating-system-unit-1-ppt
30326851 -operating-system-unit-1-ppt30326851 -operating-system-unit-1-ppt
30326851 -operating-system-unit-1-ppt
 
Processes and Processors in Distributed Systems
Processes and Processors in Distributed SystemsProcesses and Processors in Distributed Systems
Processes and Processors in Distributed Systems
 
Unit II - 2 - Operating System - Threads
Unit II - 2 - Operating System - ThreadsUnit II - 2 - Operating System - Threads
Unit II - 2 - Operating System - Threads
 
Operating Systems - "Chapter 4: Multithreaded Programming"
Operating Systems - "Chapter 4:  Multithreaded Programming"Operating Systems - "Chapter 4:  Multithreaded Programming"
Operating Systems - "Chapter 4: Multithreaded Programming"
 
Ipc in linux
Ipc in linuxIpc in linux
Ipc in linux
 
Process management in linux
Process management in linuxProcess management in linux
Process management in linux
 
Files concepts.53
Files concepts.53Files concepts.53
Files concepts.53
 
SCHEDULING ALGORITHMS
SCHEDULING ALGORITHMSSCHEDULING ALGORITHMS
SCHEDULING ALGORITHMS
 
17 cpu scheduling and scheduling criteria
17 cpu scheduling and scheduling criteria 17 cpu scheduling and scheduling criteria
17 cpu scheduling and scheduling criteria
 
Semaphore
SemaphoreSemaphore
Semaphore
 
Operating System-Threads-Galvin
Operating System-Threads-GalvinOperating System-Threads-Galvin
Operating System-Threads-Galvin
 
Introduction to OpenMP
Introduction to OpenMPIntroduction to OpenMP
Introduction to OpenMP
 
OS - Deadlock
OS - DeadlockOS - Deadlock
OS - Deadlock
 
Multithreading
MultithreadingMultithreading
Multithreading
 

Destaque

Operating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - ThreadsOperating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - ThreadsPeter Tröger
 
Ch5: Threads (Operating System)
Ch5: Threads (Operating System)Ch5: Threads (Operating System)
Ch5: Threads (Operating System)Ahmar Hashmi
 
9 virtual memory management
9 virtual memory management9 virtual memory management
9 virtual memory managementDr. Loganathan R
 
8 memory management strategies
8 memory management strategies8 memory management strategies
8 memory management strategiesDr. Loganathan R
 
Networking threads
Networking threadsNetworking threads
Networking threadsNilesh Pawar
 
Threads (operating System)
Threads (operating System)Threads (operating System)
Threads (operating System)Prakhar Maurya
 
European empires and conquest
European empires and conquestEuropean empires and conquest
European empires and conquestsantana_dianag
 
Who Wrote this $#!7: Authorship and Semantic Web Markup for SEO and Social Me...
Who Wrote this $#!7: Authorship and Semantic Web Markup for SEO and Social Me...Who Wrote this $#!7: Authorship and Semantic Web Markup for SEO and Social Me...
Who Wrote this $#!7: Authorship and Semantic Web Markup for SEO and Social Me...Nick Moline
 
Second Stage Booster: Optimizing Drupal and Wordpress for SEO, Speed and Soci...
Second Stage Booster: Optimizing Drupal and Wordpress for SEO, Speed and Soci...Second Stage Booster: Optimizing Drupal and Wordpress for SEO, Speed and Soci...
Second Stage Booster: Optimizing Drupal and Wordpress for SEO, Speed and Soci...Nick Moline
 
Bcsl 033 data and file structures lab s3-1
Bcsl 033 data and file structures lab s3-1Bcsl 033 data and file structures lab s3-1
Bcsl 033 data and file structures lab s3-1Dr. Loganathan R
 
The story of_intenet_1
The story of_intenet_1The story of_intenet_1
The story of_intenet_1Jorgelcb
 
#ThroughGlass : An Introduction to Google Glass
#ThroughGlass : An Introduction to Google Glass#ThroughGlass : An Introduction to Google Glass
#ThroughGlass : An Introduction to Google GlassNick Moline
 
The Afterburner - Optimizing Drupal for Speed and SEO
The Afterburner - Optimizing Drupal for Speed and SEOThe Afterburner - Optimizing Drupal for Speed and SEO
The Afterburner - Optimizing Drupal for Speed and SEONick Moline
 
Yadira's 37th presentation
Yadira's 37th presentationYadira's 37th presentation
Yadira's 37th presentationDenis Martinez
 

Destaque (20)

Operating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - ThreadsOperating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - Threads
 
Ch5: Threads (Operating System)
Ch5: Threads (Operating System)Ch5: Threads (Operating System)
Ch5: Threads (Operating System)
 
7 Deadlocks
7 Deadlocks7 Deadlocks
7 Deadlocks
 
3 process management
3 process management3 process management
3 process management
 
10 File System
10 File System10 File System
10 File System
 
9 virtual memory management
9 virtual memory management9 virtual memory management
9 virtual memory management
 
5 Process Scheduling
5 Process Scheduling5 Process Scheduling
5 Process Scheduling
 
8 memory management strategies
8 memory management strategies8 memory management strategies
8 memory management strategies
 
Presentation on operating system
 Presentation on operating system Presentation on operating system
Presentation on operating system
 
Networking threads
Networking threadsNetworking threads
Networking threads
 
Threads (operating System)
Threads (operating System)Threads (operating System)
Threads (operating System)
 
European empires and conquest
European empires and conquestEuropean empires and conquest
European empires and conquest
 
Who Wrote this $#!7: Authorship and Semantic Web Markup for SEO and Social Me...
Who Wrote this $#!7: Authorship and Semantic Web Markup for SEO and Social Me...Who Wrote this $#!7: Authorship and Semantic Web Markup for SEO and Social Me...
Who Wrote this $#!7: Authorship and Semantic Web Markup for SEO and Social Me...
 
Second Stage Booster: Optimizing Drupal and Wordpress for SEO, Speed and Soci...
Second Stage Booster: Optimizing Drupal and Wordpress for SEO, Speed and Soci...Second Stage Booster: Optimizing Drupal and Wordpress for SEO, Speed and Soci...
Second Stage Booster: Optimizing Drupal and Wordpress for SEO, Speed and Soci...
 
Bcsl 033 data and file structures lab s3-1
Bcsl 033 data and file structures lab s3-1Bcsl 033 data and file structures lab s3-1
Bcsl 033 data and file structures lab s3-1
 
The story of_intenet_1
The story of_intenet_1The story of_intenet_1
The story of_intenet_1
 
Ning post 2
Ning post 2Ning post 2
Ning post 2
 
#ThroughGlass : An Introduction to Google Glass
#ThroughGlass : An Introduction to Google Glass#ThroughGlass : An Introduction to Google Glass
#ThroughGlass : An Introduction to Google Glass
 
The Afterburner - Optimizing Drupal for Speed and SEO
The Afterburner - Optimizing Drupal for Speed and SEOThe Afterburner - Optimizing Drupal for Speed and SEO
The Afterburner - Optimizing Drupal for Speed and SEO
 
Yadira's 37th presentation
Yadira's 37th presentationYadira's 37th presentation
Yadira's 37th presentation
 

Semelhante a 4 threads

Semelhante a 4 threads (20)

Ch4 threads
Ch4   threadsCh4   threads
Ch4 threads
 
Ch04 threads
Ch04 threadsCh04 threads
Ch04 threads
 
OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptx
 
Operating system 22 threading issues
Operating system 22 threading issuesOperating system 22 threading issues
Operating system 22 threading issues
 
Lecture 3- Threads (1).pptx
Lecture 3- Threads (1).pptxLecture 3- Threads (1).pptx
Lecture 3- Threads (1).pptx
 
Processes and Threads in Windows Vista
Processes and Threads in Windows VistaProcesses and Threads in Windows Vista
Processes and Threads in Windows Vista
 
Multithreaded Programming Part- III.pdf
Multithreaded Programming Part- III.pdfMultithreaded Programming Part- III.pdf
Multithreaded Programming Part- III.pdf
 
Sucet os module_2_notes
Sucet os module_2_notesSucet os module_2_notes
Sucet os module_2_notes
 
Chapter 6 os
Chapter 6 osChapter 6 os
Chapter 6 os
 
OS Thr schd.ppt
OS Thr schd.pptOS Thr schd.ppt
OS Thr schd.ppt
 
Os
OsOs
Os
 
chapter4-processes nd processors in DS.ppt
chapter4-processes nd processors in DS.pptchapter4-processes nd processors in DS.ppt
chapter4-processes nd processors in DS.ppt
 
Thread
ThreadThread
Thread
 
Thread
ThreadThread
Thread
 
Lecture 3- Threads.pdf
Lecture 3- Threads.pdfLecture 3- Threads.pdf
Lecture 3- Threads.pdf
 
Module2 MultiThreads.ppt
Module2 MultiThreads.pptModule2 MultiThreads.ppt
Module2 MultiThreads.ppt
 
Threading.pptx
Threading.pptxThreading.pptx
Threading.pptx
 
1 Multithreading basics.pptx
1 Multithreading basics.pptx1 Multithreading basics.pptx
1 Multithreading basics.pptx
 
Bglrsession4
Bglrsession4Bglrsession4
Bglrsession4
 
multi-threading
multi-threadingmulti-threading
multi-threading
 

Mais de Dr. Loganathan R

Ch 6 IoT Processing Topologies and Types.pdf
Ch 6 IoT Processing Topologies and Types.pdfCh 6 IoT Processing Topologies and Types.pdf
Ch 6 IoT Processing Topologies and Types.pdfDr. Loganathan R
 
IoT Sensing and Actuation.pdf
 IoT Sensing and Actuation.pdf IoT Sensing and Actuation.pdf
IoT Sensing and Actuation.pdfDr. Loganathan R
 
Program in ‘C’ language to implement linear search using pointers
Program in ‘C’ language to implement linear search using pointersProgram in ‘C’ language to implement linear search using pointers
Program in ‘C’ language to implement linear search using pointersDr. Loganathan R
 
Implement a queue using two stacks.
Implement a queue using two stacks.Implement a queue using two stacks.
Implement a queue using two stacks.Dr. Loganathan R
 
Bcsl 033 data and file structures lab s5-3
Bcsl 033 data and file structures lab s5-3Bcsl 033 data and file structures lab s5-3
Bcsl 033 data and file structures lab s5-3Dr. Loganathan R
 
Bcsl 033 data and file structures lab s5-2
Bcsl 033 data and file structures lab s5-2Bcsl 033 data and file structures lab s5-2
Bcsl 033 data and file structures lab s5-2Dr. Loganathan R
 
Bcsl 033 data and file structures lab s4-3
Bcsl 033 data and file structures lab s4-3Bcsl 033 data and file structures lab s4-3
Bcsl 033 data and file structures lab s4-3Dr. Loganathan R
 
Bcsl 033 data and file structures lab s4-2
Bcsl 033 data and file structures lab s4-2Bcsl 033 data and file structures lab s4-2
Bcsl 033 data and file structures lab s4-2Dr. Loganathan R
 
Bcsl 033 data and file structures lab s3-3
Bcsl 033 data and file structures lab s3-3Bcsl 033 data and file structures lab s3-3
Bcsl 033 data and file structures lab s3-3Dr. Loganathan R
 
Bcsl 033 data and file structures lab s3-2
Bcsl 033 data and file structures lab s3-2Bcsl 033 data and file structures lab s3-2
Bcsl 033 data and file structures lab s3-2Dr. Loganathan R
 
Bcsl 033 data and file structures lab s2-3
Bcsl 033 data and file structures lab s2-3Bcsl 033 data and file structures lab s2-3
Bcsl 033 data and file structures lab s2-3Dr. Loganathan R
 
Bcsl 033 data and file structures lab s2-2
Bcsl 033 data and file structures lab s2-2Bcsl 033 data and file structures lab s2-2
Bcsl 033 data and file structures lab s2-2Dr. Loganathan R
 
Bcsl 033 data and file structures lab s2-1
Bcsl 033 data and file structures lab s2-1Bcsl 033 data and file structures lab s2-1
Bcsl 033 data and file structures lab s2-1Dr. Loganathan R
 
Bcsl 033 data and file structures lab s1-4
Bcsl 033 data and file structures lab s1-4Bcsl 033 data and file structures lab s1-4
Bcsl 033 data and file structures lab s1-4Dr. Loganathan R
 
Bcsl 033 data and file structures lab s1-3
Bcsl 033 data and file structures lab s1-3Bcsl 033 data and file structures lab s1-3
Bcsl 033 data and file structures lab s1-3Dr. Loganathan R
 
Bcsl 033 data and file structures lab s1-2
Bcsl 033 data and file structures lab s1-2Bcsl 033 data and file structures lab s1-2
Bcsl 033 data and file structures lab s1-2Dr. Loganathan R
 
Bcsl 033 data and file structures lab s1-1
Bcsl 033 data and file structures lab s1-1Bcsl 033 data and file structures lab s1-1
Bcsl 033 data and file structures lab s1-1Dr. Loganathan R
 
Introduction to Information Security
Introduction to Information SecurityIntroduction to Information Security
Introduction to Information SecurityDr. Loganathan R
 
Mcs 012 computer organisation and assemly language programming- ignou assignm...
Mcs 012 computer organisation and assemly language programming- ignou assignm...Mcs 012 computer organisation and assemly language programming- ignou assignm...
Mcs 012 computer organisation and assemly language programming- ignou assignm...Dr. Loganathan R
 

Mais de Dr. Loganathan R (20)

Ch 6 IoT Processing Topologies and Types.pdf
Ch 6 IoT Processing Topologies and Types.pdfCh 6 IoT Processing Topologies and Types.pdf
Ch 6 IoT Processing Topologies and Types.pdf
 
IoT Sensing and Actuation.pdf
 IoT Sensing and Actuation.pdf IoT Sensing and Actuation.pdf
IoT Sensing and Actuation.pdf
 
Ch 4 Emergence of IoT.pdf
Ch 4 Emergence of IoT.pdfCh 4 Emergence of IoT.pdf
Ch 4 Emergence of IoT.pdf
 
Program in ‘C’ language to implement linear search using pointers
Program in ‘C’ language to implement linear search using pointersProgram in ‘C’ language to implement linear search using pointers
Program in ‘C’ language to implement linear search using pointers
 
Implement a queue using two stacks.
Implement a queue using two stacks.Implement a queue using two stacks.
Implement a queue using two stacks.
 
Bcsl 033 data and file structures lab s5-3
Bcsl 033 data and file structures lab s5-3Bcsl 033 data and file structures lab s5-3
Bcsl 033 data and file structures lab s5-3
 
Bcsl 033 data and file structures lab s5-2
Bcsl 033 data and file structures lab s5-2Bcsl 033 data and file structures lab s5-2
Bcsl 033 data and file structures lab s5-2
 
Bcsl 033 data and file structures lab s4-3
Bcsl 033 data and file structures lab s4-3Bcsl 033 data and file structures lab s4-3
Bcsl 033 data and file structures lab s4-3
 
Bcsl 033 data and file structures lab s4-2
Bcsl 033 data and file structures lab s4-2Bcsl 033 data and file structures lab s4-2
Bcsl 033 data and file structures lab s4-2
 
Bcsl 033 data and file structures lab s3-3
Bcsl 033 data and file structures lab s3-3Bcsl 033 data and file structures lab s3-3
Bcsl 033 data and file structures lab s3-3
 
Bcsl 033 data and file structures lab s3-2
Bcsl 033 data and file structures lab s3-2Bcsl 033 data and file structures lab s3-2
Bcsl 033 data and file structures lab s3-2
 
Bcsl 033 data and file structures lab s2-3
Bcsl 033 data and file structures lab s2-3Bcsl 033 data and file structures lab s2-3
Bcsl 033 data and file structures lab s2-3
 
Bcsl 033 data and file structures lab s2-2
Bcsl 033 data and file structures lab s2-2Bcsl 033 data and file structures lab s2-2
Bcsl 033 data and file structures lab s2-2
 
Bcsl 033 data and file structures lab s2-1
Bcsl 033 data and file structures lab s2-1Bcsl 033 data and file structures lab s2-1
Bcsl 033 data and file structures lab s2-1
 
Bcsl 033 data and file structures lab s1-4
Bcsl 033 data and file structures lab s1-4Bcsl 033 data and file structures lab s1-4
Bcsl 033 data and file structures lab s1-4
 
Bcsl 033 data and file structures lab s1-3
Bcsl 033 data and file structures lab s1-3Bcsl 033 data and file structures lab s1-3
Bcsl 033 data and file structures lab s1-3
 
Bcsl 033 data and file structures lab s1-2
Bcsl 033 data and file structures lab s1-2Bcsl 033 data and file structures lab s1-2
Bcsl 033 data and file structures lab s1-2
 
Bcsl 033 data and file structures lab s1-1
Bcsl 033 data and file structures lab s1-1Bcsl 033 data and file structures lab s1-1
Bcsl 033 data and file structures lab s1-1
 
Introduction to Information Security
Introduction to Information SecurityIntroduction to Information Security
Introduction to Information Security
 
Mcs 012 computer organisation and assemly language programming- ignou assignm...
Mcs 012 computer organisation and assemly language programming- ignou assignm...Mcs 012 computer organisation and assemly language programming- ignou assignm...
Mcs 012 computer organisation and assemly language programming- ignou assignm...
 

Último

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 

Último (20)

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 

4 threads

  • 1. Threads • Overview • Multithreading Models • Thread Libraries • Threading Issues
  • 2. 1. Overview • A thread is a basic unit of CPU utilization, It includes a thread ID, a program counter, a register set, and a stack • It shares code section, data section, and other OS resources like open files and signals with other threads belonging to the same process • A traditional (or heavyweight) process has a single thread of control • Difference between traditional single-threaded & multithreaded process 2 Loganathan R, CSE, HKBKCE
  • 3. 1. Overview Contd… 1.1 Motivation • A single application may be required to perform several similar tasks • Example : A web server may have several of clients concurrently accessing it • Tradition Solution : The server run as a single process that accepts requests and when it receives a request, creates a separate process to service that request • Process creation is time consuming and resource intensive • The server create a separate thread to listen for client requests, when a request made, it create another thread to service the request • RPC servers are multithreaded a server receives a message, it services the message using a separate thread, which allows the server to service several concurrent requests. • Example :Java's RMI systems • OS kernels are now multithreaded, several threads operate in the kernel, and each thread performs a specific task, such as managing devices or interrupt handling • Example : Linux uses a kernel thread for managing free memory 3 Loganathan R, CSE, HKBKCE
  • 4. 1. Overview Contd… 1.2 Benefits • Responsiveness – Multithreading allow a program to continue running even if part of it is blocked or is performing a lengthy operation, thereby increasing responsiveness to the user. – For example a multithreaded web browser could allow user interaction in one thread while an image was being loaded in another thread • Resource Sharing – By default, threads share the memory and the resources of the process to which they belong • Economy – Allocating memory and resources for process creation is costly, more economical to create and context-switch threads • Utilization of Multiprocessor Architectures – Multithreading on a multi-CPU machine increases concurrency, threads may be running in parallel on different processors, Loganathan R, CSE, HKBKCE 4
  • 5. 2. Multithreading Models • Support for threads may be provided either at the user level, for user threads, or by the kernel, for kernel threads • User Threads - Thread management done by user-level threads without kernel support. User thread libraries: POSIX Pthreads , Win32 threads, Java threads • Kernel Threads - Supported and managed directly by the OS. Examples : Windows XP/2000, Solaris, Linux, Tru64 UNIX, Mac OS X • The relationship between user threads and kernel threads are established in 3 ways 2.1 Many-to-One Model • Many user-level threads mapped to single kernel thread • Thread management is done by the thread library in user space, so it is efficient • Disadvantages – The entire process will block if a thread makes a blocking system call – Multiple threads are unable to run in parallel on multiprocessors since only one thread can access the kernel at a time • Examples – Solaris Green Threads – GNU Portable Threads Loganathan R, CSE, HKBKCE 5
  • 6. 2. Multithreading Models Contd… 2.2 One -to-One Model • Each user-level thread maps to kernel thread • It provides more concurrency i.e. allows another thread to run when a thread makes a blocking system call • Allows multiple threads to run in parallel on multiprocessors • Disadvantages • Creating a user thread requires creating the corresponding kernel thread • Examples : Windows NT/XP/2000, Linux Many-to-One Model One -to-One Model Loganathan R, CSE, HKBKCE 6
  • 7. 2. Multithreading Models Contd… 2.3 Many-to-Many Model • Allows many user level threads to be mapped to many kernel threads • Allows the user and OS to create a sufficient number of user and kernel threads • When a thread performs a blocking system call, the kernel can schedule another thread for execution • Windows NT/2000 with the ThreadFiber package Two-level Model : • Similar to M:M, except that it allows a user thread to be bound to kernel thread • Examples : IRIX, HP-UX, Tru64 UNIX, Solaris 8 and earlier Many-to-Many Two-level Model Model Loganathan R, CSE, HKBKCE 7
  • 8. 3. Thread Libraries • A thread library provides the programmer an API for creating and managing threads • Two ways of implementation – Provide a library entirely in user space with no kernel support - code and data structures for the library exist in user space – A kernel-level library supported directly by the operating system - code and data structures for the library exist in kernel space 3.1 Pthreads • Provided as either a user or kernel-level library • A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization • API specifies behavior of the thread library, implementation is up to development of the library • Common in UNIX operating systems (Solaris, Linux, Mac OS X) • All Pthreads programs must include the pthread.h header file • pthread _t t id declares the identifier t id for the thread to be created • The pthread_attr_t attr declares the attributes for the thread • The attributes are set in the function call pthread_attr_init(&attr) • A separate thread is created with the pthread_creat e () function call • pthread_join () to wait Loganathan R, CSE, HKBKCE 8
  • 9. 3. Thread Libraries Contd… 3.2 Windows XP Threads • Threads are created in the Win32 API using the CreateThread() function with thread parameters • Parameters includes security information, the size of the stack, and a flag that can be set to indicate if the thread is to start in a suspended state • WaitForSingleObj ect () function for waiting 3.3 Java Threads • Threads are the fundamental model of program execution in a Java and managed by the JVM • Java threads may be created by: –Extending Thread class (derive Thread class and override run()) –Implementing the Runnable interface[public interface Runnable{public abstract void run();}] • start () method creates the new thread then allocates memory initialize it in JVM, and calls run() to run thread in JVM • Join() method to wait • Java Thread States Loganathan R, CSE, HKBKCE 9
  • 10. 4. Threading Issues … 4.1 The fork() and exec() System Calls • Does fork() duplicate only the calling thread or all threads? • Some UNIX have Both versions • Exec() system call works in the same way (will replace the entire process) • If exec() is called immediately after forking, duplicating only the calling thread is appropriate 4.2 Cancellation • Terminating a thread before it has completed • Multiple threads are concurrently searching a database and one thread returns the result, the remaining threads might be canceled • A thread that is to be canceled is referred as the target thread • Two general approaches: – Asynchronous cancellation terminates the target thread immediately – Deferred cancellation allows the target thread to periodically check if it should be cancelled • Difficulty in cancellation • Canceling a thread asynchronously may not free a necessary system-wide resource (OS will only reclaim System resources only) • Deferred cancellation occurs only after the target thread has checked a flag to determine if it should be canceled or not • Checking whether it should be canceled at a point when it can be canceled safely is known as cancellation points in Pthreads. Loganathan R, CSE, HKBKCE 10
  • 11. 4. Threading Issues Contd…… 4.3 Signal Handling • Signals are used in UNIX systems to notify a process that an particular event has occurred may be received either synchronously or asynchronously. • Signals whether synchronous(illegal memory access & Division by 0) or asynchronous (external event like CTL+C,& Timer expire), follow the same pattern: 1. Signal is generated by particular event 2. Signal is delivered to a process 3.Signal is handled • A signal handler is used to process signals – default signal handler that is run by the kernel when to handle the signal – user-defined signal handler that is called to override default action • In single-threaded programs, signals are always delivered to a process • In multithreaded programs: – Deliver the signal to the thread to which the signal applies – Deliver the signal to every thread in the process – Deliver the signal to certain threads in the process – Assign a specific thread to receive all signals for the process • Multithreaded versions of UNIX allow a thread to specify which signals it will accept and which it will block • Windows does not explicitly provide support for signals, they can be emulated using asynchronous procedure calls (APCs) Loganathan R, CSE, HKBKCE 11
  • 12. 4. Threading Issues Contd…… 4.4 Thread Pools • Create a number of threads at start & place in a pool where they wait for work • Advantages: – Usually slightly faster to service a request with an existing thread than create a new thread – Allows the number of threads in the application(s) to be bound to the size of the pool • The number of threads in the pool can be set based on factors like the number of CPUs in the system, the amount of physical memory, and the expected number of concurrent client requests • Win32 API provides several functions related to thread pools 4.5 Thread Specific Data • Allows each thread to have its own copy of data 4.6 Scheduler Activations • Both M:M and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application to be adjusted dynamically • An intermediate data structure between the user and kernel threads is placed and it is known as a lightweight process, or LWP Lightweight • To the user-thread library, the LWP appears to be a virtual processor on which the application can LWP Process schedule a user thread to run and each LWP attached to a kernal thread • If a kernel thread blocks, the LWP blocks, the user-level thread attached to the LWP also blocks • Communication between the user-thread library and the kernel is known as scheduler activation • The kernel provides an application with a set of virtual processors (LWPs) for the application to schedule user threads onto LWP and informs an application about certain events is known as an upcall • Upcalls are handled by the thread library with an upcall handler which run on a virtual processor is responsible to switch between threads 12