Skip to main content

Program for Worst Fit algorithm in Memory Management || dot clu

Program for Worst Fit algorithm in Memory Management

Worst Fit allocates a process to the partition which is largest sufficient among the freely available partitions available in the main memory. If a large process comes at a later stage, then memory will not have space to accommodate it.

Suggestion:

1.First Fit
2. Best Fit
3. Worst Fit

Example:
Input : blockSize[]   = {100, 500, 200, 300, 600};
        processSize[] = {212, 417, 112, 426};
Output:
Process No. Process Size Block no.
   1  212  5
   2  417  2
   3  112  5
   4  426  Not Allocated
Implementation:
1- Input memory blocks and processes with sizes.
2- Initialize all memory blocks as free.
3- Start by picking each process and find the
   minimum block size that can be assigned to
   current process i.e., find min(bockSize[1], 
   blockSize[2],.....blockSize[n]) > 
   processSize[current], if found then assign 
   it to the current process.
5- If not then leave that process and keep checking
   the further processes.

Source Code:

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int nBlocks,nProcess,blockSize[20],processSize[20];
  6. cout<<" Enter the number of blocks: "; cin>>nBlocks;
  7. cout<<" Enter the number of processes: "; cin>>nProcess;
  8. cout<<" Enter the size of "<<nBlocks<<" blocks: ";
  9. for(int i=0;i<nBlocks;i++) cin>>blockSize[i];
  10. cout<<" Enter the size of "<<nProcess<<" processes: ";
  11. for(int i=0;i<nProcess;i++) cin>>processSize[i];
  12. for(int i=0;i<nProcess;i++)
  13. {
  14. int max = blockSize[0];
  15. int pos = 0;
  16. for(int j=0;j<nBlocks;j++)
  17. if(max < blockSize[j]) { max = blockSize[j]; pos = j; } if(max >= processSize[i])
  18. {
  19. cout<<"\nProcess "<<i+1<<" is allocated to block "<<pos+1;
  20. blockSize[pos] = blockSize[pos]-processSize[i];
  21. }
  22. else{
  23. cout<<"\nProcess "<<i+1<<" can't be allocated";
  24. }
  25. }
  26. return 0;
  27. }

Comments

Popular posts from this blog

Shortest Remaining Time First (SRTF) Program in C++ || dot clu

Shortest Remaining Time First (SRTF) Program in C++ || dot clu Shortest Remaining Time First (SRTF) Shortest Remaining Time First (SRTF) Algorithm is preemptive version of Shortest Job First Algorithm. In this current process is executed until it is completed or a new process is added having lower burst time compare to the the remaining time for current process. SRTF algorithm may lead to starvation, if processes with lower burst time continues to add to cpu scheduler then the current process will never get a chance to get executed. For example consider the following table Process Arrival Time Burst Time P1 0 10 P2 1 6 P3 2 9 P4 3 4 At time t=0, Process P1 will start get executing as it is only the process present at that time. Then at t=1, Process P2 added to the CPU scheduler, at this time remaining time(Burst time) for Process P1 gets 9, as Burst time of P2 is less than the remaining time of other processes (for now there is only process P1) therefore pro...

First Fit Program and Algorithm in C++ || dot clu

First Fit Program and Algorithm in C++ || dot clu First Fit Algorithm in C and C++ Here you will learn about first fit algorithm in C and C++ with program examples. There are various memory management schemes in operating system like first fit, best fit and worst fit. In this section we will talk about first fit scheme. Suggestion: 1. First Fit 2.  Best Fit 3.  Worst Fit What is First Fit Memory Management Scheme? In this scheme we check the blocks in a sequential manner which means we pick the first process then compare it’s size with first block size if it is less than size of block it is allocated otherwise we move to second block and so on. First Fit Algorithm Get no. of Processes and no. of blocks. After that get the size of each block and process requests. Now allocate processes if(block size >= process size) //allocate the process else //move on to next block Display the processes with the blocks that are allocated to a respective...

Round Robin Scheduling Program in C++ || dot clu

Round Robin Scheduling Program in C++ Process scheduling is an important component for process management. In a multi-user and a time-sharing system, response time is one of the most important objective to be accomplished. Round Robin Scheduling Algorithm 1. The queue structure in ready queue is of First In First Out (FIFO) type. 2. A fixed time is allotted to every process that arrives in the queue. This fixed time is known as time slice or time quantum. 3. The first process that arrives is selected and sent to the processor for execution. If it is not able to  complete its execution within the time quantum provided, then an interrupt is generated using an automated timer. 4. The process is then stopped and is sent back at the end of the queue. However, the state is saved and context is thereby stored in memory. This helps the process to resume from the point where it was interrupted. 5. The scheduler selects another process from the ready queue and dispa...