Arithmetic overflow
*
/
In OS/161,
mips
trap
uses these codes to decide whether it has been in-
voked because of an interrupt, a system call, or an exception.
CS350
Operating Systems
Winter 2016

Processes and the Kernel
27
Interrupts (Revisited)
•
Interrupts are a third mechanism by which control may be transferred to the
kernel
•
Interrupts are similar to exceptions. However, they are caused by hardware
devices, not by the execution of a program. For example:
–
a network interface may generate an interrupt when a network packet arrives
–
a disk controller may generate an interrupt to indicate that it has finished
writing data to the disk
–
a timer may generate an interrupt to indicate that time has passed
•
Interrupt handling is similar to exception handling - current execution context is
saved, and control is transferred to a kernel interrupt handler at a fixed address.
CS350
Operating Systems
Winter 2016
Processes and the Kernel
28
Interrupts, Exceptions, and System Calls: Summary
•
Interrupts, exceptions and system calls are three mechanisms by which control
is transferred from an application program to the kernel
•
When these events occur, the hardware switches the CPU into privileged mode
and transfers control to a predefined location, at which a kernel
handler
should
be located.
•
The kernel handler creates a
trap frame
and uses it to saves the application
thread context so that the handler code can be executed on the CPU.
•
Just before the kernel handler finishes executing, it restores the application
thread context from the trap frame, before returning control to the application.
In OS/161, trap frames are placed on the
kernel stack
of the thread performed
the system call, or of the thread that was running when the interrupt or ex-
ception occurred
CS350
Operating Systems
Winter 2016

Processes and the Kernel
29
System Calls for Process Management
Linux
OS/161
Creation
fork,execv
fork,execv
Destruction
exit,kill
exit
Synchronization
wait,waitpid,pause,
...
waitpid
Attribute Mgmt
getpid,getuid,nice,getrusage,
...
getpid
CS350
Operating Systems
Winter 2016
Processes and the Kernel
30
The fork,
exit, getpid and waitpid system calls
main() {
rc = fork(); /
*
returns 0 to child, pid to parent
*
/
if (rc == 0) { /
*
child executes this code
*
/
my_pid = getpid();
x = child_code();
_exit(x);
} else {
/
*
parent executes this code
*
/
child_pid = rc;
parent_pid = getpid();
parent_code();
p = waitpid(child_pid,&child_exit,0);
if (WIFEXITED(child_exit))
printf("child exit status was %d\n",
WEXITSTATUS(child_exit))
}
}
CS350
Operating Systems
Winter 2016

Processes and the Kernel
31
Process Creation Example (Part 1)
Kernel
Process A
Parent process (Process A) requests creation of a new process.
fork
CS350
Operating Systems
Winter 2016
Processes and the Kernel
32
Process Creation Example (Part 2)
Kernel
Process A
Process B
B’s thread is
ready, not running
system call return
Kernel creates new process (Process B)
fork
CS350
Operating Systems
Winter 2016

Processes and the Kernel
33
The execv system call
int main()
{
int rc = 0;
char
*
args[4];
args[0] = (char
*
) "/testbin/argtest";
args[1] = (char
*
) "first";
args[2] = (char
*
) "second";
args[3] = 0;
rc = execv("/testbin/argtest", args);


You've reached the end of your free preview.
Want to read all 155 pages?
- Winter '10
- TIMBRECHT
- Operating Systems