使用不同的文件描述符时,结果为何不同? (系统编程) [英] when using different file descripter, why is the result different? (system programming)

查看:96
本文介绍了使用不同的文件描述符时,结果为何不同? (系统编程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究文件描述符,并意识到如果使用dup2()函数, 结果会有所不同.

I am studying about file descripter and realized that if I use dup2() function, the result will be different.

第一个代码段...

int main(void){
    char buf1[BUFSIZ] = "I am low\n";

    printf("i am high\n");
    write(1, buf1, strlen(buf1));
    write(1, buf1, strlen(buf1));
    write(1, buf1, strlen(buf1));

    return 0;
}

...产生以下结果:

... produces the following result:

i am high
i am low 
i am low
i am low

但是第二个片段...

But the second snippet ...

int main(void){
    int fd = open("dupout",O_CREAT | O_WRONLY, 0655);
    char buf1[BUFSIZ] = "I am low\n";
    dup2(fd, 1);

    printf("i am high\n");
    write(1, buf1, strlen(buf1));
    write(1, buf1, strlen(buf1));
    write(1, buf1, strlen(buf1));
    close(fd);

    return 0;
}

...在dupout中产生以下内容:

... produces the following content in dupout:

i am low 
i am low
i am low
i am high

为什么结果不同?

推荐答案

解释:

IO缓冲区具有三种类型的full bufferline bufferno buffer.

IO buffer have three types full buffer, line buffer and no buffer.

第一个示例:

默认值,stdoutline buffer,表示当缓冲区已满或遇到\n时,缓冲区将刷新.

Default, stdout is line buffer, means when the buffer is fulled or when meet \n, buffer will flush.

int main(void){
    char buf1[BUFSIZ] = "I am low\n";
    printf("i am high\n");
    write(1, buf1, strlen(buf1));
    write(1, buf1, strlen(buf1));
    write(1, buf1, strlen(buf1));
    return 0;
}

输出:

i am high
I am low
I am low
I am low

但是当我们将其更改为:

But when we change it to :

int main(void){
    char buf1[BUFSIZ] = "I am low\n";
    printf("i am high");   // no '\n'
    write(1, buf1, strlen(buf1));
    write(1, buf1, strlen(buf1));
    write(1, buf1, strlen(buf1));
    return 0;
}

输出:

I am low
I am low
I am low
i am high

继续:更改为:

int main(void){
    char buf1[BUFSIZ] = "I am low\n";
    printf("i am high"); // no '\n'
    fflush(stdout);// flush
    write(1, buf1, strlen(buf1));
    write(1, buf1, strlen(buf1));
    write(1, buf1, strlen(buf1));
    return 0;
}

输出:

i am highI am low
I am low
I am low

第二个例子:

默认值,file IOfull buffer,表示当缓冲区已满时,缓冲区将刷新.

Default, file IO is full buffer, means when the buffer is fulled, buffer will flush.

int main(void){
    int fd = open("dupout",O_CREAT | O_WRONLY, 0655);
    char buf1[BUFSIZ] = "I am low\n";
    dup2(fd, 1);

    printf("i am high\n");
    write(1, buf1, strlen(buf1));
    write(1, buf1, strlen(buf1));
    write(1, buf1, strlen(buf1));
    close(fd);

    return 0;
}

输出:

I am low
I am low
I am low
i am high

但是当我们将其更改为:

But when we change it to :

int main(void){
    int fd = open("dupout",O_CREAT | O_WRONLY, 0655);
    char buf1[BUFSIZ] = "I am low\n";
    dup2(fd, 1);

    printf("i am high\n");
    fflush(stdout);
    write(1, buf1, strlen(buf1));
    write(1, buf1, strlen(buf1));
    write(1, buf1, strlen(buf1));
    close(fd);

    return 0;
}

输出:

i am high
I am low
I am low
I am low


          old

由于IO缓冲区.如果在printf之前添加setvbuf(stdout, NULL, _IONBF, 0);,则结果正确.该行表示未设置IO缓冲区.

Because of IO buffer. If you add setvbuf(stdout, NULL, _IONBF, 0); before printf, result is right. The line means set no IO buffer.

以下是所有code:

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

int main(void){
    int fd = open("dupout",O_CREAT | O_WRONLY, 0655);
    char buf1[BUFSIZ] = "I am low\n";
    dup2(fd, 1);

    setvbuf(stdout, NULL, _IONBF, 0);
    printf("i am high\n");
    write(1, buf1, strlen(buf1));
    write(1, buf1, strlen(buf1));
    write(1, buf1, strlen(buf1));
    close(fd);

    return 0;
}

这篇关于使用不同的文件描述符时,结果为何不同? (系统编程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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