资源简介
本人在工作用的到c语言进程多线程工作,和多线程实现文件传输的功能,就网上寻找一份,供大家参考
代码片段和文件信息
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define THREADS_COUNT 4
#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)
{
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 = %d\t end = %d\n“\
pthread_self()block->startblock->end);
///lseek到同样的位置
ret = lseek(block->infdblock->startSEEK_SET);
ret = lseek(block->outfdblock->startSEEK_SET);
int bytes_read;
int bytes_write;
while(count < block->end)
{
bytes_read = read(block->infdbufsizeof(buf));
if(bytes_read >0)
{
printf(“thread = %ld\t read = %d\t count %d\n“\
pthread_self()bytes_readcount);
count += bytes_read;
//read()返回-1,同时errno为EINTR,表示读的过程中遇到了中断
if((bytes_read == -1)&&(errno !=EINTR))
break;
char *ptr_write = buf;
while((bytes_write = write(block->outfdptr_writebytes_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 = %d\t read %d\n“\
pthread_self()bytes_writebytes_read);
}//end-write;
///error while write
if(bytes_write == -1)
break;
}
}
printf(“#####Thread exit %ld#####\n“pthread_self());
pthread_exit(NULL);
}
int main(int argcchar *argv[])
{
if(argc < 3)
{
usage();
return -1;
}
///打开文件
int infd = open(argv[1]O_RDONLY);
int outfd = open(argv[2]O_CREAT|O_WRONLY0644);
// 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_sizepercent);
int i = 0;
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 383 2014-06-11 17:00 C实现多线程下载源码\readme.txt.txt
文件 3911 2014-06-11 16:07 C实现多线程下载源码\thread_download.c
目录 0 2014-06-11 16:59 C实现多线程下载源码
----------- --------- ---------- ----- ----
4294 3
- 上一篇:水果专家系统
- 下一篇:使用微软蓝牙API的
相关资源
- 人工智能之动物识别C语言
- 串口接受和发送数据--C语言代码,非
- C语言课程设计记事本
- 操作系统C语言实现银行家算法,键盘
- 《数据结构》C语言版 实验报告 基础
- 飞行弹道计算C语言
- 多目标粒子群算法C代码
- c语言实现 通过rs232可实现上位机和下
- 火车票管理系统C语言数据结构
- 纯C语言写的https模拟GET和POST
- C语言CRC32校验
- C语言名题精选百则源代码
- GoBackN协议的C语言实现
- 算术编码纯C语言实现
- C语言windowlinux平台的SNTP实现
- the C programming language ( kindle 版)
- 前向纠错的多个算法C语言
- SM3算法C语言实现
-
单项锁相环 MATLAB Simuli
nk仿真 C语言 - LDPC源代码c语言matlab.rar
-
自抗扰控制器C语言实现Simuli
nk转化 - 基于8051单片机的数字电压表设计
- AES5种加密模式源码C语言
- 图的深度优先遍历与广度优先遍历(
- C语言实现TFTP客户端代码
- 5个遗传算法C语言源码
- hashtable-C语言版折叠法+单链表
- c语言课件-循环结构
- 用c语言求梯度的算法
- C语言超市收银模拟系统
评论
共有 条评论