Home php what are STDIN, STDOUT streams?

what are STDIN, STDOUT streams?

Author

Date

Category

Trying to understand PHP multithreading.
One site says:
So, let’s begin. First, let’s close the STDIN, STDOUT streams:

fclose (STDIN);
fclose (STDOUT);
fclose (STDOERR);

The question is what is it and why should it be closed first? And if I run the script on a local machine (Denver), then it swears at these lines.


Answer 1, authority 100%

STDIN, STDOUT, and STDERR are three special files that bind to the terminal (on Windows, the console) in console applications.

The STDIN file (standard input) is “linked” to the keyboard by default – whatever you type on the keyboard goes there.

The STDOUT file (standard output) is “linked” to the monitor by default – whatever you write to it, you will see it on the screen.

The STDERR (Standard Error Output) file is a copy of STDOUT. Used to display error messages. This separation is done so that you can redirect them separately. So, the pipes (pipelines) of the command line do not touch STDERR – therefore, normal output goes further along the pipeline, and errors are displayed immediately on the screen.

The word “connected” above I took in quotes, because there really is no direct connection. Formally, all three files are associated by default with the same terminal, and the terminal somehow processes keyboard keystrokes and displays labels on the screen.

Closing these files in Linux is part of the so-called daemonization procedure (going into the background), this is done to stop the process from being considered by the OS as running on this terminal.

In multithreaded programs, these files are often closed in order to open new ones “in their place”. In other words, this is a way for a program to redirect I / O streams to itself. See the example below – something else must be done with these files.


Now why didn’t the example work for you? There are two reasons, and both reasons are in the word “Denver”.

First, as I said, these files are a sign of a console script, not a web page. The script does not need to be run through Denver, it must be run from the console:

`php path / to / script.php parameters`

You should probably look for the chapter on running php from the console before tackling multithreading.

Secondly, most of the tricks with these files are specific to Unix-like operating systems. Therefore, you will most likely have to learn to work with Linux.

This can be done in 4 ways:

  1. The most radical option is to just go and install Ubuntu. Ubuntu is a fairly popular Linux distribution for desktops. Most web projects are made under Linux (you can say Linux on the web is the “default” OS), so such a radical step is justified.

  2. You can raise a virtual machine and install Debian on it. This option is close to what you will have to face when trying to upload the result of your work to VDS. Debian is the most stable Linux distribution, well suited for general purpose servers. Especially for training servers.

  3. You can get an analogue of the linux environment by installing cygwin. A good way to understand “what is a terminal in Linux” without installing Linux. Probably the easiest option – all files are located on the same computer.

  4. If you have Windows 10 x64 – you can try Windows Subsystem for Linux . I myself did not work with this joint creation of Canonical and Microsoft – so I cannot say how this option differs from the option with cygwin.


Answer 2, authority 50%

Streams STDIN / OUT / ERR have nothing to do with the concept of multithreading. Multithreading is usually understood as the execution of several threads of commands of the same application simultaneously on different processors (cores).

And STD * streams are data streams STDIN is the standard input of the program (what comes from the terminal or from the client’s browser). STDOUT – standard output – where echo / print commands output, console or socket sending data to browser. STDERR is a stream of error messages.

But why close them – you need to look at the context (on the site where you read it). Closing may be required, for example, in order to unbind the program from the controlling terminal and it becomes a background process of the operating system (executed in conjunction with fork). Or, since you are talking about multi-threaded applications, so that the execution thread would also be independent of the operating system terminal and, possibly, then redisclose I / O to communicate with other threads or processes.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions