linux下c语言实现多线程文件复制【转】
转自:https://www.cnblogs.com/zxl0715/articles/5365989.html
- 、具体思路
- 把一个文件分成N份,分别用N个线程copy,
- 每个线程只读取指定长度字节大小的内容
- 最后一个线程的源文件所指定的结束位置是文件的实际大小
- 每个线程读取指定源文件部分的起始位置和结束位置的内容到缓冲区
- 每个线程将缓存中的内容写入目的文件的指定开始位置和结束位置
- 主线程必须等到所有线程copy完成后才能退出
- .有关文件操作的函数
- 2.1. 文件的打开和关闭
- 2.1. open()函数
- open()函数的作用是打开文件, 其调用格式为:
- int open(char *filename, int access);
- 该函数表示按access的要求打开名为filename的文件,
- 返回值为文件描述字
- open()函数打开成功, 返回值就是文件描述字的值(非负值), 否则返回-。
- 2.1. close()函数
- close()函数的作用是关闭由open()函数打开的文件, 其调用格式为:
- int close(int handle);
- 该函数关闭文件描述字handle相连的文件。
- 2.2.读写函数
- 2.2. read()函数
- read()函数的调用格式为:
- int read(int handle, void *buf, int count);
- read()函数从handle(文件描述字)相连的文件中, 读取count个字节放到buf所指的缓冲区中,
- 返回值为实际所读字节数, 返回-1表示出错。返回0 表示文件结束。
- 2.2. write()函数
- write()函数的调用格式为:
- int write(int handle, void *buf, int count);
- write()函数把count个字节从buf指向的缓冲区写入与handle相连的文件中,
- 返回值为实际写入的字节数
- 2.3.随机定位函数
- lseek()函数
- lseek()函数的调用格式为:
- int lseek(int handle, long offset, int fromwhere);
- 该函数对与handle相连的文件位置指针进行定位, 功能和用法与fseek() 函数相同。
- .源文件(copyfn.c)
- 源文件在ubuntu10.04下编译通过
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include <pthread.h>
- #include <dirent.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/time.h>
- #define THREADS_COUNT 3
- #define THREADS_BUFF_SIZE 1*1024
- struct thread_block
- {
- int infd; ///源文件句柄
- int outfd;//目的文件句柄
- size_t start;///文件的写入起始位置
- size_t end; ///文件写入的终止位置
- };
- void usage()
- {
- printf("copy %%src %%dst\n");
- }
- ///获取文件大小
- size_t get_filesize(int fd)
- {
- struct stat st;
- fstat(fd,&st);
- return st.st_size;
- }
- void *thread_copy_fn(void *arg);
- int main(int argc,char *argv[])
- {
- if(argc < )
- {
- usage();
- return -;
- }
- ///打开文件
- int infd = open(argv[],O_RDONLY);
- int outfd = open(argv[],O_CREAT|O_WRONLY,);
- // 0644也就是-文件所有者有读写权限,组有读权限,其他用户有读权限
- if(infd == -|| - ==outfd)
- {
- printf("error while open file \n");
- return -;
- }
- size_t file_size = get_filesize(infd);
- size_t thread_size = THREADS_COUNT;
- struct thread_block *blocks = (struct thread_block *)
- malloc(sizeof(struct thread_block )* thread_size);
- size_t percent = file_size / thread_size;
- printf("filesize = %d\t percent_blocks = %d\n",\
- file_size,percent);
- int i = ;
- //init-thread-block
- for(; i < thread_size;++i)
- {
- blocks[i].infd = infd;
- blocks[i].outfd = outfd;
- blocks[i].start = i * percent;
- blocks[i].end = blocks[i].start + percent;
- }
- //the last thread
- blocks[i].end = file_size;
- pthread_t ptid[thread_size];
- ///创建线程
- for(i = ; i < thread_size; ++i)
- {
- pthread_create(&ptid[i],NULL,thread_copy_fn,&(blocks[i]));
- }
- ///线程Join
- for(i = ; i < thread_size; ++i)
- {
- pthread_join(ptid[i],NULL);
- }
- ///释放资源
- free(blocks);
- close(infd);
- close(outfd);
- printf("Copy Successfully \n");
- return ;
- }
- void *thread_copy_fn(void *arg)
- {
- struct thread_block *block = (struct thread_block *)arg;
- char buf[THREADS_BUFF_SIZE];
- int ret;
- size_t count = block->start;
- printf("In Thread\t%ld\nstart = %ld\t end = %ld\n",\
- pthread_self(),block->start,block->end);
- ///lseek到同样的位置
- ret = lseek(block->infd,block->start,SEEK_SET);
- ret = lseek(block->outfd,block->start,SEEK_SET);
- int bytes_read;
- int bytes_write;
- while(count < block->end)
- {
- bytes_read = read(block->infd,buf,sizeof(buf));
- if(bytes_read >)
- {
- printf("thread = %ld\t read = %ld\t count %d\n",\
- pthread_self(),bytes_read,count);
- count += bytes_read;
- //read()返回-1,同时errno为EINTR,表示读的过程中遇到了中断
- if((bytes_read == -)&&(errno !=EINTR))
- break;
- char *ptr_write = buf;
- while((bytes_write = write(block->outfd,ptr_write,bytes_read))!=)
- {
- //write()会返回-1,同时errno为EINTR,表示在写的过程中遇到了中断
- if((bytes_write == -)&&(errno!=EINTR))
- break;
- if(bytes_write == bytes_read)
- break;
- else if(bytes_write > )
- {
- ptr_write += bytes_write;
- bytes_read -= bytes_write;
- }
- printf("thread = %ld\t write = %ld\t read %d\n",\
- pthread_self(),bytes_write,bytes_read);
- }//end-write;
- ///error while write
- if(bytes_write == -)
- break;
- }
- }
- printf("#####Thread exit %ld#####\n",pthread_self());
- pthread_exit(NULL);
- }
- 本文欢迎转载,转载请注明作者与出处
linux下c语言实现多线程文件复制【转】的更多相关文章
- linux 下C语言编程库文件处理与Makefile编写
做开发快3年了,在linux下编译安装软件算是家常便饭了.就拿gcc来说,都有不下10次了,可基本每次都会碰到些奇奇怪怪的问题.看来还是像vs.codeblocks这样的ide把人弄蠢了.便下定决心一 ...
- linux下C语言实现多线程通信—环形缓冲区,可用于生产者(producer)/消费者(consumer)【转】
转自:http://blog.chinaunix.net/uid-28458801-id-4262445.html 操作系统:ubuntu10.04 前言: 在嵌入式开发中,只要是带操作系统的 ...
- linux下c语言的多线程编程
我们在写linux的服务的时候,经常会用到linux的多线程技术以提高程序性能 多线程的一些小知识: 一个应用程序可以启动若干个线程. 线程(Lightweight Process,LWP),是程序执 ...
- Linux下c语言TCP多线程聊天室
开发环境:Linux,GCC 相关知识:TCP(博客:传送门),线程 附加:项目可能还有写不足之处,有些bug没调出来(如:对在线人数的控制),希望大佬赐教. 那么话不多说,放码过来: 码云:传送门, ...
- linux下C语言多线程编程实例
用一个实例.来学习linux下C语言多线程编程实例. 代码目的:通过创建两个线程来实现对一个数的递加.代码: //包含的头文件 #include <pthread.h> #include ...
- LINUX下C语言编程调用函数、链接头文件以及库文件
LINUX下C语言编程经常需要链接其他函数,而其他函数一般都放在另外.c文件中,或者打包放在一个库文件里面,我需要在main函数中调用这些函数,主要有如下几种方法: 1.当需要调用函数的个数比较少时, ...
- Windows10下配置Linux下C语言开发环境
今天为大家介绍如在Windows10下配置Linux下C语言开发环境,首先安装linux子系统:启用开发者模式 1.打开设置 2.点击更新和安全3.点击开发者选项 4.启用开发人员模式 5.更改系统功 ...
- linux 下C语言学习路线
UNIX/Linux下C语言的学习路线.一.工具篇“公欲善其事,必先利其器”.编程是一门实践性很强的工作,在你以后的学习或工作中,你将常常会与以下工具打交道, 下面列出学习C语言编程常常用到的软件和工 ...
- Linux下C语言编程实现spwd函数
Linux下C语言编程实现spwd函数 介绍 spwd函数 功能:显示当前目录路径 实现:通过编译执行该代码,可在终端中输出当前路径 代码实现 代码链接 代码托管链接:spwd.c 所需结构体.函数. ...
随机推荐
- webstorm的git操作使用
0. 前言 在上一篇文章中,讲述了使用webstorm去调试node程序,最近研究了一下如何使用webstorm去操作git. 对于git的使用,大家的使用方式均有不同,最王道的方式非命令行莫属,基于 ...
- vue操作select获取option值
如何实时的获取你选中的值 只用@change件事 @change="changeProduct($event)" 动态传递参数 vue操作select获取option的ID值 如果 ...
- 【BZOJ1443】[JSOI2009]游戏Game(二分图+博弈)
BZOJ 题意: 给出一个\(n*m\)的网格,其中有一些障碍点. 现在两个人玩游戏,首先先手选定一个点,然后从后手开始轮流移动,不能移动者即输掉这次游戏. 规定不能移动到那些之前已经到过的格子上. ...
- 初学JavaScript正则表达式(六)
JavaScript预定义类 ab+数字+任意字符 ab[0-9][^\r\n] 等价于 ab\d. '@123@abc@'.replace(/@./g,'Q') Q23Qbc@ 将"@加任 ...
- apache配置文件详解(中英文对照版)
# This is the main Apache server configuration file. It contains the # configuration directives that ...
- day71_10_16多表断关联
---恢复内容开始--- 本次环境: 配置settings INSTALLED_APPS = [ # ... 'rest_framework', ] DATABASES = { 'default': ...
- Log日志级别从高到低排序 ERROR、WARN、INFO、DEBUG
Log4j建议只使用四个级别,优先级从高到低分别是 ERROR.WARN.INFO.DEBUG.通过在这里定义的级别,您可以控制到应用程序中相应级别的日志信息的开关.比如在这里定义了INFO级别,则应 ...
- 剑指Offer-13.调整数组顺序使奇数位于偶数前面(C++/Java)
题目: 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. 分析: 这道题做法有很 ...
- Python网络编程基础 ❸ struct模块 基于upd的socket服务
struct模块 基于upd的socket服务
- <Array> 277 243 244 245
277. Find the Celebrity knows(i, j): By comparing a pair(i, j), we are able to discard one of them 1 ...