linux线程实现复制文件,linux下c语言实现多线程文件复制

1、具体思路

把一个文件分成N份,分别用N个线程copy,

每个线程只读取指定长度字节大小的内容

最后一个线程的源文件所指定的结束位置是文件的实际大小

每个线程读取指定源文件部分的起始位置和结束位置的内容到缓冲区

每个线程将缓存中的内容写入目的文件的指定开始位置和结束位置

主线程必须等到所有线程copy完成后才能退出

2.有关文件操作的函数

2.1. 文件的打开和关闭

2.1.1 open()函数

open()函数的作用是打开文件, 其调用格式为:

int open(char *filename, int access);

该函数表示按access的要求打开名为filename的文件,

返回值为文件描述字

open()函数打开成功, 返回值就是文件描述字的值(非负值), 否则返回-1。

2.1.2 close()函数

close()函数的作用是关闭由open()函数打开的文件, 其调用格式为:

int close(int handle);

该函数关闭文件描述字handle相连的文件。

2.2.读写函数

2.2.1 read()函数

read()函数的调用格式为:

int read(int handle, void *buf, int count);

read()函数从handle(文件描述字)相连的文件中, 读取count个字节放到buf所指的缓冲区中,

返回值为实际所读字节数, 返回-1表示出错。返回0 表示文件结束。

2.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() 函数相同。

3.源文件(copyfn.c)

源文件在ubuntu10.04下编译通过

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#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 < 3)

{

usage();

return -1;

}

///打开文件

int infd = open(argv[1],O_RDONLY);

int outfd = open(argv[2],O_CREAT|O_WRONLY,0644);

// 0644也就是-文件所有者有读写权限,组有读权限,其他用户有读权限

if(infd == -1|| -1 ==outfd)

{

printf("error while open file \n");

return -1;

}

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 = 0;

//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 = 0 ; i < thread_size; ++i)

{

pthread_create(&ptid[i],NULL,thread_copy_fn,&(blocks[i]));

}

///线程Join

for(i = 0 ; i < thread_size; ++i)

{

pthread_join(ptid[i],NULL);

}

///释放资源

free(blocks);

close(infd);

close(outfd);

printf("Copy Successfully \n");

return 0;

}

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 >0)

{

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 == -1)&&(errno !=EINTR))

break;

char *ptr_write = buf;

while((bytes_write = write(block->outfd,ptr_write,bytes_read))!=0)

{

//write()会返回-1,同时errno为EINTR,表示在写的过程中遇到了中断

if((bytes_write == -1)&&(errno!=EINTR))

break;

if(bytes_write == bytes_read)

break;

else if(bytes_write > 0)

{

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 == -1)

break;

}

}

printf("#####Thread exit %ld#####\n",pthread_self());

pthread_exit(NULL);

}

本文欢迎转载,转载请注明作者与出处

作者:流星

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的LinuxC语言实现多线程排序的代码: ``` #include <stdio.h> #include <stdlib.h> #include <pthread.h> #define NUM_THREADS 4 #define ARRAY_SIZE 10000 int array[ARRAY_SIZE]; void* sort(void* arg) { int id = *(int*)arg; int start = id * (ARRAY_SIZE / NUM_THREADS); int end = (id + 1) * (ARRAY_SIZE / NUM_THREADS); // 使用快速排序对数组进行排序 qsort(&array[start], end - start, sizeof(int), compare); pthread_exit(NULL); } int compare(const void* a, const void* b) { return (*(int*)a - *(int*)b); } int main() { pthread_t threads[NUM_THREADS]; int thread_ids[NUM_THREADS]; // 初始化数组 for (int i = 0; i < ARRAY_SIZE; i++) { array[i] = rand(); } // 创建线程 for (int i = 0; i < NUM_THREADS; i++) { thread_ids[i] = i; pthread_create(&threads[i], NULL, sort, &thread_ids[i]); } // 等待线程结束 for (int i = 0; i < NUM_THREADS; i++) { pthread_join(threads[i], NULL); } // 合并排序结果 int sorted_array[ARRAY_SIZE]; int indexes[NUM_THREADS]; for (int i = 0; i < NUM_THREADS; i++) { indexes[i] = i * (ARRAY_SIZE / NUM_THREADS); } for (int i = 0; i < ARRAY_SIZE; i++) { int min_index = -1; for (int j = 0; j < NUM_THREADS; j++) { if (indexes[j] < (j + 1) * (ARRAY_SIZE / NUM_THREADS)) { if (min_index == -1 || array[indexes[j]] < array[indexes[min_index]]) { min_index = j; } } } sorted_array[i] = array[indexes[min_index]]; indexes[min_index]++; } // 打印排序结果 for (int i = 0; i < ARRAY_SIZE; i++) { printf("%d ", sorted_array[i]); } printf("\n"); return 0; } ``` 这个代码使用了4个线程对一个10000个整数的数组进行排序,实现了快速排序算法。每个线程排序数组的一个子集,然后合并排序结果。使用pthread库实现多线程功能,包括创建线程、等待线程结束等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值