Отчет по дисциплине Операционные системы

Автор: Пользователь скрыл имя, 15 Июня 2012 в 14:25, лабораторная работа

Описание работы

Продумайте систему сегментного хранения информации. Исходные данные:
Программа на Cи (Си++), под Windows, Консольное приложение.
Вся доступная память представлена массивом 10 КБайт. Все операции с выделением, доступом и освобождением памяти проводятся только с памятью внутри этого массива.

Работа содержит 1 файл

оси.doc

— 81.00 Кб (Скачать)

      printf("%s",Data1);

      printf("\n\n");

      int b=AllocMemory(1500);

      DumpMemory();

      printf("\n");

      int a=AllocMemory(1500);

      DumpMemory();

      printf("\n"); 

      FreeMemory(h);

      DumpMemory();

      printf("\n");

      FreeMemory(k);

      DumpMemory();

      printf("\n");

      FreeMemory(a);

      DumpMemory();

      printf("\n"); 

      int z=AllocMemory(1000);

      DumpMemory();

      printf("\n");

      int x=AllocMemory(5000);

      DumpMemory();

      printf("\n");

      getch();

      return 0;

}

prot.h 

struct Block

{

      short int Adres;

      short Size;

      bool Bit;

      short int Handle;

      bool Sort;

      struct Block *next;

}; 

void InitMemory(void); 

int AllocMemory(int Size); 

void FreeMemory(int MemoryHandle); 

int WriteMemory(int MemoryHandle, int Offset, int Size, void *Data); 

int ReadMemory(int MemoryHandle, int Offset, int Size, void *Data);

void DumpMemory(void);

void Init(char *c, int start, int end);

void SortMemory(void);

memman.cpp

#include "prot.h"

#include <stdio.h>

#include <math.h> 

char Ar[10001]; 

int I; 
 

void InitMemory(void)

{

      I=10000-sizeof(I);

      *(int*)&Ar[0]=I;

}

int AllocMemory(int Size)

{

      static int i=1;

      int k=*(int*)&Ar[0];

      int m=sizeof(Block);

                  if(i==1)

                  {

                        struct Block *tmp;

                        tmp=(struct Block*)&Ar[sizeof(I)];

                        tmp->Adres=10000-Size;

                        tmp->Size=Size;

                        tmp->Bit=1;

                        tmp->Handle=i;

                        k-=tmp->Size;

                        k-=m;

                        *(int*)&Ar[0]=k;

                        i++;

                        return tmp->Handle;

                  } 
 
 
 

                  else

                  {

                        int f=0;

                        struct Block *t;

                        t=(struct Block*)&Ar[sizeof(I)];

                        while(t)

                        {

                              f++;

                              t=t->next;

                        } 
 

                        struct Block *p,*p1;

                        p=(struct Block*)&Ar[sizeof(I)];

                        p1=(struct Block*)&Ar[sizeof(I)+(i-2)*sizeof(Block)];

                        while(p)

                        {

                              if( (!p->Bit) && (p->Size>Size) )

                              {

                                    struct Block *tmp;

                                    tmp=(struct Block*)&Ar[sizeof(I)+f*sizeof(Block)];

                                    tmp->Adres=p->Adres+p->Size-Size;

                                    tmp->Bit=1;

                                    tmp->Size=Size;

                                    tmp->Handle=i;

                                    p1->next=tmp;

                                    i++;

                                    p->Size=p->Size-Size;

                                    k-=sizeof(Block);

                                    *(int*)&Ar[0]=k;

                                    return tmp->Handle;

                              }

                              p=p->next;

                        }

                        if ((k-=m) > Size)

                        

                              int z=10000;

                              struct Block *tmp,*tmp1,*b;

                              tmp=(struct Block*)&Ar[sizeof(I)+f*sizeof(Block)];

                              tmp1=(struct Block*)&Ar[sizeof(I)];

                              tmp->next=NULL;

                              for(; tmp1; tmp1=tmp1->next)

                              {

                                    if ((z>tmp1->Adres)&&tmp1->Bit)

                                    

                                          z=tmp1->Adres;

                                    }

                                    b=tmp1;

                              }

                              tmp->Adres=z-Size;

                              tmp->Size=Size;

                              tmp->Bit=1;

                              tmp->Handle=i;

                              b->next=tmp;

                              i++;

                              k-=tmp->Size;

                              *(int*)&Ar[0]=k;

                              return tmp->Handle;

                        }

                        else

                        {

                              int freesize=k;

                              struct Block *b;

                              b=(struct Block*)&Ar[sizeof(I)];

                              while(b)

                              {

                                    if(!b->Bit) freesize+=b->Size;

                                    b=b->next;

                              }

                              if(freesize>Size) SortMemory();

                              else return 0;

                              AllocMemory(Size);

                        }      

Информация о работе Отчет по дисциплине Операционные системы