使用posix read()write()linux调用,是否保证如果我通过一个文件描述符写入并通过另一个文件描述符读取,则以串行方式使这两个动作相互排斥......我的读文件描述符总是会看到写文件描述符最后写的是什么?
我相信情况确实如此,但我想确定并且手册页对此没有太大帮助
我有一个程序从文件读取并写入文件.我想阻止用户为两者指定相同的文件(出于显而易见的原因).让我们说第一条路径在char* path1,第二条路径在char* path2.我可以fopen()两个路径,fileno()每个呼叫,并得到相同的号码?
为了更清楚地解释:
char* path1 = "/asdf"
char* path2 = "/asdf"
FILE* f1 = fopen(path1, "r");
FILE* f2 = fopen(path2, "w");
int fd1 = fileno(f1);
int fd2 = fileno(f2);
if(fd1 == fd2) {
printf("These are the same file, you really shouldn't do this\n");
}
Run Code Online (Sandbox Code Playgroud)
我不想比较文件名,因为人们很容易通过类似路径/asdf/./asdf或使用符号链接来破坏它们.最终,我不想将我的输出写入我正在阅读的文件中(可能会导致严重的问题).