CSRU 3595 Operating Systems Dept. of Computer & Info Sciences
Fall 2008 C. Schweikert

Lab Assignment #2 (Chapter 3 - Processes / Chapter 4 - Threads)
Due: Friday, October 3


3.1 When a process creates a new process using the fork() operation, which of the following states is shared between the parent process and the child process? Explain.
A. Stack B. Heap C. Shared memory segments

3.2 Describe the actions taken by a kernel to context-switch between processes.

3.3 Describe the differences among short-term, medium term, and long term scheduling.

4.1 Run the following program that utilizes the Java Thread class and write a step by step explanation of the program's execution.

a) Create a file called: Consumer.java and include below code

b) Compile using the command: javac Consumer.java

c) Run by entering: java Consumer

d) Press Ctrl + C to end program.

import java.util.Vector;

class Producer extends Thread {

static final int MAXQUEUE = 5;

private Vector messages = new Vector();

public void run() {

try {

while ( true ) {

putMessage();

sleep( 1000 );

}

}

catch( InterruptedException e ) { }

}

private synchronized void putMessage()

throws InterruptedException {

while ( messages.size() == MAXQUEUE )

wait();

messages.addElement( new java.util.Date().toString() );

notify();

}

public synchronized String getMessage()

throws InterruptedException {

notify();

while ( messages.size() == 0 )

wait();

String message = (String)messages.firstElement();

messages.removeElement( message );

return message;

}

}

class Consumer extends Thread {

Producer producer;

Consumer(Producer p) {

producer = p;

}

public void run() {

try {

while ( true ) {

String message = producer.getMessage();

System.out.println("Got Message: " + message);

sleep( 2000 );

}

}

catch( InterruptedException e ) { }

}

public static void main(String args[]) {

Producer producer = new Producer();

producer.start();

new Consumer( producer ).start();

}

}