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

第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章 多级反馈队列

  1. 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

在这里插入图片描述

  1. 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中。

  1. How would you configure the scheduler parameters to behave just like a round-robin scheduler?
    只使用一个队列即可。因为这样一来所有的工作的优先级都相同,按照规则3进行轮转。
  2. 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
  1. 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提升一次优先级。
  2. 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章

  1. Compute the solutions for simulations with 3 jobs and random seeds of 1, 2, and 3.
python3 lottery.py -j 3 -s 1
  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?
    不会。导致进程基本不会被调度。
  2. 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.
    很公平。
  3. Howdoes your answer to the previous question change as the quantum size (-q) gets larger?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值