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 13, grupa cz. 12-14, 9. czerwca 2022 ###### tags: `SYK21` `ć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 | 9 | | ---------------------- | ----- | --- | --- | --- | --- | --- | --- | --- | --- | Daniel Boguszewski | x | x | x | x | x | x | | | | Adam Ciężkowski | | | | | | | | | | Jacek Długopolski | x | x | x | x | x | x | x | x | ==x== | Jakub Fila | x | x | x | x | x | x | | | | Krzysztof Grynkiewicz | | | | | | | | | | Michał Hetnar | x | x | x | x | x | x | x | | | Hubert Jabłoński | | | | | | | | | | Antoni Kamiński | | | | | | | | | | Paweł Karaś | | | | | | | | | | Emil Kisielewicz | x | x | x | x | x | x | | | | Kacper Komenda | x | x | x | x | x | x | x | x | x | Filip Komorowski | x | x | x | x | x | x | x | x | x | Piotr Piesiak | x | x | x | x | x | x | x | x | x | Artur Krzyżyński | x | x | x | x | x | x | x | x | x | Patryk Mazur | x | x | x | ==x== | x | x | | | | Szymon Mleczko | x | x | x | x | x | x | x | x | x | Michał Mróz | X |==X==| X | X | X | X | X | X | X | Dominik Olejarz | x | x | x | x | x | x | | x | | Magdalena Słonińska | ==X== | X | X | X | X | X | | | | Oleś Kulczewicz | X |==X==| X | X | X | X | | | | Adam Szeruda | X | X | X | X | X | X | X | X | X | Kamil Tasarz | X | X | X | X | X | X | | X | | Michał Zieliński | ==X== | X | X | X | X | X | X | X | X | ::: Tu można do-deklarować zadanie 7 z listy 12.: * * * :::info **Uwaga:** Po rozwiązaniu zadania należy zmienić kolor nagłówka na zielony. ::: ## Zadanie 1 :::success Autor: Magdalena Słonińska ::: > Dwa procesy wykonują funkcję `inc()` operującą na współdzielonej zmiennej `counter`, o początkowej wartości równej `0`. ``` void inc() { counter = counter + 1;} ``` > Jakie są możliwe wartości tej zmiennej po zakończeniu pracy przez obydwa procesy? Czy ma tu miejsce sytuacja wyścigu? *Sytuacja wyścigu* ma miejsce kiedy wartości współdzielonych zmiennych zależą od kolejności wykonywania instrukcji maszynowych. ![](https://i.imgur.com/F2ecMIp.jpg) Mamy dwie możliwe sytuacje końcowe: 1. `counter = 1` jeśli w pierwszej kolejności wykonamy oba odczyty, zarówno $P_1$ i $P_2$ wywoła `inc()`, a na koniec oba procesy zapiszą wynik do pamięci 2. `counter = 2` jeśli najpierw $P_1$ wykona odczyt i zapis do pamięci, a następnie $P_2$ zrobi to samo Powyżej skonstruowaliśmy ciągi instrukcji generujące różne wartości współdzielonej zmiennej `counter`, czyli w tym przypadku mamy do czynienia z sytuacją wyścigu. ## Zadanie 2 :::success Autor: Oleś Kulczewicz ::: Two processes execute the function sum ``` void sum() { for(inti=1;i<=50;i++) counter = counter + 1; } ``` The operation ``` counter = counter + 1; ``` is not atomic and is in fact ``` register1 = counter register1 = register1 + 1 counter = register1 ``` :::info we mark **FIRST** as blue process ::: :::warning and **SECOND** as yellow process ::: ### Max value: counter == 100 Normal execution of the processes interchangeably **counter == 0** :::info register1 = counter register1 = register1 + 1 counter = register1 ::: **counter == 1** :::warning register2 = counter register2 = register2 + 1 counter = register2 ::: **counter == 2** ... **counter == 98** :::info register1 = counter register1 = register1 + 1 counter = register1 ::: **counter == 99** :::warning register2 = counter register2 = register2 + 1 counter = register2 ::: **counter == 100** ### Min value: counter == 2 Both processes load the initial counter, then first process executes, only after that second process executes ``` Pierwszy czyta // counter = 0 Drugi czyta // counter = 0 Pierwszy idzie do 49, czyli jeszcze jeden krok mu został // counter = 49 Drugi zwieksza i zapisuje counter 1 // counter = 1 Pierwszy czyta // counter = 1 Drugi robi się do końca // counter = 50 Pierwszy zwiększa do 2 i zapisuje // counter = 2 ``` **counter == 0** :::info register1 = counter ::: :::warning register2 = counter ::: :::info register1 = register1 + 1 counter = register1 ::: **counter == 1** :::info register1 = counter register1 = register1 + 1 counter = register1 ::: **counter == 2** ... **counter == 49 && register2 == 0** :::warning register2 = register2 + 1 counter = register2 ::: **counter == 1** :::info register1 = counter ::: :::warning register2 = counter register2 = register2 + 1 counter = register2 ::: **counter == 2** ... **counter == 50** :::info register1 = register1 + 1 counter = register1 ::: **counter == 2** ## Zadanie 3 :::success Autor: Daniel Boguszewski ::: ![](https://i.imgur.com/eU8Eqrl.png) ![](https://i.imgur.com/NaOSp53.png) 1. Warunek Wzajemnego Wykluczania jest spełniony (2 procesy nie mogą wykonywać sekcji krytycznej w tym samym czasie): - Załóżmy nie wprost, że procesy $P_0$ oraz $P_1$ znajdują się wspólnie w sekcji krytycznej. - Wobec tego tego oba procesy musiały zakończyć wcześniej wykonywanie procedur $lock(0)$ oraz $lock(1)$. - Aby było to możliwe, wartości $flag[0]$ oraz $flag[1]$ muszą być równe $true$. - BSO jednak, jeśli sekcja krytyczna procesu $P_1$ zaczęła się wykonywać, wtedy proces ten musiał zakończyć wcześniej obieg pętli $while(flag[0] == true);$, implikując, iż $flag[0] = false$, co stoi w sprzeczności z założeniem $flag[0] = true$. $\blacksquare$ 2. Warunek Postępu nie jest spełniony (jeżeli żaden proces nie wykonuje sekcji krytycznej, wtedy wybór kolejnego procesu do jego wykonaniu musi odbyć się w skończonym czasie): - Zakładamy, że żaden z procesów $P_i$ nie wykonuje sekcji krytycznej, wobec czego musi wcześniej wykonać procedurę $lock(i)$. - Rozważmy BSO ogólności sytuację, w której proces $P_0$ wykonuję instrukcję $flag[0] = true$ po czym zrzeka się prawa do procesora na rzecz procesu $P_1$. - W wyniku tego proces $P_1$ wykonuję procedurę $lock(1)$, ustawia $flag[1] = true$ i przechodzi do wykonania pętli $while(flag[0] == true);$. - Jednak ze względu na to, że $flag[0] = true$, procedura $lock(1)$ zawiesza się, aż wreszcie proces $P_1$ zrzeka się procesora, który przystępuje do przetwarzania procesu $P_0$. - Proces ten jednak wznawia swoje działanie na instrukcji $while(flag[1] == true);$ i również się zawiesza, w wyniku czego sekcja krytyczna w żadnym z procesów nigdy nie zostanie wykonana (zawieszony proces $P_1$ oczekuje na zawieszone $P_0$). $\blacksquare$ 3. Warunek Równoległego Oczekiwania jest spełniony (jeżeli proces zadeklarował chęć wykonania sekcji krytycznej, wtedy istnieje liczba $k \in \mathbb{N}$, ograniczająca możliwą liczbę wykonań sekcji krytycznej innych procesów): - Załóżmy BSO, że $P_1$ oczekuje na przejście do sekcji krytycznej, w czasie gdy $P_0$ już je wykonuje. Pokażemy, że $k = 1$. Z poprzednich rozważań wiemy, że w tej sytuacji musi zachodzić $flag[0] = flag[1] = true$. - Przyjmijmy teraz, że proces $P_0$ zakończył wykonywanie sekcji krytycznej i wykonał procedurę $unlock(0)$. Rozważmy przypadki: 1. $P_0$ zrzeka się procesora przed wykonaniem instrukcji $flag[0] = true$ procedury $lock(i)$, w wyniku czego $P_1$ wznawia działanie na pętli $while(flag[0] == true);$, kończąc tym samym jej obieg i przechodzi do wykonania sekcji krytycznej ($\implies k = 1$). 2. $P_0$ nie zrzeka się procesora przed wykonaniem $flag[0] = true$, w wyniku czego $P_1$ nie będzie w stanie wykonać sekcji krytycznej, co implikuje jednak, że nie zostanie wykonana procedura $unlock(1)$. Następuje więc zawieszenie działania obu procedur, gdyż $flag[0] = flag[1] = true$, w czasie gdy oba procesy próbują wykonać pętlę $while(flag[1-i] == true);$, gdzie $i$ to indeks procesu $P_i$ ($i \in \{0, 1\}$). Z tego powodu nigdy nie dochodzi do ponownego (w tym wypadku drugiego) wykonania sekcji krytycznej procesów różnych od $P_1$, czyli $P_0$ ($\implies k = 1$). $\blacksquare$ ## Zadanie 4 :::success Autor: Patryk Mazur ::: ![](https://i.imgur.com/SG9qaKF.png) ![](https://i.imgur.com/S9FYwv5.png) ### 1. Mutual Exclusion Aby ten warunek nie był spełniony, musiałoby zachodzić ``` cpp= !(flag[j] && turn == j) && !(flag[i] && turn == i) ``` Czyli ``` cpp= (flag[j] == false && flag[i] == false) || (flag[j] == false && turn==j) || (turn==i && flag[i] == false) || (turn==i && turn==j) ``` ### 2. Progress Aby ten warunek nie był spełniony, musiałoby zachodzić ``` cpp= (flag[j] && turn == j) && (flag[i] && turn == i) ``` Czyli ``` cpp= turn == j && turn == i ``` Co z oczywistych względów jest niemożliwe ### 3. Bounded Waiting Jeżeli proces $P_i$ chce wejść do sekcji krytycznej, w której obecnie znajduje się $P_j$, to musi poczekać aż $P_j$ po wyjściu zmieni swoją flage na false, co złamie warunek pętli i wtedy $P_i$ wejdzie do sekcji krytycznej. Więc czas oczekiwania procesu $P_i$ jest ograniczony przez proces $P_j$ ## Zadanie 5 :::success Autor: Kamil Tasarz ::: ```cpp void lock(int my_id) { turn = 1 - my_id; flag[my_id] = true; while (flag[1 - my_id] == true && turn == 1-my_id); } void unlock(int my_id) { flag[my_id] = false; } ``` * $\color{#B22222}{\text{Mutual Exclusion}}$ Wartości początkowe: ```flag[0] = false, flag[1] = false, turn = 0``` P0 execute: ```cpp turn = 1; // flag[0] = false, flag[1] = false, turn = 1 ``` P1 execute: ```cpp turn = 0; // flag[0] = false, flag[1] = false, turn = 0 flag[1] = true; // flag[0] = false, flag[1] = true, turn = 0 while (flag[0] && turn == 0); //false //P1 rozpoczął wykonywanie sekcji krytycznej.. ``` P0 execute: ```cpp flag[0] = true; // flag[0] = true, flag[1] = true, turn = 0 while (flag[1] && turn == 1); // false //P0 rozpoczął wykonywanie sekcji krytycznej.. ``` * $\color{#32CD32}{\text{Progress}}$ * $\color{#32CD32}{\text{Bounded Waiting}}$ ## Zadanie 6 :::success Autor: Jakub Fila ::: ![](https://i.imgur.com/n2N5bNx.png) > Teraz funkcja lock nie działa poprawnie, ponieważ wyrażenie turn == 1-my_id będzie zawsze fałszywe. Oznacza to, że nie mamy spełnionego warunku Mutual Exclusion. Z uwagi na nielimitowany dostęp do sekcji krytycznej dla procesów, warunek progress jest spełniony, bounded waiting naturalnie również. 1. Warunek wzajemnego wykluczenia **nie jest spełniony** ![](https://i.imgur.com/3CGVRgU.png) 2. Warunek postępu **jest spełniony** - skoro procesy same mogą odblokować swoją turę, to niezależnie od flagi drugiego mogą wejść do sekcji krytycznej. 3. Warunek ograniczonego czekania **nie jest spełniony** - procesy mogą same odblokowywać swoje tury, tym bardziej mogą głodzić drugi proces: ![](https://i.imgur.com/eIGzASw.png) ## Zadanie 7 :::success Autor: Adam Szeruda ::: > Tym razem funkcje lock() i unlock() wyglądają następująco: ```c= void lock(int my_id) { flag[my_id] = true; while (flag[1 - my_id] == true) { if (turn != my_id) { flag[my_id] = false; while (turn != my_id); flag[my_id] = true; } } } void unlock(int my_id) { turn = 1 - my_id flag[my_id] = false; } ``` > Co można powiedzieć o tym algorytmie? - ✅ **Wzajemne wykluczenie** Proces może wejść do sekcji krytycznej jedynie, gdy wartość `turn` jest równe `my_id`. Wartość ta zmienia się, gdy drugi proces opuszcza sekcję krytyczną. - ✅ **Postęp** W przypadku, gdy oba procesy rozpoczną czekanie na wejście do secji krytycznej (ustawią `flag[my_id] = true`), proces o `my-id != turn` tymczasowo zwolni `flag`, pozwalając temu drugiemu wejście do sekcji krytycznej. - ❎ **Ograniczone czekanie** Rozważmy następujące wykonanie procesów: ``` P0 | P1 | flag[0] = false; | while (turn != 0); | | unlock(1); | turn = 0; | ... | lock(1); | while (flag[0] == true); | ... | unlock(1); | lock(1); | ... ``` Jeżeli procesowi P0 nie zostanie przydzielony procesor, to P1 może utrzymywać dostęp do sekcji krytycznej na wyłączność. ## Zadanie 8 :::success Autor: Kacper Komenda ::: ![](https://i.imgur.com/z5QshcS.png) Nie ma deadlocka, bo w sytuacji, gdy jeden ustawia zamek na false (w metodzie unlock), to oba wątki ponownie mogą go zdobyć. Nie zadziała za to warunek z n-bounded waiting, jeden z wątków może zostać zagłodzony (drugi w nieskończoność może zdobywać zamek) ## Zadanie 9 :::success Autor: Jacek Długopolski ::: ![](https://i.imgur.com/5ICKasW.jpg) ![](https://i.imgur.com/mAKSLRE.jpg) ![](https://i.imgur.com/2C2dbFp.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