Midterm Exam

CS354 Spring 2004



Name:________________________________________________________________





Question
Max.
Current
True/False
10 pts.

Short Questions 1-5
15 pts.

Short Questions 5-10
15 pts.

11,
10 pts.

12.
10 pts.

13.
20 pts.

14.
10 pts.

15.
10 pts


Total:






















Part 1. True False Questions

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

____ A call to sin(x) will show in the truss output
____ COFF is an executable format in Windows.
____ The close() system call always releases the resources of an open file object.
____ Threads created with pthread_create in the parent will be duplicated in the child after calling fork().
____ The function strcat(a,b) will allocate strlen(1)+strlen(b)+1 bytes and copy the concatenation of a and b in this memory.
____ After forking a process, a write call in the parent could modify the offset of an open file in the child.
____ A parent and child processes may communicate through a pipe created by the child process.
____ The counter of a semaphore cannot have a value larger than the initial count.
____ It is possible to modify the interrupt vector in user space.
____ a[i] is equivalent to *(&a[1] + i -1)

Part 2. Short questions.

1. (3 pts) Mention the differences between using the write system call and using printf to write to the screen.





2. (3 pts.) Give the advantages and disadvantages of user threads v.s. kernel threads.


  

 

3. (3 pts.) If in the code for the spinlock we remove the yield inside the loop, a) Will the spinlock work? b) If it works, explain what possible problems it will encounter. If it does not work explain why it will not work.



 

4. (3 pts.) Explain the checks that the kernel does during the system call write(fd, buff, n)


 

 

5. (3 pts.) Enumerate the problems that you may have calling free(ptr)





6. (3 pts.) Explain why the quantum length has to be longer than the average CPU burst.





7. (3 pts.) Explain how the average CPU burst length of a process affects the scheduling priority of the process in operating systems like Solaris.





8. (3 pts.) Write the pseudocode for sema_post(sem) and sema_wait(sem)





9. (3 pts.) Explain why malloc returns addresses that are aligned to 8 byte boundaries (multiple of 8).





10. (3 pts.) Explain the advantages and disadvantages of having the OS kernel running in kernel mode and the user programs running in user mode.





Part 3. Programming questions.

 
 
11. (10 pts.) Implement the function reverse(s) that reverses a string. The resulting string is left in s. Do not use any of the string functions.
void reverse(const char * s)
{
// Reverse string










}
      
 
 
12. (10 pts.) Write the method reverseList() that reverses the order a single-linked list. Write the implementation below.

strunct ListNode {
int _value;
ListNode * _next;
}

class List {
ListNode * _head;
public:
.. Other methods ...
void reverseList();
}

void List::reverseList()
{
// Reverses the list that starts at _head

























}
      
 
13. (20 pts.) Write a program "lssort" that implements the command "ls -laR dir | sort > out". The program should not return until the command finishes. "dir" and "out" are passed as arguments to the program. Example of the usage is "lssort /etc outfile",. Do error checking.

int main( int argc, char ** argv)
{










































}


   
 
14. (10 pts.) Write a class SynchronizedList that implements two methods: Add(int val) and int Remove().  The procedure Add(int val) appends an element at the end of the list. The method Remove() removes the element at the head of the list and returns its value.Only a maximum of 10 elements can be in the list at any time. The method Remove() will block until there is an item to remove. The method Add will block if there are already 10 elements in the list.
struct ListNode {
int _value;
ListNode * _next;
};

class SynchronizedList {
ListNode * _head;
// Other variables




public:
void Add(init val);
int Remove();
}

void
SynchronizedList::
Add(init val)
{









}

int
SynchronizedList::Remove()
{












}


 
 
15. (10 pts) Write a class Alert that has procedures waitAlert(int maxVal) and setValue(val). The method waitAlert(int maxVal) will block until there is a call to setValue(val) where val > maxVal. Multiple threads may be calling waitAlert(int maxVal) and multiple threads may wakeup at once. setValue(val) may wake up other threads but it will never block.

class Alert {

// Other variables




public:
Alert();
void waitAlert( int maxValue );
void setValue(int val);
}

Alert::Alert()
{




}

void
Alert::waitAlert( int maxValue )
{











}

void
Alert::setValue(int val)
{












}