Dydaktyka
      • 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
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners 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
    • 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 Help
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
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners 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
    # Ćwiczenia 5, grupa śr. 16-18, 23 listopada 2022 ###### tags: `PRW22` `ćwiczenia` `pwit` ## Deklaracje Gotowość rozwiązania zadania należy wyrazić poprzez postawienie X w odpowiedniej kolumnie! Jeśli pożądasz zreferować dane zadanie (co najwyżej jedno!) w trakcie dyskusji oznacz je znakiem ==X== na żółtym tle. **UWAGA: Tabelkę wolno edytować tylko wtedy, gdy jest na zielonym tle!** :::danger | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | | ----------------------:| ----- | --- | --- | --- | --- | --- | --- | --- | Daniel Boguszewski | X | X | | | | | | | Marcin Dąbrowski | | | | | | | | | Jakub Gałecki | | | | | | | | | Kamila Goszcz | X | X | | X | | X | X | | Wiktor Hamberger | X | X | | X| | | | | Jan Jankowicz | | X | | X | | | | | Mikołaj Jaszcza | X | X | | X | X | X | X | | Zuzanna Kania | X | X | | X | | | | | Wiktoria Kuna | | | | | | | | | Patryk Mazur | X | X | | X | | | | | Hubert Obrzut | X | X | X | X | X | X | X | X | Magdalena Rzepka | X |==X==| | X | | X | X | | Joanna Stachowicz | X | X | | | | | | | Rafał Starypan | ==X== | X | | X | X | X | X | | Maria Szlasa | X | X | | X | | X | X | | Daniel Wiczołek | x | x | | x | x | x | x | | Adam Jarząbek | X | X | | X | ==X== | X | | | ::: :::info **Uwaga:** Po rozwiązaniu zadania należy zmienić kolor nagłówka na zielony. ::: ## Zadanie 1 :::success Autor: Rafał Starypan ::: ![](https://i.imgur.com/8hzDtsO.png) 1. Sekwencyjnie spójne ![](https://i.imgur.com/rYKV60m.png) ![](https://i.imgur.com/DpqsG0m.png) 2. Sekwencyjnie niespójne ![](https://i.imgur.com/lKVlips.png) ![](https://i.imgur.com/221z7zu.png) Aby można było wykonać p.deq(y), to p.enq(y) musi być przed q.enq(x). Spowoduje to, że q.enq(y) będzie przed p.enq(x), czyli również przed q.enq(x). Zatem q.deq(x) nie zwróci poprawnej wartości ## Zadanie 2 :::success Autor: Magdalena Rzepka ::: **Własność kompozycji** - jeżeli dla każdej składowej x historii H H|x ma jakąś ceche, to historia H też ją ma. **Sekwencyjna spójność nie ma własności kompozycji.** Przykład: ![](https://i.imgur.com/3uewXor.png) A p.enq(x) #1 B q.enq(y) #2 A q.enq(x) #3 B p.enq(y) #4 A p.deq(y) #5 B q.deq(x) #6 Sekwencyjna spójność składowych p i q wystąpi, po zmianie #1 i #4 miejscami oraz po zmienie #2 i #3. H|p B p.enq(y) #4 A p.enq(x) #1 A p.deq(y) #5 H|q A q.enq(x) #3 B q.enq(y) #2 B q.deq(x) #6 #4 -> #1 -> #5 #3 -> #2 -> #6 Ale w obrębie wątku nie możemy zmieniać miejscami, więc musi zachodzić również: #1 -> #3 -> #5 #2 -> #4 -> #6 Podsumowując: #4 -> #1 -> #3 -> #2 -> #4 #4 -> #4 Akcja 4 musi nastąpić przed samą w sobie. Sprzeczność! ## Zadanie 3 :::success Autor: Hubert Obrzut ::: ![](https://i.imgur.com/fSZZFuo.png) ![](https://i.imgur.com/jGKu0H9.jpg) ![dsa](https://i.imgur.com/hs2jZAB.png) ## Zadanie 4 :::success Autor: Patryk Mazur ::: ![](https://i.imgur.com/xoJY7wH.png) ![](https://i.imgur.com/UnlmeH6.png) Zał. że wątek A wszedł do sekcji krytycznej: `write_A(flag[A] = true) -> write_A(victim = A) -> read_A(flag[B] == false || victim == B)` Żeby B wszedł musiałoby zachodzić: `write_B(flag[B] = true) -> write_B(victim = B) -> read_B(flag[A] == false || victim == A)` Jako, że A jest w sekcji krytycznej to `flag[A] == true` oraz `victim == B` ponieważ B ustawił na siebie zmienną victim. W modelu procesora słabszym niż sekwencyjna spójność może się okazać, że procesor zamieni kolejność operacji w celu optymalizacji (domyślnie `flag[] = false`): `read_A(flag[B] == false) -> read_B(flag[A] == false) -> write_A(flag[A] = true) -> write_B(flag[B] = true)` Zatem oba wątki wejdą do sekcji krytycznej ## Zadanie 5 :::success Autor: Adam Jarząbek ::: Ponieważ mamy model pamięci słabszy niż sekwencyjna spójność, może się zdarzyć, że instrukcje nie zostaną wykonane w takiej kolejności, jak są w kodzie. W szczególności w klasie wstawiającej element do kolejki możemy ustalić wartość `tail % capacity`, następnie zwiększyć wartość `tail` i dopiero zapisać nowy element pod wyliczonym indeksem. Jeżeli pomiędzy zwiększeniem wartości zmiennej, a zapisem do tablicy wątek odczytujący będzie chciał odczytać wartość zapisaną pod zwiększonym już `tail`, odczyta nieprawidłową wartość. Z tego powodu współdzielone między wątkami zmienne takie jak `tail` powinny być przechowywane w odpowiednio przystosowanych obiektach. ## Zadanie 6 :::success Autor:Mikołaj Jaszcza ::: ![](https://i.imgur.com/EDWaND0.png) ![](https://i.imgur.com/6uP3Bem.png) Aby pokazać fakt, iż IQueue nie jest poprawnym obiektem współ. pokażę, że nie jest linearyzowalny. Aby to zrobić posłużę się podaniem przykładu nielinearyzowalnej historii: ![](https://i.imgur.com/ZG7BXZc.png) (tj. w tym scenariuszu zajdzie, że wątek A zostanie wywłaszczony po wyjściu z pętli). Możemy więc to interpretować jako sytuację, gdzie zajęliśmy slot w tablicy, ale jeszcze go nie zapełniliśmy. Zauważmy, że w trakcie wykonywania funkcji deq przez wątek B 'head' będzie wskazywał na wartość null (komórka zajęta przez A, jak opisane powyżej). A zatem skoro head wskazuje na null - funkcja 'zwróci' EmptyException. Zauważmy jednak, że historia opisana powyżej nie może zostać zlinearyzowana. Ponieważ wywołujemy deq już po zakończeniu jednego enq, wiadomo więc, że kolejka nie jest pusta, lecz uzyskujemy EmptyException. Uzyskaliśmy tym samym sprzeczność - zatem pokazaliśmy, że przedstawiona implementacja jest niepoprawna. ## Zadanie 7 :::success Autor: Maria Szlasa ::: :::info ![](https://i.imgur.com/4G44uzw.png) ::: **AtomicInteger** An int value that may be updated atomically. An AtomicInteger is used in applications such as atomically incremented counters, and cannot be used as a replacement for an Integer. However, this class does extend Number to allow uniform access by tools and utilities that deal with numerically-based classes. **AtomicReference\<T\>** An object reference that may be updated atomically. **Punkt linearyzacji** miejsce w którym metoda daje efekt. **** Pokażemy, że enq nie ma pojedynczego punktu linearyzacji. ### Dla instrukcji *i = tail.getAndIncrement()* ![](https://i.imgur.com/tJ5M5nW.png) Najpierw wątek A zwiększy licznik, a następnie wątek B. Potem wątek B umieści swoją wartość w kolejce i zacznie się wykonywać q.deq. Ponieważ wątek A nie wstawił jeszcze swojej wartości, pod zajętym przez niego indeksem będzie null. Pętla for obróci się ponownie i ściągniemy y. ### Dla instrukcji *items[i].set(x)* ![](https://i.imgur.com/CTVw1p4.png) Wątek A otrzyma indeks i, a wątek B otrzyma i+1. Mimo, że punkt linearyzacji wątku B zakończy się szybciej (wątek B szykciej skończy wykonywać enq), to ponieważ wątek A otrzymał mniejszy indeks, to wątek C przeczyta wartość zapisaną przez A. ### Czy z tego wynika, że historia nie jest linearyzowalna? Nie! Punktem linearyzacji może być grupa instrukcji. ## Zadanie 8 :::success Autor: Hubert Obrzut ::: ![](https://i.imgur.com/AY7q4mo.png) ![](https://i.imgur.com/h3HWYD1.jpg)

    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