Sample Midterm Solutions
Page 1 of 5
CS 8803, Spring 2015.
EDIT 01-11-15 - Added additional answer for problem 1. Process Creation
1. Process Creation
Problem
How is a new process created?
Solution
-
Via fork
-
If we want to create a process that is an exact replica of the calling process
-
Via fork followed by exec
-
If we want to create a process that is not an exact replica of the calling process
-
Via fork or fork followed by exec
-
Either of the above two options
Relevant Sections
2. Multi-Threading and 1 CPU
Problem
Is there a benefit of multithreading on 1 CPU?
Solution
Yes. The main reason is to hide the latency associated with code that blocks processing (such as a disk I/O request).
Relevant Sections
3. Critical Section
Problem
In the (pseudo) code segments for the producer code and consumer code, mark and explain all the lines where there are errors.
Solution
-
Producer code
-
Line 3: uses “if” instead of “while”
-
Line 4: condition_wait doesn’t specify a mutex
-
Line 7: since only 1 item is added, no need to broadcast, should signal instead
-
Line 8: missing the mutex_unlock
-
Consumer code
-
Line 3: uses “if” instead of “while”
-
Line 4: condition_wait doesn’t specify a mutex
-
Line 7: condition_signal signals the wrong variable, should be signaling not_full
-
Line 8: missing the mutex_unlock operation
Relevant Sections
-
P2L2: Threads and Concurrency (the entire lesson is useful for this problem)
4. Calendar Critical Section
Problem
A shared calendar supports three types of operations for reservations:
-
read
-
cancel
-
enter
Requests for cancellations should have priority above reads, who in turn have priority over new updates. In pseudocode, write the critical section enter/exit code for the **read** operation.
Solution
We have not posted a solution for this problem. Instead, since there are many possible solutions, we encourage you to post and share your solutions on the class forum.
Relevant Sections
-
P2L2: Threads and Concurrency (the entire lesson is useful for this problem)
5. Signals
Problem
If the kernel cannot see user-level signal masks, then how is a signal delivered to a user-level thread (where the signal can be handled)?
Solution
Recall that all signals are intercepted by a user-level threading library handler, and the user-level threading library installs a handler. This handler determines which user-level thread, if any, the signal be delivered to, and then it takes the appropriate steps to deliver the signal.
Note: If all user-level threads have the signal mask disabled and the kernel-level signal mask is updated, then and the signal remains pending to the process.
Relevant Sections
-
All morsels including and after the Interrupts and Signals Intro
6. Solaris Papers
Problem
The implementation of Solaris threads described in the paper "Beyond Multiprocessing: Multithreading the Sun OS Kernel", describes four key data structures used by the OS to support threads.
For each of these data structures, list at least two elements they must contain:
-
Process
-
LWP
-
Kernel-threads
-
CPU
Solution
The video Kernel Level Structures in Solaris 2.0 in Relevant Sections contains many possible solutions.
Relevant Sections
7. Pipeline Model
Problem
An image web server has three stages with average execution times as follows:
-
Stage 1: read and parse request (10ms)
-
Stage 2: read and process image (30ms)
-
Stage 3: send image (20ms)
You have been asked to build a multi-threaded implementation of this server using the pipeline model. Using a pipeline model, answer the following questions:
-
How many threads will you allocate to each pipeline stage?
-
What is the expected execution time for 100 requests (in sec)?
-
What is the average throughput of this system (in req/sec)? Assume there are infinite processing resources (CPU's, memory, etc.).
Solution
-
Threads should be allocated as follows:
-
Stage 1 should have 1 thread
-
This 1 thread will parse a new request every 10ms
-
Stage 2 should have 3 threads
-
The requests parsed by Stage 1 get passed to the threads in Stage 2. Each thread picks up a request and needs 30ms to process the image. Hence, we need 3 threads in order to pick up a new request as soon as Stage 1 passes it.
-
Stage 3 should have 2 threads.
-
This is because Stage 2 will process an image and pass a request every 10ms (once the pipeline is filled). In this way, each Stage 3 thread will pick up a request and send an image in 20ms. Once the pipeline is filled, Stage 3 will be able to pick up a request and send the image every 10ms.
-
The first request will take 60ms. The last stage will continue delivering the remaining 99 requests at 10ms intervals. So, the total is 60 + (99 * 10ms) = 1050ms = 1.05s
-
100 req / 1.05 sec = 95.2 req/s
Relevant Sections
8. Performance Observations
Problem
Here is a graph from the paper "Flash: An Efficient and Portable Web Server", that compares the performance of Flash with other web servers.
For data sets where the data set size is less than 100MB why does...
-
Flash perform worse than SPED?
-
Flash perform better than MP?
Solution
-
In both cases the dataset will likely fit in cache, but Flash incurs an overhead on each request because Flash must first check for cache residency. In the SPED model, this check is not performed.
-
When data is present in the cache, there is no need for slow disk I/O operations. Adding threads or processes just adds context switching overheads, but there is no benefit of “hiding I/O latency”.
Relevant Sections
P2L5: Thread Performance Considerations (the entire lesson is useful for this problem)
My wrong answers start from here.....
1. Process Creation
How is a new process created? Select all that apply.
- (A) Via fork
- (B) Via exec
- (C) Via fork followed by exec
- (D) Via exec followed by fork
- (E) Via exec or fork followed by exec
- (F) Via fork or fork followed by exec
- (G) None of the above
- (H) All of the above
Solution: H
2. Multi-Threading and 1 CPU
Is there a benefit of multithreading on 1 CPU?
- Yes
- No
Give 1 reason to support your answer.
Yes.
Multithread performs better when there's an I/O bottleneck. (i.e. I/O time > 2*thread_create_time)
Multithread can parallelize task operations, and thus provide real time feedback for multiple tasks.
3. Critical Section
In the (pseudo) code segments for the producer code and consumer code, mark and explain all the lines where there are errors.
Global Section
int in, out, buffer[BUFFERSIZE];
mutex_t m;
cond_var_t not_empty, not_full;
Producer Code
1. while (more_to_produce) {
2. mutex_lock(&m);
3. if (out == (in + 1) % BUFFERSIZE)) // buffer full
4. condition_wait(¬_full);
5. add_item(buffer[in]); // add item
6. in = (in + 1) % BUFFERSIZE
7. cond_broadcast(¬_empty);
8.
9. } // end producer code
Consumer Code
1. while (more_to_consume) {
2. mutex_lock(&m);
3. if (out == in) // buffer empty
4. condition_wait(¬_empty);
5. remove_item(out);
6. out = (out + 1) % BUFFERSIZE;
7. condition_signal(¬_empty);
8.
9. } // end consumer code
---
4. Calendar Critical Section
A shared calendar supports three types of operations for reservations:
- read
- cancel
- enter
Requests for cancellations should have priority above reads, who in turn have priority over new updates.
In pseudocode, write the critical section enter/exit code for the read operation.
pthread_mutex_lock(&m);
++num_reader;
while(num_cancel>0){
pthread_cond_wait(&cancellation_finished,&m);
}
read();
pthread_mutex_unlock(&m);
if (num_reader == 0){
pthread_cond_broadcastl(&reading_finished);
}
5. Signals
If the kernel cannot see user-level signal masks, then how is a signal delivered to a user-level thread (where the signal can be handled)?
Signal will be passed from kernel to user-level thread library, who stores the masks for each ULT.
6. Solaris Papers
The implementation of Solaris threads described in the paper "Beyond Multiprocessing: Multithreading the Sun OS Kernel", describes four key data structures used by the OS to support threads.
For each of these data structures, list at least two elements they must contain:
- Process
- LWP
- Kernel-threads
- CPU
Process: signals, virtual address mapping, sys call args
LWP: signal mask, sys call args
KLT: stack register
CPU: ptr to current and other threads
https://www.cnblogs.com/ecoflex/p/10898900.html
7. Pipeline Model
An image web server has three stages with average execution times as follows:
- Stage 1: read and parse request (10ms)
- Stage 2: read and process image (30ms)
- Stage 3: send image (20ms)
You have been asked to build a multi-threaded implementation of this server using the pipeline model. Using a pipeline model, answer the following questions:
- How many threads will you allocate to each pipeline stage?
- What is the expected execution time for 100 requests (in sec)?
- What is the average throughput of the system in Question 2 (in req/sec)? Assume there are infinite processing resources (CPU's, memory, etc.).
1, 1:3:2
2, (10+20+30 + 99*(30))/1000 = 3.03s
3, average throughput ≈ 33 req/sec
8. Performance Observations
Here is a graph from the paper "Flash: An Efficient and Portable Web Server", that compares the performance of Flash with other web servers.
For data sets where the data set size is less than 100 MB why does...
- Flash perform worse than SPED?
- Flash perform better than MP?
1, Flash has "Helpers" facilating the I/O at large data size, but Helpers occupy the system resources. Below 100MB data size, the drawback overwhelms the advantage.
2, Flash is more memory efficient and has less context switch.