CSE141-Fall 1999 by HC121109064610


Quiz Number 1

Name: ______________________________

Email: ______________________________

         This is a closed book, closed notes quiz, plan on taking 45 minutes for the quiz.
         Please answer all questions in the space provided and attach additional sheets if
         All questions need to have enough intermediate detail to let us understand how you got
          to the answer. Absence of such detail will lead to a loss of points even if the answer is
         Write your name in the space provided. Absence of identification information will lead to a
          loss of points.
         Requests for re-grading within a week of getting the results, in person and to me during
          office hours. Written description of “why you think we should change your grade”
          accompanying the quiz solutions is required.


Question                           Max                               Score
1.a                                2
1.b                                2
1.c                                2
1.d                                2
2.a                                3
2.b                                3
2.c                                3
2.d                                5
3.a                                4
3.b                                4
3.c                                DO NOT DO IN CLASS                TRY AT HOME
Total                              30
Question 1:
You are designing a windows application meant to run under WindowsNT.

This is an electronic wallet (WinWallet) application which permits users to deposit money into an
account (WinBank), and then charge against this money when making purchases online. The
general idea is to develop an application which is accessible from the browser (similar to Adobe
Acrobat), and gets trigerred when the web-page requests it. When the user chooses to “buy”
something on a e-commerce site (such as Amazon.com), the WinWallet application gets trigerred
with the right information. At this point, the user is presented with a “purchase screen”. There are
a number of features and functions required to make this work reasonably.

Describe whether you will choose to do the following functions in a separate process, thread or in
the main application. Explain your design decisions carefully weighing the pros and cons of
threads and processes. It is not sufficient to simply make a decision. It is important to explain why
the decision was made.

a. WinWallet also has a button for “see current balance”. If the user chooses this button, a
   separate screen pops up showing him the current account balance, while the “purchase
   screen” is still around. The user can then jump back and forth between the two screens. Also,
   this “current balance” screen should update automatically when the user does make a
   purchase. The user should not have to “reload”. Should this “see current balance” screen be
   a process or a thread or inline? Why?

         Thread. We have two separate yet related tasks and they need to share
     information. Threads share memory space and have less overhead when switching
     between them than a process.

b. When the user chooses to purchase the chosen product, a separate window pops up which
   shows the details of the transaction, while the main screen keeps showing the basic product
   details. While the transaction is going on, the user should be allowed to browse the main
   screen. Depends on interpretation of question.
       If interpret the separate window for display purposes only, then could be a
   process, as no memory sharing necessary. Once information is displayed and user
   closes the window, the process execution terminates.
       If interpret separate window as completing the transaction, may need to share
   information and threads share memory.

c.   A pop-up will ask the user when he or she wants to quit, whether his (or her) wallet
     information should be saved. No other activity will be permitted in the browser when such a
     query in place. Should this be implemented using a separate process or a thread ?

     Inline. There is one task – get user input. Both windows are waiting on the same event.

d. At the time this WinWallet is initialized a separate window shall pop up and connect to an
    advertisement server and show rolling advertisements during your browsing session. Is this a
    process or a thread ?
Process. Two unrelated tasks. No sharing of memory is necessary, and one should not
effect the other once the advertisement window is started.
Question 2:
a: What is an atomic instruction? Why do you need one in a multi-tasking OS ?
An instruction that is executed as one uninterruptible unit. Needed in multi-tasking OS to
enforce mutual exclusion in critical sections.

b: Describe the test-and-set(x) instruction usually provided by hardware as an atomic instruction
Instruction that allows us to either test and modify a word or swap the contents of two
words atomically.
boolean test_and_set(boolean target) //target holds current lock status
  boolean temp = target; //preserve lock status
  target = true; //in case lock was free on call, lock it
  return temp; //return the lock status as it was on call


c: How would you use the test-and-set() instruction to guarantee mutually exclusive access to a
critical section of your code ? (pseudo code is fine)
boolean lock = false; //shared – false  unlocked i.e. c.s. free
while (true)
   while (test_and_set(lock)
  enter critical section
lock = false;

d: The pentium defines an instruction called the “atomic exchange instruction” as follows:
        xchg(a,b) { tmp = a; a = b; b = tmp; } which is guaranteed to be atomic.
Describe how you would guarantee mutually exclusive access to a critical section of your code ?
boolean lock = false; //shared
boolean key = true; //private

do {
         xchg(lock, key)
    } while ( key == true); // key = true  cs is already being accessed

Critical Section;

set_lock(false); //set the lock to false when finished with the cs
Question 3:
You are trying to implement a client-server system based on IPCs, for an online aution
application. The general idea is that the server waits for requests from a client, and on receiving
such a request, forks off a separate (child) process to handle the request.

When put in practice, one problem you are facing is that quite often clients make the request, and
then never respond to further server enquiries. This is usually due to either the requesting person
walking away from the “sale” (human error), or the client machine crashing (or losing network
connectivity) etc., all of which are conditions you have no control over. However, your team has
noticed that, at the end of the day, there are a number of such processes (zombies) sitting around
waiting for clients and using up computer resources and slowing everything down.
The code for the processes has already been written and released and you cannot change the
code for the “child processes”. You can however, change the “parent process” code.

   a. You decide to clean up the “zombie” processes by “killing” them all, every 12 hours.
      Describe what this means? And describe how you would write such a “clean up
      program”? (psuedo code is fine).

       int next_child_id; //the next child we want to check
       int child_id;
       int time_allowed_in_Wait_State; //before we consider it a “zombie”
       int time_wait; //the time the child has been in the wait state


                  sleep(12 hours);
                  next_child_id = 1;
                  child_id = get_next_id(next_child_id++);

                  for ( int j = 1; I <= NoOfChildren)
                             time_wait = get_time_wait(child_id);
                            if (time_wait > time_allowed_in_Wait_State )

                          child_id = get_next_id(next_child_id++);
        }//end for loop
   }//end while loop

   **this can be run by a separate thread in the parent process
    b. Even this is not sufficient, so you decide that every child process will be allowed to run
       only for 5 minutes, at the end of which, if it has not already terminated, the parent
       process will terminate it. Describe how you would do this ? (Parent process, psudeo-code
       is fine).

int child_id;
int next_child;
while (1)
         next_child = 1;
         for ( j = 1; j <= Num_Children; j ++ )
                   child_id = get_next_id(next_child++);
                   if ( ( (get_child_create_time() ) – ( get_current_time) ) > 5_minutes)\
                      abort(child_id); //abort this child

          } //end for loop

}//end while loop

    [Extra Thinking, Do NOT TRY IN CLASS, Ponder at Home] The previous solution is
    considered too drastic and harsh, and your users recommend that they be asked before the
    process is terminated. To make this happen, you plan to “ask” the child process, every 5
    minutes an “are you still working” question, and the child is supposed to return a “yes” if it is,
    and a “no” if it waiting for user input. Try developing the pseudo-code for the parent as well as
    the child to make this happen.

To top