- Hands-On System Programming with C++
- Dr. Rian Quinn
- 384字
- 2021-07-02 14:42:28
Console input/output
If you have ever executed a command-line application, you willbe familiar with the concept of console-based input/output. This is especially true with respect to POSIX-compliant operating systems. When outputting to the console, you can either output to stdout (typically used for normal output) or stderr (typically used for outputting error messages).
Outputting to stdout and stderr is accomplished by an application performing a system that asks the operating system to deliver a character buffer to these output devices. (It should be noted that, in this book, we typically state that we are outputting to stdout, not printing to the console.)
The reason for this is that, on POSIX-compliant systems, your application doesn't actually know where it is sending the text to. The application leverages an API to output to stdout. This can be accomplished by:
- Writing to a dedicated file handle (that is, stdout)
- Using C APIs such as printf
- Using C++ APIs such as std::cout
- Forking an application that outputs to stdout for you (for example, by using echo)
Most of these examples, when all is said and done, make a system call to the operating system to transfer a character buffer to a device that manages stdout or stderr. In some cases, this causes the operating system to relay the resulting character buffer to the parent process (likely your shell), which will ultimately make another system call to display the character buffer on the screen.
However your operating system decides to handle this, a device driver exists in the operating system that manages the physical monitor used to display text, and the simple APIs the application calls to output text (for example, printf and std::cout) eventually provide this device driver with the requested character buffer.
Although, on most systems, the text being output to stdout is usually provided to your shell and eventually displayed on the screen, this doesn't have to be the case. Since the application is making a system call to output the character buffer, the operating system is free to forward this data to a serial device, log file, as input to another application, and so on.
This flexibility is one of the reasons POSIX-compliant operating systems are so powerful, and why learning how to properly make system calls is so important.
- 計算機組成原理與接口技術(shù):基于MIPS架構(gòu)實驗教程(第2版)
- 從0到1:數(shù)據(jù)分析師養(yǎng)成寶典
- InfluxDB原理與實戰(zhàn)
- Learning Spring Boot
- MySQL從入門到精通(第3版)
- 揭秘云計算與大數(shù)據(jù)
- 數(shù)據(jù)庫技術(shù)實用教程
- 重復(fù)數(shù)據(jù)刪除技術(shù):面向大數(shù)據(jù)管理的縮減技術(shù)
- INSTANT Android Fragmentation Management How-to
- 跨領(lǐng)域信息交換方法與技術(shù)(第二版)
- 數(shù)據(jù)指標(biāo)體系:構(gòu)建方法與應(yīng)用實踐
- Access 2016數(shù)據(jù)庫應(yīng)用基礎(chǔ)
- AndEngine for Android Game Development Cookbook
- 數(shù)字孿生
- Access 2013 數(shù)據(jù)庫管理與應(yīng)用從新手到高手