All right, let's start with your "shell scripting program." Let's say your script includes this line:
Quote:
echo 'A hollow voice says, "plugh."'
|
Very soon, the shell is going to need to produce that line of text on the console. But... how does it do that? The answer, ultimately, is that it issues a "system call."
A "system call" is the mechanism that any program
must use to ask another part of the operating system to do something that it cannot do on its own, such as "anything at all to do with the physical hardware."
Nearly all of the time, the program doesn't issue the call directly on its own: it uses a pre-existing library of subroutines to do the job. After all, the necessary system-calls on
Linux are not the same as those on
Windows, nor is the mechanism for issuing the call the same... but a program such as "the shell" can run equally in all of these environments. It does this by means of the libraries which have been "ported to" each of these various environments.
Okay, finally it's time to hand-over the keys to the kernel. This is where your
non-privileged program goes hat-in-hand to the
privileged kernel-code which,
if it so chooses, can do what you ask. There are actually
several hardware mechanisms that can be used to accomplish the transition, which is "necessarily one-way." (You can't just go anywhere-you-like in the kernel code: you can only go to specific destinations.)
If you look in the
/arch subdirectory of the Linux source-tree, you'll find system-call implementations for each of the various CPU types, and you'll find that, even on a single architecture, Linux can use any one of several available methods. When the kernel is compiled for a particular CPU-type and model, it will be built to use the most-efficient mechanism available. (Only the code to support
that mechanism will actually be included. Very-conveniently, the standard macros and functions that are used to generate the calls are also built, at that same time, so that they will use that mechanism.)
Even "good ol' MS-DOS" had such a mechanism, in the form of the
INT (software interrupt) instruction. IBM mainframes had the
SVC (service call) instruction. These days, we tend to use "call gates." In any case, it's a one-way door that non-privileged programs can
use but cannot
influence. The capacity to do this is built-in to the hardware.