CSE 121 Spring 2003 Homework 1
Due Midnight, 17 April 2003 

Please turn in a tar file that contains your two programs and your solution for Problem 2. Your solution for Problem 2 can be jsut a simple text file with no formatting.


Problem 1


Write a Java monitor that prints a message under the following circumstances:
You will need to be able to create Java threads to do this. You do so by extending Thread. Here's an example Java program that takes as an argument a number of threads to create. All the threads run the same code, defined by the method run. The thread starts running when its start method is called.

Each thread goes into an infinite loop calling a single method Test on a static object tm. In your program, TM would be a class with a synchronized method Test (that is, a monitor with a single entry procedure Test) :

public class montest {

    static int N;
    static TM tm;

    static class Driver extends Thread {
        public void run () {
            int i = 0;
            while (true) tm.Test();
        }
    }

    public static void main(String[] args) {
        int i;
        try { N = Integer.parseInt(args[0]); }
        catch (java.lang.NumberFormatException e) {N = 0;};
        if (N <= 0) {
            System.out.println(N + " is not at least one.");
            return; }
        Driver d[] = new Driver[N];
        tm = new TM();
        for (i = 0; i < N; i++) {
            d[i] = new Driver();
            d[i].start();
        }
    }
}


Problem 2


Using (strong general) semaphores, give a pseudocode semaphore implementation of a Java monitor. You will need to give the code that is executed when calling a synchronized method, when returning from a synchronized method, and the code for wait, notify, and notifyall.

Problem 3


Write a C program that takes the name of a file as a single argument. The program should rewrite the file to be the reverse of the original contents. For example, if the file contains the three characters ABC, then it will rewrite the file as CBA.

Use mmap and munmap for file access. You will also need the following system calls (for which you can find documentation for by using the man command).

last edited by Keith Marzullo on 9 April, 2003