Spring 2001, CSE 428: Assignment 4


Distributed: Mar 2.
Due: Mar 22 in class.
Total maximum score: 100 points.

The purpose of this assignment is to provide experience with Concurrent Programming in Java


Implementation of a scheduler

Suppose that we have a set of agents, each performing a certain task. Each task is characterized by a number from 0 to n-1, where n is the number of tasks. There can be more than one agent with the same task number. Each agent tries to perform its task repeatedly. For simplicity, we assume that a given agent performs always a task with the same number.

Suppose that the tasks have to be started in a circular way. For instance, for n = 4, the sequence of tasks started would be: 0, 1, 2, 3, 0, 1, 2, 3, etc. Two tasks with different numbers can be performed in parallel, but two tasks with the same number cannot be performed in parallel. Hence, before starting a new instance of task #k we need to make sure that the previous instance of task #k has terminated.

The exercise consists in programming a scheduler which satisfies the above requirements.

How to write and test your solution

Click here to download the code of the main function and of the agents. Save it with name "A4_code.java".

Download this solution frame and fill it in with your definition for the scheduler. Save it with name Scheduler.java. Compile the program with the Unix command

   javac A4_code.java
Note that A4_code.java contains an instruction to import the file Scheduler.java (which should be saved in the same directory). Hence it will include your definition for the scheduler.

Now you can execute the compiled code by giving the command

   java A4_code
Different executions of the program will in general give different result. The following are some of the possible outputs: output 1 , output 2 , output 3 , output 4.

Note that you should not get a situation in which two tasks with the same number are performed in parallel. See for instance: wrong output 1.

Also, you should not get a situation in which two tasks with non-consecutive numbers are started consecutively. See for instance: wrong output 2.

Please note the following:.

  1. Please make sure that your program can be compiled with the Unix javac and executed with the Unix java. Solutions which cannot be compiled or executed will receive, at most, 40 points.
  2. Please don't change the code of A4_code.java !!!. Solutions which work only for your own definition of A4_code.java will receive, at most, 40 points.
  3. The solution should allow tasks with different numbers to be executed in parallel. Strictly sequential solutions will receive, at most, 50 points.
  4. The first task to be executed should be task #0.
  5. Its your responsibility to write a sound, reliable program, so please test your code thoroughly before you submit it.
  6. The TA will test your program by running it several times.

Format of the solution

Please give an hard copy of the source code to the instructor by the due date. Furthermore, you should also email your code to cg428@cse.psu.edu by the due date, so to allow us to check that it runs correctly. Please write the code in a single file named Scheduler.java, and send it as an attachment (not by "cut-and-paste"). Please write in the subject of the email your name and your student id.