c - 如何通过管道发送不同数量的数据并测量它们之间的执行时间?
问题描述
我目前正在尝试通过 Linux 中的父进程和子进程之间的管道发送大量不同数量的数据。
我发送的数据量是:1 KB、10 KB、100 KB、1 MB、10 MB 和 100 MB。
问题是,尽管数据包越来越大,但每种情况下的执行时间非常相似,实际上它们应该增加。
这是代码:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <sys/time.h>
#include <time.h>
char* generateData(int kbNum) {
int bytes = 1024 * kbNum;
char* data = malloc(bytes);
for (int i = 0; i<bytes; i++) data[i] = '*';
return data;
}
void errorMessage(const char *message){
printf("%s",message);
exit(1);
}
void printTimes(long int* times){
int fileSize = 1;
bool bigger = false;
for (int i = 0; i < 6; i++){
if (bigger) printf("El tiempo para %dMB fue de %ld μs usando tuberias\n", fileSize, times[i]);
else printf("El tiempo para %dKB fue de %ld μs usando tuberias\n", fileSize, times[i]);
if (fileSize < 100) fileSize = fileSize * 10;
else {
fileSize = 1;
bigger = true;
}
}
}
void checkErrors(int processId, int pipesPointers[2]){
for (int i=0; i<2; i++){
if (pipesPointers[i] < 0){
// Generating the corresponding error message for pipe error
char message[] = "Error starting the pipe ";
strcat(message, (char*) &i);
errorMessage(message);
}
}
// Generating the corresponding error message for the fork
if (processId < 0) errorMessage("Error generating the fork");
}
void childrenProcess(int pipeWrite, int pipeRead){
int check = 1;
for (int i = 0; i < 100001; i = i * 10){
char* data = malloc(1024 * i);
// Get Data Package
read(pipeRead, &data, sizeof(data));
// Sending check
write(pipeWrite, &check, sizeof(int));
}
}
void parentProcess(int pipeWrite, int pipeRead){
struct timeval start, stop;
long int times[6];
int index = 0;
for (int i = 1; i < 100001; i = i * 10){
char* data = generateData(i);
gettimeofday(&start, NULL);
int dataCheck;
// Sending Data Package to consumer
write(pipeWrite, &data, sizeof(data));
// Getting the check confirmation
read(pipeRead, &dataCheck, sizeof(dataCheck));
// Get the time elapsed time
gettimeofday(&stop, NULL);
times[index] = (stop.tv_sec - start.tv_sec) * 1000000 + stop.tv_usec - start.tv_usec;
index++;
free(data);
}
printTimes(times);
}
void startProgram(){
int pipePointers[2], parentPipes[2], childrenPipes[2];
pid_t processId = 0;
// Creating emparented processes and pipes
pipePointers[0] = pipe(parentPipes);
pipePointers[1] = pipe(childrenPipes);
processId = fork();
checkErrors(processId, pipePointers);
// Spliting the code that witch process will execute
if (processId == 0){
close(childrenPipes[0]);
close(parentPipes[1]);
childrenProcess(childrenPipes[1], parentPipes[0]);
close(childrenPipes[1]);
close(parentPipes[0]);
}
else if (processId > 0){
close(childrenPipes[1]);
close(parentPipes[0]);
parentProcess(parentPipes[1], childrenPipes[0]);
close(childrenPipes[0]);
close(parentPipes[1]);
}
}
int main(int argc, char const *argv[]){
startProgram();
return 0;
}
以下是输出时间:
The elapsed time for 1KB was 41 μs using pipes.
The elapsed time for 10KB was 88 μs using pipes.
The elapsed time for 100KB was 74 μs using pipes.
The elapsed time for 1MB was 79 μs using pipes.
The elapsed time for 10MB was 34 μs using pipes.
The elapsed time for 100MB was 24 μs using pipes.
非常感谢您的帮助,我会注意任何意见或答案。
对于其他一切,祝您有美好的一天!
解决方案
推荐阅读
- javascript - 如何将悬停颜色添加到具有线性渐变边框的 div?
- android-emulator - 带有 vnc 服务器的无头仿真器
- javascript - 我需要提取字符串中数字的前 6 位和后 4 位,并使用正则表达式删除相同的前导零
- json - powershell json打印父母和孩子
- django - 更新现有模型的 FieldField
- django - Django inineformset 保存和删除
- asp.net-core - 如何在 AddFacebook AspNetCore 身份验证方案上正确设置 auth_type 参数?
- android - 无法访问动态功能模块的资源
- java - 如何从 XML 文件生成带有 jacksonxml 注释的 java 类
- android - 将数据从广播接收器发送到活动