免费注册 查看新帖 |

Chinaunix

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

[C] 求指导~多线程拷贝文件 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-11-03 15:06 |只看该作者 |倒序浏览
程序思想:一个读源文件的线程从源文件读取固定大小的数据写入管道,然后多个写目标文件的线程互斥读取管道的内容写入目标文件
程序执行时一直挂起状态,不知道为什么,目标文件大小为0
我是刚学linux的菜鸟,求各位大神多多赐教,在此深表感谢。

#include<iostream>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<pthread.h>
#include<string>
#define bufsize  1024*4//缓冲区大小
using namespace std;
pthread_mutex_t writer=PTHREAD_MUTEX_INITIALIZER;//写目标文件线程间互斥使用管道
int flag=0;//当读源文件的线程读完数据后将其置为1,告诉写线程
const int wr_num=5;//写目标文件的线程数量
void* read(void* arg)//读源文件bufsize个数据写入fifo
{
        int fd;
        string s=*(string*)arg;
        if((fd=open(s.c_str(),O_RDONLY))<0)
                cout<<"open source file error"<<endl;
        struct stat file_info;
        if(fstat(fd,&file_info)<0)
                cout<<"lstat error"<<endl;
        if(!S_ISREG(file_info.st_mode))
                cout<<"not regular file"<<endl;
        int file_size=file_info.st_size;
        int n=file_size/bufsize;
        char* buf[bufsize];
        int fifo_fd;
        for(int i=0;i<n;i++)
        {
                if((fifo_fd=open("fifo_file",O_RDONLY))<0)
                        cout<<"open fifo error"<<endl;
                if(read(fd,buf,bufsize)!=bufsize)
                        cout<<"read source file error"<<endl;
                if(write(fifo_fd,buf,bufsize)<0)
                        cout<<"wrtie fifo error"<<endl;
        }
        sleep(10);//休眠10s后再告诉写线程
        flag=1;//告诉写线程源文件已读完
        close(fd);
        close(fifo_fd);
        cout<<"read is fulfill"<<endl;
}

void* write(void *arg)//写入目标文件的线程
{
        string s=*(string*)arg;
        int fd;
        char buf[bufsize];
        int fifo_fd;
        while(flag==0)//当读线程还在读源文件的数据循环
        {
                if((fifo_fd=open("fifo_file",O_RDONLY))<0)
                        cout<<"open fifo file error"<<endl;
                if((fd=open(s.c_str(),O_WRONLY|O_APPEND))<0)
                cout<<"open destination file error"<<endl;
                pthread_mutex_lock(&writer);
                if(read(fifo_fd,buf,bufsize)<0)
                        cout<<"read fifo error"<<endl;
                if(write(fd,buf,bufsize)<0)
                        cout<<"write desintation file error"<<endl;
                pthread_mutex_unlock(&writer);
        }
        cout<<"write destination file fulfill"<<endl;
        close(fd);
        close(fifo_fd);
        pthread_exit(NULL);
}

int main(int argc,char* argv[])
{
        if(argc!=3)
                cout<<"main para num !=3"<<endl;
        string source(argv[1]),dest(argv[2]);//源文件和目标文件的路径
        pthread_t rd,wr[wr_num];
        int fifo_fd;
        if(creat(dest.c_str(),S_IRUSR|S_IWUSR|S_IXUSR)<0)//创建目标文件
                cout<<"creat destination file error"<<endl;
        if(mkfifo("fifo_file",O_CREAT|S_IRUSR|S_IWUSR|S_IXUSR)<0)//创建管道
                cout<<"mkfifo error"<<endl;
        if(pthread_create(&rd,NULL,read,&source)!=0)//创建读源文件线程
                cout<<"creat thread read error"<<endl;
        sleep(1);
        for(int i=0;i<wr_num;i++)//创建写线程
        {
                if(pthread_create(&wr[i],NULL,write,&dest)!=0)
                        cout<<"creat thread write["<<i<<"] error"<<endl;
        }
        for(int i=0;i<wr_num;i++)//等待各个写线程
        {
                pthread_join(wr[i],NULL);
                cout<<"thread write["<<i<<"] fulfill"<<endl;
        }
        cout<<"copy file fulfill"<<endl;
        return 0;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP