10.12作业

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argc, const char *argv[])
{
	int a=100;
	pid_t wpid=wait(NULL);
	printf("%d\n",a);
	return 0;
}
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>

int main(int argc, const char *argv[])
{
	
    //以读的方式打开图片文件
	int fd_r=open("./1.JPG",O_RDONLY);
	if(fd_r<0)
	{
	printf("打开失败1");
	return -1;
	}
	//以写的方式打开目标文件
	int fd_w=open("./zy2.JPG",O_WRONLY|O_CREAT|O_TRUNC,0777);
		if(fd_w<0)
		{
		  printf("打开失败");
		  return -1;
		}
	off_t size=lseek(fd_r,0,SEEK_END);
	char buf[size/2];
	char auf[(size/2)+1];
	ssize_t res=0;
	//创建一个子进程
	pid_t cpid=fork();
		
	//在父进程中
	if(cpid>0)
	{
		off_t size1=lseek(fd_r,0,SEEK_SET);
	   res=read(fd_r,buf,sizeof(buf));
	  
	   write(fd_w,buf,res);
	}
	//从图片文件中前一半获取数据写入目标文件中
	//在子进程中
	else if(0==cpid)
	{
		//把光标移到中间
		off_t size2=lseek(fd_r,size/2,SEEK_SET);

	   res=read(fd_r,buf,sizeof(auf));
	  
	   write(fd_w,auf,res);
	}
	//把图片后一半文件写入目标文件
	
	//关闭两个文件
	close(fd_r);
	close(fd_w);
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值