SlideShare uma empresa Scribd logo
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
 

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 oprating system
Multithreaded Programming in oprating  systemMultithreaded Programming in oprating  system
Multithreaded Programming in oprating system
 
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
 

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

Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Peter Udo Diehl
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 

Último (20)

Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
In-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsIn-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT Professionals
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomSalesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2
 
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
 
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024
 
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
 

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.