操作系统导论-课后作业-ch19

1.

本书在第6章中有过介绍,gettimeofday函数最多精确到us,并且大致精确(并不完全精确),需要多迭代几次减少误差,循环次数太多也会导致结束时间小于开始时间(即回滚)的现象,需要权衡。

2.

代码如下:

c 复制代码
#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>

#define PGSIZE 4096

//return us
int time_diff(struct timeval* t1, struct timeval* t2) {
    return (t2->tv_sec - t1->tv_sec) * 1000000 + (t2->tv_usec - t1->tv_usec);
}

int main(int argc, char* argv[]) {
    if (argc != 3) {
        fprintf(stderr, "too many or too few arguments\n");
        exit(1);
    }
    int i;
    int pageCount = atoi(argv[1]);
    int num = atoi(argv[2]);
    struct timeval curr, now;
    int size = (pageCount * PGSIZE) / sizeof(int);
    int arr[size];
    if (gettimeofday(&curr, NULL) == -1) {
        fprintf(stderr, "gettimeofday error\n");
        exit(1);
    }
    for (i = 0; i < num; ++i) {
        arr[(rand() % pageCount) * (PGSIZE / sizeof(int))] = i;
    }
    if (gettimeofday(&now, NULL) != 0) {
        fprintf(stderr, "gettimeofday error\n");
        exit(1);
    }
    printf("time elaps %d us\n", time_diff(&curr, &now));
    return 0;
}

运行结果如下:

100000次访问消耗1230us,平均每次访问时间为12.3ns

3.

c 复制代码
#!/usr/bin/env python
import os
i = 1
while i < 2000 :
        print '\npage num ' + str(i)
        val = os.system('./tlb ' + str(i) + ' ' + str(10000))
        i = i * 2

本处有点问题,可参考python------报错解决:/usr/bin/env: 'python\r': No such file or directory

运行结果如下所示:

4.

5.

禁止编译器优化,有两个方案,一个是在编译的时候增加选项-O0

6.

如果进程运行移到了另一个cpu,则很容易导致tlb需要重新缓存,命中率大大降低,需要加以限制

7.

修改代码tlb.c,添加初始化逻辑:

测试结果如下:

和题2测试结果相比,确实时间减少了不少

相关推荐
炸膛坦客19 小时前
FreeRTOS 学习:(二十五)任务时间统计相关 API 函数
stm32·操作系统·freertos
c++逐梦人1 天前
进程的优先级与切换
linux·服务器·操作系统
c++逐梦人1 天前
命令⾏参数和环境变量
linux·操作系统·进程
燃于AC之乐1 天前
【Linux系统编程】进程管理探秘:从硬件架构到僵尸/孤儿进程
linux·操作系统·硬件架构·进程管理·系统编程·冯诺依曼架构·僵尸、孤儿进程
a不是橘子2 天前
03在Ubuntu中验证PV操作
笔记·ubuntu·操作系统·虚拟机·os·pv操作
明洞日记2 天前
【软考每日一练002】进程调度机制详解
c++·ai·操作系统·进程
Hello_Embed3 天前
RS485 双串口通信 + LCD 实时显示(DMA+IDLE 空闲中断版)
笔记·单片机·学习·操作系统·嵌入式·freertos
崎岖Qiu3 天前
【OS笔记44】:磁盘存储管理
笔记·操作系统·os
炸膛坦客3 天前
FreeRTOS 学习:(二十六)FreeRTOS 专用延时函数(相对延时、绝对延时)
stm32·操作系统·freertos
galaxyffang3 天前
如何理解select、poll、epoll?
操作系统