Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey Guys ,
I've coded a little piece of code that demonstrates the paging concept in Operating Systems. I've assumed the frame and page size to be 4 bytes.
The problem is that i'm unable to enter the Logical Address Values!
Request assistance on the same:
C++
#include<iostream>
#include<conio.h>
#include<time.h>
using namespace std;
int main()
{       
    char* logical[16]; 
    char* physical[33];
    int ft[4]; int pg=0; 
    int s[2]; int i,j; int page_move=0; int frame=0;
    int test[8]={0,4,8,12,16,20,24,28}; //Used for the random selection of frame numbers assuming the frame size to be 4 bytes
    time_t t;
    srand((unsigned) time(&t));
    s[0]=test[rand()%8]; i=s[0];  // We need two random frames to be occupied to represent a real scenario better !
    s[1]=test[rand()%8]; j=s[1];  // 's' contains those two frame numbers which will be made 'Full' later.
    cout<<"\n"<<s[0]<<" "<<s[1];
    for(int i=0;i<32;i++)// initialize all addresses to Empty
    { 
      physical[i]="Empty";
      logical[i]="Empty";
    }
    printf("\nThe Logical Address is Now->\n");
    for(int i=0;i<16;i++)
    {
      cout<<"\n"<<i<<" "<<logical[i];
    }
    for(int k=0;k<4;k++) // For filling in two random frames 
    { 
      physical[i]="Full";
      physical[j]="Full";
      ++i;
      ++j;
    }
    cout<<"\nEnter the Logical Address Data->\n";
    for(int i=0;i<16;i++)   // Entering Logical Data ( Doesn't Work!!!)
    {
       if(i%4==0){cout<<"\n---Page "<<page_move++<<"----\n";}
       cin>>logical[i];  
    }
    i=0;page_move=0;
    while(i<=28)   // Constructing The Frame Table
    {
       if(physical[i]=="Empty" && page_move!=4)
       {
         ft[page_move]=frame;
         ++page_move; 
       }
       ++frame;
       i=i+4;
    }
   
    cout<<"\n::::FRAME TABLE::::\n";
    cout<<"\n Page Frame\n";
    for(int i=0;i<4;i++)  // Display The Frame Table Created Above
    { 
      cout<<"\n  "<<i<<"  "<<ft[i];
    }
    printf("\nThe Physical Address is Now->\n");
    for(int i=0;i<32;i++)  //Display Physical Addresses and their Data
    {
      cout<<"\n"<<i<<" "<<physical[i];
    }
    
    getch();    
}


What I have tried:

I've tried changing how i accept the logical address data.
1.) Used another char* word and then assigned it to logical[i]...Didn't work
2.) Used char*s= logical; and then entered s followed by ++s; Did not work
Posted
Updated 18-Feb-17 21:01pm

1 solution

You have the following statement:
C++
cin>>logical[i];

but logical is an array of char* so you cannot directly write some text into one of its cells. You need to read the data into a new character string and then post its address into the array item. You should also not use expressions like:
C++
if(physical[i]=="Empty"

As that is not a valid string comparison. You should use strcmp or one of its derivatives.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900