为什么不直接写入I/O文件? [英] Why is direct I/O file not being written to?

查看:46
本文介绍了为什么不直接写入I/O文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解直接I/O.为此,我编写了这个小的玩具代码,仅用于打开文件并向其中写入文本字符串:

  #include< stdio.h>#include< stdlib.h>#include< fcntl.h>#include< errno.h>#include< unistd.h>#include< sys/types.h>#include< sys/stat.h>int main(int argc,char ** argv){字符文件[64];int fd;char message [64] ="jsfreowivanlsaskajght";sprintf(thefile,"diotestfile.dat");if((fd = open(thefile,O_DIRECT | O_RDWR | O_CREAT,S_IRWXU))== -1){printf(错误打开文件\ n");出口(1);}write(fd,message,64);关闭(FD);} 

我对Cray和GNU的编译命令是

  cc -D'_GNU_SOURCE'diotest.c 

而对于英特尔来说

  cc -D'_GNU_SOURCE'-xAVX diotest.c 

在所有三个编译器下,都使用正确的权限创建了diotestfile.dat文件,但从未写入任何数据.可执行文件完成后,输出文件为空白.罪魁祸首是 O_DIRECT (或更准确地说,是我对 O_DIRECT 的处理不当).如果我将其取出,则代码可以正常工作.我已经在尝试使用的更为复杂的代码中看到了相同的问题.我需要做些什么?

解决方案

好吧,您需要重新考虑这个问题,因为您的程序在我的系统上运行得很好,而且我无法从它的列表中猜测出错误可能出在哪里.>

  • 您在发布之前已经测试过了吗?
  • 如果程序未写入文件,则可能最好查看一下 write(2)的返回码.你做完了吗?我无法检查,因为在我的系统(intel 64bit/FreeBSD)上,程序按预期运行.

您的程序运行,没有输出,并且在目录.中出现了名为 diotestfile.dat 的文件,内容为 jsfreowivanlsaskajght .

  lcu @ europa:〜$ ll diotestfile.dat-rwx ------ 1 lcu lcu 64 1月2日.18:14 diotestfile.dat *lcu @ europa:〜$ cat diotestfile.datjsfreowivanlsaskajghtlcu @ europa:〜$ _ 

I am trying to understand direct I/O. To that end I have written this little toy code, which is merely supposed to open a file and write a text string to it:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>


int main(int argc, char **argv) {

  char thefile[64];
  int fd;
  char message[64]="jsfreowivanlsaskajght";
  sprintf(thefile, "diotestfile.dat");
  if ((fd = open(thefile,O_DIRECT | O_RDWR | O_CREAT, S_IRWXU)) == -1) {
          printf("error opening file\n");
          exit(1);
  }
  write(fd, message, 64);
  close(fd);

}

My compile command for Cray and GNU is

cc -D'_GNU_SOURCE' diotest.c

and for Intel it is

cc -D'_GNU_SOURCE' -xAVX diotest.c

Under all three compilers, the file diotestfile.dat is created with correct permissions, but no data is ever written to it. When the executable finishes, the output file is blank. The O_DIRECT is the culprit (or, more precisely I guess, my mishandling of O_DIRECT). If I take it out, the code works just fine. I have seen this same problem in a much more complex code that I am trying to work with. What is it that I need to do differently?

解决方案

Well, you need to rethink the question, because your program runs perfectly on my system, and I cannot guess from it's listing where the error can be.

  • Have you tested it before posting?
  • if the program doesn't write to the file, probably a good idea is to see about the return code of write(2). Have you done this? I cannot check because on my system (intel 64bit/FreeBSD) the program runs as you expected.

Your program runs, giving no output and a file named diotestfile.dat appeared in the . directory with contents jsfreowivanlsaskajght.

lcu@europa:~$ ll diotestfile.dat 
-rwx------  1 lcu  lcu  64  1 feb.  18:14 diotestfile.dat*
lcu@europa:~$ cat diotestfile.dat 
jsfreowivanlsaskajghtlcu@europa:~$ _

这篇关于为什么不直接写入I/O文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆