SlideShare uma empresa Scribd logo
1 de 11
Pthreads on Linux
Mark Veltzer
mark.veltzer@gmail.com
Thread identity
Linux id of threads
● Linux uses the pid space to give ids both to threads and
processes.
● The operating system itself does know which ids belong to
threads and which to processes.
● The first threads thread id also doubles as a process id.
● This does create some confusion.
● getpid() is used to get a process id.
● gettid() is used to get a thread id.
● These may return the same value depending on whether
you are the primary thread or not.
pthread_t
● To complicate matters pthread has it's own
notion of thread ids.
● This is represented by an opaque pthread_t
structure.
● This structure could be gotten when creating a
thread using pthread_create() or from within the
thread using pthread_self()
pthread_t
● The idea is that pthread should be a cross
operating system threading API and as such
such should not expose the programmer to a
particular operating systems concept of thread
id.
● There is no simple tid hiding in that structure
and the pthread_t is not cast able to it.
● In Linux pthread_t is just an unsigned long int
(see /usr/include/bits/pthreadtypes.h).
gettid()
● Returns the current thread id
● Glibc does not have a wrapper for this one. Call
it using syscall(2).
● Make your wrapper efficient (cache the result
since tid never changes...).
CPU sets
● In order to adjust affinity for threads you will need to
be able to understand and build CPU sets.
● A CPU set is a data structure that one can pass
to/from the kernel which represents a collection of
CPUs.
● The size of CPU sets in recent Linux kernels is 1024.
● This means that unless you're running on machines
that have more than 1024 CPUs you're good to go.
Stopping threads
Stopping threads - theory
● Stopping threads is one of the most difficult parts of
correct multi-threading programming.
● This sounds funny since it sounds like one should be
able to stop a thread simply by asking the operating
system to stop the thread.
● In that case one would expect the operating system
to signal the CPU that is currently running the thread
(if indeed that thread is currently running) and then
kill it's own representation of the thread.
● This is wrong!
Stopping threads - problems
● The idea of stopping a thread by having the operating system
kill it is wrong because of many reasons:
● On UNIX systems some of the threading issues (like allocating a
stack for the thread) are handled in user space so killing the thread
may leak resources.
● Locks acquired by the thread would possibly not be released
resulting in deadlocking other threads.
● The thread does not get a chance to release other resources
(memory, files etc) that it allocated.
●
The thread may leave data structures in unusable state because it
may be killed while manipulating them.
● Threads that are waiting on locks held by the thread that was killed
really should be notified that this thread was killed because this may
affect the way they tread data integrity.
Stopping threads - problem
● As a result of the above the operating system
does not support killing threads.
● The kill(1) command line and the underlying
kill(2) system call can only be used to target
processes and not threads.
● If you try to use kill(1) or kill(2) with a thread pid
you will wind up killing the entire process the
thread is a member of.

Mais conteúdo relacionado

Mais procurados

Processes and Threads in Windows Vista
Processes and Threads in Windows VistaProcesses and Threads in Windows Vista
Processes and Threads in Windows Vista
Trinh Phuc Tho
 
Modern net bsd kernel module
Modern net bsd kernel moduleModern net bsd kernel module
Modern net bsd kernel module
Masaru Oki
 
Porting FreeRTOS on OpenRISC
Porting FreeRTOS   on   OpenRISCPorting FreeRTOS   on   OpenRISC
Porting FreeRTOS on OpenRISC
Yi-Chiao
 

Mais procurados (19)

Processes and Threads in Windows Vista
Processes and Threads in Windows VistaProcesses and Threads in Windows Vista
Processes and Threads in Windows Vista
 
Process and Threads in Linux - PPT
Process and Threads in Linux - PPTProcess and Threads in Linux - PPT
Process and Threads in Linux - PPT
 
Fun with Network Interfaces
Fun with Network InterfacesFun with Network Interfaces
Fun with Network Interfaces
 
Threads and multi threading
Threads and multi threadingThreads and multi threading
Threads and multi threading
 
Os
OsOs
Os
 
The TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux KernelThe TCP/IP Stack in the Linux Kernel
The TCP/IP Stack in the Linux Kernel
 
Intro To .Net Threads
Intro To .Net ThreadsIntro To .Net Threads
Intro To .Net Threads
 
Multithreading Presentation
Multithreading PresentationMultithreading Presentation
Multithreading Presentation
 
Fun with FUSE
Fun with FUSEFun with FUSE
Fun with FUSE
 
Driver_linux
Driver_linuxDriver_linux
Driver_linux
 
Linux IO
Linux IOLinux IO
Linux IO
 
multi-threading
multi-threadingmulti-threading
multi-threading
 
Modern net bsd kernel module
Modern net bsd kernel moduleModern net bsd kernel module
Modern net bsd kernel module
 
Kernel Recipes 2016 -
Kernel Recipes 2016 - Kernel Recipes 2016 -
Kernel Recipes 2016 -
 
Porting FreeRTOS on OpenRISC
Porting FreeRTOS   on   OpenRISCPorting FreeRTOS   on   OpenRISC
Porting FreeRTOS on OpenRISC
 
Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and Concurrency
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
Thread
ThreadThread
Thread
 
Semtex.c [CVE-2013-2094] - A Linux Privelege Escalation
Semtex.c [CVE-2013-2094] - A Linux Privelege EscalationSemtex.c [CVE-2013-2094] - A Linux Privelege Escalation
Semtex.c [CVE-2013-2094] - A Linux Privelege Escalation
 

Destaque (6)

Android线程简介
Android线程简介Android线程简介
Android线程简介
 
Ch4 Threads
Ch4 ThreadsCh4 Threads
Ch4 Threads
 
Chapter 4 - Threads
Chapter 4 - ThreadsChapter 4 - Threads
Chapter 4 - Threads
 
Ch5: Threads (Operating System)
Ch5: Threads (Operating System)Ch5: Threads (Operating System)
Ch5: Threads (Operating System)
 
Threads
ThreadsThreads
Threads
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
 

Semelhante a Pthreads linux

WEEK07operatingsystemdepartmentofsoftwareengineering.pptx
WEEK07operatingsystemdepartmentofsoftwareengineering.pptxWEEK07operatingsystemdepartmentofsoftwareengineering.pptx
WEEK07operatingsystemdepartmentofsoftwareengineering.pptx
babayaga920391
 
Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01
Aravindharamanan S
 
Dfrws eu 2014 rekall workshop
Dfrws eu 2014 rekall workshopDfrws eu 2014 rekall workshop
Dfrws eu 2014 rekall workshop
Tamas K Lengyel
 
OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptx
bleh23
 

Semelhante a Pthreads linux (20)

Chap7 slides
Chap7 slidesChap7 slides
Chap7 slides
 
Concept of thread
Concept of threadConcept of thread
Concept of thread
 
WEEK07operatingsystemdepartmentofsoftwareengineering.pptx
WEEK07operatingsystemdepartmentofsoftwareengineering.pptxWEEK07operatingsystemdepartmentofsoftwareengineering.pptx
WEEK07operatingsystemdepartmentofsoftwareengineering.pptx
 
posix.pdf
posix.pdfposix.pdf
posix.pdf
 
Threads and processes
Threads and processesThreads and processes
Threads and processes
 
Threads
ThreadsThreads
Threads
 
Multithreading by rj
Multithreading by rjMultithreading by rj
Multithreading by rj
 
cachegrand: A Take on High Performance Caching
cachegrand: A Take on High Performance Cachingcachegrand: A Take on High Performance Caching
cachegrand: A Take on High Performance Caching
 
Understanding Threads in operating system
Understanding Threads in operating systemUnderstanding Threads in operating system
Understanding Threads in operating system
 
E bpf and dynamic tracing for mariadb db as (mariadb day during fosdem 2020)
E bpf and dynamic tracing for mariadb db as (mariadb day during fosdem 2020)E bpf and dynamic tracing for mariadb db as (mariadb day during fosdem 2020)
E bpf and dynamic tracing for mariadb db as (mariadb day during fosdem 2020)
 
Multithreaded processors ppt
Multithreaded processors pptMultithreaded processors ppt
Multithreaded processors ppt
 
Multithreaded_Programming_in_Python.pdf
Multithreaded_Programming_in_Python.pdfMultithreaded_Programming_in_Python.pdf
Multithreaded_Programming_in_Python.pdf
 
Coding for multiple cores
Coding for multiple coresCoding for multiple cores
Coding for multiple cores
 
Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01Introto netthreads-090906214344-phpapp01
Introto netthreads-090906214344-phpapp01
 
Operating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - ThreadsOperating Systems 1 (7/12) - Threads
Operating Systems 1 (7/12) - Threads
 
Threading
ThreadingThreading
Threading
 
Cuda
CudaCuda
Cuda
 
Gpu and The Brick Wall
Gpu and The Brick WallGpu and The Brick Wall
Gpu and The Brick Wall
 
Dfrws eu 2014 rekall workshop
Dfrws eu 2014 rekall workshopDfrws eu 2014 rekall workshop
Dfrws eu 2014 rekall workshop
 
OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptx
 

Mais de Mark Veltzer (12)

Gcc
GccGcc
Gcc
 
Gcc opt
Gcc optGcc opt
Gcc opt
 
Linux io
Linux ioLinux io
Linux io
 
Linux logging
Linux loggingLinux logging
Linux logging
 
Linux monitoring
Linux monitoringLinux monitoring
Linux monitoring
 
Linux multiplexing
Linux multiplexingLinux multiplexing
Linux multiplexing
 
Linux tmpfs
Linux tmpfsLinux tmpfs
Linux tmpfs
 
Multicore
MulticoreMulticore
Multicore
 
Realtime
RealtimeRealtime
Realtime
 
Streams
StreamsStreams
Streams
 
Volatile
VolatileVolatile
Volatile
 
Effective cplusplus
Effective cplusplusEffective cplusplus
Effective cplusplus
 

Último

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Último (20)

Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 

Pthreads linux

  • 1. Pthreads on Linux Mark Veltzer mark.veltzer@gmail.com
  • 3. Linux id of threads ● Linux uses the pid space to give ids both to threads and processes. ● The operating system itself does know which ids belong to threads and which to processes. ● The first threads thread id also doubles as a process id. ● This does create some confusion. ● getpid() is used to get a process id. ● gettid() is used to get a thread id. ● These may return the same value depending on whether you are the primary thread or not.
  • 4. pthread_t ● To complicate matters pthread has it's own notion of thread ids. ● This is represented by an opaque pthread_t structure. ● This structure could be gotten when creating a thread using pthread_create() or from within the thread using pthread_self()
  • 5. pthread_t ● The idea is that pthread should be a cross operating system threading API and as such such should not expose the programmer to a particular operating systems concept of thread id. ● There is no simple tid hiding in that structure and the pthread_t is not cast able to it. ● In Linux pthread_t is just an unsigned long int (see /usr/include/bits/pthreadtypes.h).
  • 6. gettid() ● Returns the current thread id ● Glibc does not have a wrapper for this one. Call it using syscall(2). ● Make your wrapper efficient (cache the result since tid never changes...).
  • 7. CPU sets ● In order to adjust affinity for threads you will need to be able to understand and build CPU sets. ● A CPU set is a data structure that one can pass to/from the kernel which represents a collection of CPUs. ● The size of CPU sets in recent Linux kernels is 1024. ● This means that unless you're running on machines that have more than 1024 CPUs you're good to go.
  • 9. Stopping threads - theory ● Stopping threads is one of the most difficult parts of correct multi-threading programming. ● This sounds funny since it sounds like one should be able to stop a thread simply by asking the operating system to stop the thread. ● In that case one would expect the operating system to signal the CPU that is currently running the thread (if indeed that thread is currently running) and then kill it's own representation of the thread. ● This is wrong!
  • 10. Stopping threads - problems ● The idea of stopping a thread by having the operating system kill it is wrong because of many reasons: ● On UNIX systems some of the threading issues (like allocating a stack for the thread) are handled in user space so killing the thread may leak resources. ● Locks acquired by the thread would possibly not be released resulting in deadlocking other threads. ● The thread does not get a chance to release other resources (memory, files etc) that it allocated. ● The thread may leave data structures in unusable state because it may be killed while manipulating them. ● Threads that are waiting on locks held by the thread that was killed really should be notified that this thread was killed because this may affect the way they tread data integrity.
  • 11. Stopping threads - problem ● As a result of the above the operating system does not support killing threads. ● The kill(1) command line and the underlying kill(2) system call can only be used to target processes and not threads. ● If you try to use kill(1) or kill(2) with a thread pid you will wind up killing the entire process the thread is a member of.