Home  >  Article  >  Computer Tutorials  >  Principle of Console.WriteLine under .NET9 Linux-x64

Principle of Console.WriteLine under .NET9 Linux-x64

WBOY
WBOYforward
2024-02-23 09:04:10604browse

.NET9 Linux-x64下Console.WriteLine原理

1 Introduction

I have talked about the common principles of Console.WriteLine in the running process of Console.WriteLine under Windows. Do you know? This article takes a look at its latest .NET9 running process under Linux-x64

2. Managed code

In .NET, the two classes StreamReader and StreamWriter are usually used for stream reading and writing operations, and their namespaces are located in System.IO. These classes are included in the managed library System.Runtime.dll. Here is a simple example operation:

using (StreamWriter writer = File.CreateText("newfile.txt"))
{
await writer.WriteLineAsync("First line of example");
await writer.WriteLineAsync("and second line");
}

StreamWriter and StreamReader are derived from TextWriter and TextReader respectively. The string printed by Console.WriteLine is the data stream/device written directly through TextWriter in the CLR library.

For example:

static void Main(string[] args)
{
 Console.WriteLine("Call Main");
}

Console.WriteLine will call the Out.EnsureInitialized function. Out is the TextWriter type. The code is as follows:

源码地址:https://github.com/dotnet/runtime/blob/main/src/libraries/System.Console/src/System/Console.cs

public static TextWriter Out
{
get
{
static TextWriter EnsureInitialized()
{
lock (s_syncObject)
{
if (s_out == null)
{
Volatile.Write(ref s_out, CreateOutputWriter(ConsolePal.OpenStandardOutput()));
}
return s_out;
}
}
}
}

The EnsureInitialized function calls the Volatile.Write function. The Volatile.Write function means to write the value of parameter two into the parameter one field. The explanation here is to write the value returned by CreateOutputWriter(ConsolePal.OpenStandardOutput()) into the s_out field. s_out is also a TextWriter type.

Here is the value returned by CreateOutputWriter(ConsolePal.OpenStandardOutput()). The CreateOutputWriter function is as follows:

源码地址:https://github.com/dotnet/runtime/blob/main/src/libraries/System.Console/src/System/Console.cs


private static TextWriter CreateOutputWriter(Stream outputStream)
{
return outputStream == Stream.Null ?
TextWriter.Null :
TextWriter.Synchronized(new StreamWriter(
stream: outputStream,
encoding: OutputEncoding.RemovePreamble(), 
bufferSize: WriteBufferSize,
leaveOpen: true)
{
AutoFlush = true
});
}

You can see that CreateOutputWriter still returns TextWriter, which means that Volatile.Write writes stream data to stream data and then prints it out from the device.

What is the stream data value written here? Continue to look at the ConsolePal.OpenStandardOutput() function, which returns a Stream type.

源码地址:
https://github.com/dotnet/runtime/blob/main/src/libraries/System.Console/src/System/ConsolePal.Unix.cs


public static Stream OpenStandardOutput()
{
return new UnixConsoleStream(Interop.CheckIo(Interop.Sys.Dup(Interop.Sys.FileDescriptors.STDOUT_FILENO)), FileAccess.Write);
}

Here are the operations under Linux/Unix. A UnixConsoleStream class is instantiated in OpenStandardOutput. In the constructor of UnixConsoleStream, the first parameter is Interop.CheckIo(Interop.Sys.Dup(Interop.Sys.FileDescriptors.STDOUT_FILENO) ). Interop.Sys.Dup is a function under Linux that performs file table entry operations. CheckIo is a check function and can be ignored here.

For example, generally speaking, printing a hello World on the terminal under Linux usually uses printf("hello Word")

printf -> stdout(标准输出) -> Dup(STDOUT_FILENO) ->终端输出

It can be seen that under Linux, the parameter STDOUT_FILENO is passed through the Dup function for terminal operations. The second parameter of UnixConsoleStream is FileAccess.Write, which means writing a string. Then encapsulate this Stream and return it, write it to the terminal stream through Volatile.Write and print it out.

Then it’s actually very clear. The process is roughly as follows:

Console.WriteLine ->Out.EnsureInitialized -> ConsolePal.OpenStandardOutput() -> I

The above is the detailed content of Principle of Console.WriteLine under .NET9 Linux-x64. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:mryunwei.com. If there is any infringement, please contact admin@php.cn delete