首页 > 解决方案 > 如何通过管道发送不同数量的数据并测量它们之间的执行时间?

问题描述

我目前正在尝试通过 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.

非常感谢您的帮助,我会注意任何意见或答案。

对于其他一切,祝您有美好的一天!

标签: clinuxprocesspipe

解决方案



推荐阅读