Weekly Note 2 - Week 7

08 February 2019

Lecture - Tuesday, February 12th.

16-19 in U82

We will cover chapter 3: Processes and Chapter 4 on Threads & Concurrency. These two chapters will be the primary material for exam topic 2: "Processes, Threads and Concurrency".

We will have a small review of the previous lecture, so this time it will be chapter 1 and 2, and we might sneak in a Kahoot.

Please install the Kahoot app on your phone, if you wish to participate ;)

Tutorial session

Thursday February 14th. 8-10 in U31 or Friday February 15th. 12-14 in U155

Preparation:

Make a list of 10-15 keywords for a 10 min. presentation with the topic: "Processes, Threads and Concurrency"

Prepare at home to discuss:

  1. Describe the differences among short-term, medium-term, and long-term scheduling.

  2. Describe the actions taken by a kernel to context-switch between processes.

  3. Explain the role of the init process on UNIX and Linux systems in regards to process termination.

  4. Consider the RPC mechanism. Describe the undesirable consequences that could arise from not enforcing either the "at most once" or "exactly once" semantic. Describe possible uses for a mechanism that has neither of these guarantees.

  5. Using the program shown in Figure below, explain what the output will be at lines X and Y.

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#define SIZE 5
int nums[SIZE] = { 0,1,2,3,4 } ;

int main() {
    int i;
    pid t pid;
    pid = fork();
    if (pid == 0) {
        for (i = 0; i < SIZE; i++) {
            nums[i] *= -i;
            printf("CHILD: %d ",nums[i]); /* LINE X */
        }
    } else if (pid > 0) {
        wait(NULL);
        for (i = 0; i < SIZE; i++) {
            printf("PARENT: %d ",nums[i]); /* LINE Y */
        }
    }
    return 0;
}
  1. Under what circumstances does a multithreaded solution using multiple kernel threads provide better performance than a single-threaded solution on a single-processor system?

  2. Which of the following components of program state are shared across threads in a multithreaded process?

    • Register values

    • Heap memory

    • Global variables

    • Stack memory

  3. Can a multithreaded solution using multiple user-level threads achieve better performance on a multiprocessor system than on a single processor system? Explain.

  4. Is it possible to have concurrency but not parallelism? Explain.

  5. A system with two dual-core processors has four processors available for scheduling. A CPU-intensive application is running on this system. All input is performed at program start-up, when a single file must be opened. Similarly, all output is performed just before the program terminates, when the program results must be written to a single le. Between startup and termination, the program is entirely CPU-bound. Your task is to improve the performance of this application by multi-threading it. The application runs on a system that uses the one-to-one threading model (each user thread maps to a kernel thread).

    • How many threads will you create to perform the input and output? Explain.

    • How many threads will you create for the CPU-intensive portion of the application? Explain.

  6. Consider a multiprocessor system and a multithreaded program written using the many-to-many threading model. Let the number of user-level threads in the program be more than the number of processors 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 processors.

    • The number of kernel threads allocated to the program is equal to the number of processors.

    • The number of kernel threads allocated to the program is greater than the num- ber of processors but less than the number of user-level threads.

Make a list of 10-15 keywords for a 10 min. presentation with the topic: "Processes, Threads and Concurrency"

In class:

Use the first 30 minutes to work on those exercises:

  1. Including the initial parent process, how many processes are created by the program shown in the following program:

#include <stdio.h>
#include <unistd.h>
int main() {
    /* fork a child process */
    fork();
    /* fork another child process */
    fork();
    /* and fork another */
    fork();
    return 0;
}
  1. Explain the circumstances when the line of code marked printf("LINE J") in the Figure below is reached.

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main() {
    pid_t pid;
    /* fork a child process */
    pid = fork();
    if (pid < 0) { /* error occurred */
        fprintf(stderr, "Fork Failed");
        return 1;
    } else if (pid == 0) { /* child process */
        execlp("/bin/ls","ls",NULL);
        printf("LINE J");
    } else { /* parent process */
        /* parent will wait for the child to complete */
        wait(NULL);
        printf("Child Complete");
    }
    return 0;
}
  1. What are the benefits and the disadvantages of each of the following? Consider both the system level and the programmer level.

    • Synchronous and asynchronous communication

    • Automatic and explicit buffering

    • Send by copy and send by reference

    • Fixed-sized and variable-sized messages

  2. Using Amdahl’s Law, calculate the speedup gain of an application that has a 60 percent parallel component for (a) two processing cores and (b) four processing cores.

  3. As described in Section 4.7.2, Linux does not distinguish between processes and threads. Instead, Linux treats both in the same way, allowing a task to be more akin to a process or a thread depending on the set of ags passed to the clone() system call. However, many operating systems, such as Windows XP and Solaris, treat processes and threads differently. Typically, such systems use a notation wherein the data structure for a process contains pointers to the separate threads belonging to the process. Contrast these two approaches for modeling processes and threads within the kernel.

Then spend the remaining time discussing your list of keywords, and the excercises prepared at home and in class.

  • Chapter 3 and 4 in Operating System Concepts, Enhanced eText, 10th Edition

Material (Slides, etc.)