(File descriptor 0, 1, 2), (stdin, stdout, stderr), (terminal device) the relationship between the three ???

Source: Internet
Author: User

Preface

In Linux, all devices are regarded as files. Each time a file is opened, there is a file descriptor that represents the file to be opened. When the program starts, three I/O device files are opened by default: stdin, stdout, and stderr. The file descriptors 0, 1, and 2 are obtained respectively.

Instance

Now let's look at an example of testing the ttyname function (the ttyname function returns the path name of the terminal device opened on the file descriptor ):

# Include "apue. h"

Int

Main (void)

{

Char * name;

 

If (isatty (0 ))

{

Name = ttyname (0 );

If (name = NULL)

Name = "undefined ";

}

Else

{

Name = "not a tty ";

}

Printf ("fd 0: % s \ n", name );

 

If (isatty (1 ))

{

Name = ttyname (1 );

If (name = NULL)

Name = "undefined ";

}

Else

{

Name = "not a tty ";

}

Printf ("fd 1: % s \ n", name );

 

If (isatty (2 ))

{

Name = ttyname (2 );

If (name = NULL)

Name = "undefined ";

}

Else

{

Name = "not a tty ";

}

Printf ("fd 2: % s \ n", name );

Exit (0 );

}

 

Run the program to obtain:

According to the program running result, the terminal devices opened on file descriptors 0, 1, and 2 are/dev/tty1, the preface clearly states that file descriptors 0, 1, and 2 are obtained by opening the stdin standard input file, stdout standard output file, and stderr standard output file. So which file does file descriptors 0, 1, and 2 open? How can I explain this ??? I hope you can give me some advice. Thank you very much!

 

Self-answer:

If a process is run from the shell, three file descriptors (0, 1, 2) exist by default. 0 is associated with the standard input of the process, 1 is associated with the standard output of the Process, 2 is associated with the standard error output of the process. By default, file descriptors 0, 1, and 2 are opened as standard input, standard output, and standard errors. Open a terminal device (such as/dev/tty) and naturally get a file descriptor (ttyfiledes ), copy the file descriptor opened for the terminal device to the standard input, standard output, and standard error. For example, use the following statement to copy the file:

Dup2 (ttyfiledes, 0 );

Dup2 (ttyfiledes, 1 );

Dup2 (ttyfiledes, 2 );

In this case, file descriptors 0, 1, and 2 are associated with/dev/tty.

Then, if you run the input and output operations, the objects are all terminal devices/dev/tty. That is to say, the input reads data from the terminal device/dev/tty, while the output writes the data to the terminal device/dev/tty.

The answer above is just a personal conjecture. I don't know if it is correct, but at least it is a reasonable explanation that makes me feel. If the explanation is not correct, please kindly advise me!

 

Continue self-solution:

Each process has its own standard input file, standard output file, and standard error file (which should be automatically allocated by the system ). By default, the system automatically opens the standard input file of the process on file descriptor 0, the standard output file on file descriptor 1, and the standard error on file descriptor 2. Normally, the input device (keyboard) is opened and copied to the standard input (0) of the process, the output device (Display) is turned on, and copied to the standard output (1) and standard errors (2 ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.