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 »

Advanced Operating Systems Assignment no_02



Ms140400009
Advanced Operating Systems
Assignment no_02
Question 2:
(a)    Briefly explain why AcquireReadLock() and AcquireWriteLock() functions perform P and V operations on the mutex semaphore in the above code?15
Solution:
Mutex semaphore in the above code is a binary semaphore having value 0 or 1. When its value will be 1 then reader or writer will be able to check the condition variables. If its value is 0 then reader or writer will not be able to proceed further. Basically it is used to ensure mutual exclusion. Any reader or writer wants to acquire lock he will first perform P operation on mutex semaphore which will decrease its value to 0. After this operation it will check condition variables no one else will be able to check the condition variables. As value of mutex semaphore is 0 which ensures mutual exclusion. After completing the job reader or writer will perform V operation on mutex semaphore which will increase its value to 1.
(b)   Briefly explain whether readers or writers could be starved due to this implementation?10
Solution:
It can be noted that there is possibility of starvation for writers and readers as well. So there are two possibilities:
In a reader wants to access database then it only checks if there is no active reader. In this way if readers keeps coming on before the release of previous reader then readers  will always hold the database. In this way writers will be starved because for writers to acquire  lock it is condition that there should be no active reader or writer.
When a reader or writer releases a lock, priority is always given to waiting writers. In this way if there are a lot waiting writers then they will always get the chance to access the database and readers will 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.15
Solution:
In the modified code given below there will be only one reader or writer accessing the database at any time. The main point is when a reader will release the lock it will give priority to waiting writer and when a writer will release the lock it will give priority to waiting reader to access database. In this way there will be no starvation. My modified code is given below:
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



voidAcquireReadLock()

{

P(mutex); // remember that P operation on a semaphore means decrementing its value

if ((ACTIVEWRITERS + ACTIVEREADERS== 0)

{

V(OkToRead); // remember that V operation on a semaphore means incrementing its value
ACTIVEREADERS++;

}

else

{

 WAITINGREADERS++;

}

V(mutex);

P(OkToRead);

}

voidReleaseReadLock()

{

P(mutex);

 ACTIVEREADERS--;

if (WAITINGWRITERS > 0)

 {

V(OkToWrite);

 ACTIVEWRITERS++;

 WAITINGWRITERS--;

 }

V(mutex);

}

voidAcquireWriteLock()

{

P(mutex);

if (ACTIVEWRITERS + ACTIVEREADERS == 0)

 {

V(OkToWrite);

 ACTIVEWRITERS++;

 }

else

 {

 WAITINGWRITERS++;

 }

V(mutex);

P(OkToWrite);

}





voidReleaseWriteLock()

{

P(mutex);

 ACTIVEWRITERS--;

if (WAITINREADERS > 0)

 {

V(OkToRead);

 ACTIVEREADERS++;

 WAITINNREADERS--;

 }

V(mutex);

}

} // end of class


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:
Modified code for the above question is given below:
State variables:
 # of active readers -- AR = 0
 # of active writers -- AW = 0
 # of waiting readers -- WR = 0
 # of waiting writers -- WW = 0
 Condition okToRead = NIL
 Condition okToWrite = NIL
 Lock lock = FREE
Code:
Reader() {
lock.Acquire();
while (AW > 0) { // check if safe to read
// if any writers, wait
WR++;
okToRead.Wait(&lock);
WR--;
}
AR++;
lock.Release();
Access DB
lock.Acquire();
AR--;
If (AR == 0 && WR= 0)//if no other readers still
okToWrite->Broadcast(&lock);
lock.Release();
}
Writer() { // symmetrical
lock.Acquire();
while ((AW + AR) > 0) { // check if safe to write
// if any readers or writers, wait
WW++;
okToWrite->Wait(&lock);
WW--;
}
AW++;
lock.Release();
Access DB
// check out
lock.Acquire();
AW--;
if (WR> 0) // give priority to readers
okToRead->Signal(&lock);
else if (WW> 0)
okToWrite->Broadcast(&lock);
lock.Release();
}
Read More »

Complete Solved CS712 – Distributed DBMS Due date: 12-06-2015


Complete Solved by Muhammad Sadaqat Ali

https://www.facebook.com/mscsinterface

Download Click Here
CS712 – Distributed DBMS              Due date: 12-06-2015


Assignment 1




Instructions to Solve Assignments

The purpose of the assignments is to give you hands on practice. It is expected that students will solve the assignments by themselves. Following rules will apply during the evaluation of the assignment.

·        Cheating from any source will result in zero marks in the assignment.
·        Any student found cheating in any two of the assignments submitted during the course will be awarded "F" grade in the course.
·        No assignment after due date will be accepted.



  



Answer the following questions in your own words. Plagiarism will be checked for each question. Marks will be awarded on the basis of answer and plagiarism report.




Question 1                                         (5+15+10 = 30 marks)
Read the paper entitled “A New Technique for Database Fragmentation in Distributed Systems” and answer the following questions.

a.  What are the problems in traditional techniques of fragmentation?
  
Solution: In traditional techniques of fragmentation, following problems are in common:-
1. They  use  frequency  of  queries,  minterm  predicates’ affinity or attribute affinity matrix (AAM) as a basis of fragmentation. These require sufficient empirical data that are not available in most cases at the initial stage.
2. Most of them concentrate only fragmentation problem and overlooked allocation problem to reduce complexity. 

                                          
b.  Elaborate the functionality of the proposed solution with the help of a diagram.
Solution:
Relation: A relation in a database contains different types of attributes those describe properties of the relation. But the important thing is  that the  attributes of a relation do not have same importance with respect to data distribution in different sites. According to above importance we can calculate locality precedence of each attribute for each relation and construct ALP table for the relations.    

MCRUD: Constructed  the MCRUD matrix for requirement analysis phase.

ALP Table: We can calculate  locality precedence of each attribute from the MCRUD matrix.

Predicate Set: Predicate set was generated for the attribute with highest locality precedence of Accounts relation. 

Fragmented Sub-relations:
Using predicate set, keep relations fragmented. 

Allocation:
Allocate  fragments among the sites of the distributed system.

                
c.  Describe the role of ALP matrix and CURD matrix in distributed environment.   

Solution:
Role of ALP matrix in distributed environment:

- Attribute locality precedence (ALP) can be defined as the value of importance  of an attribute with respect to sites of distributed database.
- ALP  table will be constructed by database designer.
-ALP matrix is for each relation of a DDBMS at the time of designing the database with the help of modified CRUD and Cost Function.
- ALP values of all the attributes of relations was computed from its MCRUD matrix.
-The attribute with highest recedence value will be treated as most important  attribute for fragmentation.



Role of CURD matrix in distributed environment:

-Help in Requirement analysis phase
-Enable the functions: Create, Read, Update, Delete   
- A table of which rows indicate attributes of  the  entities  of a relation  and columns indicate different  locations  of the applications.
- It is used by the system analysts and designers in the requirement analysis phase of system development life cycle  for making decision of data mapping to  different  locations.
- We have used MCRUD to generate ALP table for each relation.

Paper link: ijcaonline.org/volume5/number9/pxc3871318.pdf



Question 2                                                                                             (10+10=20 marks)

Read the paper entitled “Fragment Allocation in Distributed Database Design” and answer the following questions.
a.     As database administrator what factors should be kept in mind to resolve the issue of fragmentation allocation optimally?         
Solution:               
As a database administrator, we should consideration following points to resolve the issue of fragmentation allocation optimally:-

(1)   Data is stored close to where it is most frequently used.

(2)   Ensure about transaction can be divided into several sub queries that operate on fragments.

(3)   Global relation should be fragmented.

(4)   Copies of a fragment should be replicated.

(5)   Fragments should be allocated to the sites of the communication network.

(6)   selection of the best execution strategy for request transformation, and allocations of operations to sites.

(7)   Ensure security so that Data not required by local applications is not stored, and consequently not available to unauthorized users.


                                                                                                         
b.     Explain the difference between First and second heuristic algorithms in details.

Solution:

Comparing two heuristic algorithms, we find that Algorithm-1 performed better
Than Algorithm-2 in most cases.

The major difference between Algorithm-1 and Algorithm-2 is the scanning method used to remove a fragment copy from a site.

Algorithm-1 checks whether a fragment copy should be removed from a site in a fragment-by-fragment order.

However, Algorithm-2 checks the removal based on how many data of a fragment copy will be update data site; that is, the more data in a fragment copy is updated, the earlier the fragment copy is removed from a site.

In general, the performance of Algorithm-1 was worse than that of Algorithm-2 only if the total amount of updated data in the fragments scanned later was much more than that in the fragments scanned earlier.

However, this situation did not occur frequently, thus, Algorithm-1 was better than Algorithm-2 most of the time.
From Experimental Results, Algorithm-1 performed better Than Algorithm-2 in most cases.







Paper link:

www.iis.sinica.edu.tw/page/jise/2001/200105_08.pdf






Read More »