我想知道什么是网络套接字的文件描述符以及如何获取它?我相信这是一个数字?
我正在尝试调试一个挂起的进程,进程 id 的 strace 输出有最后一行:
recvfrom(9, <detached ...>
据此我了解到该进程正在套接字上等待。
但我不知道这是什么或什么样的插座。我如何才能发现更多相关信息?文件描述符 9 会给我更多信息吗?我如何使用此文件发现来了解有关它正在等待什么的更多信息?
它是一个Python进程,在Linux中运行。
文件描述符(例如标准输入和标准输出)的大小(以位为单位)是多少,它是 32 位整数吗?
我正在使用管道和分支的程序,需要将写入结束更改为输出文件.但是当我打开一个文件时,文件描述符为0,这通常是stdin,但我认为这是我的一些问题的原因.这是我的代码
if (outputfd = open("file", O_RDWR | O_CREAT | O_TRUNC) == -1)
{
// open failed
}
Run Code Online (Sandbox Code Playgroud)
有人能告诉我为什么它是0吗?或者如何解决?
我正在阅读POSIX规范,但无法完全理解文件描述符,文件描述和流如何相互作用。
FILE* f1 = fopen("a.txt", "r");
int fno = fileno(f1);
FILE* f2 = fdopen(fno, "r");
// is it true?
assert(fileno(f2) == fno);
// does it close only f1 or f2 too?
fclose(f1);
fgetc(f2); // valid?
Run Code Online (Sandbox Code Playgroud)
(问题在评论中。)
我有一个C++程序,它接受用户的一些文本并将其保存到文本文件中.以下是该计划的片段:
#include "stdafx.h"
#include <ctime>
#include <fcntl.h>
#include <iostream>
#include <string>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <Windows.h>
using namespace std;
int file_descriptor;
size_t nob;
int check_file(const char* full_path) //Method to check whether a file already exists
{
file_descriptor = open(full_path, O_CREAT | O_RDWR, 0777); //Checking whether the file exists and saving its properties into a file descriptor
}
void write_to_file(const char* text) //Method to create a file and write the text to it
{ …Run Code Online (Sandbox Code Playgroud) 如果有的话,我需要做什么来关闭我从中获得os.open并随后使用的文件描述符with os.fdopen?在OSError我从下面的代码让我觉得答案可能是"没有",但我无法找到的文档确认.
fd = os.open(os.path.expanduser("~/Desktop/foo"), os.O_WRONLY)
with os.fdopen(fd, "wt") as file:
pass
os.close(fd) # OSError: [Errno 9] Bad file descriptor
Run Code Online (Sandbox Code Playgroud) 我一直在寻找,找不到明确的线索来验证我从给我的脚本中得出的结论。
因此file.txt是一个打开的文件(由文件描述符3表示),并通过记录时间戳的脚本不断添加新行。每次将新行添加到文件时,下一段是否会进入while循环?
exec 3 < /path/file.txt
while read <&3
command
command..
done
Run Code Online (Sandbox Code Playgroud)
因此,只要我不关闭文件描述符,添加到我的file.txt中的新行将始终激活while循环,对吗?
请帮我解决这个问题。谢谢
我试图理解在调用fork()之后复制文件描述符的含义及其对争用的可能影响.
在"Linux编程接口"24.2.1(p517)中:
执行fork()时,子进程将收到所有父文件描述符的副本.这些重复是以dup()的方式进行的,这意味着父和子中的相应描述符引用相同的打开文件描述.
当我运行相同的代码时:
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/wait.h>
int main(void) {
char* fl = "/tmp/test_fd";
int fd;
fd = open(fl, O_CREAT|O_TRUNC|O_WRONLY, 0666);
if(!fork()) {
printf("cfd=%d\n", fd);
_exit(0);
} else {
int status;
printf("ffd=%d\n", fd);
wait(&status);
close(fd);
unlink(fl);
}
}
Run Code Online (Sandbox Code Playgroud)
我得到两个进程的相同文件描述符(数字?):ffd = 3和cfd = 3.但是当使用dup()运行此代码时:
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
int main(void) {
char* fl = "/tmp/test_fd";
int cfd, ffd;
ffd = open(fl, O_CREAT|O_TRUNC|O_WRONLY, 0666);
cfd = dup(ffd);
printf("ffd=%d\n", ffd);
printf("cfd=%d\n", cfd);
close(ffd);
unlink(fl);
}
Run Code Online (Sandbox Code Playgroud)
我得到不同的文件描述符:ffd …
我需要写入文件描述符3.我一直在搜索它,但文档很差.我发现的唯一的事情是使用libc库和fdopen方法,但我还没有找到任何关于如何使用它或在其上书写的例子.
任何人都可以给我一个写入Rust文件描述符的例子吗?
file-descriptor ×10
c ×5
file ×2
linux ×2
python ×2
assembly ×1
bash ×1
c++ ×1
contention ×1
file-io ×1
fork ×1
io ×1
networking ×1
posix ×1
rust ×1
sockets ×1
unix ×1
while-loop ×1
x86-64 ×1