进程和进程函数

cs 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main(int argc, const char *argv[])
{
    // 文件路径
    const char *src_path = "/home/ubuntu/IO/xiaoxin.bmp";
    const char *dest_path = "/home/ubuntu/IO/xiaoxin2.bmp";

    // 打开源文件和目标文件
    FILE *src = fopen(src_path, "rb");
    if (src == NULL) {
        perror("打开源文件失败");
        return -1;
    }
    FILE *dest = fopen(dest_path, "wb");
    if (dest == NULL) {
        perror("打开目标文件失败");
        fclose(src);
        return -1;
    }

    // 获取文件总大小
    fseek(src, 0, SEEK_END);
    long file_size = ftell(src);
    rewind(src);  // 将指针重置到文件开头

    // 计算父子进程各自拷贝的区域
    long half_size = file_size / 2;

    // 创建子进程
    pid_t pid = fork();

    if (pid < 0)
    {
        perror("fork失败");
        fclose(src);
        fclose(dest);
        return -1;
    }

    if (pid == 0) {
        // 子进程:拷贝文件的后半部分
        FILE *src_child = fopen(src_path, "rb");
        FILE *dest_child = fopen(dest_path, "rb+"); // 打开目标文件以读写模式
        if (src_child == NULL || dest_child == NULL) {
            perror("子进程文件打开失败");
            exit(-1);
        }

        fseek(src_child, half_size, SEEK_SET);  // 定位到文件的后半部分
        fseek(dest_child, half_size, SEEK_SET); // 定位目标文件的后半部分

        char buf[128] = {0};
        size_t n;
        while ((n = fread(buf, 1, sizeof(buf), src_child)) > 0) {
            if (fwrite(buf, 1, n, dest_child) != n) {
                perror("子进程写入失败");
                fclose(src_child);
                fclose(dest_child);
                exit(-1);
            }
        }

        printf("子进程完成文件后半部分的拷贝。\n");

        fclose(src_child);
        fclose(dest_child);
        exit(0);
    }
    else
    {
        // 父进程:拷贝文件的前半部分
        char buf[128] = {0};
        size_t n;
        while ((n = fread(buf, 1, sizeof(buf), src)) > 0)
        {
            if (fwrite(buf, 1, n, dest) != n)                                              
            {
                perror("父进程写入失败");
                fclose(src);
                fclose(dest);
                return -1;
            }
            if (ftell(src) >= half_size) {
                break;
            }
        }

        printf("父进程完成文件前半部分的拷贝。\n");

        // 等待子进程完成
        waitpid(pid, NULL, 0);

        // 使用 diff 命令检查源文件和目标文件是否相同
        char command[256];
        snprintf(command, sizeof(command), "diff %s %s", src_path, dest_path);
        system(command);

        // 关闭文件
        fclose(src);
        fclose(dest);
    }

    return 0;
}
相关推荐
yngsqq1 小时前
平面图环 内轮廓
c#
老花眼猫2 小时前
编制椭圆旋转绘图函数
c语言·经验分享·青少年编程·课程设计
rockey6273 小时前
AScript之eval函数详解
c#·.net·script·eval·expression·动态脚本
iCxhust4 小时前
微机原理实践教程(C语言篇)---A002流水灯
c语言·开发语言·单片机·嵌入式硬件·51单片机·课程设计·微机原理
qeen875 小时前
【数据结构】建堆的时间复杂度讨论与TOP-K问题
c语言·数据结构·c++·学习·
handler015 小时前
Linux 内核剖析:进程优先级、上下文切换与 O(1) 调度算法
linux·运维·c语言·开发语言·c++·笔记·算法
热心网友俣先生5 小时前
2026年第二十三届五一数学建模竞赛C题超详细解题思路+各问题可用模型推荐+部分模型结果展示
c语言·开发语言·数学建模
li1670902706 小时前
第二十七章:智能指针
c语言·数据结构·c++·visual studio
He少年7 小时前
【AI 辅助案例分享】
人工智能·c#·编辑器·ai编程
工程师0078 小时前
栈和堆的概念
c#·栈和堆