使用不同的文件描述符时,结果为何不同? (系统编程)

时间:2018-10-31 10:48:54

标签: c systems-programming dup2

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

第一个代码段...

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

但是第二个片段...

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中产生以下内容:

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

为什么结果不同?

1 个答案:

答案 0 :(得分:3)

解释:

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

第一个示例:

默认情况下,stdoutline buffer,这意味着当缓冲区已满或遇到\n时,缓冲区将刷新。

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

但是当我们将其更改为:

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,这意味着当缓冲区已满时,缓冲区将刷新。

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

但是当我们将其更改为:

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缓冲区。如果在setvbuf(stdout, NULL, _IONBF, 0);之前添加printf,则结果正确。该行表示未设置IO缓冲区。

以下是所有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;
}