CORE EXAMINATION Department of Computer Science New York University September 26, 2008

This is the common examination for the M.S. program in CS. It covers three core computer science topics: Programming Languages, Operating Systems, and Algorithms. The exam has two parts. The Systems Part (PL and OS) lasts two and one half hours and covers the first two topics. The Algorithms Part (Algo), given this afternoon, lasts one and one-half hours, and covers the last topic.

You will be assigned a seat in the examination room.

Use the proper booklet or answer sheet for each question. Each booklet is marked with the Area and Question number, in the form PL1, PL2, OS1, OS2, ALG1, ALG2 and ALG3. DO NOT put your name on the exam booklet or answer sheet. Instead, your exam number must be on every booklet.

You will be graded according to your exam number, shown on the envelope containing the booklets. Remember your exam number: when grades are given out, they will be published according to this number, not by name.

Make sure your name and signature are on the envelope. This is the only place where your name appears. Please include all the booklets inside the envelope. You can keep the exam.

Good luck!

SYSTEMS PART:

Programming Languages and Operating Systems

Question 3—Please use the Exam Booklet labeled OS1

Process Coordination and I/O

Consider the following three processes P1, P2, and P3 written in pseudocode.

Process P1              Process P2            Process P3
P(S)                    P(T)                  P(U)
Compute(10)             Compute(20)           Compute(30)
Disk(10,100)            Disk(100,10)          Disk(1,1000)
V(T)                    V(U)                  V(S)

P(X) and V(X) are binary semaphores that do not busy-wait. Specifically: If the (binary) semaphore X is open, V(X) is a nop and P(X) closes X. If X is closed, P(X) blocks the process and V(X) opens X (thus allowing one process blocked on X to proceed and re-close X). When execution begins the U semaphore is open and the S and T semaphores are closed. We make several simplifying assumptions

1. Context switching takes zero CPU time.

2. Disk(M,N) takes zero CPU time.

3. P(X) and V(X) take zero CPU time.

4. Compute(Y) takes Y ms of CPU time

Thus we see that the processes require 10ms, 20ms, and 30ms of CPU time and hence the entire job requires 60ms of CPU time. (Recall that ms abbreviates millisecond and 1ms=10^{−3}seconds.) Another simplification is that this system uses a non-preemptive scheduler. So a process is NOT involuntarily moved from the running to the ready state. Of course it can be blocked. Two important reasons why a process would be blocked are

1. The process initiates I/O or

2. The process executes P(X) on a closed semaphore X.

Assume that at time zero, P1, P2, and P3 are all created and are the only processes in the system.

Part A (5 Points)

For Part A assume that Disk(M,N) is a nop. When does each of the three Compute(Y) procedures start and when do they end? When does each of the three processes terminate?

Part B (4 Points)

For Part B, Disk(M,N) is no longer a nop, but it still is assumed to require zero CPU time. Specifically, Disk(M,N) performs M disk read operations. These M reads are directed at disk locations that are far apart from each other; however, note that each single read itself is directed at a contiguous disk block of N KB (recall that KB abbreviates Kilobyte and 1KB=10^{3}Bytes). Note that each of the processes reads a total of 1MB (recall that MB abbreviates megabyte and that 1MB=10^{6}Bytes).

To determine the I/O time required for Disk(M,N) assume all M reads are directed at a single disk with the following characteristics

1. The block size is N.

2. The seek time is 5ms.

3. The rotation rate is 6000 RPM (RPM abbreviates revolutions per minute).

4. The transfer rate is 10MB/second (after the head has reached the beginning of the block).

What is the total I/O time required for Disk(1,100), Disk(1,10), and Disk(1,1000), i.e. for ONE I/O operation of each of the three sizes.

Part C (1 Point)

Using the assumptions of part B and the processes of part A, when does each of the three Compute(Y) procedures start and end; when does each of the three Disk(M,N) procedures start and end; and when does each process terminate?

Question 4—Please use the Exam Booklet labeled OS2

Virtual Memory:

A user process executes a program that begins as follows:

   int  fd;                   // file descriptor
   char buf[4];               // buffer to hold data from the file
   fd = Open("fileX", "r");   // open fileX for reading
   Read(fd, buf, 4);          // read 4 bytes from fileX into buf

Open and Read are system calls for opening a file, and reading information from it respectively. Recall that system calls in most modern operating systems (OS) are implemented using a trap (exception) mechanism, which transfers control to the operating system. For the Read system call, the OS code retrieves the call arguments, performs the requested operation, and passes back the response by writing into memory locations that are accessible by the user process. When the OS handles the exception caused by the Read system call above, it receives the parameter values 3, 998, and 4, corresponding to the parameters fd, buf (the address of the buffer), and the number of bytes that need to be read. Assume that the first four bytes of the file fileX are the characters a, b, c, and d (in that order). The virtual memory system uses a page size of 1000 bytes. At the time of the Read call, the page table for the user process looks like this:

Page Number Frame Number Valid Use Dirty
0  3  1  1  0
1  1  1  0  0
2  0  1  1  1
3  0  0  0  0
4  2  1  0  1

Note that the Use and Dirty bits (also called the Referenced and Modified bits) keep track of page usage. These bits are set by hardware and unset in specific ways by page replacement algorithms to control which pages to evict.

Part A (3 points)

The page table above controls how virtual addresses generated by the user process are translated into physical addresses. To which physical addresses will the OS write the characters a, b, c, and d, so that they will be placed properly in the process’ buffer when the Read call returns?

Part B (3 points)

For the system as in question A above, show the state of the process page table after the Read call completes. (Hint: Consider which fields of the page table entry would change as a result of the Read system call).

Part C (4 points)

Consider the system and process in question A above, where the virtual memory system employs a local page replacement policy using the CLOCK (Second Chance) algorithm. The process is statically allocated four physical frames 0-3 (as shown in the first page table): page replacement is restricted to one of these frames. Assume that the circular list used by the CLOCK algorithm consists of frames inspected in increasing numeric order, i.e., in the order 0, 1, 2, 3. The CLOCK pointer currently points at frame 0. Starting from the process page table after the Read call (which you computed as your answer to question B), give a sequence of three virtual address references in the range 0 - 4999, such that if these are the next three addresses referenced (used) by the process, each reference will cause a page fault. Please show all your work.