Gerenciamento de Memória(2)

844 views
783 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
844
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
20
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Gerenciamento de Memória(2)

  1. 1. Gerenciamento de Mem´ria o
  2. 2. Sonho dos usu´rios a Idealmente, a mem´ria deveria ser o • r´pida, a • de custo baixo, • imensa e • n˜o vol´til. a a
  3. 3. Realidade para os usu´rios a Hierarquia de Mem´ria o Typical access time Typical capacity 1 nsec Registers <1 KB 2 nsec Cache 1 MB 10 nsec Main memory 64-512 MB 10 msec Magnetic disk 5-50 GB 100 sec Magnetic tape 20-100 GB
  4. 4. Hierarquia de Mem´ria o Registradores • Internos ` CPU a • Extremamente r´pidos a • Otimiza¸oes de c´digo podem mover temporariamente c˜ o vari´veis para registradores. a • Programas podem dar palpites sobre o que deve ficar armazenado nos registradores register int r;
  5. 5. Hierarquia de Mem´ria o Cache • Internos ou muito pr´ximos ` CPU o a • Divididos em linhas de cache • Controlados por hardware • Cache hit • Cache miss
  6. 6. Hierarquia de Mem´ria o Mem´ria Principal o • Random Access Memory (RAM) • Compromisso entre pre¸o e desempenho c • Armazenamento vol´til a
  7. 7. Hierarquia de Mem´ria o Disco Read/write head (1 per surface) Surface 7 Surface 6 Surface 5 Surface 4 Surface 3 Direction of arm motion Surface 2 Surface 1 Surface 0
  8. 8. Hierarquia de Mem´ria o Fitas magn´ticas e • Utilizadas para c´pias de seguran¸a (backups) o c • Armazenamento de grandes quantidades de dados • Acesso seq¨encial u
  9. 9. Hierarquia de Mem´ria o Outros tipos de mem´ria o • ROM (Read Only Memory) – r´pida e barata a – bootstrap loader est´ gravado em ROM a • EEPROM (Electrically Erasable ROM) – podem ser apagadas (erros podem ser corrigidos) • CMOS – dependem de uma bateria – armazenam rel´gio e configura¸oes o c˜
  10. 10. Monoprograma¸˜o ca 0xFFF … Operating Device system in drivers in ROM ROM User program User program User program Operating Operating system in system in RAM RAM 0 0 0 (a) (b) (c) mainframes palmtops MS-DOS
  11. 11. Multiprograma¸˜o e parti¸oes fixas ca c˜ Multiple input queues 800K Partition 4 Partition 4 700K Partition 3 Single Partition 3 input queue 400K Partition 2 Partition 2 200K Partition 1 Partition 1 100K Operating Operating system system 0 (a) (b)
  12. 12. Modelagem de multiprograma¸˜o ca • Quantos processos colocar na mem´ria? o • Estimativa de uso da CPU 20% I/O Wait 50% I/O Wait 80% I/O Wait
  13. 13. Modelagem de multiprograma¸˜o ca • Quantos processos colocar na mem´ria? o • Aposta otimista: 5 processos 80
  14. 14. Modelagem de multiprograma¸˜o ca • Vis˜o probabil´ a ıstica • Taxa de utiliza¸˜o da CPU = 1 - (0.8)n ca ... ... ... ... ...
  15. 15. Modelagem de multiprograma¸˜o ca 20% I/O wait CPU utilization (in percent) 100 50% I/O wait 80 60 80% I/O wait 40 20 0 1 2 3 4 5 6 7 8 9 10 Degree of multiprogramming Utiliza¸˜o da CPU = 1 - p ca n
  16. 16. An´lise de Desempenho a CPU Arrival minutes # Processes Job time needed 1 2 3 4 1 10:00 4 CPU idle .80 .64 .51 .41 2 10:10 3 CPU busy .20 .36 .49 .59 3 10:15 2 CPU/process .20 .18 .16 .15 4 10:20 2 (a) (b) 2.0 .9 .8 .3 Job 1 finishes 1 Job 2 starts .9 .8 .3 .9 .1 2 .8 .3 .9 3 .3 .9 .1 .7 4 0 0 10 15 20 22 27.6 28.2 31.7 Time (relative to job 1's arrival) (c)
  17. 17. Swapping CPU CPU scheduler Arriving job Input queue Main Memory Memory Disk Admission scheduler scheduler
  18. 18. Swapping Time C C C C C B B B B A A A A D D D Operating Operating Operating Operating Operating Operating Operating system system system system system system system (a) (b) (c) (d) (e) (f) (g)
  19. 19. Reloca¸˜o e Prote¸˜o ca ca • Reloca¸˜o: um programa deve poder rodar em endere¸os ca c f´ ısicos distintos. • Prote¸˜o: um programa n˜o pode fazer acesso ` ´rea de ca a aa mem´ria reservada a outro programa. o
  20. 20. Reloca¸˜o durante a carga ca • Todos os endere¸os precisam ser identificados e alterados c • N˜o resolve o problema da prote¸˜o a ca
  21. 21. Bits de prote¸˜o ca 1010 1010 1010 0011 0011 O PSW de cada processo deve conter os bits de prote¸˜o ca
  22. 22. Registradores base e limite Address Registers when 0xFFFFFFFF program 2 is running User program Registers Limit-2 and data when program 1 User-2 data is running Base-2 Limit Limit-2 User program Base-2 User-1 data and data Limit-1 Limit-1 User program Base Base-1 Base-1 Operating Operating System System 0 (a) (b)
  23. 23. Espa¸o para crescimento c B-Stack Room for growth Room for growth B-Data B Actually in use B-Program A-Stack Room for growth Room for growth Address (hex) A-Data FFFF A Actually in use Stack A-Program Gap Operating Operating system system Data (a) (b) Text 0000
  24. 24. Compacta¸˜o de mem´ria ca o A A — B B C — — C —
  25. 25. Bitmaps e lista de livres A B C D E 8 16 24 (a) 11111000 P 0 5 H 5 3 P 8 6 P 14 4 11111111 11001111 H 18 2 P 20 6 P 26 3 H 29 3 X 11111000 Hole Starts Length Process at 18 2 (b) (c)
  26. 26. Atualiza¸˜o da lista ca Before X terminates After X terminates (a) A X B becomes A B (b) A X becomes A (c) X B becomes B (d) X becomes
  27. 27. Algoritmos para aloca¸˜o de mem´ria ca o • First fit • Next fit • Best fit • Worst fit
  28. 28. Problema relacionado Malloc, free e realloc void *malloc(size_t size); void free(void *ptr); void *realloc(void *ptr, size_t size); A aloca¸˜o de registros dentro de um processo tem ca problemas semelhantes: • administra¸˜o de uma lista de livres ca • fragmenta¸˜o ca
  29. 29. Programas muito grandes O que fazer se um programa for muito grande para caber na mem´ria? o
  30. 30. Overlays dados d1, d2, d3, d4, d5; f1(); g1(); h1(); f2(); g2(); h2(); f3(); g3(); h3(); main() { fase_1(); /* fun¸oes f e dados d1, d2, d3 */ c fase_2(); /* fun¸oes g e dados d1, d2, d4 */ c fase_3(); /* fun¸oes h e dados d1, d2, d5 */ c }
  31. 31. Mem´ria Virtual o The CPU sends virtual CPU addresses to the MMU package CPU Memory Disk management Memory controller unit Bus The MMU sends physical addresses to the memory Memory Management Unit (MMU)
  32. 32. Pagina¸˜o ca Virtual address space 60K-64K X 56K-60K X Virtual page 52K-56K X 48K-52K X 44K-48K 7 40K-44K X Physical 36K-40K 5 memory 32K-36K X address 28K-32K X 28K-32K 24K-28K X 24K-28K 20K-24K 3 20K-24K 16K-20K 4 16K-20K 12K-16K 0 12K-16K 8K-12K 6 8K-12K 4K-8K 1 4K-8K 0K-4K 2 0K-4K Page frame
  33. 33. Mapeamento dos endere¸os c Outgoing 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 physical address (24580) 15 000 0 14 000 0 13 000 0 12 000 0 11 111 1 10 000 0 9 101 1 12-bit offset Page 8 000 0 copied directly table 7 000 0 from input 6 000 0 to output 5 011 1 4 100 1 3 000 1 2 110 1 110 1 001 1 Present/ 0 010 1 absent bit Virtual page = 2 is used as an index into the page table Incoming virtual address 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 (8196)
  34. 34. Espa¸o de endere¸amento c c • Apenas as p´ginas ocupadas precisam ser mapeadas a Address (hex) FFFF Stack Gap Data Text 0000
  35. 35. Mem´ria compartilhada o Processo A Processo B Pilha Pilha Shmem Shmem Data Data Texto Texto
  36. 36. Mem´ria compartilhada o int shmget(key_t key, size_t size, int shmflg); void *shmat(int shmid, const void *shmaddr, int shmflg); • Veja os exemplos: sh1.c sh2,c sh fork.c

×