免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5299 | 回复: 9
打印 上一主题 下一主题

我的多线程复制文件程序怎么比单线程还慢,谁能帮我看下 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-09-25 13:15 |只看该作者 |倒序浏览
#include <iostream>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <pthread.h>
using namespace std;
const int maxbuff = 4096;
bool file_exist(const char *file)
{
        int fd;
       
        assert(file != NULL);

        fd = open(file, O_RDONLY);
        if(fd < 0){
                return false;
        }else{
                close(fd);
                return true;
        }
}
double get_current_time(void)
{
        struct timeval time[1];

        if(gettimeofday(time, 0) < 0) return -1;
       
        return (double)time->tv_sec + (double)time->tv_usec / 1000000;
}
struct task
{
        char *srcfile;
        char *desfile;
        off_t start;
        off_t length;
        mode_t stmode;
};
void *thread_copy(void *arg)
{
        task *threadtask = (task*)arg;
        int src,des;
        if ((src = open(threadtask->srcfile, O_RDONLY)) == -1)
        {
                cerr<<"cannot open src file in thread"<<endl;
                pthread_exit((void *)-1);
        }
        if ((des = open(threadtask->desfile, O_WRONLY)) == -1)
        {
                cerr<<"cannot open dest file"<<endl;
                pthread_exit((void *)-1);
        }
        lseek(src,threadtask->start,SEEK_SET);
        lseek(des,threadtask->start,SEEK_SET);
        int n;
        char buff[maxbuff+1];
        int len = threadtask->length;
        int readlen = min(len,maxbuff);
        while ((readlen > 0) && (n = read(src,buff,readlen)) > 0)
        {
                if (write(des,buff,n) != n)
                        cerr<<"error write"<<endl;
                len -= n;
                readlen = min(len,maxbuff);
        }
        close(src);
        close(des);
}
int main(int argc,char *argv[])
{
       
        int srcfd,desfd;
        double start,end;
        struct stat file_stat;
        if (argc < 4)
        {
                cerr<<"arg error"<<endl;
                return -1;
        }
       
        if ((srcfd = open(argv[1], O_RDONLY)) == -1)
        {
                cerr<<"cannot open src file"<<endl;
                return -1;
        }
        fstat(srcfd,&file_stat);
        if (file_exist(argv[2]))
        {
                cerr<<"exist"<<endl;
                return -1;       
        }
        if ((desfd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, file_stat.st_mode)) == -1)
        {
                cerr<<"cannot open dest file"<<endl;
                return -1;
        }
        cout<<"file size "<<file_stat.st_size<<endl;
        int nthread = atoi(argv[3]);
        pthread_t *ptid = new pthread_t[nthread];
        task *taskid = new task[nthread];
       
        int thlength = file_stat.st_size/nthread;
        start = get_current_time();
        for (int i = 0; i < nthread; i++)
        {
                taskid.srcfile = argv[1];
                taskid.desfile = argv[2];
                taskid.length = thlength;
                taskid.start = thlength*i;
                taskid.stmode = file_stat.st_mode;
        }
        taskid[nthread-1].length = file_stat.st_size-thlength*(nthread-1);
        int err;
        for (int i = 0; i < nthread; i++)
        {
                err = pthread_create(&ptid,NULL,thread_copy,(void *)(&taskid));
                if (err != 0)
                {
                        cerr<<"create thread error"<<endl;
                        exit(1);
                }
       
        }
        for (int i = 0; i < nthread; i++)
        {
                pthread_join(ptid,NULL);       
        }
        end = get_current_time();
        cout<<"cost "<<end-start<<" s"<<endl;
        delete []ptid;
        delete []taskid;
        close(srcfd);
        close(desfd);
        return 0;
}
用的电脑是4核cpu,结果速度比单线程的慢,而且文件越大,慢的越狠。。。

论坛徽章:
0
2 [报告]
发表于 2008-09-25 13:30 |只看该作者
我说过,多线程不见得比多线程快

论坛徽章:
0
3 [报告]
发表于 2008-09-25 13:33 |只看该作者
会不会需要INTEL的编译器来搞比较好

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
4 [报告]
发表于 2008-09-25 13:35 |只看该作者
I/O 密集型 应用线程越少越好

论坛徽章:
0
5 [报告]
发表于 2008-09-25 13:37 |只看该作者
文件复制的瓶颈在disk i/o,不在CPU
多线程不能提高disk i/o的性能,反而有可能增加磁盘的寻道时间

论坛徽章:
0
6 [报告]
发表于 2008-09-25 13:38 |只看该作者
多个线程同时访问一个文件(一个物理设备),同时只有一个线程能获得这个设备的控制权,其他线程都在等待。这样还耗费了线程切换的时间,当然比单线程慢。

论坛徽章:
0
7 [报告]
发表于 2008-09-25 15:28 |只看该作者
同样是i/o操作,为什么多线程下载比单线程下载快

论坛徽章:
0
8 [报告]
发表于 2008-09-25 15:35 |只看该作者
原帖由 yuyongyu 于 2008-9-25 15:28 发表
同样是i/o操作,为什么多线程下载比单线程下载快

下载的限制在于网络传输的io,而不是和本地文件的io

论坛徽章:
0
9 [报告]
发表于 2008-09-25 15:57 |只看该作者
哦,一语点醒我梦中人啊

论坛徽章:
0
10 [报告]
发表于 2008-09-25 17:18 |只看该作者
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP