linux进程能打开文件描述符,查找进程linux(C代码)的打开文件描述符?

这是我用过的一些代码,我不知道/ proc/self(thx Donal!),但这种方式可能更通用。我已经包含了顶部所有功能的必需包含。

#include

#include

#include

#include

#include

#include

#include

#ifndef FALSE

#define FALSE (0)

#endif

#ifndef TRUE

#define TRUE (!FALSE)

#endif

/* implementation of Donal Fellows method */

int get_num_fds()

{

int fd_count;

char buf[64];

struct dirent *dp;

snprintf(buf, 64, "/proc/%i/fd/", getpid());

fd_count = 0;

DIR *dir = opendir(buf);

while ((dp = readdir(dir)) != NULL) {

fd_count++;

}

closedir(dir);

return fd_count;

}

我经历了非常不好的问题去与泄漏文件句柄一次,事实证明我实际编码解决方案汤姆H.建议:

/* check whether a file-descriptor is valid */

int pth_util_fd_valid(int fd)

{

if (fd < 3 || fd >= FD_SETSIZE)

return FALSE;

if (fcntl(fd, F_GETFL) == -1 && errno == EBADF)

return FALSE;

return TRUE;

}

/* check first 1024 (usual size of FD_SESIZE) file handles */

int test_fds()

{

int i;

int fd_dup;

char errst[64];

for (i = 0; i < FD_SETSIZE; i++) {

*errst = 0;

fd_dup = dup(i);

if (fd_dup == -1) {

strcpy(errst, strerror(errno));

// EBADF oldfd isn’t an open file descriptor, or newfd is out of the allowed range for file descriptors.

// EBUSY (Linux only) This may be returned by dup2() during a race condition with open(2) and dup().

// EINTR The dup2() call was interrupted by a signal; see signal(7).

// EMFILE The process already has the maximum number of file descriptors open and tried to open a new one.

} else {

close(fd_dup);

strcpy(errst, "dup() ok");

}

printf("%4i: %5i %24s %s\n", i, fcntl(i, F_GETOWN), fd_info(i), errst);

}

return 0;

}

你可能会想,这些也是如此,满足上述最后的printf ...

char *fcntl_flags(int flags)

{

static char output[128];

*output = 0;

if (flags & O_RDONLY)

strcat(output, "O_RDONLY ");

if (flags & O_WRONLY)

strcat(output, "O_WRONLY ");

if (flags & O_RDWR)

strcat(output, "O_RDWR ");

if (flags & O_CREAT)

strcat(output, "O_CREAT ");

if (flags & O_EXCL)

strcat(output, "O_EXCL ");

if (flags & O_NOCTTY)

strcat(output, "O_NOCTTY ");

if (flags & O_TRUNC)

strcat(output, "O_TRUNC ");

if (flags & O_APPEND)

strcat(output, "O_APPEND ");

if (flags & O_NONBLOCK)

strcat(output, "O_NONBLOCK ");

if (flags & O_SYNC)

strcat(output, "O_SYNC ");

if (flags & O_ASYNC)

strcat(output, "O_ASYNC ");

return output;

}

char *fd_info(int fd)

{

if (fd < 0 || fd >= FD_SETSIZE)

return FALSE;

// if (fcntl(fd, F_GETFL) == -1 && errno == EBADF)

int rv = fcntl(fd, F_GETFL);

return (rv == -1) ? strerror(errno) : fcntl_flags(rv);

}

FD_SETSIZE通常为1024,每个进程的最大文件通常是1024。如果你想确保,你可以用一个调用替换它这个功能,由TomH描述。

#include

#include

rlim_t get_rlimit_files()

{

struct rlimit rlim;

getrlimit(RLIMIT_NOFILE, &rlim);

return rlim.rlim_cur;

}

如果你把所有的一起到一个单一的文件(我所做的,只是为了检查它),你可以产生类似这样的输出,以确认它像宣传的那样:

0: 0 O_RDWR dup() ok

1: 0 O_WRONLY dup() ok

2: 0 O_RDWR dup() ok

3: 0 O_NONBLOCK dup() ok

4: 0 O_WRONLY O_NONBLOCK dup() ok

5: -1 Bad file descriptor Bad file descriptor

6: -1 Bad file descriptor Bad file descriptor

7: -1 Bad file descriptor Bad file descriptor

8: -1 Bad file descriptor Bad file descriptor

9: -1 Bad file descriptor Bad file descriptor

我希望能回答您的任何问题,如果您想知道,我实际上是来这里寻找OP问题的答案,并且在阅读答案后,请记住我多年前已经编写了代码。请享用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值