c - pthread 程序中例程的额外执行时间花费了什么?

标签 c linux multithreading pthreads runtime

我写了四个不同的程序来计算两个文件中的总字数。这四个版本看起来大体相同。前三个版本使用两个线程来计数,只是三个语句的顺序不同。最后一个版本使用一个线程来计算。我会先列出每个版本的不同部分和公共(public)部分,然后是每个版本的输出和我的问题。

不同部分:

// version 1
count_words(&file1); 
pthread_create(&new_thread, NULL, count_words, &file2);
pthread_join(new_thread, NULL);

// version 2
pthread_create(&new_thread, NULL, count_words, &file2);
count_words(&file1);
pthread_join(new_thread, NULL);

// version 3
pthread_create(&new_thread, NULL, count_words, &file2);
pthread_join(new_thread, NULL);
count_words(&file1);

// version 4
count_words(&file1);
count_words(&file2);

通用部分:(将不同的部分插入到这个通用部分中,形成一个完整的版本)

#include <stdio.h>
#include <pthread.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>

#define N 2000

typedef struct file_t {
    char *name;
    int words;
} file_t;

double time_diff(struct timespec *, struct timespec *);
void *count_words(void *);

// Usage: progname file1 file2
int main(int argc, char *argv[]) {
    pthread_t new_thread;
    file_t file1, file2;

    file1.name = argv[1];
    file1.words = 0;
    file2.name= argv[2];
    file2.words = 0;

    // Insert different part here

    printf("Total words: %d\n", file1.words+file2.words);
    return 0;
}

void *count_words(void *arg) {
    FILE *fp;
    file_t *file = (file_t *)arg;
    int i, c, prevc = '\0';
    struct timespec process_beg, process_end;
    struct timespec thread_beg, thread_end;
    double process_diff, thread_diff;

    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &process_beg);
    clock_gettime(CLOCK_THREAD_CPUTIME_ID, &thread_beg);

    fp = fopen(file->name, "r");
    for (i = 0; i < N; i++) {
        while ((c = getc(fp)) != EOF) {
            if (!isalnum(c) && isalnum(prevc))
                file->words++;
            prevc = c;
        }
        fseek(fp, 0, SEEK_SET);
    }
    fclose(fp);

    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &process_end);
    clock_gettime(CLOCK_THREAD_CPUTIME_ID, &thread_end);

    process_diff = time_diff(&process_beg, &process_end);
    thread_diff = time_diff(&thread_beg, &thread_end);
    printf("count_words() in %s takes %.3fs process time and" 
           "%.3fs thread time\n", file->name, process_diff, thread_diff);
    return NULL;
}

double time_diff(struct timespec *beg, struct timespec *end) {
    return ((double)end->tv_sec + (double)end->tv_nsec*1.0e-9)
         - ((double)beg->tv_sec + (double)beg->tv_nsec*1.0e-9);
}

注意

  1. file1 是一个包含 10000 个单词的文件。 file2 是 file1 的副本,由 cp 命令创建。
  2. 为了使执行时间足够长,程序重复计算单词。 N 是循环数。所以结果不是准确的总字数,而是乘以N。
  3. 请不要过分强调计数算法。我只关心这个例子中的执行时间。
  4. 重要信息:机器是 Intel® Celeron(R) CPU 420 @ 1.60GHz。一个核心。操作系统为Linux 3.2.0。也许像其他人说的那样,一个核心是造成这种奇怪现象的原因。但我还是想弄明白。

程序统计字数,使用clock_gettime()计算例程count_words()的进程cpu时间和线程cpu时间,输出次数和字数。以下是输出和我对问题的评论。如果有人能解释额外时间的原因,我将不胜感激。

// version 1
count_words() in file1 takes 2.563s process time and 2.563s thread time
count_words() in file2 takes 8.374s process time and 8.374s thread time
Total words: 40000000

点评:原线程完成count_words(),等待新线程死亡。当 count_words() 在新线程中运行时,不会发生上下文切换(因为进程时间 == 线程时间)。 为什么要花这么多时间?新线程中的 count_words() 会发生什么?

// version 2
count_words() in file1 takes 16.755s process time and 8.377s thread time
count_words() in file2 takes 16.753s process time and 8.380s thread time
Total words: 40000000

评论:这里有两个线程并行运行。发生上下文切换,因此进程时间 > 线程时间。

// version 3
count_words() in file2 takes 8.374s process time and 8.374s thread time
count_words() in file1 takes 8.365s process time and 8.365s thread time
Total words: 40000000

注释:新线程先计数,原始线程等待。新线程加入后,原线程开始计数。 两者都没有上下文切换,为什么花这么多时间,尤其是新线程加入后的计数?

// version 4
count_words() in file1 takes 2.555s process time and 2.555s thread time
count_words() in file2 takes 2.556s process time and 2.556s thread time
Total words: 40000000

评论:最快的版本。没有创建新线程。两个 count_words() 都在一个线程中运行。

最佳答案

这可能是因为任何线程的创建都会强制 libc 在 getc 中使用同步。这使得这个函数明显变慢。以下示例对我来说和版本 3 一样慢:

void *skip(void *p){ return NULL; };
pthread_create(&new_thread, NULL, skip, NULL);
count_words(&file1); 
count_words(&file2); 

要解决此问题,您可以使用缓冲区:

for (i = 0; i < N; i++) {
    char buffer[BUFSIZ];
    int read;
    do {
        read = fread(buffer, 1, BUFSIZ, fp);
        int j;
        for(j = 0; j < read; j++) {
            if (!isalnum(buffer[j]) && isalnum(prevc))
                file->words++;
            prevc = buffer[j];
        }
    } while(read == BUFSIZ);
    fseek(fp, 0, SEEK_SET);
}

在此解决方案中,IO 函数很少被调用,足以使同步开销变得微不足道。这不仅解决了奇怪的计时问题,而且使它快了好几倍。对我来说,它从 0.54s(没有线程)或 0.85s(有线程)减少到 0.15s(在这两种情况下)。

关于c - pthread 程序中例程的额外执行时间花费了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15877403/

相关文章:

linux - 使用 Ansible 执行 Karaf 客户端命令

linux - 在 Linux 系统调用序列中区分 execve() 的解释器

python - 大量的multiprocessing.Process导致死锁

在C中将char数组转换为值并放入int数组

CUDA : error C2491: 'log1p' : definition of dllimport function not allowed

c - 基本线性代数算法(MATLAB 和 C)

C: signal() 是进程级的吗?

c - 删除c中可能锁定的文件

Java SWT - 将数据从组件返回到其他线程的最佳方式

java - Java 和 C/C++ 应用程序可以共享信号量吗?