Realizing Concurrency with Thread Model

Realizing Concurrency with Thread Model
Slide Note
Embed
Share

Threads provide powerful solutions for handling multiple requests simultaneously in applications like multimedia, databases, and web applications. This content covers the thread model, POSIX threads, creating and using threads efficiently, and designing multi-threaded applications.

  • Concurrency
  • Thread Model
  • POSIX Threads
  • Multi-threaded Applications

Uploaded on Mar 03, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Realizing Concurrency using the thread model 1 B. RAMAMURTHY 3/3/2025

  2. Introduction 2 A thread refers to a thread of control flow: an independent sequence of execution of program code. Threads are powerful. As with most powerful tools, if they are not used appropriately thread programming may be inefficient. Thread programming has become viable solution for many problems with the advent of multiprocessors and client- server model of computing. Typically these problems are expected to handle many requests simultaneously. Example: multi-media, database applications, web applications, parallel processing requests. 3/3/2025

  3. Topics to be Covered 3 Objectives What are Threads? Thread implementation models POSIX threads Creating threads Using threads Summary 3/3/2025

  4. Objectives 4 To understand the thread model for realizing concurrency To study POSIX standard and library for threads called pthreads. To study thread control primitives for creation, termination, join, synchronization, concurrency, and scheduling. To learn to design multi-threaded applications. 3/3/2025

  5. The Thread Model 5 (a) Three processes each with one thread (b) One process with three threads 3/3/2025

  6. Per process vs per thread items 6 Items shared by all threads in a process Items private to each thread 3/3/2025

  7. Implementing Threads in User Space 7 A user-level threads package 3/3/2025

  8. Implementing Threads in the Kernel 8 A threads package managed by the kernel 3/3/2025

  9. Hybrid Implementations 9 Multiplexing user-level threads onto kernel- level threads 3/3/2025

  10. Scheduler Activations 10 Goal mimic functionality of kernel threads gain performance of user space threads Avoids unnecessary user/kernel transitions Kernel assigns virtual processors to each process lets runtime system allocate threads to processors Problem: Fundamental reliance on kernel (lower layer) calling procedures in user space (higher layer) 3/3/2025

  11. Pop-Up Threads 11 Creation of a new thread when message arrives (a) before message arrives (b) after message arrives Thread pools 3/3/2025

  12. Thread Scheduling (1) 12 B1, B2, B3 Possible scheduling of user-level threads 50-msec process quantum threads run 5 msec/CPU burst 3/3/2025

  13. Thread Scheduling (2) 13 B1, B2, B3 Possible scheduling of kernel-level threads 50-msec process quantum threads run 5 msec/CPU burst 3/3/2025

  14. Thread as a unit of work 14 A thread is a unit of work to a CPU. It is strand of control flow. A traditional UNIX process has a single thread that has sole possession of the process s memory and resources. Threads within a process are scheduled and execute independently. Many threads may share the same address space. Each thread has its own private attributes: stack, program counter and register context. 3/3/2025

  15. Pthread Library 15 Many thread models emerged: Apple threads, Win-32 threads 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. Simply a collection of C functions. One of the undergraduates from UB implemented a object-oriented version of the pthreads called Zthreads: /projects/bina/zthread-1.5.1 3/3/2025

  16. Posix Library Implementation in F. Mueller s Paper 16 Language Application Language Interface C Language Application Posix thread library Unix libraries User Level Kernel Level Unix Kernel 3/3/2025

  17. Creating threads 17 Always include pthread library: #include <pthread.h> int pthread_create (pthread_t *tp, const pthread_attr_t * attr, void *(* start_routine)(void *), void *arg); This creates a new thread of control that calls the function start_routine. It returns a zero if the creation is successful, and thread id in tp (first parameter). attr is to modify the attributes of the new thread. If it is NULL default attributes are used. The arg is passing arguments to the thread function. 3/3/2025

  18. Using threads 18 1. Declare a variable of type pthread_t 2. Define a function to be executed by the thread. 3. Create the thread using pthread_create Make sure creation is successful by checking the return value. 4. Pass any arguments need through arg (packing and unpacking arg list necessary.) 5. #include <pthread.h> at the top of your header. 6. Compile: g++ -o executable file.cc -lpthread 3/3/2025

  19. Threads local data 19 Variables declared within a thread (function) are called local data. Local (automatic) data associated with a thread are allocated on the stack. So these may be deallocated when a thread returns. So don t plan on using locally declared variables for returning arguments. Plan to pass the arguments thru argument list passed from the caller or initiator of the thread. 3/3/2025

  20. Thread termination (destruction) 20 Implicit : Simply returning from the function executed by the thread terminates the thread. In this case thread s completion status is set to the return value. Explicit : Use thread_exit. Prototype: void thread_exit(void *status); The single pointer value in status is available to the threads waiting for this thread. 3/3/2025

  21. Waiting for thread exit 21 int pthread_join (pthread_t tid, void * *statusp); A call to this function makes a thread wait for another thread whose thread id is specified by tid in the above prototype. When the thread specified by tid exits its completion status is stored and returned in statusp. 3/3/2025

  22. Summary 22 We looked at Implementation of threads. thread-based concurrency. Pthread programming We will look at a pthread programming demo Study the details given in thread library link. See http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html Also see: https://computing.llnl.gov/tutorials/pthreads/ 3/3/2025

  23. Demos 23 There are four demos for illustrating the basic programming using pthreads to provide parallelism, especially in a multi-core environment. 6/16/2017 CSE 651 C, B. Ramamurthy

  24. Pthreads Programs 24 Creating and activating multiple pthreads : thread1.c Passing parameters to created threads: thread2.c Assigning work loads for the threads and exit and join : thread3.c Setting thread specific data (in this case stack size): thread4.c 6/16/2017 CSE 651 C, B. Ramamurthy

More Related Content