IO数据进程day4

#include 
int main(int argc, const char *argv[])
{
	int cpy = open("tmp.jpg",O_WRONLY | O_TRUNC | O_CREAT);
	close(cpy);
	pid_t cpid = fork();
	if(cpid > 0){
		int op = open("./xiao.jpg",O_RDONLY);
		if(EOF == op){
			ERR_MSG("op");
			return -1;
		}
		int cpy = open("tmp.jpg",O_WRONLY);
		if(EOF == cpy){
			ERR_MSG("cpy");
			return -1;
		}
		off_t size = lseek(op,0,SEEK_END);
		off_t mid = size/2;
		lseek(op,0,SEEK_SET);
		char s;
		long len = 0;
		while(1){
			read(op, &s, 1);
			len++;
			write(cpy, &s, 1);
			if(len == mid){
				close(cpy);
				close(op);
				break;
			}
		}
	}else if(0 == cpid){
		int op = open("./xiao.jpg",O_RDONLY);
		if(EOF == op){
			ERR_MSG("op");
			return -1;
		}
		int cpy1 = open("tmp.jpg",O_WRONLY);
		if(EOF == cpy1){
			ERR_MSG("cpy");
			return -1;
		}
		off_t size = lseek(op, 0, SEEK_SET);
		lseek(op, size/2, SEEK_SET);
		char s;
		while(1){
			int res = read(op, &s, 1);
			if(0 == res){
				close(cpy1);
				close(op);
				exit(1);
			}
			write(cpy1, &s, 1);
		}
	}
	return 0;
}

IO数据进程day4_第1张图片

你可能感兴趣的:(java,前端,服务器)