SlideShare ist ein Scribd-Unternehmen logo
1 von 12
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

Management of I/O request & Communication among devices
Management of I/O request & Communication among devicesManagement of I/O request & Communication among devices
Management of I/O request & Communication among devicesManish Halai
 
Part 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingPart 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingTushar B Kute
 
Install and configure linux
Install and configure linuxInstall and configure linux
Install and configure linuxVicent Selfa
 
Operating system architecture
Operating system architectureOperating system architecture
Operating system architectureSabin dumre
 
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
 
Key management and distribution
Key management and distributionKey management and distribution
Key management and distributionRiya Choudhary
 
Os unit 3 , process management
Os unit 3 , process managementOs unit 3 , process management
Os unit 3 , process managementArnav Chowdhury
 
Operating system 24 mutex locks and semaphores
Operating system 24 mutex locks and semaphoresOperating system 24 mutex locks and semaphores
Operating system 24 mutex locks and semaphoresVaibhav Khanna
 
Introduction to operating system, system calls and interrupts
Introduction to operating system, system calls and interruptsIntroduction to operating system, system calls and interrupts
Introduction to operating system, system calls and interruptsShivam Mitra
 
Unix operating system
Unix operating systemUnix operating system
Unix operating systemABhay Panchal
 
Linux Boot Process
Linux Boot ProcessLinux Boot Process
Linux Boot Processdarshhingu
 
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
 
Kernel security of Systems
Kernel security of SystemsKernel security of Systems
Kernel security of SystemsJamal Jamali
 

Was ist angesagt? (20)

Management of I/O request & Communication among devices
Management of I/O request & Communication among devicesManagement of I/O request & Communication among devices
Management of I/O request & Communication among devices
 
Message passing in Distributed Computing Systems
Message passing in Distributed Computing SystemsMessage passing in Distributed Computing Systems
Message passing in Distributed Computing Systems
 
Part 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingPart 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module Programming
 
Chapter 1: Introduction to Unix / Linux Kernel
Chapter 1: Introduction to Unix / Linux KernelChapter 1: Introduction to Unix / Linux Kernel
Chapter 1: Introduction to Unix / Linux Kernel
 
Application Layer
Application Layer Application Layer
Application Layer
 
Install and configure linux
Install and configure linuxInstall and configure linux
Install and configure linux
 
Token ring
Token ringToken ring
Token ring
 
Operating system architecture
Operating system architectureOperating system architecture
Operating system architecture
 
Mac layer
Mac  layerMac  layer
Mac layer
 
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
 
Key management and distribution
Key management and distributionKey management and distribution
Key management and distribution
 
System calls
System callsSystem calls
System calls
 
Os unit 3 , process management
Os unit 3 , process managementOs unit 3 , process management
Os unit 3 , process management
 
Operating system 24 mutex locks and semaphores
Operating system 24 mutex locks and semaphoresOperating system 24 mutex locks and semaphores
Operating system 24 mutex locks and semaphores
 
Introduction to operating system, system calls and interrupts
Introduction to operating system, system calls and interruptsIntroduction to operating system, system calls and interrupts
Introduction to operating system, system calls and interrupts
 
Bandwidth utilization
Bandwidth utilizationBandwidth utilization
Bandwidth utilization
 
Unix operating system
Unix operating systemUnix operating system
Unix operating system
 
Linux Boot Process
Linux Boot ProcessLinux Boot Process
Linux Boot Process
 
Processes and Processors in Distributed Systems
Processes and Processors in Distributed SystemsProcesses and Processors in Distributed Systems
Processes and Processors in Distributed Systems
 
Kernel security of Systems
Kernel security of SystemsKernel security of Systems
Kernel security of Systems
 

Andere mochten auch

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
 
Process and Threads in Linux - PPT
Process and Threads in Linux - PPTProcess and Threads in Linux - PPT
Process and Threads in Linux - PPTQUONTRASOLUTIONS
 
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
 

Andere mochten auch (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)
 
Process and Threads in Linux - PPT
Process and Threads in Linux - PPTProcess and Threads in Linux - PPT
Process and Threads in Linux - PPT
 
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
 

Ähnlich wie 4 threads

Ähnlich wie 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
 

Mehr von 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
 

Mehr von 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...
 

Kürzlich hochgeladen

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
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
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
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
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 

Kürzlich hochgeladen (20)

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
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
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
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
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 

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