National Chengchi University



Operating System Homework #2

1. [3.20] Design a file-copying program named filecopy using ordinary pipes. This program will be passed two parameters: the first is the name of the file to be copied, and the second is the name of the copied file. The program will then create an ordinary pipe and write the contents of the file to be copied to the pipe. The child process will read this file from the pipe and write it to the destination file. For example, if we invoke the program as follows:

filecopy input.txt copy.txt

The file input.txt will be written to the pipe. The child process will read the contents of this file and write it to the destination file copy.txt. You may write this program using either UNIX or Windows pipes.

2. [4.1] Provide two programming examples in which multithreading does not provide better performance than a single-threaded solution.

3. [4.12] The following program uses the Pthreads API. What would be the output from the program at LINE C and LINE P?

#include

#include

int value = 0;

void *runner(void *param); /*the thread*/

int main(int argc, char *argv[])

{

int 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);

}

4. [4.13] 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.

a) The number of kernel threads allocated to the program is less than the number of processors.

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

c) The number of kernel threads allocated to the program is greater than the number of processors but less than the number of user-level threads.

5. [4.21] The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, …

Formally, it can be expressed as:

fib0 = 0

fib1 = 1

fibn = fibn-1 + fibn-2

Write a multithreaded program that generates the Fibonacci sequence using either the Java, Pthreads, or Win32 thread library. This program should work as follows: The user will enter on the command line the number of Fibonacci numbers that the program is to generate. The program will then create a separate thread that will generate the Fibonacci numbers, placing the sequence in data that can be shared by the threads (an array is probably the most convenient data structure). When the thread finishes execution, the parent thread will output the sequence generated by the child thread. Because the parent thread cannot begin outputting the Fibonacci sequence until the child thread finished, this will require having the parent thread wait for the child thread to finish, using the techniques described in Section 4.4.

6. [5.1] Why is it important for the scheduler to distinguish I/O-bound programs from CPU-bound programs?

7. [5.12] Consider a system running ten I/O-bound tasks and one CPU-bound task. Assume that the I/O-bound tasks issue an I/O operation once for every millisecond of CPU computing and that each I/O operation takes 10 milliseconds to complete. Also assume that the context-switching overhead is 0.1 millisecond and that all processes are long-running tasks. Describe the CPU utilization for a round-robin scheduler when:

a) The time quantum is 1 millisecond

b) The time quantum is 10 milliseconds

8. [5.?] Consider the following set of processes, with the length of the CPU burst given in milliseconds:

Process Burst Time Priority

P1 10 3

P2 1 1

P3 2 3

P4 1 4

P5 5 2

The processes are assumed to have arrived in the order P1, P2, P3, P4, P5, all at time 0.

a) Draw four Gantt charts that illustrate the execution of these processes using the following scheduling algorithms: FCFS, SJF, nonpreemptive priority (a smaller number implies a higher priority), and RR(quantum = 1).

b) What is the turnaround time of each process for each of the scheduling algorithms in part (a)?

c) What is the waiting time of each process for each of these scheduling algorithms?

d) Which of the algorithms results in the minimum average waiting time (over all processes)?

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download