Imobilis
Imobilis

Reputation: 1489

Printing to stdout without the involvement of functions

How can one print some arbitrary text on-screen without the call to any function ?


Another way of asking this question is how are i/o functions implemented ?


I tried searching in google, but unluckily, no result was found similarly to as if it was some sort of a top secret thing.

For study purposes. I personally don't feel even close to a real programmer without knowing this.

Upvotes: 0

Views: 83

Answers (1)

Matt Timmermans
Matt Timmermans

Reputation: 59144

Well, in the end, loosely speaking, the low level software in the computer sets a special memory location or uses a special instruction that changes the voltage on some pins on the CPU, and the hardware responds to these changes.

But user-level processes don't have access to those instructions or memory locations. Messing with stuff that drives the hardware is the responsibility of "device drivers" that execute in the kernel. They use these special memory locations or instructions, and each one is given responsibility over a particular hardware device.

User-level processes communicate with device drivers via system calls as mentioned in the comments. A system call is not quite like a normal function call -- you don't just call the code. After setting up a "request" for what it wants to do, the user-level process pokes the kernel, usually by using a software interrupt instruction. The kernel wakes up, looks at what you request, and then decides itself what code to execute. The kernel code runs at a higher privilege level and will call directly into the device drivers that access the hardware.

This is how the kernel keeps processes safe from each other.

To actually get from stdout to the screen is a lengthy process:

  • the standard library ends up making a system call that writes to a "pipe" that is attached to stdout. This is where it leaves your process.

  • The other end of the pipe is being read by the console. The console is a user-level process, so it has to do a system call to do the reading.

  • The console decides what to display, and how to make it visible to you. There will be a bunch more layers, but eventually there will be system calls into drivers that control the graphics hardware. They will mess with the bits that turn into pixels on the screen, making your text visible.

Upvotes: 3

Related Questions