Role of the Unix Console
#1
Question  Role of the Unix Console
Hi guys,

Am I correct to assume that the stderr of all running programs ends up in the Unix Console and stdout goes to the shell (terminal window) you used to load the program?
And... am I correct to assume that, if a program was launched from the desktop, its stdout also goes to the Console?

The reason why I am asking is...
I'm struggling a bit with Motif and I don't want to delay functionality, so I'm creating a quick CLI to set some basic parameters that I will later incorporate into menus, and I'd like this CLI to be bound to the terminal window where the program was launched from, and perhaps stderr to go to the Console.

Are there specific guidelines or each program and developer did it their own way?

Thanks, as always.
Shiunbird
Administrator

Trade Count: (1)
Posts: 553
Threads: 45
Joined: Mar 2021
Location: Czech Republic
Find Reply
02-08-2022, 09:55 PM
#2
RE: Role of the Unix Console
When you ask the shell to run a program, it first fork()s. Now there is a parent process running the shell, and a child process running the same shell. The file descriptors that were open in the parent are also open in the child. If you asked for redirections, the child then changes its open file descriptors (using creat(), dup2(), open(), etc) according to the requested redirections. Then finally, the child calls exec() with the requested program's name and arguments. exec() replaces the running program image in the child with the new program, but the file descriptors aren't changed. So whatever redirections you asked for are still in place, and if there are descriptors that remained from the parent, they are still the same (shared with the parent process).

So when you run "foo > out", the shell:
fork()s, and in the child process:
newfd = open( "out", O_WRONLY | O_CREAT | O_TRUNC, (mode_t) 666 ),
dup2( newfd, stdout ); close( newfd ),
exec( "foo", { "foo", 0 }, env )

In this example, stdin and stderr are still the same as (shared with) the parent, so anything written to stderr will go to where the parent shell's stderr goes, usually the terminal in which the shell is running. The reason stdout and stderr are different is so you can redirect output to a file, pipe, socket, etc, but with errors still going to your terminal.

What happens when programs are launched from a program that isn't a shell? They normally have the same standard I/O descriptors as the parent process, which shares with its own parent, etc... the chain terminates in /dev/console which is opened by getty. /dev/console is both stdin, stdout, and stderr for process trees that never redirect to other files/devices.

Personaliris O2 Indigo2 R10000/IMPACT Indigo2 R10000/IMPACT Indigo2 Indy   (past: 4D70GT)
(This post was last modified: 02-09-2022, 12:21 AM by robespierre.)
robespierre
refector peritus

Trade Count: (0)
Posts: 640
Threads: 3
Joined: Nov 2020
Location: Massholium
Find Reply
02-09-2022, 12:18 AM
#3
RE: Role of the Unix Console
the "console" program on IRIX is basically all the messages that go on tty0 outputted to X to allow for uncluttering.

I'm the system admin of this site. Private security technician, licensed locksmith, hack of a c developer and vintage computer enthusiast. 

https://contrib.irixnet.org/raion/ -- contributions and pieces that I'm working on currently. 

https://codeberg.org/SolusRaion -- Code repos I control

Technical problems should be sent my way.
Raion
Chief IRIX Officer

Trade Count: (9)
Posts: 4,239
Threads: 533
Joined: Nov 2017
Location: Eastern Virginia
Website Find Reply
02-09-2022, 01:56 AM
#4
RE: Role of the Unix Console
Perfect explanation. Exactly what I was looking for.
Thank you!
Shiunbird
Administrator

Trade Count: (1)
Posts: 553
Threads: 45
Joined: Mar 2021
Location: Czech Republic
Find Reply
02-09-2022, 07:32 AM


Forum Jump:


Users browsing this thread: 1 Guest(s)