Cat unix命令多线程实现

时间:2014-03-16 16:26:13

标签: c unix pthreads concatenation cat

嗨,我试图实现比提供的更快的猫。

我目前的实现如下:

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#define BUF_SIZE 1024*1024*1024

char buffer[BUF_SIZE];
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond_var = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond_var2 = PTHREAD_COND_INITIALIZER;
int readed = 0;
/*
    Read characters from standard input and saves them to buffer
*/
void *consumer(void *data) {
    int r;
    while(1) {
        //---------CRITICAL CODE--------------
        //------------REGION------------------
        pthread_mutex_lock(&mutex);
        if (readed > 0)
        {
            pthread_cond_wait(&cond_var2, &mutex);
        }
        r = read(0, buffer, BUF_SIZE);
        readed = r;

        pthread_cond_signal(&cond_var);
        pthread_mutex_unlock(&mutex);
        //------------------------------------

        if (r == -1){
            printf("Error reading\n");
        }  
        else if (r == 0) {
            pthread_exit(NULL);
        }
    }
}

/*
    Print chars readed by consumer from standard input to standard output
*/
void *out_producer(void *data) {
    int w;
    while(1){    
        //---------CRITICAL CODE--------------
        //-------------REGION-----------------
        pthread_mutex_lock(&mutex);
        if (readed == 0)
        {
            pthread_cond_wait(&cond_var, &mutex);
        }
        w = write(1, buffer, readed); 
        readed = 0;
        pthread_cond_signal(&cond_var2);
        pthread_mutex_unlock(&mutex);
        //------------------------------------ 

        if (w == -1){
            printf("Error writing\n");
        } 
        else if (w == 0) {
            pthread_exit(NULL);
        }
    }
}

你有什么建议让它更快? 有任何想法吗? 我在考虑BUF_SIZE,您认为缓冲区的最佳大小是什么?

Main只是制作主题:

int main() {
    //  Program RETURN value
    int return_value = 0;

    //  in - INPUT thread
    //  out - OUTPUT thread
    pthread_t in, out;

    //  Creating in thread - should read from standard input (0)
    return_value = pthread_create(&in , NULL, consumer, NULL);
    if (return_value != 0) {
        printf("Error creating input thread exiting with code error: %d\n", return_value);
        return return_value;
    }

    //  Creating out thread - should write to standard output (1)
    return_value = pthread_create(&out, NULL, out_producer, NULL);
    if (return_value != 0) {
        printf("Error creating output thread exiting with code error: %d\n", return_value);
        return return_value;
    }

    return_value = pthread_join(in, NULL);
    return_value = pthread_join(out, NULL);

    return return_value;
}

1 个答案:

答案 0 :(得分:3)

如何向cat添加线程以使其更快?你不能在任何程序中抛出并行性并期望它运行得更快。

Cat基本上只是将每一行输入(通常是从文件)传输到输出。由于线条有序非常重要,因此必须使用互斥来避免比赛。

并行中速度的上限(cat可以运行的最快速度)不能高于cat,因为每个线程必须执行串行操作以及同步成本