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
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:
- #include <iostream>
- using namespace std;
- int main()
- {
- int nBlocks,nProcess,blockSize[20],processSize[20];
- cout<<" Enter the number of blocks: "; cin>>nBlocks;
- cout<<" Enter the number of processes: "; cin>>nProcess;
- cout<<" Enter the size of "<<nBlocks<<" blocks: ";
- for(int i=0;i<nBlocks;i++) cin>>blockSize[i];
- cout<<" Enter the size of "<<nProcess<<" processes: ";
- for(int i=0;i<nProcess;i++) cin>>processSize[i];
- for(int i=0;i<nProcess;i++)
- {
- int max = blockSize[0];
- int pos = 0;
- for(int j=0;j<nBlocks;j++)
- if(max < blockSize[j]) { max = blockSize[j]; pos = j; } if(max >= processSize[i])
- {
- cout<<"\nProcess "<<i+1<<" is allocated to block "<<pos+1;
- blockSize[pos] = blockSize[pos]-processSize[i];
- }
- else{
- cout<<"\nProcess "<<i+1<<" can't be allocated";
- }
- }
- return 0;
- }
Comments
Post a Comment