• IO学习系列之使用多线程复制同一个文件内容


    • 实例要求:
    • 使用多线程复制同一个文件内容;
    • 实例分析:
    • 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);
    /*
    功能:
    
    		创建一个线程
    
    参数:
    
        	thread: 创建的线程的线程号
    
        	attr: 线程属性 传 NULL 表示使用默认属性
    
        	start_routine: 线程处理函数--线程体
    
        	arg: 线程处理函数的参数
    
    返回值:
    
        	成功  	0
    
        	失败  	错误码 
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • pthread_self函数:
    #include 
    
    pthread_t pthread_self(void);
    /*
    
    功能:
    
    		返回当前线程的线程号
    	
    参数:
    		无
    
    返回值:
    
    		总是成功 当前线程的线程号
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • pthread_exit函数:
    #include 
    
    void pthread_exit(void *retval);
    /*
    功能:
    
    		退出线程
    
        	线程中调用 exit 会导致整个进程退出
    
        	所以 如果只想退出线程 需要使用 pthread_exit
    参数:
    
    		退出线程时的返回值 给 pthread_join 使用的
    
    返回值:
    
    		无
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • pthread_join函数:
    #include 
    
    int pthread_join(pthread_t thread, void **retval);
    /*
    功能:
    
    		阻塞等待一个结合态的线程退出,回收资源
    
    		如果没有线程退出,该函数已知阻塞
    
    参数:
    
    		thread:线程的id
    
    		retval:接受pthread_exit返回的值的 如果不关心 传NULL即可
    
    返回值:
    
        	成功  0
    
        	失败  错误码
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • pthread_detach函数:
    #include 
    
    int pthread_detach(pthread_t thread);
    /*
    功能:	
    
    		标记一个线程为分离态
    
    参数:	
    
    		thread:线程的id
    
    返回值:
    
    		成功 	 0
    
    		失败  	错误码
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • pthread_cancel函数:
    #include 
    
    int pthread_cancel(pthread_t thread);
    /*
    功能:
    
    	给线程发一个取消信号
    
    	线程能否被取消以及能否被立即取消取决于线程的 state 和 type
    
    	也就是与下面两个函数有关
    
    	7.pthread_setcancelstate函数
    
    	8.pthread_setcanceltype函数
    
    参数:
    	thread:线程的id
    
    */
    //返回值:
    int pthread_setcancelstate(int state, int *oldstate);
    
    //state:
    	PTHREAD_CANCEL_ENABLE  	//可被取消  --默认状态
    
    
    	PTHREAD_CANCEL_DISABLE  //不可被取消
    
    
    int pthread_setcanceltype(int type, int *oldtype);
    
    //type:
    	PTHREAD_CANCEL_ASYNCHRONOUS  	//可以被立即取消
    
    
    	PTHREAD_CANCEL_DEFERRED   	//不可以被立即取消--默认状态
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 示例代码:
    #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]);
    
        //线程1复制文件内容的前一半
        //线程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;
    
        //创建线程1
        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);
        }
        //创建线程2
        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);
        }
    
        //线程1阻塞等待一个结合态的线程退出并回收资源
        pthread_join(thread_1_id,NULL);
    
        //线程2阻塞等待一个结合态的线程退出并回收资源
        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);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 运行结果:
    linux@ubuntu:~$ gcc th2.c -lpthread
    linux@ubuntu:~$ ./a.out k1.c k2.c
    linux@ubuntu:~$ diff k1.c k2.c 
    linux@ubuntu:~$ 
    
    • 1
    • 2
    • 3
    • 4
    • 本示例代码,仅供参考
  • 相关阅读:
    sublime快捷键!+tab键失效
    PyTorch
    2023年09月 Python(一级)真题解析#中国电子学会#全国青少年软件编程等级考试
    手把手推导分布式矩阵乘的最优并行策略
    机器学习参数|数学建模|自相关性
    新能源汽车行业资讯-2022-9-16
    基于php停车场
    好的内容回复区
    设计模式:适配器模式(C#、JAVA、JavaScript、C++、Python、Go、PHP)
    【问题解决】源码安装Nginx提示找不到openssl library
  • 原文地址:https://blog.csdn.net/qq_41878292/article/details/133559179