利用管道通信(pipe)测量进程间的上下文切换(context switch)开销

利用管道通信(pipe)测量进程间的上下文切换(context switch)开销

《https://pages.cs.wisc.edu/~remzi/OSTEP/cpu-mechanisms.pdf》

Measuring the cost of a context switch is a little trickier. The lmbench benchmark does so by running two processes on a single CPU, and setting up two UNIX pipes between them; a pipe is just one of many ways processes in a UNIX system can communicate with one another. The first process then issues a write to the first pipe, and waits for a read on the second; upon seeing the first process waiting for something to read from the second pipe, the OS puts the first process in the blocked state, and switches to the other process, which reads from the first pipe and then writes to the second. When the second process tries to read from the first pipe again, it blocks, and thus the back-and-forth cycle of communication continues. By measuring the cost of communicating like this repeatedly, lmbench can make a good estimate of the cost of a context switch. You can try to re-create something similar here, using pipes, or perhaps some other communication mechanism such as UNIX sockets.

然后,第一个进程(子进程)向第一个管道发出写操作,并等待第二个管道的读操作;在看到第一个进程等待从第二个管道读取内容时,操作系统会将第一个进程置于阻塞状态,并切换到另一个进程(父进程),后者从第一个管道读取内容,然后向第二个管道写操作。当第二个进程再次尝试从第一个管道中读取数据时,它就会阻塞,这样来回循环的通信就继续进行。

在这里插入图片描述

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <assert.h>const int N = 1e2 + 10; // Number of iterations for context switchesint main(int argc, char *argv[]) {int pipefd[2][2]; // Array to hold two pipes: one for each direction of communicationchar buf; // Buffer to hold the character read from the pipe// Check if the correct number of arguments are providedif (argc != 3) {fprintf(stderr, "Usage: %s <string> <string>\n", argv[0]);exit(EXIT_FAILURE);}// Create the two pipesif (pipe(pipefd[0]) == -1 || pipe(pipefd[1]) == -1) {perror("pipe");exit(EXIT_FAILURE);}// Fork the process to create a child processint rc1 = fork();if (rc1 < 0) {// Fork failedfprintf(stderr, "fork failed\n");exit(EXIT_FAILURE);} else if (rc1 == 0) { // Child processclose(pipefd[0][0]); // Close unused read end of the first pipeclose(pipefd[1][1]); // Close unused write end of the second pipefor (int i = 0; i < N; ++i) {// Write to the first pipewrite(pipefd[0][1], argv[1], strlen(argv[1]) + 1);// Read from the second pipewhile (read(pipefd[1][0], &buf, 1) > 0) {// ************//write(STDOUT_FILENO, &buf, 1);if (buf == '\0') {//write(STDOUT_FILENO, "\n", 1);break;}}}// Close the used pipe ends before exitingclose(pipefd[0][1]);close(pipefd[1][0]);exit(EXIT_SUCCESS);} else {// Parent processclose(pipefd[0][1]); // Close unused write end of the first pipeclose(pipefd[1][0]); // Close unused read end of the second pipestruct timeval start, end;// Get the start timeint rc1 = gettimeofday(&start, NULL);for (int i = 0; i < N; ++i) {// Read from the first pipewhile (read(pipefd[0][0], &buf, 1) > 0) {// ************//write(STDOUT_FILENO, &buf, 1);if (buf == '\0') {//write(STDOUT_FILENO, "\n", 1);break;}}// Write to the second pipewrite(pipefd[1][1], argv[2], strlen(argv[2]) + 1);}// Get the end timeint rc2 = gettimeofday(&end, NULL);assert(rc1 == 0 && rc2 == 0);// Calculate elapsed timedouble elapsed = (double) end.tv_sec + (double) end.tv_usec / 1e6 - ((double) start.tv_sec + (double) start.tv_usec / 1e6);// Calculate average context switch timedouble context_switch = elapsed / (2 * N);printf("Total time: %f seconds\n", elapsed);// Print the average context switch timeprintf("Average context switch time: %lf seconds\n", context_switch);// Close the used pipe endsclose(pipefd[1][1]);close(pipefd[0][0]);}return 0;
}
运行结果:[chap6] :) cc -o contextSwitch contextSwitch.c 
[chap6] :) ./contextSwitch 1111 222222
Total time: 0.010745 seconds
Average context switch time: 0.000049 seconds
[chap6] :) ./contextSwitch 1111 222222
Total time: 0.014709 seconds
Average context switch time: 0.000067 seconds
[chap6] :) ./contextSwitch 1111 222222
Total time: 0.011982 seconds
Average context switch time: 0.000054 seconds
[chap6] :) 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1424451.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

山东大学计算机考研数据分析,初复试占比6:4,复试内容不少得花精力准备!

山东大学&#xff08;ShandongUniversity&#xff09;&#xff0c;简称山大&#xff0c;位于中国山东&#xff0c;是中华人民共和国教育部直属的综合性全国重点大学&#xff0c;是国家“211工程”、“985工程”重点建设院校&#xff0c;入选“111计划”、“珠峰计划”、“卓越工…

kafka用java收发消息

用java客户端代码来对kafka收发消息 具体代码如下 package com.cool.interesting.kafka;import org.apache.kafka.clients.consumer.ConsumerConfig; import org.apache.kafka.clients.consumer.ConsumerRecord; import org.apache.kafka.clients.consumer.ConsumerRecords; i…

基于springboot+vue+Mysql的大学生社团活动平台

开发语言&#xff1a;Java框架&#xff1a;springbootJDK版本&#xff1a;JDK1.8服务器&#xff1a;tomcat7数据库&#xff1a;mysql 5.7&#xff08;一定要5.7版本&#xff09;数据库工具&#xff1a;Navicat11开发软件&#xff1a;eclipse/myeclipse/ideaMaven包&#xff1a;…

电脑缺失api-ms-win-crt-runtime-l1-1-0.dll文件的几种修复方法

当您在使用电脑过程中遇到程序启动失败&#xff0c;提示缺少“api-ms-win-crt-runtime-l1-1-0.dll”文件时&#xff0c;不必过于焦虑&#xff0c;此问题通常与Windows系统的Visual C Redistributable组件未正确安装或损坏有关。小编将介绍5种修复电脑缺失api-ms-win-crt-runtim…

STM32-09-IWDG

文章目录 STM32 IWDG1. IWDG2. IWDG框图3. IWDG寄存器4. IWDG寄存器操作步骤5. IWDG溢出时间计算6. IWDG配置步骤7. 代码实现 STM32 IWDG 1. IWDG IWDG Independent watchdog&#xff0c;即独立看门狗&#xff0c;本质上是一个定时器&#xff0c;这个定时器有一个输出端&#…

webpack生成模块关系依赖图示例:查看构建产物的组成部分 依赖关系图

npm i -D webpack-bundle-analyzer core-js babel-loaderwebpack.config.js const BundleAnalyzerPlugin require(webpack-bundle-analyzer).BundleAnalyzerPlugin; module.exports {entry: ./src/index.js,output: {filename: main.js,},// mode: production, // 或者 produ…

学前端网络安全这块还不懂?细说CSRF

什么是CSRF&#xff1f; 举个栗子&#xff0c;比如我们需要在某个博客上删除一个文章&#xff0c;攻击者首先在自己的域构造一个页面&#xff0c;使用了一个img标签&#xff0c;其地址指向了删除博客的链接。攻击者诱使目标用户&#xff0c;也就是博客主访问这个页面&#xff…

如何利用命令提示符列出文件?这里提供了几个实例供你参考

序言 什么命令可以用来列出目录中的文件&#xff1f;如何在命令提示符Windows 10/11中列出文件&#xff1f;很多人对这些问题感到困惑。在这篇文章中&#xff0c;我们详细解释了命令提示符列出文件的主题。 CMD&#xff08;命令提示符&#xff09;是一个功能强大的Windows内置…

Android实践:查看Activity信息

问题&#xff1a;本地Android SDK的monitor无法正常运行&#xff0c;看不了进程相关信息&#xff0c;确认当前显示Activity十分不便 解决办法&#xff1a;使用adb shell指令可以快速查看 命令&#xff1a; adb shell dumpsys activity activities 这个命令用于获取Android设…

8个迹象表明你需要一台新笔记本电脑,看一下你的笔记本是否有其中一个

序言 当你第一次打开你的笔记本电脑的盒子时,它会以最高性能运行,电池寿命更长,过热最小,资源使用效率高。然而,随着笔记本电脑的老化,它将不能满足预期用途。以下几个迹象表明,可能是时候寻找并投资一款新设备了。 你的设备不再具有预期用途 如果你的笔记本电脑不再…

大模型日报2024-05-15

大模型日报 2024-05-15 大模型资讯 OpenAI推出全新AI模型GPT-4o&#xff0c;具备文本、图像和音频处理能力 摘要: OpenAI公司继ChatGPT后&#xff0c;最新推出了名为GPT-4o的AI模型。这一模型不仅能够理解和生成文本&#xff0c;还新增了图像和音频的解释及生成功能。GPT-4o作为…

战网国际服怎么下载 暴雪战网一键下载安装图文教程

战网国际版&#xff0c;或称为Battle.net全球版&#xff0c;是暴雪娱乐构建的一项跨越国界的综合游戏交流平台&#xff0c;它无视地理限制&#xff0c;旨在服务全球每一个角落的游戏爱好者。不同于地区专属版本&#xff0c;国际版为玩家开启了一扇无门槛的大门&#xff0c;让每…

一文搞懂CPU是如何进行计算的?

你好&#xff0c;我是 shengjk1&#xff0c;多年大厂经验&#xff0c;努力构建 通俗易懂的、好玩的编程语言教程。 欢迎关注&#xff01;你会有如下收益&#xff1a; 了解大厂经验拥有和大厂相匹配的技术等 希望看什么&#xff0c;评论或者私信告诉我&#xff01; 文章目录 一…

参考文献自检指南

参考文献作为论文的最后组成部分&#xff0c;可能不是加分项&#xff0c;但是做不好的话绝对会被吐槽&#xff0c;而且是个要命的减分项。因此要做好检查&#xff0c;以下是一些可以遵循的规范。&#xff08;如有疏漏&#xff0c;欢迎指出&#xff09; .bib文件 1.字段的选…

Java——运行环境搭建

操作步骤&#xff1a; JDK的下载环境变量PATH的配置JDK的重点目录 bin&#xff1a; 该目录主要存放命令lib&#xff1a;该目录主要存放JDK的类库lib/src.zip:JDK源码 下载JDK 官网地址&#xff1a;https://www.oracle.com/ 安装 双击.exe文件 安装完成&#xff01; 配置环…

docker安装minio附带图片

1.拉镜像 docker pull minio/minio 2.创建挂载点目录 mkdir -p /usr/local/minio/config mkdir -p /usr/local/minio/data 3.创建minio容器 docker run \ -p 19000:9000 \ -p 9090:9090 \ --nethost \ --name minio \ -d --restartalways \ -e "MINIO_ACCESS_KEYmini…

VUE之旅—day2

文章目录 Vue生命周期和生命周期的四个阶段created应用—新闻列表渲染mounted应用—进入页面搜索框就获得焦点账单统计&#xff08;Echarts可视化图表渲染&#xff09; Vue生命周期和生命周期的四个阶段 思考&#xff1a; 什么时候可以发送初始化渲染请求&#xff1f;&#xff…

学习笔记-C++

目录 1、何为常量 2、关键字 3、实型 4、水平制表符 5、string字符串 6、C中的三目运算符 7、随机数种子 8、结构体 9、各的区 10、引用 11、函数默认参数 12、函数占位参数 13、函数重载 14、私有属性 15、让另一个类作为本类的成员 16、声明和实现的文件…

Java开发大厂面试第03讲:线程的状态有哪些?它是如何工作的?

线程&#xff08;Thread&#xff09;是并发编程的基础&#xff0c;也是程序执行的最小单元&#xff0c;它依托进程而存在。一个进程中可以包含多个线程&#xff0c;多线程可以共享一块内存空间和一组系统资源&#xff0c;因此线程之间的切换更加节省资源、更加轻量化&#xff0…

win11快速安装mysql数据库系统

win11快速安装mysql数据库系统 1、下载 1.1 打开官网 1.2 向下滚动页面 1.3 进入下载选项 1.4 下载8.0.4 LTS 1.5 开始下载 1.6 下载中 2、解压 大家注意&#xff0c;此时解压后目录是没有data目录的。 3、数据库初始化 3.1 管理员身份打开CMD 开始菜单上&#xff0c;输入…