Linux fork 多进程 实现拷贝文件
四个进程拷贝
我的代码请各位帮我找找问题
#include<stdio.h>
#include<sys/wait.h>
#include<string.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdlib.h>
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;}
四个进程拷贝
我的代码请各位帮我找找问题
#include<stdio.h>
#include<sys/wait.h>
#include<string.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdlib.h>
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;}
解决方案 »
- cnetos6.3安装redis出现的错误
- linux如何把一个程序交叉编译到板子上
- 在netfilter local_out链上捕获数据包之后怎么手动设置mac地址信息 求大神指教
- 江湖救急!!!
- 用命令行的方式来执行7z压缩和解压缩
- 程序员的电脑内存推荐几个g啊
- faster-rcnn出错:confidence = np.array([float could not convert stringto loat:
- linux epoll模型连接数丢失
- Linux虚拟机
- ELK日志系统,logstash与kafka交互出错,望大大们解答
- 如何开始学习linux
- 小白求救,fedora 17无法修改中文的问题?
父进程不能无限等待wait, 就应该等待3个wait
这样拷贝文件可能会导致内容乱序#include<stdio.h>
#include<sys/wait.h>
#include<string.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdlib.h>int main(int argc, char *argv[])
{
char buf[1024];
pid_t parent; parent = getpid();
printf("=== Parent PID:%d\n", parent);
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);
}
off_t len = lseek(fd, 0, SEEK_END);
if (len == (off_t)-1) {
perror("lseek failed!");
exit(1);
}
//rewind
lseek(fd, 0, SEEK_SET); int block_size = len / 4 + 1;
printf("=== source file len=%ld, block_size=%d\n", len, block_size);
int i = 0;
int pid;
for (i = 0; i < 3; i++) {
pid = fork();
if (pid == 0) /* child */
break;
} int n = read(fd, buf, block_size);
if(n < 0) {
perror("read failed!");
exit(0);
}
printf("=== PID:%d, parent PID:%d, Read %d bytes [%s]\n", getpid(), getppid(),n, buf);
int m = write(fd1, buf, n); //wait 3 child
if (getpid() == parent) {
for (i = 0; i < 3; i++) {
wait(NULL);
}
} close(fd);
close(fd1); return 0;
}