当前位置: 技术问答>linux和unix
open 会产生超大的 file descriptor
来源: 互联网 发布时间:2016-08-23
本文导语: 我用open,为什么会产生超大的 file descriptor? 打开第一个file 返回的是5,打开第二个,返回的是十几万? #include "stdio.h" #include "unistd.h" #include "errno.h" #include "fcntl.h" #include "sys/stat.h" #include "sys/select.h" extern int ...
我用open,为什么会产生超大的 file descriptor?
打开第一个file 返回的是5,打开第二个,返回的是十几万?
#include "stdio.h"
#include "unistd.h"
#include "errno.h"
#include "fcntl.h"
#include "sys/stat.h"
#include "sys/select.h"
extern int readwrite(int fd_rd,int fd_wr,int *pt_num_rd,int *pt_num_wr);
void print_fdset(fd_set *pt_set);
int main(int argc,char *argv[])
{
int fd_rd1,fd_wr1,fd_rd2,fd_wr2,i,max_fd,rs_fd;
fd_set fdset;
unsigned int num_rd1=0,num_wr1=0,num_rd2=0,num_wr2=0;
if(argc!=5)
{
printf("Parameter is incorrect!n");
}
printf("read file1 %st write file1 %sn read file2 %st write file2 %sn",argv[1],argv[2],argv[3],argv[4]);
if((fd_rd1=open(argv[1],O_RDONLY))==-1)
{
perror("Error");
printf("Cannot open %sn",argv[1]);
}
if((fd_wr2=open(argv[2],O_WRONLY|O_CREAT,S_IRWXU))==-1)
{
perror("Error");
printf("Cannot create file %sn",argv[2]);
}
if((fd_rd1=open(argv[3],O_RDONLY))==-1)
{
perror("Error");
printf("Cannot open %sn",argv[3]);
}
if((fd_wr2=open(argv[4],O_WRONLY|O_CREAT,S_IRWXU))==-1)
{
perror("Error");
printf("Cannot create file %sn",argv[4]);
}
max_fd=fd_rd1>fd_rd2?fd_rd1:fd_rd2;
for(;;)
{
FD_ZERO(&fdset);
FD_SET(fd_rd1,&fdset);
FD_SET(fd_rd2,&fdset);
if((rs_fd=select(max_fd+1,&fdset,NULL,NULL,NULL))==-1 && errno==EINTR)
continue;
if(rs_fd==-1)
return -1;
if(FD_ISSET(fd_rd1,&fdset))
{
if(readwrite(fd_rd1,fd_wr1,&num_rd1,&num_wr1)==-1)
break;
printf("%d bytes have been read from %sn",num_rd1,argv[1]);
printf("%d bytes have been written into %sn",num_wr1,argv[2]);
}
if(FD_ISSET(fd_rd2,&fdset))
{
if(readwrite(fd_rd2,fd_wr2,&num_rd2,&num_wr2)==-1)
break;
printf("%d bytes have been read from %sn",num_rd2,argv[3]);
printf("%d bytes have been written into %sn",num_wr2,argv[4]);
}
}
printf("%s is done! %d bytes have been readt %d bytes have been writtenn",argv[1],num_rd1,num_wr1);
printf("%s is done! %d bytes have been readt %d bytes have been writtenn",argv[2],num_rd2,num_wr2);
return 0;
}
打开第一个file 返回的是5,打开第二个,返回的是十几万?
#include "stdio.h"
#include "unistd.h"
#include "errno.h"
#include "fcntl.h"
#include "sys/stat.h"
#include "sys/select.h"
extern int readwrite(int fd_rd,int fd_wr,int *pt_num_rd,int *pt_num_wr);
void print_fdset(fd_set *pt_set);
int main(int argc,char *argv[])
{
int fd_rd1,fd_wr1,fd_rd2,fd_wr2,i,max_fd,rs_fd;
fd_set fdset;
unsigned int num_rd1=0,num_wr1=0,num_rd2=0,num_wr2=0;
if(argc!=5)
{
printf("Parameter is incorrect!n");
}
printf("read file1 %st write file1 %sn read file2 %st write file2 %sn",argv[1],argv[2],argv[3],argv[4]);
if((fd_rd1=open(argv[1],O_RDONLY))==-1)
{
perror("Error");
printf("Cannot open %sn",argv[1]);
}
if((fd_wr2=open(argv[2],O_WRONLY|O_CREAT,S_IRWXU))==-1)
{
perror("Error");
printf("Cannot create file %sn",argv[2]);
}
if((fd_rd1=open(argv[3],O_RDONLY))==-1)
{
perror("Error");
printf("Cannot open %sn",argv[3]);
}
if((fd_wr2=open(argv[4],O_WRONLY|O_CREAT,S_IRWXU))==-1)
{
perror("Error");
printf("Cannot create file %sn",argv[4]);
}
max_fd=fd_rd1>fd_rd2?fd_rd1:fd_rd2;
for(;;)
{
FD_ZERO(&fdset);
FD_SET(fd_rd1,&fdset);
FD_SET(fd_rd2,&fdset);
if((rs_fd=select(max_fd+1,&fdset,NULL,NULL,NULL))==-1 && errno==EINTR)
continue;
if(rs_fd==-1)
return -1;
if(FD_ISSET(fd_rd1,&fdset))
{
if(readwrite(fd_rd1,fd_wr1,&num_rd1,&num_wr1)==-1)
break;
printf("%d bytes have been read from %sn",num_rd1,argv[1]);
printf("%d bytes have been written into %sn",num_wr1,argv[2]);
}
if(FD_ISSET(fd_rd2,&fdset))
{
if(readwrite(fd_rd2,fd_wr2,&num_rd2,&num_wr2)==-1)
break;
printf("%d bytes have been read from %sn",num_rd2,argv[3]);
printf("%d bytes have been written into %sn",num_wr2,argv[4]);
}
}
printf("%s is done! %d bytes have been readt %d bytes have been writtenn",argv[1],num_rd1,num_wr1);
printf("%s is done! %d bytes have been readt %d bytes have been writtenn",argv[2],num_rd2,num_wr2);
return 0;
}
|
if((fd_wr2=open(argv[2],O_WRONLY|O_CREAT,S_IRWXU))==-1)
参数设置的问题:
http://blog.csdn.net/weixiuc/archive/2008/09/17/2941080.aspx
S_IRWXG 00070权限,代表该文件用户组具有可读、可写及可执行的权限。
看看argv[2]的文件是否存在,存在的话看看权限是否正确?
参数设置的问题:
http://blog.csdn.net/weixiuc/archive/2008/09/17/2941080.aspx
S_IRWXG 00070权限,代表该文件用户组具有可读、可写及可执行的权限。
看看argv[2]的文件是否存在,存在的话看看权限是否正确?
|
怎么不把返回什么值也贴出来?
打开失败了 你也不处理?
打开失败了 你也不处理?
|
代码问题很多啊,首先代码文格不好,看着费劲;第二,fd_rd1和fd_wr1被赋值了两次,第三,头文件没有使用尖括号
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。