当前位置: 首页 > news >正文

9月2日

1> 使用多进程完成两个文件的拷贝:父进程拷贝前一半内容,子进程拷贝后一半内容

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>

#define BUF_SIZE 4096

int main(int argc, char *argv[])
{
if (argc != 3) {
fprintf(stderr, "Usage: %s <src_file> <dest_file>\n", argv[0]);
exit(1);
}

    int src_fd = open(argv[1], O_RDONLY);
if (src_fd < 0) {
perror("open src");
exit(1);
}

    int dest_fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (dest_fd < 0) {
perror("open dest");
close(src_fd);
exit(1);
}

    // 用 lseek 获取文件大小
off_t filesize = lseek(src_fd, 0, SEEK_END);
if (filesize == -1) {
perror("lseek");
close(src_fd);
close(dest_fd);
exit(1);
}
off_t halfsize = filesize / 2;

    // 把源文件读指针重置到开头
lseek(src_fd, 0, SEEK_SET);

    pid_t pid = fork();
if (pid < 0) {
perror("fork");
exit(1);
}
else if (pid > 0) {
// 父进程:拷贝前一半
char buf[BUF_SIZE];
off_t to_copy = halfsize;
ssize_t n;

        while (to_copy > 0 && (n = read(src_fd, buf, (to_copy > BUF_SIZE ? BUF_SIZE : to_copy))) > 0) {
write(dest_fd, buf, n);
to_copy -= n;
}

        wait(NULL);  // 等待子进程完成
}
else {
// 子进程:拷贝后一半
char buf[BUF_SIZE];
ssize_t n;

        // 将文件指针移动到后一半
lseek(src_fd, halfsize, SEEK_SET);
lseek(dest_fd, halfsize, SEEK_SET);

        while ((n = read(src_fd, buf, BUF_SIZE)) > 0) {
write(dest_fd, buf, n);
}
exit(0);
}

    close(src_fd);
close(dest_fd);

    printf("File copy finished.\n");
return 0;
}

2> 父进程创建两个子进程,子进程1负责拷贝文件前一半内容,子进程2负责拷贝文件后一半内容,父进程阻塞回收两个子进程资源

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>

#define BUF_SIZE 4096

int main(int argc, char *argv[])
{
if (argc != 3) {
fprintf(stderr, "Usage: %s <src_file> <dest_file>\n", argv[0]);
exit(1);
}

    int src_fd = open(argv[1], O_RDONLY);
if (src_fd < 0) {
perror("open src");
exit(1);
}

    int dest_fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (dest_fd < 0) {
perror("open dest");
close(src_fd);
exit(1);
}

    // 用 lseek 获取文件大小
off_t filesize = lseek(src_fd, 0, SEEK_END);
if (filesize == -1) {
perror("lseek");
close(src_fd);
close(dest_fd);
exit(1);
}
off_t halfsize = filesize / 2;

    // 回到文件开头
lseek(src_fd, 0, SEEK_SET);

    pid_t pid1 = fork();
if (pid1 == 0) {
// 子进程1:拷贝前半部分
char buf[BUF_SIZE];
off_t to_copy = halfsize;
ssize_t n;
int sfd = open(argv[1], O_RDONLY);
int dfd = open(argv[2], O_WRONLY);

        while (to_copy > 0 && (n = read(sfd, buf, (to_copy > BUF_SIZE ? BUF_SIZE : to_copy))) > 0) {
write(dfd, buf, n);
to_copy -= n;
}
close(sfd);
close(dfd);
exit(0);
}

    pid_t pid2 = fork();
if (pid2 == 0) {
// 子进程2:拷贝后半部分
char buf[BUF_SIZE];
ssize_t n;

        // 子进程自己打开文件
int sfd = open(argv[1], O_RDONLY);
int dfd = open(argv[2], O_WRONLY);

        // 定位到后一半
lseek(sfd, halfsize, SEEK_SET);
lseek(dfd, halfsize, SEEK_SET);

        while ((n = read(sfd, buf, BUF_SIZE)) > 0) {
write(dfd, buf, n);
}
close(sfd);
close(dfd);
exit(0);
}

    // 父进程:等待两个子进程完成
wait(NULL);
wait(NULL);

    close(src_fd);
close(dest_fd);

    printf("File copy finished.\n");
return 0;
}

http://www.dtcms.com/a/364138.html

相关文章:

  • Scikit-learn从入门到实践
  • Xcode 编译速度慢是什么原因?如何提高编译速度?
  • 英伟达Jetson Orin NX-YOLOv8n图像分类模型耗时分析
  • 《浪浪山小妖怪》知识竞赛来袭!测测你是几级影迷?
  • Java试题-选择题(29)
  • 天心:天下为公的君子之道
  • 多人协作修改文档时如何避免内容丢失
  • 【C++】迭代器详解与失效机制
  • Shell字符串操作技巧及if语句使用
  • Docker零基础入门指南
  • 中文分词器之结巴分词
  • 00 - 浏览器的基础认知
  • lesson52:CSS进阶指南:雪碧图与边框技术的创新应用
  • LLM 能不能发展为 AGI?
  • Shell 秘典(卷七)—— 流刃裁文秘术・sed 玄章精解
  • windows下wsl2 ubuntu开发配置
  • VMWare虚拟网络编辑器:“将主机虚拟适配器连接到此网络”含义
  • RHEL9源码编译MySQL8.0.40
  • 25.9.2_NSSCTF-[HNCTF 2022 WEEK2]TTTTTTTTTea_WP
  • Corrosion: 2靶场渗透
  • 数字化转型的终极关怀:以人为本
  • day3
  • 运作管理学习笔记4-产能规划
  • Flowable——配置使用Flowable-UI
  • 日语学习-日语知识点小记-构建基础-JLPT-N3阶段(24):文法+单词第8回1
  • STM32—SPI协议
  • vLLM推理框架:pagedAttention算法+pipeline介绍+源码逐行注释
  • 【技术详解】 OpenZeppelin ERC1155:Solidity 多代币标准实现原理(附完整 Solidity 源码)​
  • 网络通信IP细节
  • 【Vue】前端 vue2项目搭建入门级(二)