type interrupt
typical action panic
hardware error emergency shutdown
power failure emergency shutdown
exception illegal instruction
kill process privileged or
undefined arithmetic exception
kill process e.g. divide by zero
page fault page in the missing page
device clock interrupt
do bookkeeping, check timers, reschedule disk interrupt
wakeup waiting process, start new IO operation terminal interrupt
wakeup waiting process trap
software trap switch on requested service
After setting up the interrupt vector, the system creates the first process environ- ment. This process then forks additional system processes and the init process. The
init process forks login processes for the various terminals connected to the system or the virtual terminals, if connections are through a network.
9.2 Timers
providing time-related services. example: videoaudio needs to occur at given rate in real time; old games would run faster when the PC was upgraded...
interface: ask for signal after certain delay. best effort service, may miss. using periodic clock sets the resolution. improved by soft timers [1], one-shot timers [5].
dependence on clock resolution [3]. Vertigo [4] — reduce hardware clock rate when not needed, to save power.
question of periodic timers, and need to keep control. if only one app, just let it run: control will return if something happens e.g. network or terminal interrupt.
9.3 Kernel Priorities
considerations for priorities in kernel: the more low-level a process, and the more resources it holds, the higher its priority [2, p. 249]. for example, if waiting for disk
IO has higher priority than if waiting for a buffer, because the former already has a buffer and might finish and release it and other resources.
alternative of priorities based on device speeds – higher priority of waiting on slower device =terminal =user. check sun
178
9.4 Logging into the System
9.4.1 Login
When you sit at a terminal connected to a Unix system, the first program you en- counter is the login program. This program runs under the root user ID the supe-
ruser, as it needs to access system information regarding login passwords. And in any case, it doesn’t know yet which user is going to log in.
The login program prompts you for your login and password, and verifies that it is correct. After verifying your identity, the login program changes the user ID
associated with the process to your user ID. It then execs a shell.
A trick
The login program need not necessarily exec the shell. For example, new student reg- istration may be achieved by creating a login called “register”, that does not require a
password, and execs a registration program rather than a shell. New students can then initially login as “register”, and provide their details to the system. When the registration
program terminates they are automatically logged off.
9.4.2 The Shell
A shell is the Unix name for a command interpreter. Note that the shell runs in the context of the same process as the one that previously ran the login program, and
therefore has the same user ID and resulting priviledges. The shell accepts your commands and executes them. Some are builtin commands of the shell, and others
are separate programs. Separate programs are executed by the forkexec sequence described below.
The shell can also string a number of processes together using pipes, or run pro- cesses in the background. This simply means that the shell does not wait for their
termination, but rather immediately provides a new prompt. Exercise 138 What happens if a background program needs to communicate with the
user?
9.5 Starting a Process
In all systems, processes can be started in two main ways: by a direct user command, or by another process.
In unix, processes are typically started by issuing a command to the shell. The shell then forks, and the resulting new process runs the desired program. But the
option to fork is not restricted to the shell — every program can fork. Thus any pro- gram can create additional processes, that either continue to run the same program
or convert to another program using the exec system call.
179
In Windows, a program is started by clicking on its icon on the desktop or by select- ing it from a menu. It also has a very nice mechanism for invoking one application
from within another, called “object linking and embedding” OLE. For example, a text processor such as Word may embed a graphic created by a spreadsheet like Excel
this means that the graphic appears at a certain location in the text. When editing the text, it is then possible to click on the graphic and invoke the application that
originally created it.
9.5.1 Constructing the Address Space
The exec system call constructs the new address space based on information con- tained in the executable file. Such a file starts with a predefined header structure
which describes its contents. First comes a “magic number” that identifies this file as an executable of the right type there is nothing magic about it — it is just a 16-bit
pattern that hopefully will not appear by accident in other files. Next comes a list of sections contained in the file. Some of the sections correspond to memory segments,
such as the text area for the process, and the initialized part of the data segment. Other sections contain additional information, such as a symbol table that may be
used by a debugger. The header also contains the address of the entry point — the function in the text area where execution should begin.
Exercise 139 Script files often start with a line of the form bin hinterpreteri. What
is the magic number in this case? Is it relevant? exec creates a text segment and initializes it using the text section of the exe-
cutable. It creates a data segment and initializes it using another section. However, the created segment in this case is typically larger than the corresponding section, to
account for uninitialized data. A stack segment is also created, which has no corre- sponding section in the executable.
A special case occurs when shared libraries are used shared libraries are called dynamic link libraries DLLs in Windows. Such libraries are not incorporated into
the text segment by the compiler or rather, the linker. Instead, an indication that they are needed is recorded. When the text segment is constructed, the library is
included on the fly. This enables a single copy of the library code to serve multiple applications, and also reduces executable file sizes.
9.6 Context Switching
To switch from one process to another, the operating system has to store the hardware context of the current process, and restore that of the new process.
Rather than creating a special mechanism for this purpose, it is possible to use the existing mechanism for calling functions. When a function is called, the current
180
register values are typically stored on the stack, only to be restored when the func- tion returns. The trick in using this for context switching is to swap stacks in the
middle Thus the current process calls the context switching function, causing all the hardware context to be stored on the kernel stack. This function loads the memory
mapping registers with values of the new process, including those used to access the stack. At this instant, the CPU switches from executing the old process to the new one
— which is also in the middle of the same function... The function completes already in the context of the new process, and returns, restoring the state of the new process
as it was when it called the context switching function in order to stop running.
9.7 Making a System Call