Fall 2006 Midterm Solutions |
int x = 0; // shared variable Lock(); If (x == 0) { Unlock(); Lock(); x++; } Unlock();Clearly there is a race condition that would cause the x++ to be executed multiple times on different threads.
(b) initialized data: location and content. uninitialized data: location
(c) stack segment: neither
// We declare a semaphore for every pairing of threads - a total of NUM_THREADS*NUM_THREADS semaphores. // These semaphores are used so a thread can notice when all other threads have entered the barrier. Semaphore waitFor[NUM_THREADS][NUM_THREADS] = 0; //[ownerThread][otherThread] initialized to 0 void Barrier(int threadID) { int i; for (i = 0; i < NUM_THREADS; i++) { // Tell all threads that "threadID" is at barrier V(waitFor[threadID][i]); // V() or UP() } for (i = 0; i < NUM_THREADS;i++) { // Wait for all other threads to hit barrier before continuing P(waitFor[i][threadID]); } }
5b) When you add a second CPU, the priority concept loses some significance since both jobs could run in parallel on those CPUs. However, the priority relation between Job A and Job B remains the same: namely, Job A priority stays higher than Job B's. Adding a second CPU would not really change the scenario much in priority terms: Job A is still doing plenty of I/O operations as before (priority boosted), and Job B can still easily fit in memory (priority lowered, since it's a compute-intensive job).
5c) Job A can now evict Job B's pages. This means that Job A has a chance to run longer (more pages in memory, fewer page faults), and its priority may lower a little. Job B will instead see its priority boosted: having its pages taken away by Job A, causes Job B to perform I/O operations to bring in its pages. Therefore, Job B's priority raises.
6.b) The advantages of static linking is it can avoid some of the problems of dynamic linking such as missing libraries or changes in libraries breaking a programs. It can also have a performance benefit since no runtime linking overheads is needed. Static linking can sometimes use less memory than dynamic linking since only the routines being used by the program are brought into memory.