1. 1) NTHREADS lines of "Hello from thread" - All threads are executed before main thread exit. 2) 0 line of "Hello from thread" - All are not executed (only created but not scheduled) before main thread exit. 3) 0 ~ NTHREAD lines of "Hello from thread" - All thread are created but only some of them are executed. 2. Any reasonable results around (10 ~ 200) ms with appropriate source code. 3. Your answer can be varied based on your design. a) Add a special pointer (Local Variable Pointer, LVP) in TCB to point array (1024) of pointers like a stack pointer. b) Add a step to allocate memory for array of pointers (will be initialized to NULL) and set its address allocated to LVP. c) Using LVP, finding the NULL pointer from 1024 entries and set it to point a new thread-local variable allocated. d) Based on the index of thread-local variable, running thread can access a particular thread-local variable by incrementing address stored in LVP. 4. Safety Proof. Assume for the sake of contradiction that the algorithm is not safe - both A and B buy milk. Consider the state of the two variables (noteB, mile) when thread A is at the second line - while (noteB == 1), at the precise moment when the atomic load of note B from shared memory to A' register occurs. There are three cases to consider: Case 1 : (noteB = 1, milk = any value). We know that thread B set noteB = 1 at this moment but don't know whether thread B test noteA == 0 or not. B will buy a milk while thread A waiting for it if thread B already check noteA = 0 before thread A set noteA = 1. B will not buy a milk but A will if thread A set noteA = 1 before thread B test noteA == 0. So either A or B will buy a milk. This contradicts our assumption that both A and B buy milk. Case 2 : (noteB = 0, milk > 0). Same reason mentioned in text book (p.192) Case 3 : (noteB = 0, milk = 0). Same reason mentioned in text book. (p.192) Since every case contradicts the assumption, the algorithm is safe. 5. a) Any reasonable definition and explanation of state and synchronization variables in your implementation. b) Any reasonable codes with lock, condition variable. (wait for condition variable should be in a while loop) c) Any reasonable codes with lock, condition variable.