|
|
Subject:
computer, operating system
Category: Computers > Operating Systems Asked by: axmedeey-ga List Price: $20.00 |
Posted:
18 Dec 2003 05:44 PST
Expires: 17 Jan 2004 05:44 PST Question ID: 288262 |
1. The kernel of an operating system maintains a variety of data structures. Simply state which kind of abstract data type (Set, List or Map) models each of the following: a. a ready queue from which processes are dispatched in the same order as that in which they arrive; b. a process table indexed by process identifier. 2. Here is a Java program that creates a Set using a HashSet implementation: import java.util.*; public class SetDemo { public static void main( String [] args ) { // Create a set called mySet Set mySet = new HashSet(); // Ensure that this set contains an interesting selection of fruit String fruit1 = "pear", fruit2 = "banana", fruit3 = "tangerine", fruit4 = "strawberry", fruit5 = "blackberry"; mySet.add( fruit1 ); mySet.add( fruit2 ); mySet.add( fruit3 ); mySet.add( fruit2 ); mySet.add( fruit4 ); mySet.add( fruit5 ); // Display contents of mySet System.out.println( "mySet now contains:" ); System.out.println( mySet ); } } Compile and execute this program as follows: % javac SetDemo.java % java -cp . SetDemo a. Why don't the fruit get listed in the order in which they were added? Extend the program in a stepwise manner using the standard collection interface methods to do the following: b. display the cardinality of (i.e. the number of elements in) mySet; c. remove the blackberry and strawberry; display contents of mySet again to confirm success; d. remove the remaining fruit using a single method invocation; e. show that the set is now empty by using the isEmpty() method. Compilation and execution of your program should result in output similar to this: % javac SetDemo.java % java -cp . SetDemo mySet now contains: [blackberry, tangerine, strawberry, pear, banana] number of elements = 5 blackberry and strawberry removed; mySet now contains: [tangerine, pear, banana] mySet has been cleared. mySet is now empty. % You must submit just the final version of your program in answer to b, c, d and e, plus the output resulting from compilation and execution of your program as evidence of success. You can capture your terminal session to a file (e.g. temp.txt) using the following command... % script temp.txt ...then when you've finished, type the command exit to quit capturing the session. 3. Construct a List (rather than a Set as in question 2), containing a pear, a banana, a tangerine, a strawberry and a blackberry inserted consecutively. Then extend your program to do the following: a. Display the contents of the List in order of insertion using an Iterator; b. Display the contents of the List in reverse order using a ListIterator; c. Insert a second banana into the List in-between the tangerine and the strawberry; display contents again to confirm success. Compilation and execution of your program should result in output similar to this: % javac ListDemo.java % java -cp . ListDemo elements are (in order of insertion): pear banana tangerine strawberry blackberry elements are (in reverse order): blackberry strawberry tangerine banana pear second banana added; elements are: pear banana tangerine banana strawberry blackberry % As for question 2, you must submit just the final version of your program in answer to a, b and c, plus the output resulting from compilation and execution of your program as evidence of success. d. Why would you choose to use an ArrayList rather than a LinkedList, and vice versa? 4. Consider the following standalone Java application, consisting of three classes, TestHost1, Host1 and Helper1: public class TestHost1 { public static void main(String argv[]) { Host1 ho = new Host1(); System.out.println("3 + 4 + 17 + 5 = " + ho.sum(3,4,17,5)); } } public class Host1 { public int sum(int a, int b, int c, int d) { Helper1 he = new Helper1(c,d); Thread aux = new Thread(he); aux.start(); int temp = a+b; try{ aux.join(); } catch(InterruptedException e) { } temp = temp + he.getResult(); return temp; } } public class Helper1 implements Runnable { int x, y, result; public Helper1(int e, int f) { x = e; y = f; } public int getResult() { return result; } public void run() { result = x + y; } } You will have observed that work is delegated by the main thread to an auxiliary thread. Which of the two threads performs each of the following operations: a. creates a helper; b. adds 3 and 4; c. adds 17 and 5; d. invokes getResult(); e. returns 22; f. adds 7 and 22. 5. In the above program, what are each of the two threads doing at the point at which they synchronize? 6. Now consider another way to implement the program: public class TestHost2 { public static void main(String argv[]) { Host2 ho = new Host2(); System.out.println("3 + 4 + 17 + 5 = " + ho.sum(3,4,17,5)); } } public class Host2 { int z; public Host2() { } public synchronized int sum(int a, int b, int c, int d) { Helper2 he = new Helper2(c,d,this); Thread aux = new Thread(he); aux.start(); int temp = a+b; try{ wait(); } catch(InterruptedException e) { } temp = temp + z; return temp; } public synchronized void callback(int a) { z = a; notify(); } } public class Helper2 implements Runnable { int x, y, result; Host2 client; public Helper2(int e, int f, Host2 h) { x = e; y = f; client = h; } public void run() { result = x + y; client.callback(result); } } Again work is delegated by the main thread to an auxiliary thread. Which of the two threads performs each of the following operations: a. Adds 3 and 4; b. Adds 17 and 5; c. Invokes callback() d. Invokes notify() e. Adds 7 and 22; 7. In Host2, why will notify() always occur after wait()? |
|
Subject:
Re: computer, operating system
Answered By: endo-ga on 18 Dec 2003 12:11 PST Rated: |
Hi, Thank you for your question. 1-) a) A FIFO list, because processes are kept in a given order and taken out in the same order. b) A Map, because you need to be able to link a given process to a given process identifier, such as a key, which is the model that a Map implements. 2-) Everything you need to answer 2-) is on: http://java.sun.com/j2se/1.4.2/docs/api/java/util/HashSet.html a) Because a HashSet is a set and there is no order in a set. The Java documentation clearly specifies: It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. http://java.sun.com/j2se/1.4.2/docs/api/java/util/HashSet.html b) The function size() displays the number of elements in a set. c) The function remove(Object o) removes an object from a set. d) The function clear() removes all the elements from a set. e) import java.util.*; public class SetDemo { public static void main( String [] args ) { // Create a set called mySet Set mySet = new HashSet(); // Ensure that this set contains an interesting selection of fruit String fruit1 = "pear", fruit2 = "banana", fruit3 = "tangerine", fruit4 = "strawberry", fruit5 = "blackberry"; mySet.add( fruit1 ); mySet.add( fruit2 ); mySet.add( fruit3 ); mySet.add( fruit2 ); mySet.add( fruit4 ); mySet.add( fruit5 ); System.out.println( "mySet now contains:" ); System.out.println( mySet ); System.out.println( "number of elements in mySet:" ); System.out.println( mySet.size() ); mySet.remove( fruit4); mySet.remove( fruit5); System.out.println( "blackberry and strawberry removed. mySet now contains:" ); System.out.println( mySet ); mySet.clear(); if(mySet.isEmpty()) System.out.println( "mySet is now empty." ); else System.out.println( "mySet is not empty:" ); } } 3. a. The function iterator() returns an Iterator for a list. http://java.sun.com/j2se/1.4.2/docs/api/java/util/AbstractList.html#iterator() An Iterator is: http://java.sun.com/j2se/1.4.2/docs/api/java/util/Iterator.html b. Use ListIterator http://java.sun.com/j2se/1.4.2/docs/api/java/util/ListIterator.html Go to the last element in the list, then use the previous() method. c. Use add(int index, Object element) Inserts the specified element at the specified position in this list. http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html#add(int,%20java.lang.Object) Resulting program: import java.util.*; public class ListDemo { public static void main( String [] args ) { // Create a list called myList List myList = new ArrayList(); // Ensure that this set contains an interesting selection of fruit String fruit1 = "pear", fruit2 = "banana", fruit3 = "tangerine", fruit4 = "strawberry", fruit5 = "blackberry"; myList.add( fruit1 ); myList.add( fruit2 ); myList.add( fruit3 ); myList.add( fruit4 ); myList.add( fruit5 ); Iterator i = myList.iterator(); System.out.println( "elements are (in order of insertion):" ); while(i.hasNext()) System.out.println( i.next()); ListIterator li = myList.listIterator(myList.size()); System.out.println( "elements are (in reverse order of insertion):" ); while(li.hasPrevious()) System.out.println( li.previous()); myList.add(3, fruit2); i = myList.iterator(); System.out.println( "second banana added; elements are:" ); while(i.hasNext()) System.out.println( i.next()); } } d) An ArrayList permits direct access to specific elements, and is faster for directly accessing certain positions in a list. In this case, the difference is negligeable and an ArrayList is used because of c) For a longer comparison have a look at: http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.html 4. public class TestHost1 { public static void main(String argv[]) { Host1 ho = new Host1(); System.out.println("3 + 4 + 17 + 5 = " + ho.sum(3,4,17,5)); } } public class Host1 { public int sum(int a, int b, int c, int d) { Helper1 he = new Helper1(c,d); Thread aux = new Thread(he); aux.start(); int temp = a+b; try{ aux.join(); } catch(InterruptedException e) { } temp = temp + he.getResult(); return temp; } } public class Helper1 implements Runnable { int x, y, result; public Helper1(int e, int f) { x = e; y = f; } public int getResult() { return result; } public void run() { result = x + y; } } a) The main thread creates the helper with the line: Helper1 he = new Helper1(c,d); b) The main thread adds 3 and 4 with the line: int temp = a+b; c) The auxiliary thread adds 17 and 5 with the line: aux.start(); d) The main thread because of the line: aux.join(); This states the program was wait till the auxiliary thread stops. e) The main thread, because at that point the aux thread doesn't exist anymore. f) The main thread, because at that point the aux thread doesn't exist anymore. 5) The main thread just added 3 and 4, the aux thread just added 17 and 5. 6) a) Main thread. b) Auxiliary thread. c) Auxiliary thread. d) Auxiliary thread. e) The main thread because of the wait. Causes current thread to wait until another thread invokes the notify() method http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#wait() 7) Because sum() and callback() are synchronized. Therefore callback() must wait until sum() is finished. I hope this answers your question. If anything is unclear or if you require any clarifications, please do not hesitate to ask. Thanks. endo |
axmedeey-ga
rated this answer:
endo, there was much thoughtful research in this. Many thanks. This is my first use of Google Search, and I will be back. I have yet to expore your findings in detail, but you made my job a lot easier. endom thanks allot |
|
Subject:
Re: computer, operating system
From: endo-ga on 20 Dec 2003 13:54 PST |
Thank you for the great rating and kind comments. Regards, endo |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |