作業系統
      • 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
    • Engagement control
    • 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 Versions and GitHub Sync Note Insights Sharing URL Help
Menu
Options
Engagement control 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Chp 3 Process ###### tags: `作業系統` [TOC] ## Structure of Process + <font color=#ff0000>**考**</font>**Layout** ![](https://i.imgur.com/DDPWXs2.png =80%x) + **Process State** - new: The process is being created - running: Instructions are being executed - waiting: The process is waiting for some event to occur - ready: The process is waiting to be assigned to a processor - terminated: The process has finished execution <font color=#ff0000>**考**</font> ![](https://i.imgur.com/h95Hpb1.png) Note: Buffer Overflow >[color=#00a000] > >分配Memory時process占用到kernel的部分 >[name=Omnom] ## Process Control Block (PCB) * a.k.a task control block * Definition: Store information associated with each process * Contents - Process state - Program counter - CPU registers - CPU scheduling information - Memory-management information - Accounting information - I/O status information ## Scheduling ### Scheduling queues 1. **Job queue** * contain all process * 通常在大型主機才有 2. **Ready queue** * 待在main memory的process準備被執行 3. **Device queue** * wait for I/O device ### Scheduler Type ![](https://i.imgur.com/9GM2kmZ.png =75%x) * CPU scheduler (Short-term) - select next-executed process and allocates CPU - invoke frequently * Job scheduler (Long-term) - select process brought into the ready queue - find good process mix - invoke **infrequently** - long-term scheduler strives for good process mix * Medium-term - Remove process from memory, store on disk, bring back in from disk to continue execution: **swapping** - Removing process from memory reduces the degree of multiprogramming ![](https://i.imgur.com/j8yerUj.png =80%x) ### Process Type according to Time Consumption * I/O-Bound - spend more time seeking **I/O** operations - CPU burst : many, short * CPU-Bound - spend more time on **Computations** - CPU burst : few, long ### Multitasking in Mobile Systems * **foreground**: - application currently open - appearing on the **display**. * **background**: - application remains in memory - does **not occupy the display screen** ### Context Switch * with context switch, **Multitasking** is possible * time it takes are highly dependent on hardware support ![](https://i.imgur.com/uS2lyOp.png =80%x) ## Operations on Processes ### Creation * Process is identified and managed via a **process identifier** (**pid**) * Resource sharing option - Parent and children share all resources - Children share subset of parent’s resources - Parent and child share no resources * Execution option - Parent & Child excecute concurrently - Parent wait for child's termination * Example - fork(): create new process - exec(): a new process ![](https://i.imgur.com/P0ucxmy.png) ### Termination * Child **finished** and then exit * **Aborted** by Parent 1. Child exceeds allocated resource 2. task assigned to child isn't required 3. parent exit ## InterProcess Communication (IPC) * independent v.s. cooperating (待補) >[color=#ff00ff]一個task分成好幾個subtask由好幾個process同時進行運算,就是cooperating process的一種情況(computation speedup),或者說這些process會使用到大家一起分享的記憶體區塊,所以說會彼此影響。 >[name=Neko] ### process cooperation 1. Information Sharing - e.g. copy, paste 2. Computational Speedup - break task into **subtasks** - subtasks execute in **parallel** - require multiple processing cores 3. Modularity - dividing the system functions into separate processes or threads ### Producer-Consumer Problem $i.e.$ Buffer using >[color=#00a000] > >Bounded Buffer在例子中看起來是circular queue >[name=Omnom] ### Communication Models (待整理) ![](https://i.imgur.com/FoE02tS.png =60%x) #### **Shared Memory** * 宣告memory時OS才參與,剩餘管理交由user process * 需要 **Process Synchronization** * Pros: Faster * Cons: cache coherence issues, ensures no writing to the same location simultaneously * Producer-Consumer Problem + unbounded-buffer : no limit on the size of the buffer + bounded-buffer : a fixed buffer size #### **Message Passing** * OS管理、mailbox * Pros: useful for exchanging small amounts of data, no conflicts, easier to implement in a distributed system * Cons: Slower * require **Communication Link** (待修) :::spoiler Issues - How to establish ? - one link multiple processes? - pair of processes mul tiple link? - Capacity of a link - size of a message fixed or variable? - unidirectional or bi-directional? ::: * Implementation - Physical 1. Shared memory 2. Hardware bus 3. Network - Logical: 1. Direct or indirect 2. Synchronous or asynchronous 3. Automatic or explicit buffering #### Logical Implementation * Direct - Processes must name each other explicitly - message contain **process name** - Links are established automatically - A link is associated with **exactly** one pair of communicating processes - usually bi-directional - send(P, message)、receive(Q, message), P、Q are processes * Inderect (use mailbox) - message contain **mailbox name**(mailbox不用時可以destroy掉) - Each mailbox has a unique id - Processes can communicate only if they share a mailbox - A link may be associated with many processes - send(A, message)、receive(A, message), A is a mailbox - operation: + create a new mailbox (port) + send and receive messages through mailbox + destroy a mailbox * Message-passing can be blocking or non-blocking * **Synchronous(Blocking)** - Blocking send: sender is blocked until the message is received - Blocking receive: receiver is blocked until a message is available - If *both* send and receive are blocking, we have a **rendezvous** * **Asynchronous(Non-Blocking)** - Non-blocking send: the sender sends the message and continue(送完後不等,直接往下走) - Non-blocking receive: the receiver receives: + A valid message, or + Null message * buffering - buffer messages (Queue) 1. Zero capacity: no messages are queued. (need rendezvous) 2. Bounded capacity: finite length, sender waits if queue is full 3. Unbounded capacity: infinite length, sender never waits ## Examples of IPC Systems (待補) ### POSIX ### Mach ## Communication in Client-Server Systems ### Sockets * Define: endpoint of communication * in Java: 1. Connection-oriented (TCP)(待補) + Virtual + reliable 2. Connectionless (UDP) + unreliable 3. MulticastSocket class + data can be sent to multiple recipients ### Remote Procedure Calls (RPC) ![](https://i.imgur.com/9IQp1Gs.png =80%x) 1. abstract the procedure-call mechanism for use between systems with network connections **Stubs** : **client-side** proxy for the actual procedure on the server 2. Data representation handled via **External Data Representation (XDL)** format to account for different architectures (Big-endian and little-endian >[color=#00a000] > >e.g. [摘] >因為數據傳輸的數據包必須是二進位的,你直接丟一個Java對象過去,人家可不認識,你必須把Java對象序列化為二進位格式,傳給Server端,Server端接收到之後,再反序列化為Java對象。 > >[原文網址:RPC](https://kknews.cc/news/yz3mjma.html) >[name=Omnom] * <font color=#ff0000>**考**</font> Difference between Sockets and Remote Procedure Calls (RPC): - Sockets allow only unstructured stream of bytes(傳送的資料無結構) - RPC provides functions to pass parameters(傳送的資料有結構) ### Pipes * Definition: allowing two processes to communicate * Ordinary pipes(單向): a.k.a. anonymous pipes - a parent process creates a pipe and communicate with its child process - Producer writes to one end, Consumer reads from the other end >[color=#00a000] > >看起來就是Queue >不過和messeage passing的差異是啥? >[name=Omnom] >[color=#FF00FF]Pipe是FIFO,但是message queue其實不是。 >要從message queue裡面讀資料要使用msgrcv()這個function >```ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,int msgflg);``` >可以藉由指定msgtyp決定要從message queue中讀出什麼type的資料,說明文件中說了```If msgtyp is greater than 0, then the first message in the queue of type msgtyp is read``` >而這也顯示出了Pipe跟message queue的差異,Pipe是沒有辦法指定型態的,你在讀寫的時候可以指定要讀出或寫入多少的byte,也就是說你自己必須要知道你這次讀要讀多少資料才會是完整的message,這是程式設計者要自己處理的問題。 >另外,pipe只能被創建他的Process以及child process使用,而message queue則沒有此限制,pipe在其中一端的process終止後,就沒辦法再被其他的process讀取,在message queue的情況下process可以寫入資料後就終止,但其他的process仍然可以讀取到他剛剛被寫入的資料。 >[name=Neko] * Named pipes(雙向): more powerful than ordinary pipes. can be accessed <font color=#ff0000>without</font> a parent-child relationship.

    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