Midterm Exam

CS354 Spring 2006



Name:________________________________________________________________





Question
Max.
Current
True/False
10 pts.

Short Questions 1-5
15 pts.

Short Questions 6-10
15 pts.

11,
10 pts.

12.
10 pts.

13.
15 pts.

14.
15 pts.

15.
10 pts


Total:






















Part 1. True False Questions

Answer True/False (T/F) (1 point each)

____ The user time + kernel time in a process is always equal to the real time
____ In a soft real-time system critical tasks take the highest priority until completion.
____ A short quantum will benefit CPU-intensive applications
____ SMP stands for Simultaneous Multi processing.
____ The function strdup(s) allocates strlen(s)+1 memory  to allocate a string.
____ execvp will return 0 if successfull.
____ After fork(), each process will have its own file descriptor table.
____ The ordering of mutex_unlock() calls may cause a deadlock.
____ Interrupt handlers run in user space.
____ A semaphore initialized with 1 can be used to instead of a mutex.

Part 2. Short questions.

1. (3 pts) Explain the advantages and disadvantages of a cluster vs. a parallel machine





2. (3 pts.) Explain the advantages and disadvantages of choosing a small quantum.

 


 

3. (3 pts.) Explain how Multi-Level Feedback-Queue Scheduling works.




 

4. (3 pts.) Explain in what situations the user time + kernel time is larger than the wall-clock time.


 

 

5. (3 pts.) Explain the advantages and disadvantages of  Preemptive Scheduling





6. (3 pts.) Enumerate the memory sections of a program and explain what they store.





7. (3 pts.) What are the roles of the Loader





8. (3 pts.) What are the advantages and disadvantages of using threads vs. using processes?





9. (3 pts.) What is the problem of having long critical sections in a multiprocessor machine?





10. (3 pts.) Explain what is a race condition and why they are difficult to reproduce.






Part 3. Programming questions.

 
11. (10 pts.) Implement the function char *strrchr(char *s, int c); that  returns a pointer to the last occurrence
     of c or NULL if c is not part of the string.
char *strrchr(char *s, int c)
{











}
      










12. (10 pts.) Implement the class Array4D of type double that creates a 4D array of 4 dimensions m x n x r x q as indicated in the constructor. Also implement the function getElementAt and setElementAt that gets and sets the element at this location. Base your implementation in the array of pointers to rows that we covered in class.
class Array4D {

// Add any variables you need




public:
  Array4D( int m, int n, int r, int q);
  double getElementAt( int m, int n, int r, q);
  void setElementAt( int m, int n, int r, q);
};

Array4D::Array4D( int m, int n, int r, int q)
{




}

double
Array4D::getElementAt( int i, int j, int k, int q)
{



}

void
Array4D::setElementAt( int i, int j, int k, int q, double val)
{



}
     
 
14. (15 pts.) Write a class AlertTemp  that implements two methods: void listenTemp(int minTemp, int maxTemp) and void setCurrentTemp( int currentTemp). A thread calling the function listenTemp will block until there is another thread calling setCurrentTemp(currentTemp) where minTemp <=curentTemp<=maxTemp.  A thread calling setCurrentTemp(currentTemp)  may wakeup multiple threads calling void listenTemp(int minTemp, int maxTemp) if curentTemp matches these calls. If the call to  setCurrentTemp(currentTemp) does not match any of the listenTemp calls or no thread is waiting , the setCurrentTemp call is ignored. Note: Use condition variables.

class AlertTemp {
// Add any member variables here




public:
AlertTemp();
void listenTemp(int minTemp, int maxTemp);
void setCurrentTemp(int currentTemp);
};

AlertTemp ::AlertTemp ()
{
// Write ay initializations.



}

void
AlertTemp::listenTemp(int minTemp, int maxTemp){







}

void
AlertTemp::setCurrentTemp(int currentTemp);{








}


 
 
15. (10 pts) Implement a class SynchronizedStack  that implements a stack that stores elements of type int. The methods push and pop. If pop is called and the stack is empty it will  block until the stack is not empty. push will block if the stack is full and will continue when the stack has space to perform the operation.  The constructor initializes the stack with the maximum size and any other necessary variables.

class SynchronizedStack {
// Add any other member variables you need




public:
SynchronizedStack(int maxSize);
void push(int element);
void pop(int element);
};

SynchronizedStack ::SynchronizedStack(int maxSize){
// initialize variables and stores maxSize of the stack






}

void
SynchronizedStack::push(int element)
{
// pushes element into the stack.. Wait if stack is full











}

int
SynchronizedStack::pop() {

// pops an element from the stack.. Wait if stack is empty.
// return popped element











}