若没有子进程wait函数是否阻塞,wait函数是否运行成功。
#include <head.h>
int main(int argc, const char *argv[])
{
printf("hello\n");
if( wait(NULL)==-1)
{
ERR_MSG("wait");
return -1;
}
printf("word\n");
return 0;
}
用文件I0函数拷贝- -张图片,要求子进程拷贝后半部分,父进程拷贝前半部分。
#include "head.h"
int main(int argc, const char *argv[])
{
pid_t fo=fork();
int fp=open("./1.png",O_RDONLY);
if(fp<0)
{
perror("open");
return -1;
}
int fp2=open("./2.png",O_WRONLY | O_CREAT |O_TRUNC,0764);
if(fp2<0)
{
ERR_MSG("open");
return -1;
}
off_t size=lseek(fp,0,SEEK_END);
if(fo>0)
{
wait(NULL);
off_t size3=lseek(fp,0,SEEK_SET);
char buf=0;
while(1)
{
int ret=read(fp,&buf,sizeof(buf));
if(ret==0)
{
break;
}
ssize_t n=write(fp2,&buf,ret);
}
close(fp);
close(fp2);
}
else if(fo==0)
{
printf("%ld\n",size);
off_t size2=lseek(fp,(size/2),SEEK_SET);
char buf=0;
while(1)
{
int ret=read(fp,&buf,sizeof(buf));
if(ret==0)
{
break;
}
ssize_t n=write(fp2,&buf,ret);
}
close(fp);
close(fp2);
}
else
{
ERR_MSG("fork");
return -1;
}
return 0;
}