Threads - FinaleAdministrativePart 1 - Thread conceptsPractice exam question: List 5 items/resources that are shared by threads of the same processList 5 items that are shared by threads of the same process?Thread PackagesMore example exam questionsUser-level ThreadUser Level ThreadsKernel-level ThreadsKernel-Level ThreadTrade-offs (review)?Hybrid Implementations (Solaris)Part 2 POSIX threadsWhat’s POSIX Got To Do With It?Pthreads--- POSIX ThreadsPthread Operations (review)Return Values (review)Creating a Thread (review)POSIX functions that are not required to be thread-safe.Creating a thread with pthreadRestrict KeywordPowerPoint PresentationThe Thread IDExiting and CancellationSlide 26ExitingCancel that thread!POSIX functions that are defined to be cancellation pointsThread AttributesSettable properties of thread attributesAttribute Initialization and DeletionExample: Create a detached threadThe thread stackThread Detach & JoinDetaching a ThreadHow to make a thread detachedHow a thread can detach itself“Wating” on a Thread: pthread_join()Pthread_joinThread schedulingCreate a thread that contends with other processesSummaryCS 241 Spring 2007System Programming 01/13/19 CS241 © 2006-2007 LA, RHC and YZ, All Rights Reserved1Threads - FinaleLawrence AngraveLecture 92AdministrativeThis week SMP2 Quiz for SMP1Compass self-assessment quizLecture quiz Wed or FridayBook sectionsR & R Chapter 12 (POSIX Threads) p. 409-4463Part 1 - Thread concepts4Practice exam question:List 5 items/resources that are shared by threads of the same process5List 5 items that are shared by threads of the same process?address space: code and global variables (“Code section”) (“Data section”)open files signals timers process ID A thread has its own program counter and stack, but shares a number of resources with its process and other threads of the process:6Thread PackagesKernel thread packages Implemented and supported at the kernel levelUser-level thread packagesImplemented at the user level7More example exam questions“Illustrate how the threads of a multi-threaded application using user-level threads map to kernel-level threads”“Describe 3 advantages & 3 disadvantages of User-level threads compared to kernel-level threads”8User-level Thread9User Level ThreadsUser-level threads without direct O/S supportThreads are invisible to the kernel Simpler kernel implementationHave lower context-switch overhead compared to K.L.TsCan only use one processor at a timeImplementation dependentCPU-bound threads can block other threads(or) Requires a special library of system calls to prevent blocking10Kernel-level ThreadsKernel can schedule threads in addition to processes. Multiple threads of a process can run simultaneously on multiple CPUs. Synchronization more efficient than for processes (but less than for user-level threads).Kernel-level threads can make blocking I/O calls without blocking other threads of same process11Kernel-Level Thread12Trade-offs (review)?Kernel thread packages Each thread can make blocking I/O callsCan run concurrently on multiple processorsThreads in User-level Fast context switchCustomized schedulingSo which one do we choose?13Hybrid Implementations (Solaris) Multiplexing user-level threads onto kernel- level threads14Part 2 POSIX threadsAttributesSchedulingDetachingCancelingrestrict How many ways can my program exit?15What’s POSIX Got To Do With It?Each OS had it’s own thread library and styleThat made writing multithreaded programs difficult because:you had to learn a new API with each new OSyou had to modify your code with each port to a new OSPOSIX (IEEE 1003.1c-1995) provided a standard known as PthreadsUnix International (UI) threads (Solaris threads) are available on Solaris (which also supports POSIX threads)16Pthreads--- POSIX ThreadsIt is a standard API Supported by most vendorsGeneral concepts applicable to other thread APIs (java threads, NT threads,etc).Low level functionsNo high level constructs17Pthread Operations (review)POSIXfunction descriptionpthread_cancelterminateanotherthreadpthread_createcreateathreadpthread_detachsetthreadtoreleaseresourcespthread_equaltesttwothreadIDsforequalitypthread_exitexitathreadwithoutex itingprocesspthread_killsendasignaltoathreadpthread_joinwaitforathreadpthread_selffindoutownthreadID18Return Values (review)Most POSIX functions return 0 on success and a nonzero error code on failure.They do not set errno but the value returned when an error occurs has the value that errno would have.19Creating a Thread (review)When a new thread is created it runs concurrently with the creating process.When creating a thread you indicate which function the thread should execute.20POSIX functions that are not required to be thread-safe.asctime fcvt getpwnam nl_langinfobasename ftw getpwuid ptsnamecatgets gcvt getservbyname putc_unlockedcrypt getc_unlocked getservbyport putchar_unlockedctime getchar_unlocked getservent putenvdbm_clearerr getdate getutxent pututxlinedbm_close getenv getutxid randdbm_delete getgrent getutxlinereaddirdbm_error getgrgid gmtime setenvdbm_fetch getgrnam hcreate setgrentdbm_firstkey gethostbyaddr hdestroy setkeydbm_nextkey gethostbyname hsearch setpwentdbm_open gethostent inet_ntoa setutxentdbm_store getlogin l64a strerrordirname getnetbyaddr lgammastrtokdlerror getnetbyname lgammaf ttynamedrand48 getnetent lgammal unsetenvecvt getopt localeconv wcstombsencrypt getprotobyname localtime wctombendgrent getprotobynumber lrand48endpwent getprotoent mrand48endutxent getpwent nftw21Creating a thread with pthreadA thread is created withint pthread_create( pthread_t *restrict thread, const pthread_attr_t *restrict attr, void *(*start_routine)(void *), void *restrict arg); The creating process (or thread) must provide a location for storage of the thread id. The third parameter is just the name of the function for the thread to run. The last parameter is a pointer to the arguments.22Restrict KeywordOne of the new features in the recently approved C standard C99 This qualifier can be applied to a data pointer to indicate thatDuring the scope of that pointer declaration, all data accessed through it will be accessed only through that pointer but not through any other pointer. It enables the compiler to perform certain optimizations based on the premise that a given object cannot be changed
View Full Document