《Cracking the Coding Interview》——第16章:线程与锁——题目2
2014-04-27 19:14
题目:如何测量上下文切换的时间?
解法:首先,上下文切换是什么,一搜就知道。对于这么一个极短的时间,要测量的话,可以通过放大N倍的方法。比如:有A和B两件事,并且经常一起发生,每件只需要花几纳秒。如果你把A事件连续做几百万次,而B时间只做了几次,这样就能排除B事件对于测量的影响。如果总时间S = mA + nB。当m >> n 时,A≈S / m。下面的测量方法类似于打乒乓球,在主线程和副线程间互相传递一个令牌,这个令牌可以是变量、管道之类的用于通信的工具。与此同时,利用操作系统提供的调度函数强制决定调度顺序,用于触发上下文切换。下面这份代码我是照着读完了才写的,初次碰见这个题目还真是无从下手,因为完全没有想到如何触发上下文切换。
代码:
// 16.2 How to measure the time of a context switch?
// reference code from http://blog.csdn.net/dashon2011/article/details/7412548
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <sched.h>
#include <sys/types.h>
#include <unistd.h> int main()
{
int x, i, fd[], p[];
int tv[];
char send = 's';
char receive;
pipe(fd);
pipe(p);
pipe(tv);
struct timeval tv_start,tv_end;
struct sched_param param;
param.sched_priority = ; while ((x = fork()) == -);
if (x == ) {
sched_setscheduler(getpid(), SCHED_FIFO, ¶m);
gettimeofday(&tv_start, NULL);
write(tv[], &tv_start, sizeof(tv_start));
//printf("Before Context Switch Time1.sec %u s\n", tv_start.tv_sec);
//printf("Before Context Switch Time1.usec %u us\n", tv_start.tv_usec);
for (i = ; i < ; i++) {
read(fd[], &receive, );
//printf("Child read!\n");
write(p[], &send, );
//printf("Child write!\n");
}
exit();
}
else {
sched_setscheduler(getpid(), SCHED_FIFO, ¶m);
for (i = ; i < ; i++) {
write(fd[], &send, );
//printf("Parent write!\n");
read(p[], &receive, );
//printf("Parent read!\n");
}
gettimeofday(&tv_end, NULL);
//printf("After Context SWitch Time1.sec %u s\n", tv_end.tv_sec);
//printf("After Context SWitch Time1.usec %u us\n", tv_end.tv_usec); }
read(tv[], &tv_start, sizeof(tv_start));
//printf("Before Context Switch Time2.sec %u s\n", tv_start.tv_sec);
//printf("Before Context Switch Time2.usec %u us\n", tv_start.tv_usec);
//printf("Before Context Switch Time %u us\n", tv_start.tv_usec);
//printf("After Context SWitch Time2.sec %u s\n", tv_end.tv_sec);
//printf("After Context SWitch Time2.usec %u us\n", tv_end.tv_usec);
printf("Task Switch Time: %f us\n", ( * (tv_end.tv_sec - tv_start.tv_sec) + tv_end.tv_usec - tv_start.tv_usec) / 20000.0); return ;
}
《Cracking the Coding Interview》——第16章:线程与锁——题目2的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 《Cracking the Coding Interview》——第16章:线程与锁——题目6
2014-04-27 20:25 题目:关于java中标有synchronized的成员方法? 解法:这代表同一个对象实例的synchronized方法不能被多个线程同时调用.注意有这么多个地方都加粗 ...
- 《Cracking the Coding Interview》——第16章:线程与锁——题目5
2014-04-27 20:16 题目:假设一个类Foo有三个公有的成员方法first().second().third().请用锁的方法来控制调用行为,使得他们的执行循序总是遵从first.seco ...
- 《Cracking the Coding Interview》——第16章:线程与锁——题目1
2014-04-27 19:09 题目:线程和进程有什么区别? 解法:理论题,操作系统教材上应该有很详细的解释.我回忆了一下,写了如下几点. 代码: // 16.1 What is the diffe ...
随机推荐
- C#学习基础,面向对象的三大特征
学习C#编程,相信大家除了经常接触的是hello world之外,更多的是进一步的去熟悉这门语言的基本特征,以及有哪些概念是我们必要掌握了解的,相信大家都是会知道面向对象的三大特性分别是:封装,继承, ...
- To my dear friends in SFAE
To my dear friends in SFAE, 这不是farewell,我还在西门子大家庭.2018年1月份我会转到SLC MCBU.在SFAE十年,一些敢想,唠叨唠叨~ 十年弹指一挥间.记得 ...
- ABAP和Java SpringBoot的单元测试
ABAP 在ABAP类里,本地类(Local Class)里用关键字FOR TESTING声明过的方法, 在单元测试启动后会自动被调用到. Spring Boot 在Spring及Spring Boo ...
- ArcGIS License Server Administrator 10.2 无法启动许可的解决办法
刚刚重装了电脑,安装ArcGIS的时候,安装完desktop之后又安装了License Manager,结果把破解文件替换完之后,发现ArcGIS License Server Administrat ...
- 进程管理—进程描述符(task_struct)
http://blog.csdn.net/qq_26768741/article/details/54348586 当把一个程序加载到内存当中,此时,这个时候就有了进程,关于进程,有一个相关的叫做进程 ...
- mysql随机字符串函数
drop function if exists rand_str; delimiter $$ ) charset 'utf8' begin # 定义接收初始化类型 ) ; # 定义初始化数字 ) '; ...
- Velocity中加载vm文件的三种方式
Velocity中加载vm文件的三种方式: a. 加载classpath目录下的vm文件 /** * 初始化Velocity引擎 * --VelocityEngine是单例模式,线程安全 * @th ...
- 第一个AngularJS表达式实例
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- C#中this保留字的用法
一.this保留字 this保留字一般只在构造函数.类的方法和类的实例中使用.它有以下含义: ?在类的构造函数中出现的this,则作为一个值类型,表示对正在构造的对象本身的引用. ?在类的方法中出现的 ...
- python显示灰度图
import matplotlib import matplotlib.pyplot as plt %matplotlib inline im=plt.imread('../lena.jpg', py ...