FinalExamSolutionCS3214F11

FinalExamSolutionCS3214F11 - CS 3214 Fall 2011 Final Exam...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 3214 Fall 2011 Final Exam Solutions 1/11 CS 3214 Final Exam Solutions 58 students took the midterm. After assigning 5 points for question 1 (d), the total obtainable number of points became 105. See the table and chart below for point distribution and histogram. Final exams can be viewed in my office; send me email if you want to see yours. 1 2 3 4 5 Total Median 18.5 12 8 11 9 61.5 Average 17.2 12.2 8.5 10.7 9.3 57.9 StDev 6.2 5.3 5.2 5.0 2.3 17.3 Min 3 2 0 0 3 24 Max 29 20 18 20 13 91 Total 29 20 20 20 16 105 Solutions are shown in this style. Grading Comments in this style. 0 2 4 6 8 10 12 14 16 18 21 30 31 40 41 50 51 60 61 70 71 80 81 90 91 105 CS3214 Final Exam Fall 2011 n=58, med= 61.5
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS 3214 Fall 2011 Final Exam Solutions 2/11 1. Multithreading (29 pts) a) (12 pts) Semaphores . Semaphores can be used to express scheduling constraints between activities performed by different threads. Consider the diamond shown on the right, denoting the dependencies between activities A, B, C, and D, which are executed by 4 different threads. Complete the program below to ensure these constraints! // declare any semaphores you need here; // be sure to show how to initialize them // initial value of all semaphores is 0, // for proper code see main function; shorthand is accepted sem_t a_done, b_done, c_done; static void * thread_A(void *_) { printf(“A\n”); sem_post(&a_done); sem_post(&a_done); } static void * thread_B(void *_) { sem_wait(&a_done); printf(“B\n”); sem_post(&b_done); } static void * thread_C(void *_) { sem_wait(&a_done); printf(“C\n”); sem_post(&c_done); } static void * thread_D(void *_) { sem_wait(&b_done); sem_wait(&c_done); printf(“D\n”); } int main() { sem_init(&a_done, 0, 0); sem_init(&b_done, 0, 0); sem_init(&c_done, 0, 0); pthread_t t[N]; pthread_create(t+0, NULL, thread_D, NULL); pthread_create(t+1, NULL, thread_C, NULL); pthread_create(t+2, NULL, thread_B, NULL); pthread_create(t+3, NULL, thread_A, NULL); pthread_exit(0); } A C B D
Background image of page 2
CS 3214 Fall 2011 Final Exam Solutions 3/11 b) (6 pts) Condition Variables. The first edition 1 of Thomas/Hunt’s book “Programming Ruby – The Pragmatic Programmer’s Guide” (the so-called “pickaxe” book) contained the following example of how to use condition variables in Ruby: require 'thread' mutex = Mutex.new cv = ConditionVariable.new a = Thread.new { mutex.synchronize { puts "A: I have critical section, but will wait for cv" cv.wait(mutex) puts "A: I have critical section again! I rule!" } } puts "(Later, back at the ranch. ..)" b = Thread.new { mutex.synchronize { puts "B: Now I am critical, but am done with cv" cv.signal puts "B: I am still critical, finishing up" } } a.join b.join The accompanying description read: “A condition variable is simply a semaphore that is associated with a resource and is used within the protection of a particular mutex. When you need a resource that's unavailable, you wait on a condition variable. That action releases
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/31/2011 for the course CS 3214 taught by Professor Staff during the Fall '11 term at Virginia Tech.

Page1 / 11

FinalExamSolutionCS3214F11 - CS 3214 Fall 2011 Final Exam...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online