Enviar pesquisa
Carregar
POSIX Threads
•
1 gostou
•
4,171 visualizações
S
SysPlay eLearning Academy for You
Seguir
POSIX Threads in Linux
Leia menos
Leia mais
Tecnologia
Denunciar
Compartilhar
Denunciar
Compartilhar
1 de 21
Recomendados
Linux Network Management
Linux Network Management
Anil Kumar Pugalia
Signals
Signals
Anil Kumar Pugalia
Introduction to Linux Drivers
Introduction to Linux Drivers
Anil Kumar Pugalia
Kernel Timing Management
Kernel Timing Management
SysPlay eLearning Academy for You
Mobile Hacking using Linux Drivers
Mobile Hacking using Linux Drivers
Anil Kumar Pugalia
Linux DMA Engine
Linux DMA Engine
SysPlay eLearning Academy for You
Embedded C
Embedded C
Anil Kumar Pugalia
SPI Drivers
SPI Drivers
SysPlay eLearning Academy for You
Recomendados
Linux Network Management
Linux Network Management
Anil Kumar Pugalia
Signals
Signals
Anil Kumar Pugalia
Introduction to Linux Drivers
Introduction to Linux Drivers
Anil Kumar Pugalia
Kernel Timing Management
Kernel Timing Management
SysPlay eLearning Academy for You
Mobile Hacking using Linux Drivers
Mobile Hacking using Linux Drivers
Anil Kumar Pugalia
Linux DMA Engine
Linux DMA Engine
SysPlay eLearning Academy for You
Embedded C
Embedded C
Anil Kumar Pugalia
SPI Drivers
SPI Drivers
SysPlay eLearning Academy for You
Linux Internals Part - 3
Linux Internals Part - 3
SysPlay eLearning Academy for You
Kernel Debugging & Profiling
Kernel Debugging & Profiling
Anil Kumar Pugalia
Kernel Debugging & Profiling
Kernel Debugging & Profiling
Anil Kumar Pugalia
Block Drivers
Block Drivers
Anil Kumar Pugalia
Toolchain
Toolchain
Anil Kumar Pugalia
Understanding the BBB
Understanding the BBB
SysPlay eLearning Academy for You
Interrupts
Interrupts
Anil Kumar Pugalia
Synchronization
Synchronization
Anil Kumar Pugalia
Processes
Processes
Anil Kumar Pugalia
Architecture Porting
Architecture Porting
Anil Kumar Pugalia
Linux Kernel Overview
Linux Kernel Overview
Anil Kumar Pugalia
Embedded Software Design
Embedded Software Design
Anil Kumar Pugalia
Video Drivers
Video Drivers
Anil Kumar Pugalia
Introduction to Linux
Introduction to Linux
Anil Kumar Pugalia
Linux User Space Debugging & Profiling
Linux User Space Debugging & Profiling
Anil Kumar Pugalia
Real Time Systems
Real Time Systems
Anil Kumar Pugalia
Shell Scripting
Shell Scripting
Anil Kumar Pugalia
I2C Drivers
I2C Drivers
SysPlay eLearning Academy for You
Bootloaders
Bootloaders
Anil Kumar Pugalia
BeagleBone Black Bootloaders
BeagleBone Black Bootloaders
SysPlay eLearning Academy for You
P threads
P threads
Dr. C.V. Suresh Babu
Intake 38 12
Intake 38 12
Mahmoud Ouf
Mais conteúdo relacionado
Mais procurados
Linux Internals Part - 3
Linux Internals Part - 3
SysPlay eLearning Academy for You
Kernel Debugging & Profiling
Kernel Debugging & Profiling
Anil Kumar Pugalia
Kernel Debugging & Profiling
Kernel Debugging & Profiling
Anil Kumar Pugalia
Block Drivers
Block Drivers
Anil Kumar Pugalia
Toolchain
Toolchain
Anil Kumar Pugalia
Understanding the BBB
Understanding the BBB
SysPlay eLearning Academy for You
Interrupts
Interrupts
Anil Kumar Pugalia
Synchronization
Synchronization
Anil Kumar Pugalia
Processes
Processes
Anil Kumar Pugalia
Architecture Porting
Architecture Porting
Anil Kumar Pugalia
Linux Kernel Overview
Linux Kernel Overview
Anil Kumar Pugalia
Embedded Software Design
Embedded Software Design
Anil Kumar Pugalia
Video Drivers
Video Drivers
Anil Kumar Pugalia
Introduction to Linux
Introduction to Linux
Anil Kumar Pugalia
Linux User Space Debugging & Profiling
Linux User Space Debugging & Profiling
Anil Kumar Pugalia
Real Time Systems
Real Time Systems
Anil Kumar Pugalia
Shell Scripting
Shell Scripting
Anil Kumar Pugalia
I2C Drivers
I2C Drivers
SysPlay eLearning Academy for You
Bootloaders
Bootloaders
Anil Kumar Pugalia
BeagleBone Black Bootloaders
BeagleBone Black Bootloaders
SysPlay eLearning Academy for You
Mais procurados
(20)
Linux Internals Part - 3
Linux Internals Part - 3
Kernel Debugging & Profiling
Kernel Debugging & Profiling
Kernel Debugging & Profiling
Kernel Debugging & Profiling
Block Drivers
Block Drivers
Toolchain
Toolchain
Understanding the BBB
Understanding the BBB
Interrupts
Interrupts
Synchronization
Synchronization
Processes
Processes
Architecture Porting
Architecture Porting
Linux Kernel Overview
Linux Kernel Overview
Embedded Software Design
Embedded Software Design
Video Drivers
Video Drivers
Introduction to Linux
Introduction to Linux
Linux User Space Debugging & Profiling
Linux User Space Debugging & Profiling
Real Time Systems
Real Time Systems
Shell Scripting
Shell Scripting
I2C Drivers
I2C Drivers
Bootloaders
Bootloaders
BeagleBone Black Bootloaders
BeagleBone Black Bootloaders
Semelhante a POSIX Threads
P threads
P threads
Dr. C.V. Suresh Babu
Intake 38 12
Intake 38 12
Mahmoud Ouf
Threads
Threads
Sameer Shaik
Python multithreading
Python multithreading
Janu Jahnavi
Python multithreading
Python multithreading
Janu Jahnavi
Improving app performance using .Net Core 3.0
Improving app performance using .Net Core 3.0
Richard Banks
Pthread
Pthread
Paheerathan Mahalingam
Coding for multiple cores
Coding for multiple cores
Lee Hanxue
Cuda 2011
Cuda 2011
coolmirza143
Chap7 slides
Chap7 slides
BaliThorat1
Five cool ways the JVM can run Apache Spark faster
Five cool ways the JVM can run Apache Spark faster
Tim Ellison
SfDay 2019: Head first into Symfony Cache, Redis & Redis Cluster
SfDay 2019: Head first into Symfony Cache, Redis & Redis Cluster
André Rømcke
Project Jugaad
Project Jugaad
n|u - The Open Security Community
Parallel program design
Parallel program design
ZongYing Lyu
Java Multithreading
Java Multithreading
Ganesh Samarthyam
Processes
Processes
Anil Kumar Pugalia
Amazon EC2 deepdive and a sprinkel of AWS Compute | AWS Floor28
Amazon EC2 deepdive and a sprinkel of AWS Compute | AWS Floor28
Amazon Web Services
Why rust?
Why rust?
Mats Kindahl
A quick start on Zend Framework 2
A quick start on Zend Framework 2
Enrico Zimuel
Understanding Threads in operating system
Understanding Threads in operating system
Harrytoye2
Semelhante a POSIX Threads
(20)
P threads
P threads
Intake 38 12
Intake 38 12
Threads
Threads
Python multithreading
Python multithreading
Python multithreading
Python multithreading
Improving app performance using .Net Core 3.0
Improving app performance using .Net Core 3.0
Pthread
Pthread
Coding for multiple cores
Coding for multiple cores
Cuda 2011
Cuda 2011
Chap7 slides
Chap7 slides
Five cool ways the JVM can run Apache Spark faster
Five cool ways the JVM can run Apache Spark faster
SfDay 2019: Head first into Symfony Cache, Redis & Redis Cluster
SfDay 2019: Head first into Symfony Cache, Redis & Redis Cluster
Project Jugaad
Project Jugaad
Parallel program design
Parallel program design
Java Multithreading
Java Multithreading
Processes
Processes
Amazon EC2 deepdive and a sprinkel of AWS Compute | AWS Floor28
Amazon EC2 deepdive and a sprinkel of AWS Compute | AWS Floor28
Why rust?
Why rust?
A quick start on Zend Framework 2
A quick start on Zend Framework 2
Understanding Threads in operating system
Understanding Threads in operating system
Mais de SysPlay eLearning Academy for You
Linux Internals Part - 2
Linux Internals Part - 2
SysPlay eLearning Academy for You
Linux Internals Part - 1
Linux Internals Part - 1
SysPlay eLearning Academy for You
Cache Management
Cache Management
SysPlay eLearning Academy for You
Introduction to BeagleBone Black
Introduction to BeagleBone Black
SysPlay eLearning Academy for You
Introduction to BeagleBoard-xM
Introduction to BeagleBoard-xM
SysPlay eLearning Academy for You
Platform Drivers
Platform Drivers
SysPlay eLearning Academy for You
BeagleBone Black Bootloaders
BeagleBone Black Bootloaders
SysPlay eLearning Academy for You
BeagleBone Black Booting Process
BeagleBone Black Booting Process
SysPlay eLearning Academy for You
BeagleBoard-xM Bootloaders
BeagleBoard-xM Bootloaders
SysPlay eLearning Academy for You
BeagleBoard-xM Booting Process
BeagleBoard-xM Booting Process
SysPlay eLearning Academy for You
Serial Drivers
Serial Drivers
SysPlay eLearning Academy for You
SPI Drivers
SPI Drivers
SysPlay eLearning Academy for You
Linux System
Linux System
SysPlay eLearning Academy for You
Mais de SysPlay eLearning Academy for You
(13)
Linux Internals Part - 2
Linux Internals Part - 2
Linux Internals Part - 1
Linux Internals Part - 1
Cache Management
Cache Management
Introduction to BeagleBone Black
Introduction to BeagleBone Black
Introduction to BeagleBoard-xM
Introduction to BeagleBoard-xM
Platform Drivers
Platform Drivers
BeagleBone Black Bootloaders
BeagleBone Black Bootloaders
BeagleBone Black Booting Process
BeagleBone Black Booting Process
BeagleBoard-xM Bootloaders
BeagleBoard-xM Bootloaders
BeagleBoard-xM Booting Process
BeagleBoard-xM Booting Process
Serial Drivers
Serial Drivers
SPI Drivers
SPI Drivers
Linux System
Linux System
Último
How to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
naman860154
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
HampshireHUG
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
Radu Cotescu
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
BookNet Canada
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Safe Software
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
Delhi Call girls
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
Padma Pradeep
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
Memoori
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
2toLead Limited
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
Ridwan Fadjar
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
Neo4j
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
naman860154
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
shyamraj55
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
Pixlogix Infotech
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
Mark Billinghurst
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
Sinan KOZAK
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
Rafal Los
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Katpro Technologies
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
Softradix Technologies
Último
(20)
How to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
POSIX Threads
1.
© 2019 SysPlay
Workshops <workshop@sysplay.in> All Rights Reserved. POSIX Threads
2.
2© 2019 SysPlay
Workshops <workshop@sysplay.in> All Rights Reserved. What to Expect? W’s of Threads Single & Multi-threaded Processes Advantages of using threads Creating & Passing data to threads Thread Attributes Thread Cancellation Thread Specific data
3.
3© 2019 SysPlay
Workshops <workshop@sysplay.in> All Rights Reserved. W’s of Threads Need for the multitasking within the program Event driven programming Thread exists within a process Each thread may be executing a different part of the program at any given time Linux Kernel schedules them asynchronously
4.
4© 2019 SysPlay
Workshops <workshop@sysplay.in> All Rights Reserved. Multi-Process vs Multi-Threaded code data files regs stack ... Parent Process ...code data files regs stack ... Child Process Child Process code data files regs stack ... code data files regs stack ... Main Process regs stack ... Thread regs stack ... Thread
5.
5© 2019 SysPlay
Workshops <workshop@sysplay.in> All Rights Reserved. Threads vs Multiple Processes Threads Advantages Inexpensive Creation & Termination Inexpensive Context Switching Communication is Inherent & Far More As already sharing almost all resources No kernel interaction involved Threads Disadvantages Security because of inherent sharing
6.
6© 2019 SysPlay
Workshops <workshop@sysplay.in> All Rights Reserved. W's of pthread POSIX standard (IEEE Std 1003.1) thread APIs Defined as “pthread” library with a set of C language programming types C language function calls Prototyped in pthread.h header/include file Library may or may not be part of standard C library On Linux pthread library is not part of libc (libc.so, libc.a) Provided separately as: libpthread.so, libpthread*.a Compiling: gcc -o <out_file> <input_file> -lpthread
7.
7© 2019 SysPlay
Workshops <workshop@sysplay.in> All Rights Reserved. Thread Creation int pthread_create( pthread_t *thread_id, → Id of the thread created const pthread_attr_t *attr, → Thread interaction attributes void *(*start_routine)(void *), → Function to start execution void *arg → Argument to pass any data to thread ); → 0 on success; error code < 0 on error attr = NULL implies default thread attributes Thread exits on return by the *start_routine arg could be used to create different threads with same start function Function returns immediately scheduling both the threads asynchronously
8.
8© 2019 SysPlay
Workshops <workshop@sysplay.in> All Rights Reserved. Thread Termination Thread terminates Normally On start_routine termination By call to pthread_exit(void *exit_val); Abnormally On cancellation Return value / status is a void * Return value from start_routine Or, exit_val Or, PTHREAD_CANCELED
9.
9© 2019 SysPlay
Workshops <workshop@sysplay.in> All Rights Reserved. Waiting for my threads Parent could wait for its threads using int pthread_join(pthread_t tid, void **retval); tid → Id of thread to wait for *retval → Thread exit status, if retval != NULL Returns 0 on success error code < 0 on error Try out: thread_join.c, thread_return.c Observe: thread_unsafe.c
10.
10© 2019 SysPlay
Workshops <workshop@sysplay.in> All Rights Reserved. Not waiting for my threads What if the main thread doesn't join? A clean up of the thread is not guaranteed If main thread quits, process quits All threads in it cease to exist & are cleaned If main thread runs, and don't want to join Creating detached threads is the option By modifying the thread attributes Detached threads can't be joined And they clean up on their termination
11.
11© 2019 SysPlay
Workshops <workshop@sysplay.in> All Rights Reserved. Thread Attributes Provides a mechanism for fine-tuning the behaviour of individual threads Configurable by passing a valid “attr” to pthread_create Before that, pthread_attr_t object needs Initialization: int pthread_attr_init(pthread_attr_t *attr); Modification using various pthread_attr_set* functions And after that, pthread_attr_t object needs Destruction: int pthread_attr_destroy(pthread_attr_t *attr); Have a look @ thread_attr.c
12.
12© 2019 SysPlay
Workshops <workshop@sysplay.in> All Rights Reserved. Let's Detach Threads Attribute Modification Function int pthread_attr_setdetachstate( pthread_attr_t *attr, int detachstate ); detachstate PTHREAD_CREATE_DETACHED PTHREAD_CREATE_JOINABLE Try out: thread_detach.c
13.
13© 2019 SysPlay
Workshops <workshop@sysplay.in> All Rights Reserved. Some Thread Id Functions pthread_t pthread_self(); Returns the id of the current thread Can be used for thread specific variations / data int pthread_equal(pthread_t t1, pthread_t t2) Compares thread ids t1 & t2 Returns non-zero on equal, zero otherwise pthread_join(pthread_self(), NULL); ??? Returns EDEADLK
14.
14© 2019 SysPlay
Workshops <workshop@sysplay.in> All Rights Reserved. Thread Cancellation Terminating other thread int pthread_cancel(pthread_t tid); When & Where the cancelled thread exits? Depends on its cancel state & type attribute These attributes could be set using int pthread_setcancelstate(int state, int *oldstate); int pthread_setcanceltype(int type, int *oldtype); States PTHREAD_CANCEL_DISABLE, PTHREAD_CANCEL_ENABLE Type PTHREAD_CANCEL_DEFERRED, PTHREAD_CANCEL_ASYNCHRONOUS Try out: thread_cancel.c
15.
15© 2019 SysPlay
Workshops <workshop@sysplay.in> All Rights Reserved. Thread Cancellation ... Why Uncancellable? To enable critical section implementations What are Cancellable Points? Calls to certain functions specified by POSIX.1 standards. Refer “man 7 pthreads” for list Deferred Cancellation still doesn't solve Resource Leak during Cancellation Only solution: Cleanup Handler
16.
16© 2019 SysPlay
Workshops <workshop@sysplay.in> All Rights Reserved. Cleanup Handler Specifics Function which gets called when a thread exits either Due to cancellation Call to pthread_exit But not by return of the thread start function Should be called explicitly Typical tasks are cleanups of Thread-specific data keys Memory allocations Mutex waits
17.
17© 2019 SysPlay
Workshops <workshop@sysplay.in> All Rights Reserved. Cleanup Handler ... Registration int pthread_cleanup_push( void (*handler)(void *), → cleanup handler void *arg → argument to the cleanup handler ); Unregistration int pthread_cleanup_pop( int execute → also execute the handler, if non-zero ); These two calls must be balanced Experiment: thread_cancel_cleanup.c
18.
18© 2019 SysPlay
Workshops <workshop@sysplay.in> All Rights Reserved. Thread-Specific Data By default all data is shared (public) Good for inter thread communication But no privacy for a thread Though in some cases it may be desirable An Example Multiple threads to log in their respective files Individual thread needs to store & retrieve Its own log file pointer And so pthreads do support it
19.
19© 2019 SysPlay
Workshops <workshop@sysplay.in> All Rights Reserved. Thread-Specific Data ... Implemented using a thread specific data area In the same Process Address Space Variables in this area are duplicated for each thread But should be accessed with special atomic functions for setting and retrieving values APIs needed int pthread_key_create(pthread_key_t *k, void (*dest)(void *)); int pthread_key_delete(pthread_key_t key); int pthread_setspecific(pthread_key_t key, void *val); void *pthread_getspecific(pthread_key_t key); Let's see the thread_logging.c example
20.
20© 2019 SysPlay
Workshops <workshop@sysplay.in> All Rights Reserved. What all have we learnt? W’s of Threads Single & Multi-threaded Processes Advantages of using threads Creating & Passing data to threads Thread attributes Thread Cancellation Thread Specific data
21.
21© 2019 SysPlay
Workshops <workshop@sysplay.in> All Rights Reserved. Any Queries?