日期:2014-05-16  浏览次数:20725 次

命名管道的通信效率的测试
C/C++ code
write.c
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pthread.h>
#include <time.h>

#define FIFO_NAME "/tmp/my_fifo"
#define BUFFER_SIZE PIPE_BUF   //4096
#define TEN_MEG (1024 * 1024 * 1) //决定每次循环传输数据的大小


void* thread_tick(void* arg)  //用来测试管道会不会阻塞进程
{
    printf("hello, world!\n");
    sleep(1);

}
void* thread_write(void* arg)
{
    int pipe_fd;
    int res;
    int bytes_sent = 0;
    char buffer[BUFFER_SIZE ];
    int count=0;    //决定总共循环多少次

    if (access(FIFO_NAME, F_OK) == -1) {
        res = mkfifo(FIFO_NAME, 0777);
        if (res != 0) {
            fprintf(stderr, "Could not create fifo %s\n", FIFO_NAME);
            exit(EXIT_FAILURE);
        }
    }
     while(count<1){
    printf("write: Process %d opening FIFO O_WRONLY\n", getpid());
    pipe_fd = open(FIFO_NAME, O_WRONLY);
    printf("write: Process %d result %d \n", getpid(), pipe_fd);

    if (pipe_fd != -1) {
        while(bytes_sent < TEN_MEG) {
            res = write(pipe_fd, buffer, BUFFER_SIZE);
            if (res == -1) {
                fprintf(stderr, "Write error on pipe\n");
                exit(EXIT_FAILURE);
            }
            bytes_sent += res;
        }
        (void)close(pipe_fd);
    }
    else {
        exit(EXIT_FAILURE);
    }

    printf("write: Process %d finished , count =%d\n", getpid(),count);
    count++;
  }
}

int main()
{
    pthread_t write;
    pthread_t tick;
    int resw, rest ;

        rest = pthread_create(&tick, NULL, thread_tick, 0);
    if(rest != 0)
    {
        perror("tick thread creation failed\n");
        exit(EXIT_FAILURE);
    }
    resw = pthread_create(&write, NULL, thread_write, 0);
    if(resw != 0)
    {
        perror("write thread creation failed\n");
        exit(EXIT_FAILURE);
    }

    resw = pthread_join(write, 0);
    rest = pthread_join(tick, 0);
    return 0;
}




C/C++ code
read.c
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>

#define FIFO_NAME "/tmp/my_fifo"
#define BUFFER_SIZE PIPE_BUF  //4096

int main()
{
    int pipe_fd;
    int res;
    char buffer[BUFFER_SIZE ];
    int bytes_read = 0;
    int count =0;
    memset(buffer, '\0', sizeof(buffer));
    while(count < 1){
    printf("read: Process %d opening FIFO O_RDONLY\n", getpid());
    pipe_fd = open(FIFO_NAME, O_RDONLY);
    printf("read: Process %d result %d\n", getpid(), pipe_fd);

    if (pipe_fd != -1) {
        do {
            res = read(pipe_fd, buffer, BUFFER_SIZE);
            bytes_read += res;
        } while (res > 0);
        (void)close(pipe_fd);
    }
    else {
        exit(EXIT_FAILURE);
    }

    printf("read: Process %d finished, %d bytes read , count =%d\n", getpid(), bytes_read,count);
    count++;
  }
}





目的:测试命名管道的通信效率
1) 在后台运行write程序(向管道内写数据)
2) 前台运行read程序(从管道内读数据),并利用Linux系统函数time 记录read的运行时间
3) 通过改变两个程序中的while循环次数来改变每次测试的通信数据的大小

我的测试结果是基于每次循环传输1M。
但是如果总共传输的数据不变,但是每次循环传输的数据变大(即循环次数变小了),那么程序的运行时间也会变大。
例如,总共传输50M,我循环50次,每次1M和循环1次,每次50M花费的时间是不一样的。
问题:
1)为什么会出现上面这中情况?
2)命名管道的传输速度跟哪些因素有关系?会受到CPU的运行速度和内存大小的影响么?


------解决方案--------------------
你循环50次,至少有50次的系统调用
而循环1次,只有一次系统调用