You are on page 1of 3

National University of Computer and Emerging Sciences, Lahore Campus

Course: Operating System Course Code: CS-205


Program: BS(Computer Science) Semester: Fall 2017
Duration: 3 hour Total Marks: 54
Paper Date: 27th December, 2017 Weight: 45%
Section: Page(s): 3
Exam: Final Roll No.
Instructions/Notes: Answer questions on the question paper. Write answers clearly and precisely, if the answers are not
easily readable then it will result in deduction of marks. Use extra sheet for rough work, cutting and blotting on this sheet
will result in deduction of marks.

Question 1 (10 points): Although practically it is impossible to implement shortest job first algorithm, but if we had
following class implementations, we could easily implement SJF. So lets do it. Hint: read the declarations carefully!

class List { // it is the list of all p r o c e s s e s ready to run .


public :
bool addToList ( int element ) ; // adds a process d e s c r i b e d by element to the list .
Return value is not used here .
bool removeFromList ( int element ) ; // removes a process d e s c r i b e d by element to the
list . Returns value is not used here .
};
class Iterator {
public :
Iterator ( List list ) ; // i n i t i a l i z e s the iterator with the list .
int getNext () ; // iterates over the process list , just like an iterator in STL .
Returns -1 when reaches the end . Othewise returns the PID and moves next .
friend class List ;
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
double g e t P r o c e s s R e m a n i n g T i m e ( int pid ) // returns the time the process , d e s c r i b e d by the pid ,
will take in the next burst . Based on the return value of this fucntion we can make
decisions .

int ge t N e x t P r o ce s s T o R u n ( int leavingProcessID , List list ) // First p a r a m e t e r is the process


which is leaving the CPU . Second the list of ready p r o c e s s e s . The function returns the ID
of the process to run next .
{
Iterator iter = new Iterator ( list ) ;
int pid = -1;
int nextPID = leavingProcessID ;
double remainingTime = DBL_MAX ;
double thisTime = 0;
while (( pid = Iterator . getNext () ) > -1)
{
if (( thisTime = g e t P r o c e s s R e m a n i n g T i m e ( pid ) ) < remainingTime )
{
nextPID = pid ;
remainingTime = thisTime ;
}
}
if ( pid > -1)
{
list . removeFromList ( nextPID ) ;
// list . a d d T o L i s t ( l e a v i n g P r o c e s s I D ) ; that is error , as SJF is non - p r e e m p t i v e
}
return nextPID ;
}

School of Computer Science Page 1 of 3


Question 2 (10 points): Implement a function which takes the logical address and returns the physical address. Use the
functions provided below. Hint: read the declarations carefully!

int getPageNumber ( int logicalAddress ) ; // takes logical address and returns the a s s o c i a t e d
page number .
int getFrameNumber ( int pageNumber ) ; // takes the p a g e n u m b e r and returns the a s s o c i a t e d frame
number .
int loadPageIn Memory ( int pageNumber ) ; // loads a page from backing store into the physical
memory , and return the f r a m e n u m b e r where the page was loaded .
void setFrameNumber ( int pagenumber , int framenumber ) ; // sets the f r a m e n u m b e r of the
p a g e n u m b e r . Also sets all relevant bits of the page table .
int r e p l a c e P a g e B y F r a m e N u m b e r ( int logicaladdress , int framenumber ) ; // converts the
l o g i c a l a d d r e s s into a physical address by r e p l a c i n g the page number by frame number .

int ge tP h ys i ca lA d dr e ss ( int logicalAddress )


{
int pn = getPageNumber ( logicalAddress ) ;
int fn = -1;
if ( getFrameNumber ( pn ) < 0)
{
int fn = loadPageInMemory ( pn ) ;
setFrameNumber ( pn , fn ) ;
}
if ( fn == -1)
{
// PANIC
}
return r e p l a c e P a g e B y F r a m e N u m b e r ( logicaladdress , fn ) ;

Question 3 (10 points): Get the physical byte stored in a file which exists in a file system that uses single indexed table.
Parameters are the logical address of the byte, and the file ID.

# define BLOCK_SIZE xxxx ; // tells how many bytes are there in one block

int ge t I n d e x B l oc k N u m b e r ( int fileID ) ; // takes the file ID and returns the block where index
table is stored .
int * lo ad I nd ex F ro m Bl oc k ( int blockNumber ) ; // takes the block number and loads the index table
in memory and returns its pointer .
byte * lo ad B yt es F ro m Bl oc k ( int blockNumber ) ; // takes the block number and loads raw bytes in
that block in memory , and returns its address .

int getByte ( int logicalByteNumber , int fileID )


{
int lo gi c al B lo ck N um be r = logi cal Byt eNu mber / BLOCK_SIZE ;
int offset = logi cal Byt eNu mbe r % BLOCK_SIZE ;
int indexNumber = ge t I n d e x B l oc k N u m b e r ( fileID ) ;
int * index = lo ad I nd e xF ro m Bl o ck ( indexNumber ) ;
byte * bs = lo ad B yt e sF ro m Bl o ck ( index [ lo gi c al B lo ck N um be r ]) ;
return bs [ offset ];
}

School of Computer Science Page 2 of 3


Question 4 (10 points): Implement the optimal page replacement algorithm using following functions Hint: read the
declarations carefully!

Class List ; // the same class d e f i n i t i o n given in Question 1


Class Iterator ; // the same class d e f i n i t i o n given in Question 1
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int getNextOcc urence ( int pageNumber ) ; // returns the position of next o c c u r e n c e of the
p a g e N u m b e r in the r e f e r e n c e string .
List getPageList () ; // returns the list of all pages loaded in the memory .

int getPageToR eplace ()


{
List l = getPageList () ;
Iterator iter = new Iterator ( l ) ;
int pos = 0;
int page = -1;
int pageToReplace = -1;
while (( page = iter . getNext () ) > -1)
{
if ( ( thisPos = getNextOccurence ( page ) ) > pos )
{
pageToReplace = page ;
pos = thisPos ;
}
}
return pageToReplace ;
}

Question 5 (6 points): List any three conditions which need to be true for a deadlock to occur.

1. 3.
2.

Question 6 (4 points): In deadlock avoidance algorithms, deadlocks are possible structurally, but we keep a gaurd and
do not let all those conditions to be true that can result into a deadlock.

1. True 2. False

Question 7 (4 points): In deadlock prevention algorithms, deadlocks are structurally not possible.

1. True 2. False

School of Computer Science Page 3 of 3

You might also like