开发者社区> 问答> 正文

Linux fork 多进程 实现拷贝文件 四个进程拷贝

代码有问题 ,段错误

include

include

include

include

include

include

include

include

int main(int argc,char *argv[])
{

char buf[1024];
int fd=open(argv[1],O_RDONLY);
if(fd==-1)
{
perror("open source file failed!");
exit(1);
}

int fd1=open(argv[2],O_WRONLY |O_CREAT| O_EXCL,0777);
if(fd1==-1)
{
perror("open tap file failed!");
exit(1);
}

int len=lseek(fd,0,SEEK_END);

if(len==-1)
{
perror("lseek failed!");
exit(1);
}
int block_size=len%4+1;
// char *buf=NULL;
//char buf[1024];
int i=0;
int pid;
for(i=0;i<3;i++)
{
pid=fork();
if(pid==0)
break;

}
int n=read(fd,buf,block_size);
if(n<0)
{
perror("read failed!");
exit(0);
}
int m=write(fd1,buf,n);

if(pid>0)
while(wait(NULL));

//free(buf);
close(fd);
close(fd1);
return 0;

}

展开
收起
康康&mdash;&mdash;康 2017-10-11 20:13:54 3414 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
Alibaba Cloud Linux 3 技术揭秘 立即下载
Alibaba Cloud Linux 3 发布 立即下载
ECS系统指南之Linux系统诊断 立即下载