Strzelacz48
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Systemy operacyjne lista 8 Przydział pamięci ## Zadanie 1 kasia ![](https://i.imgur.com/uypnEHJ.png) :::info **sbrk()** changes the location of the program break, which defines the end of the process's data segment (i.e., the program break is the first location after the end of the uninitialized data segment). Increasing the program break has the effect of allocating memory to the process; decreasing the break deallocates memory. sbrk() increments the program's data space by increment bytes. Calling sbrk() with an increment of 0 can be used to find the current location of the program break. **mmap()** creates a new mapping in the virtual address space of the calling process. The starting address for the new mapping is specified in addr. The length argument specifies the length of the mapping (which must be greater than 0). The **munmap()** system call deletes the mappings for the specified address range, and causes further references to addresses within the range to generate invalid memory references. The region is also automatically unmapped when the process is terminated. On the other hand, closing the file descriptor does not unmap the region. ::: brk/sbrk is LIFO (last in first out). Let's say you increase the segment size by X number of bytes to make room for allocation A and X number of bytes to make allocation B, and then free A. You cannot reduce the allocated memory because B is still allocated. And since the segment is shared across the entire program, if multiple parts of the program use it directly, you will have no way of knowing whether particular part is still in use or not. And if one part of the program (let's say malloc) assumes entire control over the use of brk/sbrk, then calling them elsewhere will break the program. By contrast, mmap can be unmapped in any order and allocation by one part of the program doesn't conflict with other parts of the program. brk/sbrk are not part of the POSIX standard and thus not portable. By contrast, mmap is standard and portable. mmap can also do things like map files into memory which is not possible using brk/sbrk. ## Zadanie 2 Kuba ![](https://i.imgur.com/hwI8PzL.png) Wyjaśnij różnicę między fragmentacją wewnętrzną i zewnętrzną. fragmentacja wewnętrzna – następuje w przypadku, gdy rozmiar alokowanej pamięci jest mniejszy niż rozmiar jednego bloku. Wówczas nadmiarowa pamięć marnuje się. fragmentacja zewnętrzna – występuje gdy łączna suma wolnej pamięci jest wystarczająco duża do obsłużenia żądania alokacji, ale żaden z wolnych bloków nie jest wystarczająco duży i nie jest możliwa ich konsolidacja. Czemu nie można zastosować kompaktowania w bibliotecznym algorytmie przydziału pamięci? kompaktowanie – defragmentacja zaalokowanej pamięci poprzez przeniesienie bloków tak, aby leżały obok siebie, co eliminuje fragmentację zewnętrzną. Nie można zastosować kompaktowania w bibliotecznym algorytmie przydziału pamieci, gdyż kompaktowanie unieważnia stare wskaźniki (wskaźnik przestaje być poprawny, gdyż odpowiadająca mu pamięć została przeniesiona pod inny adres i nie ma możliwości automatycznego zaktualizowania tych wskaźników). Na podstawie §2.3 opowiedz o dwóch głównych przyczynach występowania fragmentacji zewnętrznej. Fragmentation is caused by isolated deaths: krytycznym problemem jest tworzenie wolnych obszarów, których sąsiadujące obszary nie są wolne. Składają się na to dwie rzeczy: które obiekty zostają umieszczone w przyległych obszarach, i kiedy te obiekty umierają. Jeżeli obiekty zostają zwolnione w rożnych momentach, to powstają pomiędzy nimi przerwy. Fragmentation is caused by time-varying behavior: fragmentacja powstaje przez to, że z upływem czasu może zmieniać się zachowanie programu w sensie tego, jakich alokacji dokonuje – np. zwalnianie małych bloków i alokowanie bardzo dużych, bądź alokowanie wielu obiektów różnych typów. Alokator powinien umieć zauważyć wzorce w alokacji w celu minimalizacji fragmentacji. ## Zadanie 3 Marcin ![](https://i.imgur.com/74JLW1A.png) ::: spoiler WYKRES : ![](https://i.imgur.com/WbUM3BO.png) ::: **first-fit** - **next-fit** - **best-fit** - ## Zadanie 4 Kornelia ![](https://i.imgur.com/zXJVtNq.png) **Dwukierunkowa lista wolnych bloków i boundary tag bez optymalizacji :** ![](https://i.imgur.com/XT9hPP3.png) **a = 1: Allocated block, a = 0: Free block Size: Total block size** Najmniejszy rozmiar wolnego bloku to 4 słowa (nagłówek, wskaźnik na poprzedni, wskaźnik na następny, stopka) | Header | Next | Prev | Footer | | ------ | ---- | ---- | ------ | **Polityka best-fit** - alloc zwraca pierwszy wolny blok o najmniejszym rozmiarze ze wszystkich wolnych nie mniejszych niż rozmiar tego, co chcemy zaalokować **Gorliwe złączanie bloków** - przy wywołaniu free powstaje nowy blok, jeśli poprzedni lub następny był wolny, to łączymy je w jeden (nigdy nie chcemy, żeby 2 wolne bloki ze sobą sąsiadowały) *Symulacja* zielona strzałka -> pierwszy element listy wolnych bloków, czerwona -> ostatni ![](https://i.imgur.com/hUqkJVs.png) ![](https://i.imgur.com/fmB0w1G.png) Przy wywołaniu alloc(18) powstaje nieużytek o rozmiarze 2 słów (nie mogliśmy podzielić wolnego bloku o rozmiarze 22 słów na blok o rozmiarze 20 dla alloca i pusty o rozmiarze 2, bo minimalny rozmiar pustego bloku to 4 słowa). Gdyby algorytm wykożystywał strategię first-fit, to przedostatnie wywołanie alloca (alloc(6)) podzieliłoby wolny blok o rozmiarze 16 na zajęty o rozmiarze 8 i wolny o rozmiarze 8. Nawet po wywołaniu free(2) nie mielibyśmy tak dużego wolnego bloku, żeby następny alloc(18) się powiódł bez powiększenia tablicy. ![](https://i.imgur.com/UTT8qNE.png) ## Zadanie 5 Olek ![](https://i.imgur.com/Cbxsion.png) :::info **Algorytm kubełkowy** (*segregated-fit*) - polega na przechowywaniu list wolnych bloków pamięci, ze względu na rozmiar tych bloków. W momencie wykonywania alokacji wybiera się wolny blok z listy, która przechowuje bloki o najmniejszym wystarczającym rozmiarze. **Węzeł strażnik** (*sentinel node*) - specjalny wyznaczony węzeł używany z listami połączonymi i drzewami jako terminator ścieżki przejścia. **Złączanie leniwe** (*deferred coalescing*) polega na łaczeniu bloków dopiero w momencie, gdy kolejny raz chcemy zaalokować pamieć i nie napotkaliśmy bloku o wystarczająco dużym rozmiarze. ::: ![](https://i.imgur.com/lmQuvAf.png) > Jak przebiegają operacje `malloc` i `free`? > Co robi `malloc`, gdy na dane liście nie ma wolnego bloku żądanego rozmiaru? `malloc` -> wybierany jest odpowiedni najmniejszy kubełek, w przypadku gdy ten kubełek zawiera wolny blok, to blok ten jest usuwany z z listy i oznaczany jako zajęty, zwracany jest wskaźnik na ten blok. W przypadku gdy kubełek nie zawiera wolnego bloku, należy szukać w kolejnym większym kubełku. W momencie znalezienia wolnego bloku wykonywany jest podział, alokujemy pamięć a pozostałe miejsce jest umieszczane w kubełku odpowiedniej wielkości. W przypadku gdy wszystkie kubełki są zajęte prosimy jądro o powiększenie sterty przy pomocy `sbrk()` `free` -> oznacza blok jako wolny, a następnie jeśli sąsiadujący blok jest wolny, są one łączone. Finalny cały wolny blok jest umieszczany w odpowiednim kubełku. > Gdzie należałoby przechowywać **węzeł strażnik** (*sentinel node*) każdej z list wolnych bloków? Najpewniej chcemy umieścić naszego wartownika na końcu listy, tak aby zwalnianie ostatniego bloku nie było problematyczne. To znaczy po przejściu całej listy, chcemy zacząć przeglądanie kolejnej. > Rozważ zastosowanie **leniwego złączania** wolnych bloków w algorytmie kubełkowym przydziału pamięci - jakie probelmy zauważasz? Gdybyśmy chcieli leniwie złączać bloki, moglibyśmy zmniejszyć czas wyszukiwania bloków przy małych alokacjach. Problemem wynikającym z tego podejścia jest zwiększenie fragmentacji. --- ![](https://i.imgur.com/SPsW24g.png) ## Zadanie 6 ![](https://i.imgur.com/11bSvwo.png) ## Zadanie 7 ![](https://i.imgur.com/VB8OGX1.png) ## Zadanie 8(bonus) ![](https://i.imgur.com/P7BwnrT.png)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully