1Copyright ©: Nahrstedt, Angrave, Abdelzaher 1ThreadsCopyright ©: Nahrstedt, Angrave, Abdelzaher2Announcement Quiz1 graded Reminder: SMP2 due Wednesday Reminder: Please keep up with reading material from textbook2Copyright ©: Nahrstedt, Angrave, Abdelzaher3Question 1 Q1) Which of the following code snippets is/are wrong?Case 1 Case 2 Case 3int *p; char a[2]; int b[10];*p=10; strcpy (a, “Hi”); *b=11;a) Case 1.b) Case 2c) Case 3d) Both Case 1 and Case 2e) Both Case 1 and Case 3Wrong:Dereferencing uninitializedpointerWrong:No room for NULL characterHalf CreditCopyright ©: Nahrstedt, Angrave, Abdelzaher4Question 2Q2) Given what you know about memory caches, which of the following content is best to removeto make room for new content that arrives to a full cache? a) Remove randomly selected contentb) Remove the oldest content (i.e., content that resided in the cache for the longest amount of time)c) Remove the newest content (i.e., content that resided in the cache for the shortest amount of time) d) Remove the most recently requested contente) Remove least recently requested contentThink what Blockbuster might dowhen it’s time to rotate the shelvesRandom?? Clearly a bad idea!Bad! Will remove the classics and other“oldies but goodies”That’s crazy! Will remove the new releases!Best thing to do! If it’s been ages since anyonerented the movie, it’s time to take it off the shelves.3Copyright ©: Nahrstedt, Angrave, Abdelzaher5Question 3Q3) Which of the following lines is/are wrong, if any: Line 1: char s[10];Line 2: strcpy (s, “Hi”);Line 3: strcat (“John: ”, s);Line 4: strcat (s, “, there!!”);a) Line 2b) Line 3c) Line 4d) Lines 3 and 4e) All is correct.Wrong. Concatenating to a constant.Wrong.“Hi, there!!”exceeds array size.CorrectI will consider this correct if you did not realize that the four lines are the same program.Half creditCopyright ©: Nahrstedt, Angrave, Abdelzaher6Question 4Q3) Which of the following lines is/are wrong, if any: Line 1: int **p, *q;Line 2: p=&q;Line 3: q = (int*) malloc (10*sizeof(int));Line 4: **p = 10;a) Line 1b) Line 2c) Line 3d) Lines 4e) All is correct.Writes 10 in the location pointed to by thepointer pointed to by p. Since p points to qand q points to a location that has been properly allocated via malloc, all is well.4Copyright ©: Nahrstedt, Angrave, Abdelzaher7Question 5Q1: What is wrong with the code snippet below:int* p, x, y=10;p= &y;while (y>0) {x=y--;printf (“x= %d”, x);y=x; }*p = x;a) Pointer treated as an integer b) Infinite loopc) Dereferencing an invalid pointerd) y is a constant but treated as a variablee) Both (a) and (d)Assigns y to x before decrementing y. Restores the old (undecremented) y. Hence the loop never changes y.Copyright ©: Nahrstedt, Angrave, Abdelzaher8Review: Why Threads? Processes do not share resources very well Why? Process context switching cost is very high Why? Threads: light-weighted processes5Copyright ©: Nahrstedt, Angrave, Abdelzaher9Threads: Lightweight Processes (a) Three processes each with one thread (b) One process with three threadsEnvironment (resource)executionCopyright ©: Nahrstedt, Angrave, Abdelzaher10Threads vs. Processes Each thread execute separately Threads in the same process share resources No protection among threads!!6Copyright ©: Nahrstedt, Angrave, Abdelzaher11What’s POSIX Got To Do With It? Each OS had it’s own thread library and style That made writing multithreaded programs difficult because: you had to learn a new API with each new OS you had to modify your codewith each port to a new OS POSIX (IEEE 1003.1c-1995) provided a standard known as PthreadsCopyright ©: Nahrstedt, Angrave, Abdelzaher12Pthread Operationsfind out own thread IDpthread_selfwait for a threadpthread_joinsend a signal to a threadpthread_killexit a thread without exiting processpthread_exittest two thread IDs for equalitypthread_equalset thread to release resourcespthread_detachcreate a threadpthread_createterminate another threadpthread_canceldescriptionPOSIX function7Copyright ©: Nahrstedt, Angrave, Abdelzaher13Example Program#include <phtread.h>#include <thread.h>#include <stdio.h>void *threadex(void *); int main() {pthread_t tid; /* stores the new thread ID */pthread_create(&tid, NULL, threadex, NULL); /*create a new thread*/pthread_join(tid, NULL); /*main thread waits for new thread to terminate */return 0; /* main thread exits */} void *threadex(void *arg) /*thread routine*/ {int i;for (i=0; i<5; i++)fprintf(stderr, `Hello, world! \n''); return NULL; } Copyright ©: Nahrstedt, Angrave, Abdelzaher14Thread Creation vs. Process Creationhttp://www.llnl.gov/computing/tutorials/pthreads8Copyright ©: Nahrstedt, Angrave, Abdelzaher15Thread Usage: Word Processor What if it is single-threaded?Copyright ©: Nahrstedt, Angrave, Abdelzaher16Thread Usage: Web Server9Copyright ©: Nahrstedt, Angrave, Abdelzaher17Web Server Rough outline of code for previous slide (a) Dispatcher thread (b) Worker threadCopyright ©: Nahrstedt, Angrave, Abdelzaher18Things Suitable for threading Block for potentially long waits Use many CPU cycles Must respond to asynchronous events Are of lesser or greater importance than other tasks Are able to be performed in parallel with other tasks10Copyright ©: Nahrstedt, Angrave, Abdelzaher19Common Multi-thread Software ArchitecturesManager/worker a single thread, the managerassigns work to other threads, the workers. Typically, the manager handles all input and parcels out work to the other tasks Pipeline: a task is broken into a series of sub-operations, each of which is handled by a different thread. An automobile assembly line best describes this model Peer similar to the manager/worker model, but after the main thread creates other threads, it participates in the work.Copyright ©: Nahrstedt, Angrave, Abdelzaher20Questions What are the similarities between processes and threads? What are the differences between processes and threads?11Copyright ©: Nahrstedt, Angrave, Abdelzaher21Thread Packages Kernel thread packages Implemented and supported at kernel level User-level thread packages Implemented at user levelCopyright ©: Nahrstedt, Angrave, Abdelzaher22Implementing Threads in User Space (Old Linux) A user-level threads package12Copyright ©: Nahrstedt, Angrave, Abdelzaher23User-level ThreadsCopyright ©:
View Full Document