• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
computer notes - Memory organization
 

computer notes - Memory organization

on

  • 2,632 views

 

Statistics

Views

Total Views
2,632
Views on SlideShare
2,632
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    computer notes - Memory organization computer notes - Memory organization Document Transcript

    • ecomputernotes.com Data Structures Lecture No. 09___________________________________________________________________Data StructuresLecture No. 09Memory OrganizationBy the end of last lecture, we discussed the uses of stack to develop a process from anexecutable file and then in function calls. When you run an executable, the operatingsystem makes a process inside memory and constructs the followings for that purpose.- A code section that contains the binary version of the actual code of the program written in some language like C/C++- A section for static data including global variables- A stack and- Finally, a heap Page 1 of 12
    • ecomputernotes.com Data Structures Lecture No. 09___________________________________________________________________Stack is used in function calling while heap area is utilized at the time of memoryallocation in dynamic manner. Process 1 (Browser) Code Process 3 Static Data (Word) Process 4 Stack (Excel) Process 2 (Dev-C++) Heap Windows OS Fig 1. Memory OrganizationStack Layout during a Function Call Parameters (F) Parameters (F) Parameters (F) Local variables (F) Local variables (F) Local variables (F) Return address (F) Return address (F) Return address (F) sp Parameters (G) Parameters (G)sp Local variables (G) After Call At point of call Return address (G) sp During Execution of G Fig 2: Stack Layout; When function F calls function GThe above diagrams depict the layout of the stack when a function F calls a function G.Here sp stands for stack pointer. At the very left, you will find the layout of the stack justbefore function F calls function G. The parameters passed to function F are firstlyinserted inside the stack. These are followed by the local variables of the function F andfinally the memory address to return back after the function F finishes. Just beforefunction is made to the function G, the parameters being passed to the function G, areinserted into the stack. Page 2 of 12
    • ecomputernotes.com Data Structures Lecture No. 09___________________________________________________________________In the next diagram, there is layout of the stack on the right side after the call to thefunction G. Clearly, the local variables of the function G are inserted into the stack afterits parameters and the return address. If there are no local variables for a function, thereturn address is inserted (pushed) on to the stack.The layout of the stack, when the function G finishes execution is shown on the right.You can see that the local variables of function G are no more in the stack. They havebeen removed permanently along with the parameters passed to the function G. Now, itis clear that when a function call is made, all local variables of the called function and theparameters passed to it, are pushed on to the stack and are destroyed, soon after the thecompletion of the called function s execution.In C/C++ language, the variables declared as static are not pushed on the stack. Rather,these are stored in another separate section allocated for static data of a program. Thissection for global or static data can be seen in the fig 1 of this lecture. It is not destroyedtill the end of the process s execution. If a variable, say x is declared as static insidefunction G, x will be stored in the static data section in the process s memory. Whereas,its value is preserved across G function calls. The visibility of x is restricted to thefunction G only. But a static variable declared as a class data is available to all memberfunctions of the class and a static variable declared at global scope (outside of any classor function body) is available to all functions of the program.Now, let s move on to another data structure called queue.QueuesA queue is a linear data structure into which items can only be inserted at one end andremoved from the other. In contrast to the stack, which is a LIFO (Last In First Out)structure, a queue is a FIFO (First In First Out) structure.The usage of queue in daily life is pretty common. For example, we queue up whiledepositing a utility bill or purchasing a ticket. The objective of that queue is to servepersons in their arrival order; the first coming person is served first. The person, whocomes first, stands at the start followed by the person coming after him and so on. At theserving side, the person who has joined the queue first is served first. If the requirement isto serve the people in some sort of priority order, there is a separate data structure thatsupports priorities. The normal queue data structure, presently under discussion, onlysupports FIFO behavior.Now, let s see what are the operations supported by the queue.Queue OperationsThe queue data structure supports the following operations: Operation Description enqueue(X) Place X at the rear of the queue. dequeue() Remove the front element and return it. front() Return front element without removing it. isEmpty() Return TRUE if queue is empty, FALSE otherwise Page 3 of 12
    • ecomputernotes.com Data Structures Lecture No. 09___________________________________________________________________Implementing QueueThere are certain points related to the implementation of the queue. Suppose we areimplementing queue with the help of the linked -list structure. Following are the keypoints associated with the linked list implementations:- Insert works in constant time for either end of a linked list.- Remove works in constant time only.- Seems best that head of the linked list be the front of the queue so that all removes will be from the front.- Inserts will be at the end of the list. front rearfront rear 1 7 5 1 7 5 2 Fig 3. Queue implementation using linked listThe above figure shows queue elements on the left with two pointers front and rear. Thisis an abstract view of the queue, independent of its implementation method of array orlinked list. On the right side is the same queue ,using linked list and pointers of front andrear. When dequeue() function is called once, the front element 1 is removed. The pictureof the queue showing one element removal is also depicted below. Note that frontpointer has been moved to the next element 7 in the list afer removing the front element1.After dequeue() is called once front rear front rear 7 5 2 1 7 5 2 Fig 4. Removal of one element from queue using dequeue()Now at this stage of the queue, we will call enqueue (9) to insert an element 9 in it. . Thefollowing figure shows that the new element is inserted at the rear end and rear pointerstarts pointing this new node with element 9.At this point of time, the code of these functions of dequeue() and enqueue() should notbe an issue. Page 4 of 12
    • ecomputernotes.com Data Structures Lecture No. 09___________________________________________________________________ Queue after enqueue(9) call front rear front rear 7 5 2 9 7 5 2 9 Fig 5. Insertion of one element using enqueue(9)Note that in this queue data structure, the new elements are inserted at rear end andremoved from the front. This is in contrast to stack structure where the elements areinserted and removed from the same end.Let s see the code for queue operations: /* Remove element from the front */ 1. int dequeue() 2. { 3. int x = front->get(); 4. Node* p = front; 5. front = front->getNext(); 6. delete p; 7. return x; 8. } /* Insert an element in the rear */ 9. void enqueue(int x) 10. { 11. Node* newNode = new Node(); 12. newNode->set(x); 13. newNode->setNext(NULL); 14. rear->setNext(newNode); 15. rear = newNode; 16. }In dequeue() operation, at line 3, the front element is retrieved from the queue andassigned to the int variable x.In line 4, the front pointer is saved in Node pointer variable p.In line 5, the front pointer is moved forward by retrieving the address of the next node byusing front->getNext() and assigning it to the front pointer.In line 6, the node pointed to by the front pointer is deleted by using delete frontstatement.At the end of dequeue() implementation, the value of deleted node that was saved in theint variable x, is returned back.The enqueue(int ) is used to add an element in the queue. It inserts the element in the rear Page 5 of 12
    • ecomputernotes.com Data Structures Lecture No. 09___________________________________________________________________of the queue. At line 11, a new Node object is created using the new Node() statement andthe returned starting address of the created object is assigned to the newNode pointervariable.In line 12, the value of the passed in parameter x, is set in the newly created node objectusing the set() method.In line 13, the next pointer in the newly created node is set to NULL.In line 14, the newly created node is set as the next node of the node currently pointed bythe rear pointer.Ine line 15, the rear pointer is set to point to the newly created node.The code of two smaller functions is as under: /* To retrieve the front element */ int front() { return front->get(); } /* To check if the queue is empty */ int isEmpty() { return ( front == NULL ); }The front() method is used to retrieve the front element. This is the oldest elementinserted in the queue. It uses the get() method of the Node class.The isEmpty() method is used to check whether the queue is empty or not. It checks theaddress inside the front pointer, if it is NULL. It will return true indicating that the queueis empty or vice versa.While studying stack data structure, we implemented it by using both array and linkedlist. For queue, until now we have been discussing about implementing queue usinglinked list. Now, let s discuss implementing queue with the help of an array.Queue using ArrayA programmer keeps few important considerations into view account beforeimplementing a queue with the help of an array:If we use an array to hold the queue elements, both insertions and removal at the front(start) of the array are expensive. This is due to the fact that we may have to shift up to n elements.For the stack, we needed only one end but for a queue, both are required. To get aroundthis, we will not shift upon removal of an element. Page 6 of 12
    • ecomputernotes.com Data Structures Lecture No. 09___________________________________________________________________front real 1 7 5 2 1 7 5 2 0 1 2 3 4 5 6 7 front rear 0 3 Fig 6. Queue implemented using an arrayIn the above figure, queue implementation using array is shown. As the array size is 8,therefore, the index of the array will be from 0 to 7. The number of elements inside arrayare 1, 7, 5 and 2, placed at start of the array. The front and rear in this implementation arenot pointers but just indexes of arrays. front contains the starting index i.e. 0 while rearcomprises 3.Let s see, how the enqueue() works: enqueue(6)front real 1 7 5 2 6 1 7 5 2 6 0 1 2 3 4 5 6 7 front rearFig 7. Insertion of one element 6 0 4As shown in the above diagram, an element i.e. 6 has been inserted in the queue. Now,the rear index is containing 4 while the front has the same 0 index. Let s see the figureof the array when another element 8 is inserted in the queue. enqueue(8) front real 1 7 5 2 6 8 1 7 5 2 6 8 0 1 2 3 4 5 6 7 front rear Fig 8. Insertion of another element 8 0 5When an element is removed from the queue. It is removed from the front index. Page 7 of 12
    • ecomputernotes.com Data Structures Lecture No. 09___________________________________________________________________ dequeue( ) front real 7 5 2 6 8 7 5 2 6 8 0 1 2 3 4 5 6 7 front rear Fig 9. Removal of an element from front 1 5After another call of dequeue() function: dequeue( ) front real 5 2 6 8 5 2 6 8 0 1 2 3 4 5 6 7 front rear 2 5 Fig 10. Removal of another element from frontWith the removal of element from the queue, we are not shifting the array elements. Theshifting of elements might be an expensive exercise to perform and the cost is increasedwith the increase in number of elements in the array. Therefore, we will leave them as itis. enqueue(9) enqueue(12) front real 5 2 6 8 9 12 5 2 6 8 9 12 0 1 2 3 4 5 6 7 front rear 2 7 Fig 11. Insertion of elements in the queue Page 8 of 12
    • ecomputernotes.com Data Structures Lecture No. 09___________________________________________________________________After insertion of two elements in the queue, the array that was used to implement it, hasreached its limit as the last location of the array is in use now. We know that there issome problem with the array after it attained the size limit. We observed the similarproblem while implementing a stack with the help of an array.We can also see that two locations at the start of the array are vacant. Therefore, weshould can consider how to use those locations appropriately in to insert more elementsin the array.Although, we have insert and removal operations running in constantly, yet we created anew problem that we cannot insert new elements even though there are two placesavailable at the start of the array. The solution to this problem lies in allowing the queueto wrap around.How can we wrap around? We can use circular array to implement the queue. We knowhow to make a linked list circular using pointers. Now we will see how can we make acircular array. 0 1 frontfront rear 7 2 2 12 5 5 2 6 8 9 12 6 9 2 8 6 3 rear Fig 12. Circular array to implement queue 7 5 4The number of locations in the above circular array are also eight, starting from index 0to index 7. The index numbers are written outside the circle incremented in the clock-wise direction. To insert an element 21 in the array , we insert this element in thelocation, which is next to index 7. enqueue(21) 0 1 front size front rear 21 7 2 2 8 12 5 5 2 6 8 9 12 21 6 9 2 8 6 3 rear noElements Fig 13. An element added in circular array 0 7 5 4Now, we will have to maintain four variables. front has the same index 2 while the, size is8. rear has moved to index 0 and noElements is 7. Now, we can see that rear index hasdecreased instread of increasing. It has moved from index 7 to 0. front is containing index2 i.e. higher than the index in rear. Let see, how do we implement the enqueue()method. Page 9 of 12
    • ecomputernotes.com Data Structures Lecture No. 09___________________________________________________________________ void enqueue( int x) { 1. rear = (rear + 1) % size; 2. array[rear] = x; 3. noElements = noElements + 1; }In line 1 of the code, 1 is added in rear and the mod operator (that results in remainder ofthe two operands) is applied with size variable. This expression on the right ofassignment in line 1 can result from 0 to 7 as size is containing value 8. This operatorensures that value of this expression will always be from 0 to 7 and increase or decreasefrom this. This resultant is assigned to the rear variable.In line 2, the x (the value passed to enqueue() method to insert in the queue) is inserted inthe array at the rear index position. Therefore, in the above case, the new element 21 isinserted at index 0 in the array.In line 3, noElements is added to accumulate another element in the queue.Let s add another element in the queue. enqueue(7) 0 1 front sizefront rear 21 7 7 2 2 8 12 5 5 2 6 8 9 12 21 7 6 9 2 8 6 3 rear noElements 1 8 5 4Fig 14. Another element added in circular arrayNow, the queue, rather the array has become full. It is important to understand, that queuedoes not have such characteristic to become full. Only its implementation array hasbecome full. To resolve this problem, we can use linked list to implement a queue. Forthe moment, while working with array, we will write the method isFull(), to determinethe fullness of the array. int isFull() { return noElements == size; } int isEmpty() { return noElements == 0; } Page 10 of 12
    • ecomputernotes.com Data Structures Lecture No. 09___________________________________________________________________isFull() returns true if the number of elements (noElements) in the array is equal to thesize of the array. Otherwise, it returns false. It is the responsibility of the caller of thequeue structure to call isFull() function to confirm that there is some space left in thequeue to enqueue() more elements.Similarly isEmpty() looks at the number of elements (noElements) in the queue. If thereis no element, it returns true or vice versa..Let s see the dequeue() method. 0 1 dequeue() front size front rear 21 7 7 2 4 8 12 6 8 9 12 21 7 6 9 8 6 3 rear noElements 1 8 5 4 Fig 15. Element removed from the circular array int dequeue() { int x = array[front]; front = (front + 1) % size; noElements = noElements - 1; return x; }In the first line, we take out an element from the array at front index position and store itin a variable x. In the second line, front is incremented by 1 but as the array is circular,the index is looped from 0 to 7. That is why the mod (%) is being used. In the third line,number of elements (noElements) is reduced by 1 and finally the saved array element isreturned.Use of QueuesWe saw the uses of stack structure in infix, prefix and postfix expressions. Let s see theusage of queue now.Out of the numerous uses of the queues, one of the most useful is simulation. Asimulation program attempts to model a real-world phenomenon. Many popular videogames are simulations, e.g., SimCity, Flight Simulator etc. Each object and action in thesimulation has a counterpart in the real world. Computer simulation is very powerful tooland it is used in different high tech industries, especially in engineering projects. Forexample, it is used in aero plane manufacturing. Actually Computer Simulation is full-fledged subject of Computer Science and contains very complex Mathematics,sometimes. For example, simulation of computer networks, traffic networks etc. Page 11 of 12
    • ecomputernotes.com Data Structures Lecture No. 09___________________________________________________________________If the simulation is accurate, the result of the program should mirror the results of thereal-world event. Thus it is possible to understand what occurs in the real-world withoutactually observing its occurrence.Let us look at an example. Suppose there is a bank with four tellers.A customer enters the bank at a specific time (t1) desiring to conduct a transaction.Any one of the four tellers can attend to the customer. The transaction (withdraws,deposit) will take a certain period of time (t2). If a teller is free, the teller can process thecustomer s transaction immediately and the customer leaves the bank at t1+t2. It ispossible that none of the four tellers is free in which case there is a line of customers ateach teller. An arriving customer proceeds to the back of the shortest line and waits forhis turn. The customer leaves the bank at t2 time units after reaching the front of the line.The time spent at the bank is t2 plus time waiting in line.So what we want to simulate is the working environment of the bank that there arespecific number of queues of customers in the bank in front of the tellers. The tellers areserving customers one by one. A customer has to wait for a certain period of time beforehe gets served and by using simulation tool, we want to know the average waiting time ofa bank customer. We will talk about this simulation in the next lecture and will do codingalso in order to understand it well. Page 12 of 12