)货叉公平的比较(VS主题 [英] Fair comparison of fork() Vs Thread

查看:212
本文介绍了)货叉公平的比较(VS主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个任务并行)约fork()的线程VS(的相对成本的讨论。

I was having a discussion about the relative cost of fork() Vs thread() for parallelization of a task.

我们了解VS线程处理之间的基本区别

We understand the basic differences between processes Vs Thread

主题:


  • 易于线程间通信

  • 快速上下文切换。

流程:


  • 的容错能力。

  • 与父母沟通不是一个真正的问题(打开一个管道)

  • 与其他孩子沟通过程辛苦

但我们不同意的VS线程进程启动成本。结果
因此,为了测试理论我写了下面code。我的问题:这是测量启动成本还是我失去了一些东西的一个有效的测试。我也有兴趣在每个测试中表现如何在不同的平台。

But we disagreed on the start-up cost of processes Vs threads.
So to test the theories I wrote the following code. My question: Is this a valid test of measuring the start-up cost or I am missing something. Also I would be interested in how each test performs on different platforms.

#include <boost/lexical_cast.hpp>
#include <vector>
#include <unistd.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>

extern "C" int threadStart(void* threadData)
{
    return 0;
}

int main(int argc,char* argv[])
{
    int threadCount =  boost::lexical_cast<int>(argv[1]);

    std::vector<pid_t>   data(threadCount);
    clock_t                 start   = clock();
    for(int loop=0;loop < threadCount;++loop)
    {
        data[loop]  = fork();
        if (data[looo] == -1)
        {
            std::cout << "Abort\n";
            exit(1);
        }
        if (data[loop] == 0)
        {
            exit(threadStart(NULL));
        }
    }
    clock_t                 middle   = clock();
    for(int loop=0;loop < threadCount;++loop)
    {
        int   result;
        waitpid(data[loop], &result, 0);
    }
    clock_t                 end   = clock();

   std::cout << threadCount << "\t" << middle - start << "\t" << end - middle << "\t"<< end - start << "\n";
}

Thread.cpp

#include <boost/lexical_cast.hpp>
#include <vector>
#include <iostream>
#include <pthread.h>
#include <time.h>


extern "C" void* threadStart(void* threadData)
{
    return NULL;
}   

int main(int argc,char* argv[])
{
    int threadCount =  boost::lexical_cast<int>(argv[1]);

    std::vector<pthread_t>   data(threadCount);

    clock_t                 start   = clock();
    for(int loop=0;loop < threadCount;++loop)
    {
        if (pthread_create(&data[loop], NULL, threadStart, NULL) != 0)
        {
            std::cout << "Abort\n";
            exit(1);
        }
    }   
    clock_t                 middle   = clock();
    for(int loop=0;loop < threadCount;++loop)
    {   
        void*   result;
        pthread_join(data[loop], &result);
    }
    clock_t                 end   = clock();

   std::cout << threadCount << "\t" << middle - start << "\t" << end - middle << "\t"<< end - start << "\n";

}

我希望Windows能够在创作过程做差。结果
但我希望现代的Unix类系统有一个相当轻叉成本至少相当于线程。在旧的Unix类系统(叉()之前被实现为使用写页复印件),这将是雪上加霜。

I expect Windows to do worse in processes creation.
But I would expect modern Unix like systems to have a fairly light fork cost and be at least comparable to thread. On older Unix style systems (before fork() was implemented as using copy on write pages) that it would be worse.

反正我的时间的结果是:

Anyway My timing results are:

> uname -a
Darwin Alpha.local 10.4.0 Darwin Kernel Version 10.4.0: Fri Apr 23 18:28:53 PDT 2010; root:xnu-1504.7.4~1/RELEASE_I386 i386
> gcc --version | grep GCC
i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5659)
> g++ thread.cpp -o thread -I~/include
> g++ fork.cpp -o fork -I~/include
> foreach a ( 1 2 3 4 5 6 7 8 9 10 12 15 20 30 40 50 60 70 80 90 100 )
foreach? ./thread ${a} >> A
foreach? end
> foreach a ( 1 2 3 4 5 6 7 8 9 10 12 15 20 30 40 50 60 70 80 90 100 )
foreach? ./fork ${a}  >> A
foreach? end
vi A 

Thread:                             Fork:
C   Start   Wait    Total           C   Start   Wait    Total
==============================================================
 1    26     145     171             1   160     37      197
 2    44     198     242             2   290     37      327
 3    62     234     296             3   413     41      454
 4    77     275     352             4   499     59      558
 5    91     107   10808             5   599     57      656
 6    99     332     431             6   665     52      717
 7   130     388     518             7   741     69      810
 8   204     468     672             8   833     56      889
 9   164     469     633             9  1067     76     1143
10   165     450     615            10  1147     64     1211
12   343     585     928            12  1213     71     1284
15   232     647     879            15  1360    203     1563
20   319     921    1240            20  2161     96     2257
30   461    1243    1704            30  3005    129     3134
40   559    1487    2046            40  4466    166     4632
50   686    1912    2598            50  4591    292     4883
60   827    2208    3035            60  5234    317     5551
70   973    2885    3858            70  7003    416     7419
80  3545    2738    6283            80  7735    293     8028
90  1392    3497    4889            90  7869    463     8332
100 3917    4180    8097            100 8974    436     9410

编辑:

做一个1000名儿童造成了叉版本失败。结果
所以我减少了数儿。但在做一个测试似乎也是不公平所以这里是一个范围值。

Doing a 1000 children caused the fork version to fail.
So I have reduced the children count. But doing a single test also seems unfair so here is a range of values.

推荐答案

咕哝...我不喜欢你的原因是多方面的解决方案:

mumble ... I do not like your solution for many reasons:


  1. 您还没有账号采取子进程的执行时间/线程。

  1. You are not taking in account the execution time of child processes/thread.

您应该比较CPU使用率不裸经过的时间。这样,你的统计信息将不从,例如,磁盘访问拥堵依赖。

You should compare cpu-usage not the bare elapsed time. This way your statistics will not depend from, e.g., disk access congestion.

让您的孩子过程中做一些事情。请记住,现代叉使用写入时复制机制,避免,直到需要分配内存给子进程。这是太容易立即退出。这样就避免叉相当各种弊端。

Let your child process do something. Remember that "modern" fork uses copy-on-write mechanisms to avoid to allocate memory to the child process until needed. It is too easy to exit immediately. This way you avoid quite all the disadvantages of fork.

CPU时间是不是你必须考虑的唯一成本。内存消耗和IPC迟缓是叉解决方案的两个缺点。

CPU time is not the only cost you have to account. Memory consumption and slowness of IPC are both disadvantages of fork solution.

您可以使用rusage而不是时钟来衡量真正的资源使用情况。

You could use "rusage" instead of "clock" to measure real resource usage.

P.S。我不认为你可以真正衡量进程/线程的开销编写一个简单的测试程序。有太多的因素,并且通常线程和进程之间的选择是由其他原因不仅仅是CPU使用率驱动

P.S. I do not think you can really measure the process/thread overhead writing a simple test program. There are too many factors and, usually, the choice between threads and processes is driven by other reasons than mere cpu-usage.

这篇关于)货叉公平的比较(VS主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆