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, &param);
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, &param);
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的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

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

  5. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  6. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  7. 《Cracking the Coding Interview》——第16章:线程与锁——题目6

    2014-04-27 20:25 题目:关于java中标有synchronized的成员方法? 解法:这代表同一个对象实例的synchronized方法不能被多个线程同时调用.注意有这么多个地方都加粗 ...

  8. 《Cracking the Coding Interview》——第16章:线程与锁——题目5

    2014-04-27 20:16 题目:假设一个类Foo有三个公有的成员方法first().second().third().请用锁的方法来控制调用行为,使得他们的执行循序总是遵从first.seco ...

  9. 《Cracking the Coding Interview》——第16章:线程与锁——题目1

    2014-04-27 19:09 题目:线程和进程有什么区别? 解法:理论题,操作系统教材上应该有很详细的解释.我回忆了一下,写了如下几点. 代码: // 16.1 What is the diffe ...

随机推荐

  1. 如何让.NET Core应用的配置与源文件保持同步?

    配置的同步涉及到两个方面:第一,对原始的配置文件实施监控并在其发生变化之后从新加载配置;第二,配置重新加载之后及时通知应用程序进而使后者能够使用最新的配置.接下来我们利用一个简单的.NET Core控 ...

  2. Python基本数据类型(一)

    一.int的函数说明(部分函数Python2特有,Python3已删除,部分函数Python3新增:) class int(object): """ int(x=0) - ...

  3. COGS 182. [USACO Jan07] 均衡队形

    ★★   输入文件:lineup.in   输出文件:lineup.out   简单对比时间限制:4 s   内存限制:128 MB 题目描述 农夫约翰的 N (1 ≤ N ≤ 50,000) 头奶牛 ...

  4. pat甲级1085

    1085 Perfect Sequence (25 分) Given a sequence of positive integers and another positive integer p. T ...

  5. 使用ABAP和JavaScript代码生成PDF文件的几种方式

    ABAP 方法1:使用ABAP + Adobe Lifecycle Enterprise Service 详细步骤参考我的博客Convert word document into PDF via Ad ...

  6. IOS笔记 : 一些小技巧

    计算单元格高度,在自定义cell中 -(void) resizeTheHeight{ CGFloat contentWidth = 280; UIFont *font = [UIFont fontWi ...

  7. soap使用xml调用webapi后返回xml信息进行JSON转换处理,以顺丰查询接口为例

    expressUrl = string.Format(可以卸载配置文件的域名URL + "/bsp-oisp/ws/expressService"); StringBuilder ...

  8. 2018.8.19 mybatis 环境搭建---配置mysql 。(Windows环境下面)

    安装mysql Install/Remove of the Service Denied!错误的解决办法 在windos 的cmd下安装mysql 在mysql的bin目录下面执行: mysqld - ...

  9. caffe怎么把全连接层转成convolutional层

    caffe中有把fc层转化为conv层的,其实怎么看参数都是不变的,对alex模型来说,第一个fc层的参数是4096X9216,而conv的维度是4096x256x6x6,因此参数个数是不变的,只是需 ...

  10. jQuery插件的使用和写法

    插件(plugin)也称为扩展(Extension),是一种遵循一定规范的应用程序接口编写出来的程序. jQuery的易扩展性,吸引了来自全球的开发者来共同编写jQuery的插件. jQuery表单验 ...