我如何接受逻辑地址数据? [英] How do I accept logical address data?
问题描述
Hey Guys,
我编写了一段代码来演示操作系统中的分页概念。我假设框架和页面大小为4个字节。
问题是我无法输入逻辑地址值!
请求帮助:
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:
#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();
}
我的尝试:
我已经尝试改变我接受逻辑地址数据的方式。
1.)使用另一个char *字然后将其分配给逻辑[i] ...没有工作
2.)使用char * s =逻辑;然后输入s,然后输入++ s;没有工作
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
推荐答案
你有以下声明:
You have the following statement:
cin>>logical[i];
但逻辑
是一个 char * $ c的数组$ c>所以你不能直接在其中一个单元格中写入一些文本。您需要将数据读入新的字符串,然后将其地址发布到数组项中。你也不应该使用如下表达式:
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:
if(physical[i]=="Empty"
因为这不是有效的字符串比较你应该使用 strcmp
或其衍生产品之一。
这篇关于我如何接受逻辑地址数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!