You are on page 1of 64

Chapter 4

Recursion: Writing a recursive function, Flow of control in recursive functions, Winding and unwinding phase,
Recursive data structures, Implementation of recursion. Tail recursion. Indirect and Direct Recursion.

Storage Management: Sequential Fit Methods: First Fit, Best Fit and Worst Fit methods. Fragmentation, Freeing Memory,
Boundary Tag Method. Buddy Systems: Binary Buddy System, Fibonacci Buddy System. Compaction, Garbage Collection.
Recursion
Recursion is a process in which a problem is define in terms of itself.
The problem is solved by repeatedly breaking it into smaller problems ,which are similar in nature to the original problems
The smaller problem are solved and their solutions are applied to get the final solution of the original problem.

To implement recursion technology in programming , a function should be capable of calling itself.

A recursive fun is a fun that calls itself.

Main()
{
rec();
}//end of the main
Void rec()
(
rec(); recursive call
)// end of recursive call
Recursion

➢ Here the rec() is calling itself inside its own fun body, so rec() is a recursive fun. ‘
➢ When main() calls rec(), the code of rec() will be executed and since there is call of rec bracket inside rec() bracket,
again rec() bracket will be executed.
➢ It seems that this process will go on infinitely but in practice , a terminating condition is written inside the recursive
fun which ends this recursion.
➢ This terminating condition is also known as exit condition or the base case. This is the case when fun will stop calling
itself and will finally starts returning.
➢ Recursion proceeds by repeatedly breaking a problem into smaller version of the same problem , till finally we get
the smallest version of the problem which is simple enough to solve.
➢ The smallest version of problem can be solved without rec and this is actually the base case.
Writing a recursive function

Recursion is actually a way of thing about problem so before writing a recursive function for a problem we should be
able to define the sol of the problem in terms of similar types of smaller prob.
The two main steps in writing recursive fun are
1 identification of the base case: its solution I,e, the case where solution can be achieved without recursion.
There may be more than one base case.
2 Identification the general case all the rec case that is the case in which the rec call will be made , identifying the base
case in very important bcause w/o it in the fun will keep on calling itself resulting in infinite rec.

We must ensure that each recursive call takes us closer to the base case ti.e. size of prob shd be diminished at each rec
call. The rec call shd be made in such a way that finally we arrive at the base case.
If we don’t do so we will hv infinite recursion. So merely defining a base case will not help us avoid infinite recursion, we
shd implement the fun such a that the base case is finally reached.
Flow of control in recursive functions
Flow of control in recursive call
Winding and unwinding phase
Example of recursion : Factorial of a number:
Finding Factorial of a number recursively
Recursive Data Structure
Strings and recursion
Linked List and recursion
Implementation of recursion
Tail recursion
Indirect and Direct Recursion
Storage Management:
➢ Sequential Fit Methods: First Fit, Best Fit and Worst Fit methods.
➢ Fragmentation, Freeing Memory, Boundary Tag Method.
➢ Buddy Systems: Binary Buddy System, Fibonacci Buddy System.
➢ Compaction, Garbage Collection.
Storage Management
Work of OS to provide the specified memory to user and manage allocation and release process.
Dynamic memory allocation requires allocation and release of different size of memory at different times.
Many applications might be running on a system and they can request for different size memory.
The memory management of system of the OS is responsible for managing the memory and fulfilling the memory
requirements of the users.
OS needs to keep the record of all free memory so they can allocate whenever required. This is done by maintaining
inked list of all free blocks, known as free list.

• Each free block contain a free link field that contains the address of next free block.

• Along with size of block is also there.


Sequential Fit Methods:
1. First Fit,

2. Best Fit and

3. Worst Fit methods


First Fit method
Best Fit method
Worst Fit method
Fragmentation
Compaction
Freeing Memory
Suppose now P5 is freed we can remove block at address 180 from the free list and combine these two blocks to form
one block and put this larger block size 190 on the free list.
Boundary Tag Method
Buddy Systems
Binary Buddy Systems
The Fibonacci sequence can be defined recursively as-
F0=f1=1 Fibonacci Buddy System
Fi=fi-1+fi-2 for i>2
Garbage Collection.
Garbage Collection :1 Reference Counting
Garbage Collection: 2 Mark and Sweep
Garbage Collection: 2 Mark and Sweep

You might also like