Trying to understand multithreading in PHP. On one site it says: Let's get started. To begin, close the streams STDIN, STDOUT:

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 I swear at these lines.

  • stdin (standard input), stdout (standard output) and stderr (standard error). Typically, these streams are sent to the console, but in environments that support I / O redirection, they can be redirected by the operating system to another device. - Invision

2 answers 2

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

The STDIN file (standard input) by default is "connected" with the keyboard - everything you type on the keyboard gets there.

The STDOUT (standard output) file is by default “linked” to the monitor — you will see everything that you write into it on the screen.

The STDERR file (standard error output) is a copy of STDOUT. Used to display error messages. This separation is made to be fashionable to redirect them separately. So, the command line pipes (pipelines) do not touch STDERR - therefore the normal output goes further along the pipeline, and errors are displayed immediately on the screen.

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

Closing these files in Linux is part of the so-called demonization procedure (going into the background), this is done so that the process can no longer be considered 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 - there must be something else done with these files.


Now why haven't you got an example? There are two reasons, and both 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 run through Denver, it must be run from the console:

 `php путь/к/скрипту.php параметры` 

Perhaps you should look for a chapter about running php from the console before you start multithreading.

Secondly, most 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 as many as 4 ways:

  1. The most radical option is to simply take and install Ubuntu for yourself. 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 OS by default), so this radical step is justified.

  2. You can raise the virtual machine and put Debian on it. This option is close to what you will encounter when trying to post the result of work on the 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 setting yourself cygwin. A good way to understand "what is a terminal in Linux" without installing Linux. Probably the easiest option - because all the files are on the same computer.

  4. If you have Windows 10 x64, you can try Windows Subsystem for Linux . I myself didn’t work with this joint creation of Canonical and Microsoft - so I can’t say how this option differs from the cygwin version.

  • I installed Ubuntu 14.04 on an Oracle virtual machine, and how to continue working with Linux, that it was as close as possible to VPS / VDS? - Vitaliy Fesyura
  • @VitaliyFesyura to be as close as possible - remove all graphics programs and work from the console. - Pavel Mayorov
  • I read right now that Ubuntu Server 64-bit does not have a graphical interface. Maybe it makes sense to install it. Type closer to reality. What do you advise? And another question, can I use the browser without a graphical interface? - Vitaliy Fesyura
  • @ VitaliyFesyura why do you need a browser on the server? - Pavel Mayorov
  • Well, how to check what I created. I thought it would be like Denver on Windows, wrote and immediately looked at how it works. - Vitaliy Fesyura

STDIN / OUT / ERR streams have nothing to do with the concept of multithreading. Multi-threading usually refers to the execution of several command streams of the same application simultaneously on different processors (cores).

And STD * streams are STDIN data streams - the standard input of a program (what comes when entering from a terminal or from a client browser). STDOUT - standard output - this is where the echo / print commands output, the console or the socket that sends the data to the browser. STDERR is a stream of error messages.

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