[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17]
Name at least two generic purposes of an operating system.
There were three that we examined, but potentially there are others too.
Because the disk takes quite some time to respond, the operating system cannot simply wait until the disk responds to any requests it makes of the disk. Instead, the operating system will send the request and proceed to other work. Nonetheless, when the disk's response is ready, the operating system must be able to respond quickly. How is this normally implemented?
The CPU is designed so that an electrical signal can be passed into it signaling a hardware interrupt. When the CPU receives a signal, it suspends the work on the current process and enters the OS interrupt handler so the OS can deal with the disk response promptly.
Older operating systems provided a library of subroutines that a
program could call in order to interact with devices, using
instructions akin to the ARM's BL instruction. Why do
modern operating systems eschew this simple technique in favor
of the more complex system where system calls are made via
software interrupts? Explain your answer.
The primary reason is that calling a subroutine maintains the same
privileges; thus, with this technique, the operating system
would have to work at the same privilege level as regular
processes, and so regular processes would need access to all
facets of the system. This allows software (both malicious
software such as viruses, as well as trusted
programs that
simply have bugs) to wreak havoc on the system. In modern
systems, an interrupt provides a way to switch from the user
mode
of regular processes into the supervisor mode
used by the
operating system.
We saw that when an ARM CPU executes its SWI
instruction, it enters supervisor mode, which allows access to all
parts of the computer. How does it allow such mode switching while allowing
the operating system to prevent malicious programs such as viruses
from executing code while in supervisor mode?
At the same time it switches into supervisor mode, the ARM
CPU transfers control to the instruction at memory address 0x8.
The operating system should have placed its own code at this
address and arranged the memory permissions so that the user
process cannot modify it. Thus, a virus executing SWI
would be transferring control into the operating system
code.
Because other users' information is stored on the disk, a multiuser computer system cannot allow a process to access the disk directly. Yet programs often need to read from the disk. Explain how a computer system can allow this without compromising security.
The CPU implements two modes, supervisor mode and user mode, and prevents a process from accessing the disk directly while in user mode. The operating system enters user mode every time it exits into a user program, so that the user program runs in user mode and cannot access the disk. But the user program can initiate an interrupt using a CPU instruction, whereupon the CPU will switch into supervisor mode as part of the interrupt process and jump into the interrupt handler, implemented by the operating system. The operating system's handler, running in supervisor mode, can access the disk on the process's behalf (after it verifies that the request is legitimate) before returning back into the user program in user mode.
Explain the difference between user mode and supervisor mode, and explain why modern CPUs include the capability to run in either of these modes.
When in user mode, the CPU places many restrictions on its behavior (particularly restricting access to memory and to external devices); these restrictions do not apply when the CPU is executing in supervisor mode. Having multiple modes allows the operating system to maintain control over the computer, so that buggy or malevolent user software cannot damage its resources.
For each of the following CPU instructions, which of the following applies?
A. The instruction should be permitted whether the CPU is running in user mode or supervisor mode. B. The instruction should be allowed only when the CPU is running in supervisor mode.
| a. | Switch between modes (supervisor to user, or user to supervisor). |
| b. | Send a software-initiated interrupt to the CPU. |
| c. | Disable interrupts by clearing the interrupt flag. |
| d. | Send information to an I/O device. |
| e. | Call a subroutine. |
| a. | B. |
| b. | A. |
| c. | B. |
| d. | B. |
| e. | A. |
What distinguishes system calls (such as write)
from library functions (such as printf)? Given
that they are less efficient, why do programmers typically
prefer library functions?
Code for system calls are included in the operating system, while code for library functions are included with the running program. Thus, a system call's code runs in supervisor (unprotected) mode, while a library function's code runs in user mode. A program enters a system call by initiating a software interrupt, while a program enters a library function by calling a subroutine included with the program.
Programmers typically prefer library functions because
they are typically less platform-dependent (a program written
under Linux can more easily be ported to Windows) and provide more
convenient functionality (the OS system calls typically
provide bare minimum convenience — like
printf providing so many formatting options in contrast
to no formatting at al with write, its closest
system-call relative under Linux.
Explain what a Unix shell does so that the output of a process is redirected into a file instead of to the screen, as it is told to do in the following shell command.
unix% wc words > count
Since the wc command writes to file descriptor 1, the shell must insure that file descriptor 1 for the process running wc will refer to the count file. To accomplish this, it closes file descriptor 1 in the child process it creates for running wc and then designates descriptor 1 to refer to the count file instead. Thus, when wc runs and writes to file descriptor 1, the output will go into the file instead of to the screen.
Explain why the operating system would move a process in the Running state to the Blocked state instead.
When a running process requests interaction with a device that cannot immediately respond, the operating system moves it into the Blocked state so that it will not occupy CPU time while the device is working.
Describe a situation where the operating system would move a process from the Blocked state to the Ready state.
A process is typically in the Blocked state when it is waiting for some response from a I/O device (such as the keyboard or hard drive). The operating system would move it into the Ready state when it has received a response for the I/O device for the blocked process.
In the following diagram illustrating the various states a process can be in, draw arrows connecting each pair of states that a preemptive operating system may move a process between. Label each arrow with a brief description of a situation where the operating system would move the process as indicated.
|
|
Running to Ready: process's time slice has expired. Ready to Running: CPU is idle, ready to work. Running to Blocked: processes makes request forcing communication with I/O device Blocked to Ready: I/O device has completed action necessitated by process |
Because the CPU can run only one thread of execution at once, when a user program is running, the operating system cannot be. How, then, can the OS manage to preempt a running program that runs for a long period of time and attempts to run beyond its time slice?
Before beginning to run the user program, the OS schedules the clock device to send an interrupt. When this interrupt occurs, the CPU automatically jumps into the exception handler for the clock's interrupt, which would be code existing within the OS. The OS's handler can then preempt the process and schedule another to run.
Suppose we were to run the following program below.
#include <stdio.h>
int i = 1;
int main() {
while(i < 4 && fork() != 0) {
i++;
}
printf("%d ", i);
exit(0);
}
Which of the following outputs might occur?
| a. | 1 1 2 4 |
| b. | 1 2 3 |
| c. | 1 2 3 4 |
| d. | 1 2 4 |
| e. | 3 1 2 4 |
| f. | 4 |
| g. | 4 3 2 1 |
| h. | 4 4 4 4 |
The possible outputs are c., e., and g..
Suppose we were to run the following program.
#include <stdio.h>
int i = 0;
int arr[4] = { 2, 3, 5, 7 };
int main() {
while(i < 2 && fork() != 0) i++;
arr[i] = arr[i] + 1;
printf("%d ", arr[i + 1]);
exit(0);
}
Which of the following outputs might occur?
| a. | 2 3 5 | d. | 3 6 7 | g. | 7 4 6 | ||
| b. | 2 3 5 7 | e. | 4 6 7 | h. | 7 7 7 | ||
| c. | 3 5 7 | f. | 7 5 3 | i. | 7 7 7 7 |
c. and f.
What might be displayed by the below program?
#include <stdio.h>
int main() {
int i = 1;
while(i < 3) {
fork();
i++;
}
printf("%d ", i);
exit(0);
}
3 3 3 3
What does Linux do when requested to perform the execvp
(or execve) system call?
It replaces the currently running process with the program specified in
the parameters to execvp.
The new process keeps the state of the replaced process, but its memory
changes to the memory image required by the program, and execution
enters the started program.
Execution will not return to the program calling execvp unless
an error prevents the OS from executing the request.