标签:files 要求 进一步 fcntl 等于 大文件 相等 错误处理 最大
编写与dup2功能相同的函数,要求不调用fcntl函数,并且要有正确的出错处理。
不能用fcntl,能够返回一个文件描述符的只有open和dup。而open会创建一个新的文件表项,返回的fd指向新的文件表项,与dup2的表现不符。dup基本能满足要求,但是返回的是最小的可用fd,需要进一步操作满足要求。另外需要自己添加错误处理,以及处理oldfd与newfd相等的情况等。具体地,
#include <stdio.h>
#include <unistd.h>
#include <sys/resource.h>
int main()
{
int r;
struct rlimit old_rlim={0};
getrlimit(RLIMIT_NOFILE, &old_rlim);
printf("NOFILE limits: soft=%lld; hard=%lld\n",
(long long) old_rlim.rlim_cur, (long long) old_rlim.rlim_max);
r = dup2(10000, 10000);
if(r == -1) {
perror("dup2(10000, 10000) fail: ");
}
else {
printf("dup2(10000, 10000) success return %d\n", r);
}
r = dup2(100, 100);
if(r == -1) {
perror("dup2(100, 100) fail: ");
}
else {
printf("dup2(100, 100) success return %d\n", r);
}
r = dup2(1, 10000);
if(r == -1) {
perror("dup2(1, 10000) fail: ");
}
else {
printf("dup2(1, 10000) success return %d\n", r);
}
r = dup2(1, 100);
if(r == -1) {
perror("dup2(1, 100) fail: ");
}
else {
printf("dup2(1, 100) success return %d\n", r);
}
return 0;
}
^_^$ ./a.out
NOFILE limits: soft=7168; hard=9223372036854775807
dup2(10000, 10000) fail: : Bad file descriptor
dup2(100, 100) fail: : Bad file descriptor
dup2(1, 10000) fail: : Bad file descriptor
dup2(1, 100) success return 100
可见是参数出错判断是先于oldfd == newfd判断的
#include <stdio.h>
#include <unistd.h>
#include <sys/resource.h>
int main()
{
int r;
int max_fd = 0;
struct rlimit old_rlim={0};
getrlimit(RLIMIT_NOFILE, &old_rlim);
printf("NOFILE limits: soft=%lld; hard=%lld\n",
(long long) old_rlim.rlim_cur, (long long) old_rlim.rlim_max);
while((r = dup(0))!= -1)
{
max_fd = r;
}
perror(NULL);
printf("max fd is %d\n", max_fd);
r = dup2(1, 10000);
if(r == -1) {
perror("dup2(1, 10000) fail: ");
}
else {
printf("dup2(1, 10000) success return %d\n", r);
}
r = dup2(1, 100);
if(r == -1) {
perror("dup2(1, 100) fail: ");
}
else {
printf("dup2(1, 100) success return %d\n", r);
}
return 0;
}
^_^$ ./a.out
NOFILE limits: soft=7168; hard=9223372036854775807
Too many open files
max fd is 7167
dup2(1, 7168) fail: : Bad file descriptor
dup2(1, 7167) success return 7167
可见在进程打开文件数达到上限时,dup2替换已经打开的文件是可以的
#include <stdio.h>
#include <unistd.h>
#include <sys/resource.h>
#include <sys/errno.h>
/*dup实现dup2的功能*/
int dup2_(int oldfd, int newfd) {
int ret;
int stack[7168];
int count = 0;
struct rlimit old_rlim={0};
getrlimit(RLIMIT_NOFILE, &old_rlim);
if (newfd < 0 || newfd > old_rlim.rlim_cur - 1) {
errno = EBADF;
return -1;
}
while(1) {
ret = dup(oldfd);
if(ret == -1 && errno != EMFILE) {
break;
}
else if(ret == -1 && errno == EMFILE) {
if(oldfd == newfd) {
return newfd;
}
printf("close(newfd)\n");
close(newfd);
}
else {
if(oldfd == newfd) {
close(ret);
return newfd;
}
if(ret == newfd) {
break;
}
else if(ret < newfd) {
stack[count++] = ret;
}
else {
close(ret);
printf("close(newfd)\n");
close(newfd);
}
}
}
while(count) {
close(stack[--count]);
}
return ret;
}
int main()
{
int r, max_fd;
struct rlimit old_rlim={0};
getrlimit(RLIMIT_NOFILE, &old_rlim);
printf("NOFILE limits: soft=%lld; hard=%lld\n",
(long long) old_rlim.rlim_cur, (long long) old_rlim.rlim_max);
r = dup2_(7168, 7168);
if(r == -1) {
perror("dup2_(7168, 7168) fail: ");
}
else {
printf("dup2_(7168, 7168) success return %d\n", r);
}
r = dup2_(100, 100);
if(r == -1) {
perror("dup2_(100, 100) fail: ");
}
else {
printf("dup2_(100, 100) success return %d\n", r);
}
r = dup2_(1, 7168);
if(r == -1) {
perror("dup2_(1, 7168) fail: ");
}
else {
printf("dup2_(1, 7168) success return %d\n", r);
}
r = dup2_(1, 7167);
if(r == -1) {
perror("dup2_(1, 7167) fail: ");
}
else {
printf("dup2_(1, 7167) success return %d\n", r);
}
r = dup2_(2, 2);
if(r == -1) {
perror("dup2_(2, 2) fail: ");
}
else {
printf("dup2_(2, 2) success return %d\n", r);
}
while((r = dup(0))!= -1)
{
max_fd = r;
}
perror(NULL);
printf("max fd is %d\n", max_fd);
r = dup2_(1, 7168);
if(r == -1) {
perror("dup2_(1, 7168) fail: ");
}
else {
printf("dup2_(1, 7168) success return %d\n", r);
}
r = dup2_(1, 7167);
if(r == -1) {
perror("dup2_(1, 7167) fail: ");
}
else {
printf("dup2_(1, 7167) success return %d\n", r);
}
r = dup2_(2, 2);
if(r == -1) {
perror("dup2_(2, 2) fail: ");
}
else {
printf("dup2_(2, 2) success return %d\n", r);
}
return 0;
}
NOFILE limits: soft=7168; hard=9223372036854775807
dup2_(7168, 7168) fail: : Bad file descriptor
dup2_(100, 100) fail: : Bad file descriptor
dup2_(1, 7168) fail: : Bad file descriptor
dup2_(1, 7167) success return 7167
dup2_(2, 2) success return 2
Too many open files
max fd is 7167
dup2_(1, 7168) fail: : Bad file descriptor
close(newfd)
dup2_(1, 7167) success return 7167
close(newfd)
dup2_(1, 100) success return 100
dup2_(2, 2) success return 2
结果都符合预期
UNIX环境高级编程APUE练习3.2-不用fcntl实现dup2的功能
标签:files 要求 进一步 fcntl 等于 大文件 相等 错误处理 最大
原文地址:https://www.cnblogs.com/logchen/p/12056351.html