kentaniguchi
    • 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
    # ECS150 Project2 ## Members - Daisuke Horiuchi (919535721) - Ken Taniguchi (919537457) ## Summary The goal of our project is to create a user-level threads library named `uthread`. With `uthread`, create multiple threads in a process and create an API for managing them as a static library. When multiple threads are used, a lock is applied to ensure data integrity, interrupts are generated like timers to prevent one thread from taking too long, and threads are switched in a round-robin scheduling. `uthread` can be implemented by user-level calls as APIs. ## queue API The Queue API provides functions at the user-level to perform operations on queues. Our queue API is a First In First Out structure, which means that it is possible to retrieve data in order of oldest to newest. To achieve `O(1)` operations, we created Queue using linked lists. Each element is represented by a node, and the queue is represented by linking addresses. #### The queue struct has the following elements: - head: address to the oldest element - tail: address to the latest element - length: length of the queue #### The following six functions are provided as the queue API: - `queue_create` - `queue_destroy` - `queue_dequeue` - `queue_delete` - `queue_iterate` - `queue_length` ### queue_create Creating an empty queue with a length of 0. ### queue_destroy Deleting a queue and releasing used memory. ### queue_dequeue Removing the oldest element from the queue and moving head to the next element. ### queue_delete For a given data, if there is the same data in the queue, delete it. If there are two or more, delete the oldest one. The link of the queue is unlinked by moving the pointer to the next node. The deleted node releases its memory. ### queue_iterate Change the value of each element of the queue by adapting the given function to each element. ### queue_length Returns the length of elements in the queue. ## uthread API The uthread API provides thread-related functions to the user-level. A thread is an execution flow that runs concurrently within a process. In this project, we implement parallel processing, there is always one thread that consumes CPU time. ### Each thread is in one of the following states: | State | Description | | -------- | -------- | | running | a thread that is currently executing | | ready | a thread that is waiting executing | | blocked | a thread that is blocked by semaphore | | zombie | a thread that has completed execution | Each thread has its data stored in the `uthread_tcb` struct and maintains its state, context, and stack. Each thread is managed by Ready queue, Blocked queue, and Current thread shared by this process. #### The uthread API provides following four functions to the user-level: - `uthread_run` - `uthread_create` - `uthread_yield` - `uthread_exit` ### uthread_run When `uthread_run` is called, one process runs by this function, and other multiple threads could be created by the process. For a given function, use `uthread_create()` to create a thread. As long as there is a ready queue or current thread, `uthread_yield()` is called and CPU is allocated to each thread in turn. ### uthread_create Creates a new thread for the given function and puts it into the ready queue. ### uthread_yield If there are waiting threads, get the oldest thread among them. The currently the running thread is stopped and the acquired thread is executed. The stopped thread is stored in the ready queue. ### uthread_exit Sends the currently executing thread to the Zombie state and releases its memory. Then, the next thread to be executed is retrieved from the ready queue and it is executed. ### Helper functions (Following three functions are not uthread API) #### uthread_current Returns the currently executing thread. #### uthread_block Block the currently executing thread. Moves to the currently executing thread Blocked queue, stops execution, and executes the thread in the ready queue. #### uthread_unblock Place the blocked thread in the waiting queue. Remove the given thread from the Blocked queue and move it to the ready queue. ## semaphore API The semaphore API provides functions related to semaphores for exclusion control to the user side. When a resource is accessed by multiple threads, if the resources are used at the same time, it will not be consistent. Therefore, it is necessary to always allow access to only one thread. This is called exclusion control. Semaphore is used to realize this exclusion control by increasing or decreasing the value of semaphore each time a resource is accessed. #### A semaphore, defined by a `struct`, called `semaphore` contains following: - `look`: a lock to exclusively control the value of the semaphore - `internal_value`: value of the semaphore (x>0) - `waiting queue`: a queue of threads blocked by the semaphore Since this project assumes a concurrent process using multiple threads and not a parallel process, spinlock is not necessary, but we implemented spinlock for extensibility. #### Semaphore provides four functions to the user-level: - `sem_create` - `sem_destroy` - `sem_down` - `sem_up` ### sem_create() A semaphore is created by `sem_create()`. Only the number of threads given as the initial value can access the resources managed by this semaphore at this moment. ### sem_destroy() After the use of semaphore, `sem_destroy()` will be deleted and it will release memory. ### sem_down() When accessing a resource managed by a semaphore, `sem_down()` is called to check if the `internal_value` is greater than 0, that is, if the thread can access the resource, and if so, the resource is accessed. If the `internal_value` is 0, the thread cannot access the resources at this moment and will be stored in the waiting_queue and move the state to a blocking state. While the resource is being accessed, the `internal_value` is decreased by one to indicate to semaphore that the resource is being used. ### sem_up() This releases access to a resource blocked by the above functions. To tell the semaphore that access has been released, increase the `internal_value` by one. Then, retrieve the oldest blocked task stored in `waiting_queue` and change the state from blocked to ready. ## preemption The Preemption provides the following four functions to the user-level. - `preempt_disable` - `preempt_enable` - `preempt_start` - `preempt_stop` ### preempt_disable Start a critical section, preventing other threads from being executed by timer interrupt. Use this function to perform operations that must not be interrupted or interfered with under any circumstances. ### preempt_enable Terminate a critical section. When this function is called, the preempt signal is re-enabled and the timer interrupt is triggered again. ### preempt_start Generates 100 signals per second. These signals call `uthread_yield()` to prevent one thread from using too much CPU time. ### preempt_stop Stops the signals that were set to be generated by preempt_start(). ### Testing Preempt Our implementation of `test_preempt.c` is to consume CPU execution time during the execution of `thread1`. The purpose of this test case is to see whether one thread can be correctly switched to another thread by Round-Robin scheduling. The difficulty in writing this test case was that using sleep() does not consume CPU execution time. Therefore, we decided to use clock_t to consume CPU execution time.

    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