《操作系统导论》部分代码习题

tech2022-07-30  149

第5章

8.Write a program that creates two children, and connects the standard output of one to the standard input of the other, using the pipe() system call.

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/wait.h> int main(int argc, char *argv[]) { int rc; int fd[2], i; pipe(fd); for(i = 0; i < 2; i++) { rc = fork(); if(rc <= 0) break; } if(rc < 0) exit(1); else if(i == 0) { close(fd[1]); char buf[1024]; read(fd[0], buf, 1024); printf("%s\n", buf); } else if(i == 1) { close(fd[0]); char buf[1024]; scanf("%s", buf); write(fd[1], buf, strlen(buf) + 1); } else { for(int j = 0; j < 2; j++) wait(NULL); } return 0; }

第8章 多级反馈队列

Run a fewrandomly-generated problemswith just two jobs and two queues; compute the MLFQ execution trace for each. Make your life easier by limiting the length of each job and turning off I/Os. ./mlfq.py -n 2 -j 2 -m 10

How would you run the scheduler to reproduce each of the examples in the chapter? ./mlfq.py -n 3 -q 10 -l 0,200,0 -c

./mlfq.py -n 3 -q 10 -l 0,180,0:100,20,0 -c

注意:该行代码的执行结果与上图有出入,因为上图假设长工作已经运行了一段时间了,而执行该代码时长工作首先被放入queue 2中。

How would you configure the scheduler parameters to behave just like a round-robin scheduler? 只使用一个队列即可。因为这样一来所有的工作的优先级都相同,按照规则3进行轮转。Craft a workload with two jobs and scheduler parameters so that one job takes advantage of the older Rules 4a and 4b (turned on with the -S flag) to game the scheduler and obtain 99% of the CPU over a particular time interval. ./mlfq.py -q 100 -l 0,100,99:0,100,0 -i 0 -S -c Given a systemwith a quantum length of 10 ms in its highest queue, howoften would you have to boost jobs back to the highest priority level (with the -B flag) in order to guarantee that a single long-running (and potentiallystarving) job gets at least 5% of the CPU? 最坏的情况是,该长工作只有被放在最高优先级队列中时,才有被调度的机会。 在1000ms的CPU时间中,至少需要每200ms提升一次优先级。One question that arises in scheduling is which end of a queue to add a job that just finished I/O; the -I flag changes this behavior for this scheduling simulator. Play around with some workloads and see if you can see the effect of this flag. 执行完调度的进程应该插入到队列的首端还是末端?比较一下。(略)

第9章

Compute the solutions for simulations with 3 jobs and random seeds of 1, 2, and 3. python3 lottery.py -j 3 -s 1 Now run with two specific jobs: each of length 10, but one (job 0) with just 1 ticket and the other (job 1) with 100 (e.g., -l 10:1,10:100). What happens when the number of tickets is so imbalanced? Will job 0 ever run before job 1 completes? How often? In general, what does such a ticket imbalance do to the behavior of lottery scheduling? 不会。导致进程基本不会被调度。When runningwith two jobs of length 100 and equal ticket allocations of 100 (-l 100:100,100:100), how unfair is the scheduler? Run with some different random seeds to determine the (probabilistic) answer; let unfairness be determined by how much earlier one job finishes than the other. 很公平。Howdoes your answer to the previous question change as the quantum size (-q) gets larger?
最新回复(0)