CS704 - Advanced Computer Architecture II Solved Mid Term Exam Papers by Muhammad Sadaqat Ali

      

CS704 - Advanced Computer Architecture II Solved Mid Term Exam Papers by Muhammad Sadaqat Ali

Thanks to Allah Almighty whose give me ability to done this.


Dedicated to All Students of MSCS in the name of My Great Brother 

Mr. Muhammad Bilal Ali


COMPLETELY SOLVED IN PDF: TO DOWNLOAD CLICK HERE




Give your feedback at: muhammadsadaqatali@yahoo.com or Comment here

Read More »

CS701 - Theory of Computation Solved Mid Term Exam Papers by Muhammad Sadaqat Ali

      CS701 - Theory of Computation Solved Mid Term Exam Papers by Muhammad Sadaqat Ali

Thanks to Allah Almighty whose give me ability to done this.


Dedicated to All Students of MSCS in the name of My Great Brother 

Mr. Muhammad Bilal Ali


COMPLETELY SOLVED IN PDF: TO DOWNLOAD CLICK HERE

TO ORDER HARD COPY CLICK HERE



Give your feedback at: muhammadsadaqatali@yahoo.com or Comment here

Read More »

Mid Term Papers

More Links: 1 2  3  4  5  6   7   8   9   10    11  12   13   14   15   16  17  More Past Papers Will be Uploaded before Mid Term Exam. So Wait Please. Hope Complete Solved Past Papers will also be uploaded before Mid Term Exam. Regards.


Download Click here
Read More »

Software Engineering by Ian Somerville

                    https://www.facebook.com/mscsinterface

Software Engineering by Ian Somerville

Click here to download




Read More »

CS703 – ADVANCED OPERATING SYSTEMS MS (CS), SPRING 2015 ASSIGNMENT NO. 2 SOLVED BY Muhammad Sadaqat Ali Due Date: 11th June, 2015


CS703 – ADVANCED OPERATING SYSTEMS
MS (CS), SPRING 2015
ASSIGNMENT NO. 2                                                                            Due Date: 11th June, 2015
SOLVED BY
 Muhammad Sadaqat Ali

 
Question 1                        
Reader/writer locks are specialized locks used to solve the readers/writers problem. Consider the
following pseudo-code implementation of reader-writer locks which is a variant of the
readers/writers solution discussed in lectures but implemented via semaphores. Note that readers
must call the function AcquireReadLock before reading the data while writers must call
AcquireWriteLock before modifying or updating the data. Once data access has been completed, the
locks must be released by calling the ReleaseReadLock() and ReleaseWriteLock() functions
respectively :
Class ReaderWriterLock {
Semaphore mutex = 1;   // declaration of a semaphore
OkToRead = 0; // a flag to check it is OK to read the database
OkToWrite = 0;  // a flag to check it it is OK to write the database

int ACTIVEREADERS=0;// number of readers holding the read lock and accessing the database
WAITINGREADERS=0; // number of readers waiting to acquire read lock
ACTIVEWRITERS=0; // number of writers that have acquired write lock
                                          // (practically this will always be 1)
WAITINGWRITERS=0; // number of writers that are waiting for write lock

void AcquireReadLock() 
{
P(mutex); // remember that P operation on a semaphore means decrementing its value
if ((ACTIVEWRITERS == 0) 
{
   V(OkToRead); // remember that V operation on a semaphore means incrementing its value  
ACTIVEREADERS++;
} 
else 
{ 
    WAITINGREADERS++;
} 
V(mutex);
P(OkToRead);
}
void ReleaseReadLock() 
{
   P(mutex);
   ACTIVEREADERS--;
   if ((ACTIVEREADERS == 0) && (WAITINGWRITERS > 0)) 
   {
      V(OkToWrite);
      ACTIVEWRITERS++; 
      WAITINGWRITERS--;
   }
   V(mutex);
}
void AcquireWriteLock() 
{
   P(mutex);
   if (ACTIVEWRITERS + ACTIVEREADERS == 0) 
   {
     V(OkToWrite);
     ACTIVEWRITERS++;
   } 
   else 
   {
     WAITINGWRITERS++;
   }
   V(mutex);
   P(OkToWrite);
}
void ReleaseWriteLock() 
{
  P(mutex);
  ACTIVEWRITERS--;
  if (WAITINGWRITERS > 0) 
  {
     V(OkToWrite);
     ACTIVEWRITERS++; 
     WAITINGWRITERS--;
  } 
  else 
  {
     while (WAITINGREADERS > 0) 
     {
        V(OkToRead);
        ACTIVEREADERS++; 
        WAITINGREADERS--;
     }
  }
  V(mutex);
}
} // end of class
a)
Briefly explain why AcquireReadLock() and AcquireWriteLock() functions perform P and V
operations on the mutex semaphore in the above code? 

Solution:
A semaphore is a protected variable whose value can be accessed and altered only by the operations P and V.
When semaphore operations has started, no other process can access the semaphore until operation has completed. Mutual exclusion on the semaphore is enforced within P and V.
P semaphore function signals that the task requires a resource and if not available waits for it.
V semaphore function signals which the task passes to the OS that the resource is now free for the other users.
In the above code, P and V semaphore functions used with mutex property.
For AcquireReadLock() :
P and V Semaphore functions with mutex property ─ Wait for starting Critical Section
P(mutex); // remember that P operation on a semaphore means decrementing its value
 The codes will execute only when mutex not less than 0. it keep wait until the resource become available for further processing.
P and V Semaphore functions with mutex property ─ running end exiting Critical Section
V(OkToRead); // remember that V operation on a semaphore means incrementing its value  
 if OkToRead not equal to 0 or not less than 0. No other process is executing at present. Now reader can access DB.
V(mutex);
mutex not equal to 0 or not less than 0. No processor (reader) process executing at present.
P(OkToRead);
The following codes will execute only when OkToRead not less than 0. Wait for Reader.
For AcquireWriteLock():
P(mutex);
The codes will execute only when mutex not less than 0. it keep wait until the resource become available for further processing.
V(OkToWrite);
if OkToWrite not equal to 0 or not less than 0. No other process is executing at present. Now writer can access DB.
V(mutex);
mutex not equal to 0 or not less than 0. No processor (writer) process executing at present.
P(OkToWrite);
The following codes will execute only when OkToWrite not less than 0. Wait for Writer.


(b)                      
Briefly explain whether readers or writers could be starved due to this implementation?
     Solution:
In this code, Writer could be starved as the priority is given to Reader.
In the function, void ReleaseWriteLock(), implementation for WaitingReader gives access to ActiveReader and if waitingwriter available, only access given to ActiveWriter and then instead of accessing DB to waiting writer access is given to Reader.
Thus, Writer could be starved.


c) Suggest a mechanism through which a no starvation policy could be implemented. In other
words, suggest in words, how would you modify the code such that a starvation-free
implementation results.                                
Solution:
To get starvation free implementation results, we can use any scheduling algorithm as the task of the scheduler is to find a conflict free matching based on input requests.
Four major scheduling algorithm are:
First Come First Serve FCFS Scheduling
Shortest Job First Scheduling
Priority Scheduling
Round Robin Scheduling
Multilevel Queue Scheduling
All these algorithm have their own advantages and disadvantages.

Question 2                   
Review the Readers/Writers problem discussed in lecture 12, write the code for Reader() and
Writer() functions, when readers are given priority over writers, keeping the problem constraints in
mind.
Solution:
Reader () {
 lock.Acquire();
 while (AW > 0) {
  WR++;
  okToRead.wait(&lock);
  WR--;
 }
 AR++;
 lock.Release();
 Access DB
 lock.Acquire();
 AR--;
 If (WR > 0) {
  okToRead.Broadcast(&lock);
 } else if (AR == 0 & WW > 0) {
   okToWrite.Signal(&lock);
  }
 lock.Release();
}

Writer () {
 lock.Acquire();
 while ((AR + WR + AW) > 0) {
  WW++;
  okToWrite.Wait(&lock);
  WW--;
 }
 AW++;
 lock.Release();
 Access DB
 lock.Acquire();
 AW--;
 If (WR > 0) 
  okToRead.Broadcast(&lock);
 else if (WW > 0)
  okToWrite.Signal(&lock)
 lock.Release();
}
Read More »

CS703 - Assignment No 2




UNIVERSITY OF WISCONSIN-MADISON
Computer Sciences Department


                        
CS 537 
Spring 2000 
               CS703 - Assignment No 2 
A. Arpaci-Dusseau

Solutions to Quiz #3: Wednesday, February 23

Readers and Writers

The following code implements the readers-writers problem with semaphores. It should be identical to that given to you in the Lecture Notes (no tricks here).
The main thread of the program (not shown) creates many reader and writer threads, starts each of them, and then waits forever for them to terminate by calling each thread's join() method.
 
class ReaderOrWriter implements Runnable {
     private static int sharedBuffer[] = new int[1000];     
 
     private static Semaphore mutex = new Semaphore(??);
     private static Semaphore OKToRead = new Semaphore(??);
     private static Semaphore OKToWrite = new Semaphore(??);
     private static int ActiveWriters = 0; 
     private static int WaitingWriters = 0;
     private static int ActiveReaders = 0; 
     private static int WaitingReaders = 0;
 
     public static final int READER = 0; 
     public static final int WRITER = 1;
 
     private int whichAmI;
ReaderOrWriter(int ReaderOrWriter) {
    whichAmI = ReaderOrWriter;
}

public void run() {
    while (true) {
        if (whichAmI == READER) {
             reader();
        } else {
             writer();
        }
    }
}

private void reader() {
 
     mutex.P();
     if ((ActiveWriters + WaitingWriters) == 0){
         OKToRead.V();
         ActiveReaders++;
     } else {
         WaitingReaders++;
     }
     mutex.V();
     OKToRead.P();
 
     // Perform read of sharedBuffer
 
     mutex.P();
     ActiveReaders--;
     if ((ActiveReaders == 0) && (WaitingWriters > 0)) {
          OKToWrite.V();
          ActiveWriters++;
          WaitingWriters--;
     }
     mutex.V();
}

private void writer() {
 
     mutex.P();
     if ((ActiveWriters + ActiveReaders + WaitingWriters) == 0) {
          OKToWrite.V();
          ActiveWriters++;
     } else {
          WaitingWriters++;
     }
     mutex.V();
     OKToWrite.P();
 
     // Perform write of dataBuffer 
 
     mutex.P();
     ActiveWriters--;
     if (WaitingWriters > 0) {
         OKToWrite.V();
         ActiveWriters++;
         WaitingWriters--;
     } else {
         while (WaitingReaders > 0) {
             OKToRead.V();
             ActiveReaders++;
             WaitingReaders--;
         }
     }
     mutex.V();
}

You should assume that semaphores have been implemented in Java in a reasonable way. This could be done by building on synchronized methods. You should assume that all semaphores are implemented with a First-in-First-out (FIFO) list of processes; that is, the process that has been waiting the longest for a semaphore will be the next one woken up.
You need to answer five questions about this code segment. Each will be worth the same number of points. No partial credit will be given for a question, so just state your final answer -- no need to explain your reasoning.
  1. What values should each of the semaphores be initialized to?
    a) mutex: 1
    All semaphores used for mutual exclusion are always initialized to 1, representing the number of threads that can be in the critical section.
b) OKToRead: 0
OKToRead is set to the number of additional reader threads that are allowed to access the shared buffer. It starts out at zero, and then once a thread checks and sees that it can read the buffer, it increments the semaphore count by calling V().
c) OKToWrite: 0
OKToWrite is set to the number of writer threads that are allowed to access the shared buffer (it can only be 0 or 1). It starts out at zero, and then once a thread checks and sees that it can write the buffer, it increments the semaphore count by calling V().
  1. a) Assume a reader is currently using the buffer.
    If there are both waiting writers and waiting readers, will a writer or a reader get access to the buffer first?
No matter what type of thread is accessing the buffer, this code gives priority to writers. Therefore a writer will always get access to the buffer first. A reader can't access the buffer if there are any waiting writers.
b) Assume a writer is currently using the buffer.
If there are both waiting writers and waiting readers, will a writer or a reader get access to the buffer first?
As above, a writer will get access next.
  1. a) Is it possible for readers to starve with this code?
Yes, since writers always get priority, it is possible a reader will always have to wait for a continuous stream of writers to finish.
b) Is it possible for writers?
No, it is not possible. First, waiting readers can't prevent a waiting writer from accessing the buffer as explained in question 2. Second, because the list of waiting writers on the semaphore, OKToWrite, is kept in FIFO order, every waiting writer thread is guaranteed to be eventually woken up and scheduled.
  1. a) Can the semaphore OKToRead have a value greater than 1?
Yes, it will get incremented to the number of readers that can still access the buffer, but haven't yet. Multiple readers can access the buffer simultaneously (as long as there aren't any waiting writers).
b) Can OKToWrite?
No, there can be only one writer at a time.
  1. Assume there are many readers currently accessing the buffer.
    a) Is the first writer to execute mutex.P() guaranteed to be the first writer to use the buffer?
No, there could be a context-switch between the time when the writer releases the mutex and when it calls OKToWrite.P(); if a second writer is scheduled at this point, it may sneak in and execute all of the code between mutex.P() and mutex.V() and be the first process to wait on OKToWrite.P().
b) Is the first writer to execute OKToWrite.P() guaranteed to be the first writer to use the buffer?
Yes, because the semaphore is implemented with a FIFO list of waiting processes, the first process to wait will be the first process woken up by OKToWrite.V().

Read More »