SlideShare a Scribd company logo
1 of 15
M.SANDHIYA (MSC INFO TECH)
NADAR SARASWATHI COLLEGE OF ARTS
AND SCIENCE
 The threads library allows concurrent programming in OCaml. It
provides multiple threads of control (also called lightweight
processes) that execute concurrently in the same memory space.
Threads communicate by in-place modification of shared data
structures, or by sending and receiving data on communication
channels.
 The threads library is implemented by time-sharing on a single
processor. It will not take advantage of multi-processor machines.
Using this library will therefore never make programs run faster.
However, many programs are easier to write when structured as
several communicating processes.
 Two implementations of the threads library are available, depending
on the capabilities of the operating system:
 System threads. This implementation builds on the OS-provided
threads facilities: POSIX 1003.1c threads for Unix, and Win32
threads for Windows. When available, system threads support both
bytecode and native-code programs.
 VM-level threads. This implementation performs time-sharing and
 The POSIX (libpthread) routines introduced here have
programming interfaces that are similar to the original
(libthread) Solaris multithreading library.
 The following brief roadmap directs you to the discussion of a
particular task and its associated man page.
 "Create a Default Thread"
 "Wait for Thread Termination"
 "Detaching a Thread"
 "Create a Key for Thread-Specific Data"
 "Delete the Thread-Specific Data Key"
 "Set the Thread-Specific Data Key"
 "Get the Thread-Specific Data Key"
 "Get the Thread Identifier"
 "Compare Thread IDs"
 "Initializing Threads"
 "Yield Thread Execution"
 Create a Default Thread
 When an attribute object is not specified, it
is NULL, and the default thread is created with
the following attributes:
 Unbound
 Nondetached
 With a default stack and stack size
 With the parent's priority
 You can also create a default attribute object
with pthread_attr_init(), and then use this
attribute object to create a default thread. See
the section "Initialize Attributes" for detail
 A Simple Threads Example
 In one thread executes the procedure at the top, creating
a helper thread that executes the procedure fetch(), which
involves a complicated database lookup and might take
some time.
 The main thread wants the results of the lookup but has
other work to do in the meantime. So it does those other
things and then waits for its helper to complete its job by
executing pthread_join().
 An argument, pbe, to the new thread is passed as a stack
parameter. This can be done here because the main thread
waits for the spun-off thread to terminate. In general,
though, it is better to use malloc(3C) to allocate storage
from the heap instead of passing an address to thread
stack storage, because this address might disappear or be
reassigned if the thread terminated.
Signal handling
 Signals are used in UNIX systems to notify a process that a particular
event has occurred
 A signal handler is used to process signals
1.Signal is generated by particular event
2.Signal is delivered to a process
3.Signal is handled
 Options:
◦ 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 threa to receive all signals for the process
Thread pools
 Create a number of threads in a pool where they await 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
Thread specific data
 Allows each thread to have its own copy of data
 Useful when you do not have control over the
thread creation process (i.e., when using a thread
pool)
Scheduler activations
 Many:Many models require communication to
maintain the appropriate number of kernel
threads allocated to the application
 Scheduler activations provide upcalls - a
communication mechanism from the kernel to
the thread library
 This communication allows an application to
maintain the correct number kernel threads
PThreads
 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)
Windows Threads
 Implements the one-to-one mapping
 Each thread contains
◦ A thread id
◦ Register set
◦ Separate user and kernel stacks
◦ Private data storage area
 The register set, stacks, and private storage area are known as
the context of the threads
Linux Threads
 Linux refers to them as tasks rather than threads
 Thread creation is done through clone() system call
 clone() allows a child task to share the address space of the parent
task (process)
Java Threads
 Java threads may be created by:
◦ Extending Thread class
◦ Implementing the Runnable interface
 Java threads are managed by the JVM.
 Responsiveness
 Resource Sharing
 Economy
 Utilization of MP Architectures
 Thread management done by user-level threads library
 Examples
- POSIX Pthreads
- Mach C-threads
- Solaris threads
• Supported by the Kernel
• Examples
- Windows 95/98/NT/2000
- Solaris
- Tru64 UNIX
- BeOS
• Supported by the Kernel
• Examples
• - Windows 95/98/NT/2000Supported by the Kernel
• Examples
- Windows 95/98/NT/2000
- Solaris
- Tru64 UNIX
- BeOS
- Linux
- Solaris
- Tru64 UNIX
- BeOS
- Linux
 Each user-level thread maps to kernel thread.
 Examples
- Windows 95/98/NT/2000
- Linux
Semantics of fork() and exec() system calls
 Does fork() duplicate only the calling thread or all threads?
Thread cancellation
 Terminating a thread before it has finished
 Two general approaches:
◦ Asynchronous cancellation terminates the target thread
immediately
◦ Deferred cancellation allows the target thread to
periodically check if it should be cancelled
 Modern operating systems have multi-threaded processes.
 A program starts with one main thread, but once running it may
create more threads.
 Threads may enter the kernel (e.g., syscall).
 Threads are known to the kernel and have separate kernel
stacks, so they can block independently in the kernel.
◦ Kernel has syscalls to create threads (e.g., Linux clone).
Inherent costs of kernel threads
◦ Cost of accessing thread management functions.
 Kernel trap
 Copying and checking parameters
 Costly compared to user level.
◦ Cost of generality
Thread management is not inherently expensive.
◦ Can be within order of magnitude of procedure call.
◦ So overhead added by kernel is significant.

More Related Content

What's hot

multi-threading
multi-threadingmulti-threading
multi-threadingEzzat Gul
 
Thread management
Thread management Thread management
Thread management Ayaan Adeel
 
Multithreading
MultithreadingMultithreading
MultithreadingA B Shinde
 
Multithreading Presentation
Multithreading PresentationMultithreading Presentation
Multithreading PresentationNeeraj Kaushik
 
Multithreading models
Multithreading modelsMultithreading models
Multithreading modelsanoopkrishna2
 
Basic Multithreading using Posix Threads
Basic Multithreading using Posix ThreadsBasic Multithreading using Posix Threads
Basic Multithreading using Posix ThreadsTushar B Kute
 
Intro To .Net Threads
Intro To .Net ThreadsIntro To .Net Threads
Intro To .Net Threadsrchakra
 
Correct and efficient synchronization of java thread
Correct and efficient synchronization of java threadCorrect and efficient synchronization of java thread
Correct and efficient synchronization of java threadoutofmemoryerror
 
MULTITHREADING CONCEPT
MULTITHREADING CONCEPTMULTITHREADING CONCEPT
MULTITHREADING CONCEPTRAVI MAURYA
 
Direct Code Execution - LinuxCon Japan 2014
Direct Code Execution - LinuxCon Japan 2014Direct Code Execution - LinuxCon Japan 2014
Direct Code Execution - LinuxCon Japan 2014Hajime Tazaki
 
Linux Kernel Library - Reusing Monolithic Kernel
Linux Kernel Library - Reusing Monolithic KernelLinux Kernel Library - Reusing Monolithic Kernel
Linux Kernel Library - Reusing Monolithic KernelHajime Tazaki
 
Linux containers-namespaces(Dec 2014)
Linux containers-namespaces(Dec 2014)Linux containers-namespaces(Dec 2014)
Linux containers-namespaces(Dec 2014)Ralf Dannert
 

What's hot (20)

multi-threading
multi-threadingmulti-threading
multi-threading
 
P-Threads
P-ThreadsP-Threads
P-Threads
 
Thread management
Thread management Thread management
Thread management
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Multithreading Presentation
Multithreading PresentationMultithreading Presentation
Multithreading Presentation
 
Multithreading models
Multithreading modelsMultithreading models
Multithreading models
 
Basic Multithreading using Posix Threads
Basic Multithreading using Posix ThreadsBasic Multithreading using Posix Threads
Basic Multithreading using Posix Threads
 
Bglrsession4
Bglrsession4Bglrsession4
Bglrsession4
 
Pthreads linux
Pthreads linuxPthreads linux
Pthreads linux
 
Section04 threads
Section04 threadsSection04 threads
Section04 threads
 
Intro To .Net Threads
Intro To .Net ThreadsIntro To .Net Threads
Intro To .Net Threads
 
Treads
TreadsTreads
Treads
 
Os Threads
Os ThreadsOs Threads
Os Threads
 
Threads
ThreadsThreads
Threads
 
Correct and efficient synchronization of java thread
Correct and efficient synchronization of java threadCorrect and efficient synchronization of java thread
Correct and efficient synchronization of java thread
 
MULTITHREADING CONCEPT
MULTITHREADING CONCEPTMULTITHREADING CONCEPT
MULTITHREADING CONCEPT
 
Direct Code Execution - LinuxCon Japan 2014
Direct Code Execution - LinuxCon Japan 2014Direct Code Execution - LinuxCon Japan 2014
Direct Code Execution - LinuxCon Japan 2014
 
IPC
IPCIPC
IPC
 
Linux Kernel Library - Reusing Monolithic Kernel
Linux Kernel Library - Reusing Monolithic KernelLinux Kernel Library - Reusing Monolithic Kernel
Linux Kernel Library - Reusing Monolithic Kernel
 
Linux containers-namespaces(Dec 2014)
Linux containers-namespaces(Dec 2014)Linux containers-namespaces(Dec 2014)
Linux containers-namespaces(Dec 2014)
 

Similar to Os

Operating System 4 1193308760782240 2
Operating System 4 1193308760782240 2Operating System 4 1193308760782240 2
Operating System 4 1193308760782240 2mona_hakmy
 
Operating System 4
Operating System 4Operating System 4
Operating System 4tech2click
 
OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptxbleh23
 
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
 
Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01Aravindharamanan S
 
WEEK07operatingsystemdepartmentofsoftwareengineering.pptx
WEEK07operatingsystemdepartmentofsoftwareengineering.pptxWEEK07operatingsystemdepartmentofsoftwareengineering.pptx
WEEK07operatingsystemdepartmentofsoftwareengineering.pptxbabayaga920391
 
Operating System Chapter 4 Multithreaded programming
Operating System Chapter 4 Multithreaded programmingOperating System Chapter 4 Multithreaded programming
Operating System Chapter 4 Multithreaded programmingguesta40f80
 
Networking threads
Networking threadsNetworking threads
Networking threadsNilesh Pawar
 
Processes and Threads in Windows Vista
Processes and Threads in Windows VistaProcesses and Threads in Windows Vista
Processes and Threads in Windows VistaTrinh Phuc Tho
 
Lecture 3- Threads (1).pptx
Lecture 3- Threads (1).pptxLecture 3- Threads (1).pptx
Lecture 3- Threads (1).pptxAmanuelmergia
 
Ch5 OS
Ch5 OSCh5 OS
Ch5 OSC.U
 

Similar to Os (20)

Chapter 6 os
Chapter 6 osChapter 6 os
Chapter 6 os
 
CH04.pdf
CH04.pdfCH04.pdf
CH04.pdf
 
Operating System 4 1193308760782240 2
Operating System 4 1193308760782240 2Operating System 4 1193308760782240 2
Operating System 4 1193308760782240 2
 
Operating System 4
Operating System 4Operating System 4
Operating System 4
 
Threading.pptx
Threading.pptxThreading.pptx
Threading.pptx
 
OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptx
 
Ch04 threads
Ch04 threadsCh04 threads
Ch04 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"
 
Sucet os module_2_notes
Sucet os module_2_notesSucet os module_2_notes
Sucet os module_2_notes
 
Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01
 
WEEK07operatingsystemdepartmentofsoftwareengineering.pptx
WEEK07operatingsystemdepartmentofsoftwareengineering.pptxWEEK07operatingsystemdepartmentofsoftwareengineering.pptx
WEEK07operatingsystemdepartmentofsoftwareengineering.pptx
 
Operating System Chapter 4 Multithreaded programming
Operating System Chapter 4 Multithreaded programmingOperating System Chapter 4 Multithreaded programming
Operating System Chapter 4 Multithreaded programming
 
4.Process.ppt
4.Process.ppt4.Process.ppt
4.Process.ppt
 
4 threads
4 threads4 threads
4 threads
 
Topic 4- processes.pptx
Topic 4- processes.pptxTopic 4- processes.pptx
Topic 4- processes.pptx
 
Networking threads
Networking threadsNetworking threads
Networking threads
 
Processes and Threads in Windows Vista
Processes and Threads in Windows VistaProcesses and Threads in Windows Vista
Processes and Threads in Windows Vista
 
Lecture 3- Threads (1).pptx
Lecture 3- Threads (1).pptxLecture 3- Threads (1).pptx
Lecture 3- Threads (1).pptx
 
Chapter04 new
Chapter04 newChapter04 new
Chapter04 new
 
Ch5 OS
Ch5 OSCh5 OS
Ch5 OS
 

Recently uploaded

Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxabhijeetpadhi001
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 

Recently uploaded (20)

Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 

Os

  • 1. M.SANDHIYA (MSC INFO TECH) NADAR SARASWATHI COLLEGE OF ARTS AND SCIENCE
  • 2.  The threads library allows concurrent programming in OCaml. It provides multiple threads of control (also called lightweight processes) that execute concurrently in the same memory space. Threads communicate by in-place modification of shared data structures, or by sending and receiving data on communication channels.  The threads library is implemented by time-sharing on a single processor. It will not take advantage of multi-processor machines. Using this library will therefore never make programs run faster. However, many programs are easier to write when structured as several communicating processes.  Two implementations of the threads library are available, depending on the capabilities of the operating system:  System threads. This implementation builds on the OS-provided threads facilities: POSIX 1003.1c threads for Unix, and Win32 threads for Windows. When available, system threads support both bytecode and native-code programs.  VM-level threads. This implementation performs time-sharing and
  • 3.  The POSIX (libpthread) routines introduced here have programming interfaces that are similar to the original (libthread) Solaris multithreading library.  The following brief roadmap directs you to the discussion of a particular task and its associated man page.  "Create a Default Thread"  "Wait for Thread Termination"  "Detaching a Thread"  "Create a Key for Thread-Specific Data"  "Delete the Thread-Specific Data Key"  "Set the Thread-Specific Data Key"  "Get the Thread-Specific Data Key"  "Get the Thread Identifier"  "Compare Thread IDs"  "Initializing Threads"  "Yield Thread Execution"
  • 4.  Create a Default Thread  When an attribute object is not specified, it is NULL, and the default thread is created with the following attributes:  Unbound  Nondetached  With a default stack and stack size  With the parent's priority  You can also create a default attribute object with pthread_attr_init(), and then use this attribute object to create a default thread. See the section "Initialize Attributes" for detail
  • 5.  A Simple Threads Example  In one thread executes the procedure at the top, creating a helper thread that executes the procedure fetch(), which involves a complicated database lookup and might take some time.  The main thread wants the results of the lookup but has other work to do in the meantime. So it does those other things and then waits for its helper to complete its job by executing pthread_join().  An argument, pbe, to the new thread is passed as a stack parameter. This can be done here because the main thread waits for the spun-off thread to terminate. In general, though, it is better to use malloc(3C) to allocate storage from the heap instead of passing an address to thread stack storage, because this address might disappear or be reassigned if the thread terminated.
  • 6. Signal handling  Signals are used in UNIX systems to notify a process that a particular event has occurred  A signal handler is used to process signals 1.Signal is generated by particular event 2.Signal is delivered to a process 3.Signal is handled  Options: ◦ 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 threa to receive all signals for the process Thread pools  Create a number of threads in a pool where they await 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
  • 7. Thread specific data  Allows each thread to have its own copy of data  Useful when you do not have control over the thread creation process (i.e., when using a thread pool) Scheduler activations  Many:Many models require communication to maintain the appropriate number of kernel threads allocated to the application  Scheduler activations provide upcalls - a communication mechanism from the kernel to the thread library  This communication allows an application to maintain the correct number kernel threads
  • 8. PThreads  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) Windows Threads  Implements the one-to-one mapping  Each thread contains ◦ A thread id ◦ Register set ◦ Separate user and kernel stacks ◦ Private data storage area  The register set, stacks, and private storage area are known as the context of the threads
  • 9.
  • 10. Linux Threads  Linux refers to them as tasks rather than threads  Thread creation is done through clone() system call  clone() allows a child task to share the address space of the parent task (process) Java Threads  Java threads may be created by: ◦ Extending Thread class ◦ Implementing the Runnable interface  Java threads are managed by the JVM.
  • 11.  Responsiveness  Resource Sharing  Economy  Utilization of MP Architectures  Thread management done by user-level threads library  Examples - POSIX Pthreads - Mach C-threads - Solaris threads • Supported by the Kernel • Examples - Windows 95/98/NT/2000 - Solaris - Tru64 UNIX - BeOS
  • 12. • Supported by the Kernel • Examples • - Windows 95/98/NT/2000Supported by the Kernel • Examples - Windows 95/98/NT/2000 - Solaris - Tru64 UNIX - BeOS - Linux - Solaris - Tru64 UNIX - BeOS - Linux
  • 13.  Each user-level thread maps to kernel thread.  Examples - Windows 95/98/NT/2000 - Linux
  • 14. Semantics of fork() and exec() system calls  Does fork() duplicate only the calling thread or all threads? Thread cancellation  Terminating a thread before it has finished  Two general approaches: ◦ Asynchronous cancellation terminates the target thread immediately ◦ Deferred cancellation allows the target thread to periodically check if it should be cancelled
  • 15.  Modern operating systems have multi-threaded processes.  A program starts with one main thread, but once running it may create more threads.  Threads may enter the kernel (e.g., syscall).  Threads are known to the kernel and have separate kernel stacks, so they can block independently in the kernel. ◦ Kernel has syscalls to create threads (e.g., Linux clone). Inherent costs of kernel threads ◦ Cost of accessing thread management functions.  Kernel trap  Copying and checking parameters  Costly compared to user level. ◦ Cost of generality Thread management is not inherently expensive. ◦ Can be within order of magnitude of procedure call. ◦ So overhead added by kernel is significant.