Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторная работа №4
.cpp#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <iostream.h>
void main(){
int choice,flag1=0,flag2=0,flag3=0;
do{
system("cls");
cout<<"1. Get virtual memory Info\n";
cout<<"2. Allocate Virtual Memory\n";
cout<<"3. Free Virtual Memory\n";
cout<<"4. Create Heap Object And Allocate Memory\n";
cout<<"5. Exit\n";
cout<<"Enter your choice: ";
cin>>choice;
switch(choice){
case 1: SYSTEM_INFO SysInfo;
/*
DWORD dwLength;
DWORD dwMemoryLoad;
DWORD dwTotalPhys;
DWORD dwAvailPhys;
DWORD dwTotalPageFile;
DWORD dwAvailPageFile;
DWORD dwTotalVirtual;
DWORD dwAvailVirtual;
*/
GetSystemInfo(&SysInfo);
cout<<"\nThe page size used by the VirtualAlloc function: "<<SysInfo.dwPageSize;
cout<<"\nMinimum application address: "<<SysInfo.lpMinimumApplicationAddress;
cout<<"\nMaximum application address: "<<SysInfo.lpMaximumApplicationAddress;
cout<<"\nAllocation Granularity: "<<SysInfo.dwAllocationGranularity;
cout<<flush;
getch();
break;
case 2: size_t siz;
LPVOID address;
LPVOID allocAddress;//allocated memory address
DWORD size;
DWORD allocationType;//type of allocation
DWORD protect;//type of access protection
address = 0;//0 - the system determines where to allocate the region
siz=100;
allocationType = MEM_COMMIT | MEM_RESERVE;
protect = PAGE_EXECUTE_READWRITE;//execute, read, and write access
cout<<" Input size of region in bytes: ";
cin>>size;
allocAddress = VirtualAlloc(address, size*20000, allocationType, protect);
if(allocAddress){flag1=1;printf(" Allocated memory address: %x",allocAddress);/*Memory_query(allocAddress,siz);*/}
else cout<<"Error"<<endl;
getch();
break;
case 3: if(!flag1)break;
DWORD freeType;
freeType = MEM_RELEASE;
size = 0;
if(!VirtualFree(allocAddress, size, freeType)){
cout<<"Error"<<endl;
}
else{cout<<"Function is executed successfully!";flag1=0;}
cout<<flush;
getch();
break;
case 4: HANDLE processHeap;
LPVOID heapAlloc;
DWORD heapSize;
do{
system("cls");
cout<<"1. Get Process Heap\n2. Heap Create\n3. Heap Alloc\n4. Heap Free\n5. Heap Destroy\n6. Exit\n";
cout<<"";
cout<<"";
cout<<"Enter your choice: ";
cin>>choice;
switch(choice){
case 1: processHeap = GetProcessHeap();
if(!processHeap){
cout<<"Error"<<endl;
}else{flag3=1;cout<<"Function is executed successfully!";}
cout<<flush;
getch();
break;
case 2: DWORD options;
DWORD initialSize;
DWORD maximumSize;
options = 0;//no options
initialSize = 100;
maximumSize = 1000000;
processHeap = HeapCreate(options, initialSize, maximumSize);
if(!processHeap){
cout<<"Error";
}
else {flag3=1;cout<<"Function is executed successfully!";}
cout<<flush;
getch();
break;
case 3:cout<<" Input heap size: ";
cin>>heapSize;
processHeap=GetProcessHeap();
if(!processHeap)break;
flag3=1;
heapAlloc = HeapAlloc(processHeap,0, heapSize);
if(!heapAlloc){cout<<"Error"<<endl;}
else {flag2=1;cout<<"Function is executed successfully!";}
cout<<flush;
getch();
break;
case 4:if(!flag2)break;
if(!HeapFree(processHeap,0,heapAlloc)){cout<<"Error"<<endl;}
else{flag2=0; cout<<"Function is executed successfully!";}
cout<<flush;
getch();
break;
case 5:if(!flag3)break;
if(!HeapDestroy(processHeap)){cout<<"Error"<<endl;}
else{flag3=0;
cout<<"Function is executed successfully!";}
cout<<flush;
getch();
break;
case 6:if(flag2){
HeapFree(processHeap,0,heapAlloc);
flag2=0;
}
if(flag3){
if(!HeapDestroy(processHeap))
flag3=0;
}
cout<<flush;
getch();
break;
default: cout<<"Error";
getch();
}
}while(choice!=6);
choice = 4;
break;
case 5: break;
default: cout<<"Error"<<endl;
getch();
}
}while(choice!=5);
}
Соседние файлы в предмете Операционные системы