Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
16
Добавлен:
01.05.2014
Размер:
6.1 Кб
Скачать
// lab5.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#define DIV 1024

using namespace std;

void pagesize();
void globalmemstatus();
void virtualalloc();
void virtualfree();
void getheap();
int heapcreate();
int heapalloc();
void heapfree();
void heapdestroy();


int a=0,b=0,c=0,i=0,j=0;
DWORD dwPageSize,pagelimit,heaplimit,err;             
LPVOID lpvBase,heapmem;              
BOOL bSuccess;                                
SYSTEM_INFO sSysInfo;       
MEMORYSTATUS stat;
HANDLE heap,heapcr;


int _tmain(int argc, _TCHAR* argv[])
{
 system("cls");
 cout<<"\n                                The fifth laboratory work "<<endl;
 cout<<"                               Operations with virtual memory"<<endl;
 getch();
 do
  { 
   system("cls");
   cout<<"\n                                     Menu\n"<<endl;
   cout<<" 1.Information about the system's current usage memory"<<endl;
   cout<<" 2.Allocation of virtual memory"<<endl;
   cout<<" 3.Releasing of allocated virtual memory"<<endl;
   cout<<" 4.Obtaining a handle to the heap of the calling process"<<endl;
   cout<<" 5.Creating a heap object that can be used by the calling process"<<endl;
   cout<<" 6.Allocating a block of memory from a heap"<<endl;
   cout<<" 7.Releasing a memory block allocated from a heap "<<endl;
   cout<<" 8.Destoying the specified heap object"<<endl;
   cout<<" 9.Exit"<<endl;
   do
	{
	 cout<<"\n   Vibirite punkt menu:"<<endl;
     cin>>a;
     if(a<0||a>9)
      {
	   cout<<"\n   Vi dopustili oshibku!!!(1-9)"<<endl;
	   getch();
	  }
	}
   while(a<0||a>9);
   switch(a)
    {
	 case 1:
	  {
	   globalmemstatus();
	   break;
	  }
	 case 2:
	  {
	   virtualalloc();
	   break;
	  }
     case 3:
	  {
	   virtualfree();
	   break;
	  }
     case 4:
	  {
	   getheap();
	   break;
	  }
     case 5:
	  {
	   i=heapcreate();
	   break;
	  }
     case 6:
	  {
	   j=heapalloc();
	   break;
	  }
     case 7:
	  {
	   heapfree();
	   break;
	  }
     case 8:
	  {
	   heapdestroy();
	   break;
	  }
	}
  }
 while(a!=9);
 return 0;
}

 void pagesize()
  {
   GetSystemInfo(&sSysInfo);     
   dwPageSize = sSysInfo.dwPageSize;
  }

 void globalmemstatus()
  {
   system("cls");
   GlobalMemoryStatus(&stat);
   cout<<"\nThe MemoryStatus structure is "<<stat.dwLength<<" bytes long"<<endl;
   cout<<stat.dwLength<<" "<<"percent of memory is in use"<<endl;
   cout<<"There are "<<(stat.dwTotalPhys)/DIV<<" total Kbytes of physical memory"<<endl;
   cout<<"There are "<<(stat.dwAvailPhys)/DIV<<" free Kbytes of physical memory"<<endl;
   cout<<"There are "<<(stat.dwTotalPageFile)/DIV<<" total Kbytes of paging file"<<endl;
   cout<<"There are "<<(stat.dwAvailPageFile)/DIV<<" free Kbytes of paging file"<<endl;     
   cout<<"There are "<<(stat.dwTotalVirtual)/DIV<<" total Kbytes of virtual memory"<<endl;    
   cout<<"There are "<<(stat.dwAvailVirtual)/DIV<<" free Kbytes of virtual memory"<<endl;
   getch();
  }

void virtualalloc()
 {
  system("cls");
  pagesize();    
  cout<<"This computer has a page size of "<<sSysInfo.dwPageSize<<endl;
  cout<<"Define a number of pages to be allocated:"<<endl;
  cin>>pagelimit;
  dwPageSize = sSysInfo.dwPageSize;
  lpvBase = VirtualAlloc(
                         NULL,                 
                         pagelimit*dwPageSize, 
                         MEM_COMMIT,         
                         PAGE_NOACCESS);     
  if (lpvBase == NULL )
   {
    cout<<"VirtualAlloc reserve failed"<<endl;
	getch();
   }
  else
   {
    cout<<"Virtual memeory was successfully allocated!!!"<<endl;
    getch();
   }
 }

void virtualfree()
 {
  system("cls");
  bSuccess = VirtualFree(
                lpvBase,      
                0,             
                MEM_RELEASE); 
  printf ("Release was %s.\n", bSuccess ? "successful" : "unsuccessful" );
  getch();
 }

void getheap()
 {
  system("cls");
  cout<<"Obtaining a handle to the heap of the calling process..."<<endl;
  if((heap=GetProcessHeap())==0)
   {

	cout<<"Error!!!"<<endl;   
    getch();
   }
  else
   {
    cout<<"The handle was succssefully obtained"<<endl;   
    getch();
   }
 }

int heapcreate()
 {
  system("cls");
  pagesize();
  cout<<"Define a number of pages to be allocated for the heap:"<<endl;
  cin>>heaplimit;
  if(!(heapcr=HeapCreate(HEAP_NO_SERIALIZE,heaplimit*dwPageSize,NULL)))
   {
    cout<<"Error!"<<endl;   
    getch();
   }
  else
   {
    cout<<"The heap was successfully created!"<<endl; 
	i=1;
    getch();
   }
   return i;
 }

int heapalloc()
 {
  if(i)
   {
    system("cls");
    pagesize();
    cout<<"Define a number of pages to be allocated from a heap:"<<endl;
    cin>>heaplimit;
    if((heapmem=HeapAlloc(heapcr,HEAP_NO_SERIALIZE,heaplimit*dwPageSize))==0)
     {
      cout<<"Error!"<<endl;   
      getch();
     }
    else
     {
      cout<<"Memory was successfully allocated from the heap!"<<endl;  
	  j=1;
      getch();
     }
   }
  else
   {
    cout<<"Error!Create a heap first! "<<endl;   
	getch();
   }
   return j;
 }

void heapfree()
 {
  if(i)
   { 
    if(j)
	 {
      system("cls");
      if(HeapFree(heapcr,0,heapmem))
       {
        cout<<"Memory from the heap was successfully released!"<<endl;   
        getch();
       }
      else
       {
        cout<<"Error!"<<endl;   
        getch();
       }
     }
	else
	 {
	   cout<<"Allocate a memory from the heap first!"<<endl;   
       getch();
	 }
   }
  else
   {
    cout<<"Error!Create a heap first! "<<endl;   
    getch();
   }
 }

void heapdestroy()
 {
  if(i)
  {
   system("cls");
   if(HeapDestroy(heapcr))
    {
     cout<<"The specified heap object was successfully destroyed!"<<endl;   
     getch();
    }
   else
    {
     cout<<"Error!"<<endl;   
     getch();
    }
  }
  else
  {
   cout<<"Error!Create a heap first! "<<endl;   
   getch();
  }
 }
Соседние файлы в папке 5