- 实例要求:
- 使用
多线程
复制同一个文件内容;
- 实例分析:
- 1.
创建两个线程,即线程1、线程2
,设置光标
在指定文件中的偏移量
,实现对同一个文件的复制。
- 2.比如:可以指定
线程1复制文件内容的前一半
,而线程2复制文件内容的后一半
。
- 3.根据
时间片轮询法则
,最终线程1和线程2可以把同一个文件复制成功。
- 相关的线程库接口函数如下:
- pthread_create函数:
#include
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
#include
pthread_t pthread_self(void);
#include
void pthread_exit(void *retval);
#include
int pthread_join(pthread_t thread, void **retval);
#include
int pthread_detach(pthread_t thread);
#include
int pthread_cancel(pthread_t thread);
int pthread_setcancelstate(int state, int *oldstate);
PTHREAD_CANCEL_ENABLE
PTHREAD_CANCEL_DISABLE
int pthread_setcanceltype(int type, int *oldtype);
PTHREAD_CANCEL_ASYNCHRONOUS
PTHREAD_CANCEL_DEFERRED
#include
#include
#include
#include
#include
#include
#include
#include
#include
typedef struct INFO
{
const char *src_file;
const char *dest_file;
int offset;
int size;
}info_t;
int get_src_file_size_and_create_dest_file(const char *src_file,const char *dest_file);
void *cp_src_file_to_dest_file(void *arg);
int main(int argc, char const *argv[])
{
if(3 != argc)
{
printf("Usage : %s src_file dest_file\n",argv[0]);
return -1;
}
int size = 0;
size = get_src_file_size_and_create_dest_file(argv[1],argv[2]);
info_t arg[2] = {
{argv[1],argv[2],0,size/2},
{argv[1],argv[2],size/2,size - size/2}
};
pthread_t thread_1_id = 0;
pthread_t thread_2_id = 0;
int ret = 0;
if(0 != (ret = pthread_create(&thread_1_id,NULL,cp_src_file_to_dest_file,&arg[0])))
{
printf("pthread_create error : errno = [%d] errstr = [%s]\n",ret,strerror(ret));
exit(EXIT_FAILURE);
}
if(0 != (ret = pthread_create(&thread_2_id,NULL,cp_src_file_to_dest_file,&arg[1])))
{
printf("pthread_create error : errno = [%d] errstr = [%s]\n",ret,strerror(ret));
exit(EXIT_FAILURE);
}
pthread_join(thread_1_id,NULL);
pthread_join(thread_2_id,NULL);
return 0;
}
int get_src_file_size_and_create_dest_file(const char *src_file,const char *dest_file)
{
int src_fd = open(src_file,O_RDONLY);
if(-1 == src_fd)
{
perror("open error");
return -1;
}
int size = lseek(src_fd,0,SEEK_END);
close(src_fd);
int dest_fd = open(dest_file,O_WRONLY | O_CREAT | O_TRUNC,0666);
if(-1 == dest_fd)
{
perror("open error");
return -1;
}
close(dest_fd);
return size;
}
void *cp_src_file_to_dest_file(void *arg)
{
info_t info = *(info_t *)arg;
int src_fd = 0;
int dest_fd = 0;
if(-1 == (src_fd = open(info.src_file,O_RDONLY)))
{
perror("open error");
pthread_exit(NULL);
}
if(-1 == (dest_fd = open(info.dest_file,O_WRONLY)))
{
perror("open error");
pthread_exit(NULL);
}
lseek(src_fd,info.offset,SEEK_SET);
lseek(dest_fd,info.offset,SEEK_SET);
int ret = 0;
int num = 0;
char buf[64] = {0};
while (true)
{
ret = read(src_fd,buf,sizeof(buf));
if(0 == ret)
{
break;
}
num += ret;
if(num > info.size)
{
write(dest_fd,buf,info.size - (num - ret));
break;
}
write(dest_fd,buf,ret);
}
close(src_fd);
close(dest_fd);
}
linux@ubuntu:~$ gcc th2.c -lpthread
linux@ubuntu:~$ ./a.out k1.c k2.c
linux@ubuntu:~$ diff k1.c k2.c
linux@ubuntu:~$