OPERATING SYSTEMS

Homework 2 Solution

1.     (10pts) Threads: What is the difference between a process and a thread. Which one consumes more resources?

A process defines the address space, text, resources, etc.

A thread defines a single sequential execution stream within a process.

Threads are bound to a single process.

Each process may have multiple threads of control within it.

It’s much easier to communicate between threads than between processes.

It’s easy for threads to inadvertently disrupt each other since they share the entire address space.

Process consumes more resources.

2.     (5pts) Threads: Assume that the OS implements Many-to-Many multithreading model. What is the minimum number of kernel threads required to achieve better concurrency than in the Many-to-One model? Why?

At least two kernel threads are required to achieve better concurrency in the many-to many model than in the Many-to-One model. With more than two kernel threads, CPU can still schedule other threads for execution when one threads is performing a blocking system calls.

3.     (10pts) Threads: What is the relation between Solaris user threads and Java application threads in a Solaris environment?

In Solaris 2.6, the many-to-many model is implemented.  Typically, a Java application thread is implemented using a user-level thread in Solaris.

4. (20pts) Java Threads:

Develop an execution driven simulator for a multiprocessor that solves a 3x3 matrix multiply (i.e., AxB=C, where A,B,C are 3x3 matrixes) following these steps:

 

// file: Matrix.java

import java.util.Random;

 

public class Matrix {

    public static int[][] A, B, C;

    public int n;

 

    public void printMatrix(int[][] M) {

            for(int i = 0; i < n; i++) {

                for(int j = 0; j < n; j++)

                        System.out.print("\t"+M[i][j]);

                System.out.println();

            }

    }

 

    public Matrix() {

            n = 3;

            A = new int[n][n];

            B = new int[n][n];

            C = new int[n][n];

            Random r = new Random();

            for(int i = 0; i < n; i++)

                for(int j = 0; j < n; j++) {

                        A[i][j] = r.nextInt() % 10; // keep numbers below 10 :)

                        B[i][j] = r.nextInt() % 10;

                }

    }

 

    public void doWork() {

            MultThread[][] mt = new MultThread[n][n];

 

            for(int i = 0; i < n; i++)

                for(int j = 0; j < n; j++) {

                        mt[i][j] = new MultThread(3, i, j);

                        mt[i][j].start();

                }

 

            try {

            for(int i = 0; i < n; i++)

                for(int j = 0; j < n; j++)

                        mt[i][j].join();

            } catch(InterruptedException ie) {}

 

            System.out.println("\nA =");

            printMatrix(A);

            System.out.println("\nB =");

            printMatrix(B);

            System.out.println("\nC =");

            printMatrix(C);

    }

 

    public static void main(String args[]) {

            Matrix m = new Matrix();

            m.doWork();

    }

}

 

 

// file: MultThread.java

public class MultThread extends Thread {

    private int n;  // n*n matrix

    private int i, j; // row, column

 

    public MultThread(int size, int ii, int jj) {

            n = size;

            i = ii;

            j = jj;

    }

 

    public void run() {

            Matrix.C[i][j] = 0;

            for(int x = 0; x < n; x++)

                Matrix.C[i][j] += Matrix.A[i][x] * Matrix.B[x][j];

    }

}

5.     (10pts) Scheduling: Given the following mix of job, job lengths, and arrival times, assume a time slice of 10 and compute the completion and average response time of each job for the FIFO, RR, and SRTF algorithms. Use the following format for the solution:’

Assume the ready queue is empty.

Parameters

Scheduling Algorithms

Job

length

arrival time

FIFO

RR

SRTF

0

75

0

 75

 200

205

1

40

10

 115

 115

75

2

25

10

 140

 85

35

3

20

80

 160

 145

100

4

45

85

 205

 205

145

 

 

Avg. RT

 102

 113

 75