leonhsi
    • 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
    • Engagement control
    • 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 Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
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
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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Linux operating system project 2 ###### tags: `linux OS` ## Description * 在kernel中define 3個wait queue : project2_queue_1、project2_queue_2、project2_queue_3 * 新增兩個system call 1. int enter_wait_queue(int x), * process can use it to sleep in project2_queue_x, where x is equal to 1 or 2 or 3 2. int clean_wait_queue(int x) * process can use it to wake up **all** sleeping processes in project2_queue_x where x is equal to 1 or 2 or 3. ## user program * enter_queue.c : 用來檢驗enter_wait_queue() ```c= #include <stdlib.h> #include <stdio.h> #include <sys/time.h> #include <unistd.h> #include <sys/types.h> #include <sys/syscall.h> #define _GNU_SOURCE #define gettid() syscall(SYS_gettid) void main() { int random_num; int ttid, count; char str[300]; FILE *fp; struct timeval t1, t2; gettimeofday(&t1, NULL); ttid=gettid(); count=0; /*open a file for writing. The filename consist of the following two substrings, "file_" and the string converting from the result of gettid(). Hence, if the result of gettid is 123, the file name is file_123. We use file_gettid to call this file in the following pseudo code. */ char filename[30]; sprintf(filename, "file_%d.txt\n", ttid); for(;;) { gettimeofday(&t2, NULL); /*If the time difference between t2 and t1 is greater than 3 minutes, break;*/ int timediff=t2.tv_sec-t1.tv_sec; if (timediff>=180)break; random_num= (rand()%3)+1; count++; sprintf(str, "process %d is added into wait queue project2_queue_%d the %d th time at time ...\n", ttid, random_num, count); /*write the string stored in array str[] into file file_gettid.*/ printf("%d\n",random_num); fp = fopen(filename, "w"); fprintf(fp,str); fclose(fp); int a= syscall(359,random_num); printf("%d\n",a); } printf("process %d completes!\n", ttid); } ``` * wait_queue.c : 336 ```c= #include <linux/kernel.h> #include <linux/wait.h> #include <linux/sched.h> #include <linux/list.h> DECLARE_WAIT_QUEUE_HEAD(project2_queue_1); DECLARE_WAIT_QUEUE_HEAD(project2_queue_2); DECLARE_WAIT_QUEUE_HEAD(project2_queue_3); asmlinkage int enter_wait_queue(int x) { DEFINE_WAIT(p); if (x==1){ printk("project2_queue_1\n"); prepare_to_wait(&project2_queue_1,&p,TASK_INTERRUPTIBLE); printk("project2_queue_1 schedule\n"); schedule(); printk("project2_queue_1 exit.\n"); return 1; } else if (x==2){ printk("project2_queue_2\n"); prepare_to_wait(&project2_queue_2,&p,TASK_INTERRUPTIBLE); printk("project2_queue_2 schedule\n"); schedule(); printk("project2_queue_2 exit.\n"); return 1; } else { printk("project2_queue_3\n"); prepare_to_wait(&project2_queue_3,&p,TASK_INTERRUPTIBLE); printk("project2_queue_3 schedule\n"); schedule(); printk("project2_queue_3 exit.\n"); return 1; } return 0; } asmlinkage int clean_wait_queue(int x) { if (x==1){ printk("wake up Q1 \n"); wake_up_all(&project2_queue_1); return 1; } else if (x==2){ printk("wake up Q2 \n"); wake_up_all(&project2_queue_2); return 1; } else { printk("wake up Q3 \n"); wake_up_all(&project2_queue_3); return 1; } return 0; } ``` * clear_queue.c : 用來檢驗clear_wait_queue() ```c= #include <stdlib.h> #include <stdio.h> #include <sys/time.h> #include <unistd.h> #include <sys/types.h> #include <syscall.h> #include <time.h> #define _GNU_SOURCE #define gettid() syscall(SYS_gettid) void main() { int random_num1,random_num2; int ttid,count; char str[300]; struct timeval t1, t2; FILE *fp; gettimeofday(&t1, NULL); ttid=gettid(); count=0; srand(time(NULL)); /*open a file for writing. The filename consist of the following two substrings, "file_" and the string converting from the result of gettid(). Hence, if the result of gettid is 123, the file name is file_123. We use file_gettid to call this file in the following pseudo code. */ char filename[20]; sprintf(filename, "file_%d.txt\n", ttid); while(1) { gettimeofday(&t2, NULL); /*If the time difference between t2 and t1 is greater than 5 minutes, break;*/ int timediff=t2.tv_sec-t1.tv_sec; if (timediff>=15)break; random_num1= (rand()%3)+1; random_num2= (rand()%10); sleep(random_num2); sprintf(str, "Wake up all processes in wait queue project2_queue_%d at time ...\n", 1); /*write the string stored in array str[] into file file_gettid;*/ fp = fopen(filename, "w"); fprintf(fp, str); fclose(fp); syscall(360, random_num1); printf("Done Q%d\n", random_num1); } /*write the string "Clean wait wait queue project2_queue_1" into file file_gettid;*/ syscall(360, 1); str[100]="Clean wait wait queue project2_queue_1"; fp = fopen(filename, "w"); fprintf(fp, str); fclose(fp); //write the string "Clean wait wait queue project2_queue_2" into file file_gettid; syscall(360, 2); str[100]="Clean wait wait queue project2_queue_2"; fp = fopen(filename, "w"); fprintf(fp, str); fclose(fp); //write the string "Clean wait wait queue project2_queue_3" into file file_gettid; syscall(360, 3); str[100]="Clean wait wait queue project2_queue_3"; fp = fopen(filename, "w"); fprintf(fp, str); fclose(fp); //close file file_gettid; printf("process %d completes!\n", ttid); } ``` ## method wait_queue 提供4個 function 可以使用,兩個是用來將 process 加到 wait_queue 的: * sleep_on( struct wait_queue **wq ); * interruptible_sleep_on( struct wait_queue **wq ); 另外兩個則是將process從wait_queue上叫醒的。 * wake_up( struct wait_queue **wq ); * wake_up_interruptible( struct wait_queue **wq ); 如果是用 interruptible_sleep_on() 來將 process 放到 wait_queue 時,如果有人送一個 signal 給這個 process,那它就會自動從 wait_queue 中醒來。 但是如果你是用 sleep_on() 把 process 放到 wq 中的話,那不管你送任何的 signal 給它,它還是不會理你的。除非你是使用 wake_up() 將它叫醒。 sleep 有兩組。wake_up 也有兩組。wake_up_interruptible() 會將 wq 中使用 interruptible_sleep_on() 的 process 叫醒。 至於 wake_up() 則是會將 wq 中所有的 process 叫醒。包括使用 interruptible_sleep_on() 的 process。**所以根據project要求,我們應該用wake_up()** [wake up 家族funciton](https://blog.csdn.net/myxmu/article/details/7973990) [wait queue的使用](https://blog.csdn.net/u012218309/article/details/81148083?utm_medium=distribute.pc_relevant.none-task-blog-searchFromBaidu-7.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-searchFromBaidu-7.control) ## error message error: ‘project2_queue_1’ undeclared

    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