Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Introduction to kernel<br />
}<br />
{<br />
printf("can't create file %s\n", argv[2]);<br />
exit(1);<br />
}<br />
copy (fdold, fdnew);<br />
exit(0);<br />
copy(int old, int new)<br />
{<br />
int count;<br />
while ((count = read(old, buffer, sizeof(buffer))) > 0)<br />
write (new, buffer, count);<br />
}<br />
UNIX system can execute in two modes, kernel or user, it uses a separate stack for each mode.<br />
The user stack contains <strong>the</strong> arguments, local variables, and o<strong>the</strong>r data for functions executing in user mode.<br />
The kernel stack of a process is null when <strong>the</strong> process executes in user mode.<br />
! User and<br />
Kernel<br />
Stack<br />
for Copy<br />
Program<br />
User Stack Kernel Stack<br />
local not<br />
vars shown<br />
addr of frame 2<br />
ret addr after write<br />
params new<br />
to<br />
buffer<br />
write count<br />
local count<br />
vars<br />
addr of frame 1<br />
ret addr after copy<br />
params old<br />
to<br />
new<br />
copy<br />
local count<br />
vars<br />
addr of frame 0<br />
ret addr after main<br />
params argc<br />
to<br />
argv<br />
main<br />
frame 3<br />
call<br />
write()<br />
frame 2<br />
call<br />
write()<br />
frame 1<br />
call<br />
main()<br />
local<br />
vars<br />
addr of frame 1<br />
ret addr after func2<br />
params func2<br />
to<br />
kernel<br />
local<br />
vars<br />
addr of frame 0<br />
ret addr after func1<br />
params func1<br />
to<br />
kernel<br />
frame 3<br />
frame 2<br />
call<br />
func2()<br />
frame 1<br />
call<br />
func1()<br />
start frame 0 system call interface frame 0<br />
Data Structures for Processes<br />
Every process has an entry in <strong>the</strong> kernel process table. Each process is allocated a u area (private data manipulated<br />
only by <strong>the</strong> kernel).<br />
The process table contains pointers to a per process region table, whose entries point to entries in a region table. A<br />
region is a contiguous area of a process's address space, such as text, data and stack.<br />
Region table entries describe <strong>the</strong> attributes of <strong>the</strong> region, whe<strong>the</strong>r it contains text or data, whe<strong>the</strong>r it is shared or<br />
private, and where <strong>the</strong> "data" of <strong>the</strong> region is located in memory.<br />
When a process invokes. "fork", <strong>the</strong> kernel duplicates <strong>the</strong> address space of <strong>the</strong> old process, allowing processes to<br />
share regions when possible and making a physical copy o<strong>the</strong>rwise.<br />
84