<操作系统导论>第四章 抽象:进程

课后习题

首先要在Github上找到我们的作业资源
传送门:https://github.com/remzi-arpacidusseau/ostep-homework/
可以看到我们习题相关的python程序
在这里插入图片描述
里面有详细的描述比如代码的功能、如何使用、参数的含义等等。
在这里插入图片描述
这里有提到可以将这里的内容全部复制到ubuntu里面去,将
git clone https://github.com/remzi-arpacidusseau/ostep-homework/
复制到ubuntu中即可。之后便能运行我们需要的程序。
在这里插入图片描述
这样就找到了我们此次作业要用到的程序。

  1. 用以下标志运行程序: ./process-run.py -l 5:100,5:100。CPU的利用率(CPU的使用时间的百分比)应该是多少?为什么你知道这一点?利用-c标记查看你的答案是否正确。

在这里插入图片描述
在这里插入图片描述

先运行进程0,cpu占用100%,此时进程1处于就绪状态,耗时5个单位时间【1-5】。此时进程0结束,进程1开始运行,cpu占用率为100%,耗时5个单位时间【6-10】。

2.现在用这些表示运行: ./process-run.py -l 4:100,1:0.这些标志指定了一个包含4条指令的进程(都要使用CPU),并且只是简单地发出I/O并等待它完成。完成这两个进程需要多长时间?利用-c检查你的答案是否正确。
在这里插入图片描述
在这里插入图片描述

先运行进程0,有四个指令【1-4】,cpu占用率都是100%,此时进程1处于就绪状态。进程0结束,进程1请求I/O【5】,cpu阻塞【6-10】,之后请求完成【11】,进程1结束,总共耗时11个单位时间。

3.现在交换进程的顺序: ./process-run.py -l 1:0,4:100。现在发生了什么?交换顺序是否重要?为什么?同样,用-c看看你的答案是否正确。
在这里插入图片描述
在这里插入图片描述

进程0先请求I/O【1】,cpu阻塞该状态,同时进程1处于运行状态,I/O耗时4个单位时间【2-5】,I/O操作完成耗时一个单位时间【7】,进程1在等待I/O操作时同时进行,耗时4个单位时间【2-5】,总耗时7个单位时间。
交换进程顺序很重要!比如只有后一个进程需要I/O,那么前一个进程运行的时候后一个进程处于就绪状态;而只有前一个进程需要申请I/O的时候,前一个进程在等待的时候后一个进程可以同时运行。这就会产生不同的结果,不同的时间消耗。

4.现在探索另一些标志。一个重要的标志是-S,它决定了当一个进程发出I/O时系统如何反应。将标志设置为 SWITCH_ON_END,在进程进行I/O操作时,系统将不会切换到另一个程序,而是等待程序完成。当你运行以下两个程序时,会发生什么情况?一个执行I/O,另一个执行CPU工作。(-l 1:0,4:100 -c -S SWITCH_ON_END)
在这里插入图片描述

进程0请求I/O的时候,CPU阻塞该进程,又因为SWITCH_ON_END(在进程进行I/O操作时,系统将不会切换到另一个程序,而是等待程序完成),所以进程1处于就绪状态,请求I/O耗时1个单位时间【1】,等待I/O耗时5个单位时间【2-6】,I/O完成需要1个单位时间【7】,之后进程1开始运行,耗时4个单位时间【8-11】,总耗时11个单位时间

  1. 现在,运行相同的进程,但切换行为设置,在等待I/O时切换到另一个进程
    (-l 1:0,4:100 -c -S SWITCH_ON_IO)现在会发生什么?,利用-c来确定你的答案是否正确。
    在这里插入图片描述

显然,和第三问的答案完全一致,因此我们可以知道,如果没有写-S SWITCH_ON_IO或者-S SWITCH_ON_END那么系统会默认在等待I/O时切换到另一个进程。

(手敲题目好累qWq)

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
优化这段代码<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>图书购物车</title> <style> </style> <script src="js/vue.js"></script> </head> <body> <div id="demo"> <table border="1"> <tr> <td></td> <td>书籍名称</td> <td>出版日期</td> <td>价格</td> <td>购买数量</td> <td>操作</td> </tr> <tr> <td></td> <td>{{books1.name}}</td> <td>{{books1.date}}</td> <td>¥{{books1.price}}</td> <td><button @click="down(books1)">-</button>{{books4.count}}<button @click="up(books1)">+</button></td> <td><button @click="del">移除</button></td> </tr> <tr> <td></td> <td>{{books2.name}}</td> <td>{{books2.date}}</td> <td>¥{{books2.price}}</td> <td> <button @click="down(books2)">-</button>{{books4.count}}<button @click="up(books2)">+</button> </td> <td><button @click="del">移除</button></td> </tr> <tr> <td></td> <td>{{books3.name}}</td> <td>{{books3.date}}</td> <td>¥{{books3.price}}</td> <td> <button @click="down(books3)">-</button>{{books4.count}}<button @click="up(books3)">+</button> </td> <td><button @click="del">移除</button></td> </tr> <tr> <td></td> <td>{{books4.name}}</td> <td>{{books4.date}}</td> <td>¥{{books4.price}}</td> <td> <button @click="down(books4)">-</button>{{books4.count}}<button @click="up(books4)">+</button> </td> <td><button @click="del">移除</button></td> </tr> </table> <div>总价: ¥{{sum}}</div> </div> <!-- js部分 --> <script> const vm = new Vue({ el: "#demo", data: { books1: { name: '《算法导论》', date: '2006-9', price: 85.00, count: 1 }, books2: { name: '《UNIX编程艺术》', date: '2006-2', price: 59.00, count: 1 }, books3: { name: '《编程珠玑》', date: '2008-10', price: 39.00, count: 1 }, books4: { name: '《代码大全》', date: '2006-3', price: 128.00, count: 1 } }, computed: { sum () { return this.books1.price * this.books1.count + this.books2.price * this.books2.count + this.books3.price * this.books3.count + this.books4.price * this.books4.count } }, methods: { down (books1) { this.books1.count = books1.count - 1; } } }) </script> </body> </html>
04-21
优化建议: 1. 使用数组来存储书籍信息,可以避免重复的代码和变量 2. 使用计算属性来计算总价,避免重复计算 3. 将操作封装成方法,并使用可复用的组件来渲染每一行 优化后的代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>图书购物车</title> <style> </style> <script src="js/vue.js"></script> </head> <body> <div id="demo"> <table border="1"> <tr> <td></td> <<td>书籍名称</td> <td>出版日期</td> <td>价格</td> <td>购买数量</td> <td>操作</td> </tr> <book-row v-for="(book, index) in books" :book="book" :index="index" :key="index" @change-count="changeCount" @remove-book="removeBook" /> </table> <div>总价: ¥{{totalPrice}}</div> </div> <!-- js部分 --> <script> Vue.component('book-row', { props: ['book', 'index'], methods: { down() { if (this.book.count > 1) { this.book.count--; this.$emit('change-count'); } }, up() { this.book.count++; this.$emit('change-count'); }, remove() { this.$emit('remove-book', this.index); } }, template: ` <tr> <td></td> <td>{{book.name}}</td> <td>{{book.date}}</td> <td>¥{{book.price}}</td> <td> <button @click="down">-</button> {{book.count}} <button @click="up">+</button> </td> <td> <button @click="remove">移除</button> </td> </tr> ` }); const vm = new Vue({ el: "#demo", data() { return { books: [ { name: '书籍1', date: '2022-01-01', price: 30, count: 1 }, { name: '书籍2', date: '2022-01-01', price: 40, count: 1 }, { name: '书籍3', date: '2022-01-01', price: 50, count: 1 }, { name: '书籍4', date: '2022-01-01', price: 60, count: 1 } ] }; }, methods: { changeCount() { // 重新计算总价 }, removeBook(index) { this.books.splice(index, 1); // 重新计算总价 }, }, computed: { totalPrice() { return this.books.reduce((acc, cur) => acc + cur.price * cur.count, 0); } } }); </script> </body> </html>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值