【问题标题】:Write System Call编写系统调用
【发布时间】:2014-04-08 05:12:01
【问题描述】:

我正在用 LINUX 编写这个程序

#include <fcntl.h>
int main()
{

    int rd,id;
    char buff[10]={0};

    id = open("p_child.c",O_RDONLY|O_APPEND|O_WRONLY);
    if(id>1)
    {
        printf("I am file descriptor %d \n",id);

        rd = read(id,buff,8);
        printf("I am Reading file %s",buff);

        write(1,"New\n",4);
        printf("\nI am Writing file %s \n",buff);

    }
}

它的输出就像在 READ 系统调用之前打印 'New'。 为什么会这样??因为写系统调用是在读系统调用的“printf”之后使用的。

【问题讨论】:

  • printf 缓冲其输出。尝试在第二次 printf 调用之后添加 fflush(stdout);
  • or, printf("我正在读取文件 %s\n",buff);
  • 您对open() 使用了非法的flags 参数。您不能使用O_RDONLY | O_WRONLY 代替O_RDWR。阅读手册页。

标签: c


【解决方案1】:

这是因为输出被缓冲,直到你写出一个换行符。

【讨论】:

    【解决方案2】:

    更新这一行就可以了

    printf("I am Reading file %s \n",buff);
    

    【讨论】:

      【解决方案3】:

      在编写系统调用之前忘记在 printf 中添加“\n”。

      rd = read(id,buff,8);
      printf("I am Reading file %s\n",buff);
      write(1,"New\n",4);
      

      【讨论】:

        猜你喜欢
        • 2010-12-15
        • 2013-07-19
        • 2023-03-11
        • 1970-01-01
        • 2012-09-15
        • 2014-07-15
        • 2016-01-16
        • 1970-01-01
        • 2015-02-05
        相关资源
        最近更新 更多