《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 ...
随机推荐
- May 8th 2017 Week 19th Monday
Art lies in concealing art. 隐而不露即艺术. Sometimes, concealing is much more seductive than totally naked ...
- 搭建TFTP服务器配置
实验内容: TFTP是TCP/IP协议族中的一个用来在客户机与服务器之间进行简单文件传输的协议,提供不复杂,开销不大的文件传输服务.TFTP承载在UDP上,提供不可靠的数据传输服务,不提供存取授权与认 ...
- Entity Framework 6事务回滚
使用EF6你有新的事务处理可以使用类似于: 复制代码 using (var context = new PostEntityContainer()) { using (var dbcxtransact ...
- jade简介
模板引擎:将动静部分糅合的一种实现机制或者技术 var items = [ {title:'..',photo:'http://',id:1,desc:'a'}, {title:'..',photo: ...
- 2017.11.9 如何利用JS做登陆验证界面
()案例----JavaScript实现输入验证 需要验证的表单输入域和要求 用户名不能为空,是否符合规定的格式 密码长度是否超过6,两次密码输入一致 邮箱地址:邮箱地址必须符合邮箱形式 ~~~注意提 ...
- 改变shape solid color
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...
- java斗地主扑克 扑克牌 洗牌 发牌 Collection 集合练习
package com.swift.poker; import java.util.ArrayList; import java.util.Collections; /*训练考核知识点:Collect ...
- jquery操作DOM 元素(2)
.after() 在匹配的元素集合中的每个元素后面插入参数指定的内容,作为其兄弟节点. .after(content[,content]) content HTML字符串 DOM 元素 元素数组 对象 ...
- Python 统计不同url svn代码变更数
#!/bin/bash/python # -*-coding:utf-8-*- #svn统计不同url代码行数变更脚本,过滤空行,不过滤注释. import subprocess,os,sys,tim ...
- 描述linux目录结构以及目录结构命名规定
FHS全称(Filesystem Hierarchy Standard),中文意思是目录层次标准,是linux的目录规范标准. 详情点击查看 FHS定义了两层规范: 第一层:“/”目录下的各个目录应该 ...