diff --git a/src/blog/post-1.md b/src/blog/en/post-1.md similarity index 100% rename from src/blog/post-1.md rename to src/blog/en/post-1.md diff --git a/src/blog/post-2.md b/src/blog/en/post-2.md similarity index 100% rename from src/blog/post-2.md rename to src/blog/en/post-2.md diff --git a/src/blog/en/post-3.md b/src/blog/en/post-3.md new file mode 100644 index 0000000..81eafa8 --- /dev/null +++ b/src/blog/en/post-3.md @@ -0,0 +1,61 @@ +--- +title: "[Study Notes] A Step-by-Step Analysis of Why Peterson's Algorithm Does Not Deadlock" +pubDate: 2025-06-10 +description: "Study notes on Peterson's Algorithm" +author: "Cloverta" +image: + url: "https://files.seeusercontent.com/2026/03/25/sTq9/pasted-image-1774456630694.webp" + alt: "zako2" +tags: ["Peterson's algorithm", "Operating System"] +--- + +In my OS course, I came across an interesting algorithm — Peterson's Algorithm. So why does Peterson's Algorithm satisfy the three conditions: "mutual exclusion", "progress", and "bounded waiting"? + +First, here is the pseudocode: + +```cpp +bool flag[2]; // Array indicating intention to enter critical section, initially false +int turn = 0; // turn indicates which process is given priority to enter the critical section + +// Process P0 +flag[0] = true; // First set its own flag to true, declaring it needs the critical section +turn = 1; // Let P1 execute first if P1 needs the critical section +while (flag[1] && turn == 1); // Check if P1 needs the critical section +CRITICAL_SECTION; +flag[0] = false; +REMAINDER_SECTION; + +// Process P1 +flag[1] = true; +turn = 0; +while (flag[0] && turn == 0); +CRITICAL_SECTION; +flag[1] = false; +REMAINDER_SECTION; +``` + +Let's analyze it case by case. + +Assume that P0 and P1 are executing concurrently, and coincidentally they both complete the first step together — both of their flags are false. + +At this point: + +> **[Case 1]** +> If P0 gets on the CPU first, it sets turn = 1; +> P1 gets on the CPU, sets turn = 0; +> P0 gets on the CPU, checks flag and turn — finds turn has been changed to 0, the waiting condition is not met, so P0 enters the critical section; +> P1 gets on the CPU, checks flag and turn — finds flag[0] is true and turn is unchanged, so P1 waits. +> P0 gets on the CPU and finishes using the critical section, sets flag[1] = false; +> P1 gets on the CPU, finds flag[0] is false, stops waiting and enters the critical section; +> +> **[Case 2]** +> If P0 gets on the CPU first, sets turn = 1; +> P0 continues using the CPU, finds flag[0] is true and turn is still 1, the waiting condition is met, so P0 waits; +> P1 gets on the CPU, sets turn = 0; +> P1 continues using the CPU, finds flag[1] is true and turn is still 0, the waiting condition is met, so P1 waits; +> P0 gets on the CPU, finds turn has become 0, the waiting condition is not met, so P0 exits the wait and enters the critical section; +> P1 gets on the CPU, finds the waiting condition still holds, so P1 continues waiting; +> P0 gets on the CPU and finishes using the CPU, sets flag[0] = false; +> P1 gets on the CPU, finds flag[0] == false, the waiting condition is not met, stops waiting and enters the critical section; + +_YES, IT WORKS ON MY MACHINE._ diff --git a/src/blog/zh/post-1.md b/src/blog/zh/post-1.md new file mode 100644 index 0000000..3fb19d5 --- /dev/null +++ b/src/blog/zh/post-1.md @@ -0,0 +1,24 @@ +--- +title: '测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试' +pubDate: 2022-07-01 +description: 'This is the first post of my new Astro blog.' +author: 'Astro Learner' +image: + url: 'https://s2.loli.net/2022/05/01/UNzy8c6pTHBSuMO.jpg' + alt: 'The Astro logo on a dark background with a pink glow.' +tags: ["astro", "blogging", "learning in public", "Hello"] +--- + +Welcome to my _new blog_ about learning Astro! Here, I will share my learning journey as I build a new website. + +## What I've accomplished + +1. **Installing Astro**: First, I created a new Astro project and set up my online accounts. + +2. **Making Pages**: I then learned how to make pages by creating new `.astro` files and placing them in the `src/pages/` folder. + +3. **Making Blog Posts**: This is my first blog post! I now have Astro pages and Markdown posts! + +## What's next + +I will finish the Astro tutorial, and then keep adding more posts. Watch this space for more to come. \ No newline at end of file diff --git a/src/blog/zh/post-2.md b/src/blog/zh/post-2.md new file mode 100644 index 0000000..824d44a --- /dev/null +++ b/src/blog/zh/post-2.md @@ -0,0 +1,24 @@ +--- +title: 'My Second Blog Post' +pubDate: 2022-07-01 +description: 'This is the first post of my new Astro blog.' +author: 'Astro Learner' +image: + url: 'https://files.seeusercontent.com/2026/03/25/0rSi/rikka-manga.jpeg' + alt: 'The Astro logo on a dark background with a pink glow.' +tags: ["astro", "blogging", "learning in public"] +--- + +Welcome to my _new blog_ about learning Astro! Here, I will share my learning journey as I build a new website. + +## What I've accomplished + +1. **Installing Astro**: First, I created a new Astro project and set up my online accounts. + +2. **Making Pages**: I then learned how to make pages by creating new `.astro` files and placing them in the `src/pages/` folder. + +3. **Making Blog Posts**: This is my first blog post! I now have Astro pages and Markdown posts! + +## What's next + +I will finish the Astro tutorial, and then keep adding more posts. Watch this space for more to come. \ No newline at end of file diff --git a/src/blog/post-3.md b/src/blog/zh/post-3.md similarity index 99% rename from src/blog/post-3.md rename to src/blog/zh/post-3.md index 870c408..d382d07 100644 --- a/src/blog/post-3.md +++ b/src/blog/zh/post-3.md @@ -13,7 +13,7 @@ tags: ["peterson算法", "操作系统"] 先上伪代码: -``` +```cpp bool flag[2]; // 表示进入临界区意愿的数组,初始值都为false int turn = 0; // turn表示优先让哪个进程进入临界区 diff --git a/src/components/Posts/PostList.astro b/src/components/Posts/PostList.astro index c6cb235..4dd27dd 100644 --- a/src/components/Posts/PostList.astro +++ b/src/components/Posts/PostList.astro @@ -79,7 +79,12 @@ const sortedPosts = [...allPosts].sort( new Date(b.data.pubDate).getTime() - new Date(a.data.pubDate).getTime(), ); -const latestPosts = sortedPosts.slice(0, 5); +const filteredPosts = sortedPosts.filter((post: any) => { + const postLang = post.id.split("/")[0]; + return postLang === lang; +}); + +const latestPosts = filteredPosts.slice(0, 5); ---