作业4.14

#include <stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
int main(int argc, const char *argv[])
{
int fd=open(“./1.png”,O_RDONLY);
if(fd<0){
printf(“fd”);
perror(“open”);
return -1;
}

struct stat x;;
stat("./1.png",&x);
off_t a=x.st_size;
char y;
int i=0;

pid_t c =fork();
if(c>0)//父进程中为真
{
    int cp=open("./cp.png",O_WRONLY|O_CREAT,0777);
    if(cp<0){
        printf("cp");
        perror("open");
        return -1;
    }
    printf("父进程");
        while(i<a/2){
        read(fd,&y,1);
        write(cp,&y,1);
        i++;
    }
}else if(c==0)//子进程中为真
{
    int cp1=open("./cp1.png",O_WRONLY|O_CREAT,0777);
    if(cp1<0){
        printf("cp");
        perror("open");
        return -1;
    }
    printf("子进程");
        while(i<a/2){
        read(fd,&y,1);
        write(cp1,&y,1);
        i++;
    }
}else{
    perror("fork");
    return -1;
}
return 0;

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值