• 多线程拷贝文件


                                                                  PS:多进程拷贝文件--->(多进程拷贝文件-CSDN博客

    要求如下

            创建两个线程thread1和thread2拷贝同一个文件,thread1拷贝前一半,thread2拷贝后一半。

    demo.c      (编译 gcc demo.c -lpthread)

    1. #include
    2. typedef struct _ARG
    3. {
    4. const char *srcfile;
    5. const char *destfile;
    6. int start;
    7. int len;
    8. } arg_t;
    9. int get_file_len(const char *srcfile)
    10. {
    11. int srcfd;
    12. if (-1 == (srcfd = open(srcfile, O_RDONLY)))
    13. {
    14. perror("open error");
    15. exit(-1);
    16. }
    17. int len = lseek(srcfd, 0, SEEK_END);
    18. close(srcfd);
    19. return len;
    20. }
    21. int copy_file(const char *src, const char *dest, int start, int len)
    22. {
    23. char buff[16] = {0};
    24. int ret, count;
    25. int srcfd, destfd;
    26. if (-1 == (srcfd = open(src, O_RDONLY)))
    27. {
    28. perror("open src error");
    29. exit(-1);
    30. }
    31. if (-1 == (destfd = open(dest, O_WRONLY)))
    32. {
    33. perror("open dest error");
    34. exit(-1);
    35. }
    36. lseek(srcfd, start, SEEK_SET);
    37. lseek(destfd, start, SEEK_SET);
    38. while (1)
    39. {
    40. if (-1 == (ret = read(srcfd, buff, sizeof(buff))))
    41. {
    42. perror("read error");
    43. exit(-1);
    44. };
    45. if (len < sizeof(buff))
    46. {
    47. write(destfd, buff, len);
    48. break;
    49. }
    50. else
    51. {
    52. write(destfd, buff, ret);
    53. }
    54. len -= ret;
    55. }
    56. close(srcfd);
    57. close(destfd);
    58. return 0;
    59. }
    60. int init_file(const char *destfile)
    61. {
    62. int fd;
    63. if (-1 == (fd = open(destfile, O_WRONLY | O_CREAT | O_TRUNC, 0666)))
    64. {
    65. perror("open error");
    66. exit(-1);
    67. }
    68. close(fd);
    69. return 0;
    70. }
    71. void *thread1(void *args)
    72. {
    73. // 子线程1拷贝前一半
    74. arg_t args1;
    75. args1 = *(arg_t *)args;
    76. copy_file(args1.srcfile, args1.destfile, args1.start, args1.len);
    77. printf("子线程1拷贝前一半完成...\n");
    78. }
    79. void *thread2(void *args)
    80. {
    81. // 子线程2拷贝后一半
    82. arg_t args2;
    83. args2 = *(arg_t *)args;
    84. copy_file(args2.srcfile, args2.destfile, args2.start, args2.len);
    85. printf("子线程2拷贝后一半完成...\n");
    86. }
    87. int main(int argc, const char *argv[])
    88. {
    89. // 入参合理性检查
    90. if (argc != 3)
    91. {
    92. printf("usage error:%s srcfile destfile\n", argv[0]);
    93. exit(-1);
    94. }
    95. // 创建两个线程
    96. pthread_t pid1, pid2;
    97. int fd;
    98. int len;
    99. init_file(argv[2]);
    100. len = get_file_len(argv[1]);
    101. arg_t argg[] = {
    102. [0] = {argv[1], argv[2], 0, len / 2},
    103. [1] = {argv[1], argv[2], len / 2, (len-(len/2))}};
    104. if (errno = (pthread_create(&pid1, NULL, thread1, (void *)&argg[0])) != 0)
    105. {
    106. perror("pthread_create error");
    107. exit(-1);
    108. }
    109. if (errno = (pthread_create(&pid2, NULL, thread2, (void *)&argg[1])) != 0)
    110. {
    111. perror("pthread_create error");
    112. exit(-1);
    113. }
    114. // 主线程
    115. pthread_join(pid1, NULL);
    116. pthread_join(pid2, NULL);
    117. return 0;
    118. }

  • 相关阅读:
    ThreadPoolExecutor线程池原理
    用DBCC checkcatalog(数据库)检测出结构异常
    电脑网速慢怎么解决?推荐这3个方法
    SpringBoot( 整合篇 ==> springboot 2.7.x 整合 Swagger 3.0)
    JVM学习之 内存结构
    爪哇,我初学乍道
    基于快速增量式视觉感知的类脑SLAM
    秋招突击——6/10——复习{(树形DP)树的最长路径、}——新作{电话号码的字母组合}
    初识数据结构
    k8s 使用HPA 进行弹性扩容pod节点,
  • 原文地址:https://blog.csdn.net/CSDN_DU666666/article/details/139842164