#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;
}