Beruflich Dokumente
Kultur Dokumente
CSE451
Andrew Whitaker
Overview
Multiprogramming:
Firefox
Word
javac
Firefox
Word
time
Transfer
The Process
The
process is an OS
abstraction for a running
program
is associated
with an address space
kernel space
stack
(dynamic allocated mem)
SP
Process
Preview:
Thread is a
running program without
its own address space
heap
(dynamic allocated mem)
static data
(data segment)
code
(text segment)
PC
On
a context switch
Save
New PCB
In
process ID (PID)
execution state (Ready, Running, Blocked)
program counter, stack pointer, registers
memory management info
UNIX username of owner
scheduling priority
accounting info
linux:
defined in task_struct (include/linux/sched.h)
over 95 fields!!!
States of a process
running
interrupt
(unschedule)
dispatch
ready
interrupt
(I/O complete)
blocked
blocking I/O
State queues
The
As
State queues
Ready queue header
head ptr
tail ptr
netscape pcb
emacs pcb
cat pcb
netscape pcb
ls pcb
There
defineswitch_to(prev,next,last)do{
unsignedlongesi,edi;
asmvolatile("pushl%%ebp\n\t"
"movl%%esp,%0\n\t/*savestackptr*/
"movl%5,%%esp\n\t/*restorestackptr*/
"movl$1f,%1\n\t" /*saveinstr_ptr*/
"pushl%6\n\t"
/*restoreinstr_ptr*/
"jmp__switch_to\n/*ReturntoC*/
"1:\t"
/*1:is$1f*/
"popl%%ebp\n\t"
:"=m"(prev>thread.esp),/*%0*/
"=m"(prev>thread.eip),/*%1*/
"=a"(last),/*%2*/
"=S"(esi),"=D"(edi)
:"m"(next>thread.esp),/*%5*/
"m"(next>thread.eip),/*%6*/
"2"(prev),"d"(next));
}while(0)
The
Fork example
intvalue=5;
intmain(){
pid_tpid;
value=7;
pid=fork();
if(pid==0){/*Child*/
value+=15;
}
else{/*Parent*/
wait(NULL);/*Waitforchildtoterminate*/
printf("PARENT:value=%d\n",value);
}
}
exec()
UNIX shells
int main(int argc, char **argv)
{
while (1) {
char *cmd = get_next_command();
int child_pid = fork();
if (child_pid == 0) {
exec(cmd);
panic(exec failed!);
} else {
waitpid(child_pid);
}
}
}