DOC PREVIEW
UB CSE 421 - Realizing Concurrency using the thread model

This preview shows page 1-2-21-22 out of 22 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Realizing Concurrency using the thread modelIntroductionTopics to be CoveredObjectivesThe Thread ModelPer process vs per thread itemsImplementing Threads in User SpaceImplementing Threads in the KernelHybrid ImplementationsScheduler ActivationsPop-Up ThreadsThread Scheduling (1)Thread Scheduling (2)Thread as a unit of workPthread LibraryPosix Library Implementation in F. Mueller’s PaperCreating threadsUsing threadsThread’s local dataThread termination (destruction)Waiting for thread exitSummary01/14/191Realizing Concurrency using the thread model B. Ramamurthy01/14/192Introduction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.01/14/193Topics to be CoveredObjectivesWhat are Threads?Thread implementation modelsPOSIX threadsCreating threadsUsing threadsSummary01/14/194ObjectivesTo understand the thread model for realizing concurrencyTo study POSIX standard for threads called Pthreads.To study thread control primitives for creation, termination, join, synchronization, concurrency, and scheduling.To learn to design multi-threaded applications.01/14/195The Thread Model(a) Three processes each with one thread(b) One process with three threads01/14/196Per process vs per thread itemsItems shared by all threads in a processItems private to each thread01/14/197Implementing Threads in User SpaceA user-level threads package01/14/198Implementing Threads in the KernelA threads package managed by the kernel01/14/199Hybrid Implementations Multiplexing user-level threads onto kernel- level threads01/14/1910Scheduler Activations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)01/14/1911Pop-Up ThreadsCreation of a new thread when message arrives(a) before message arrives(b) after message arrivesThread pools01/14/1912Thread Scheduling (1)Possible scheduling of user-level threads50-msec process quantumthreads run 5 msec/CPU burstB1, B2, B301/14/1913Thread Scheduling (2)Possible scheduling of kernel-level threads50-msec process quantumthreads run 5 msec/CPU burstB1, B2, B301/14/1914Thread as a unit of work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.01/14/1915Pthread LibraryMany thread models emerged: Solaris 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.101/14/1916Posix Library Implementationin F. Mueller’s PaperLanguage ApplicationLanguage Interface C Language ApplicationPosix thread libraryUnix KernelUnix librariesUser LevelKernel Level01/14/1917Creating threads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.01/14/1918Using threads1. Declare a variable of type pthread_t2. Define a function to be executed by the thread.3. Create the thread using pthread_createMake 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 -lpthread01/14/1919 Thread’s local data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.01/14/1920Thread termination (destruction)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.01/14/1921Waiting for thread exit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.01/14/1922Summary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


View Full Document

UB CSE 421 - Realizing Concurrency using the thread model

Documents in this Course
Security

Security

28 pages

Threads

Threads

24 pages

Security

Security

20 pages

Security

Security

52 pages

Security

Security

20 pages

Load more
Download Realizing Concurrency using the thread model
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Realizing Concurrency using the thread model and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Realizing Concurrency using the thread model 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?