Weekly Notes for Week 10

Tutorial Session Week 10

Prepare for the Tutorial Session in Week 10 the following exercises from Chapter 4.

  1. Provide three programming examples in which multithreading provides better performance than a single-threaded solution.

  2. Using Amdahl’s Law, calculate the speedup gain of an application that has a 60 percent parallel component for
    • two processing cores and
    • four processing cores.
  3. Does the multithreaded web server described in Section 4.1 exhibit task or data parallelism?

  4. What are two differences between user-level threads and kernel-level threads? Under what circumstances is one type better than the other?

  5. Describe the actions taken by a kernel to context-switch between kernellevel threads.

  6. What resources are used when a thread is created? How do they differ from those used when a process is created?

  7. Assume that an operating system maps user-level threads to the kernel using the many-to-many model and that the mapping is done through LWPs. Furthermore, the system allows developers to create real-time threads for use in real-time systems. Is it necessary to bind a real-time thread to an LWP? Explain.

  8. Under what circumstances does a multithreaded solution using multiple kernel threads provide better performance than a single-threaded solution on a single-processor system?

  9. Which of the following components of program state are shared across threads in a multithreaded process?
    • Register values
    • Heap memory
    • Global variables
    • Stack memory
  10. Using Amdahl’s Law, calculate the speedup gain for the following applications:
    • 40 percent parallel with (a) eight processing cores and (b) sixteen processing cores
    • 67 percent parallel with (a) two processing cores and (b) four processing cores
    • 90 percent parallel with (a) four processing cores and (b) eight processing cores
  11. Determine if the following problems exhibit task or data parallelism:
    • Using a separate thread to generate a thumbnail for each photo in a collection
    • Transposing a matrix in parallel
    • A networked application where one thread reads from the network
    • The fork-join array summation application described in Section 4.5.2
    • The Grand Central Dispatch system
  12. Consider the following code segment:
        pid t pid;
        pid = fork();
        if (pid == 0) { /* child process */
            fork();
            thread create( . . .);
        }
        fork();
    
    • How many unique processes are created?
    • How many unique threads are created?
  13. The following program uses the Pthreads API. What would be the output from the program at LINE C and LINE P?
    #include <pthread.h>
    #include <stdio.h>
    int value = 0;
    void *runner(void *param); /* the thread */
    int main(int argc, char *argv[])
    {
        pid t pid;
        pthread t tid;
        pthread attr t attr;
    
        pid = fork();
    
        if (pid == 0) { /* child process */
            pthread attr init(&attr);
            pthread create(&tid,&attr,runner,NULL);
            pthread join(tid,NULL);
            printf("CHILD: value = %d",value); /* LINE C */
        } else if (pid > 0) { /* parent process */
            wait(NULL);
            printf("PARENT: value = %d",value); /* LINE P */
        }
    }
    
    void *runner(void *param) {
        value = 5;
        pthread exit(0);
    }
    
  14. Consider a multicore system and a multithreaded program written using the many-to-many threading model. Let the number of user-level threads in the program be greater than the number of processing cores in the system. Discuss the performance implications of the following scenarios.
    • The number of kernel threads allocated to the program is less than the number of processing cores.
    • The number of kernel threads allocated to the program is equal to the number of processing cores.
    • The number of kernel threads allocated to the program is greater than the number of processing cores but less than the number of user-level threads.