Sie sind auf Seite 1von 15

Multiprogramming

CSE451
Andrew Whitaker

Overview
Multiprogramming:

Running multiple programs

at the same time

Requires multiplexing (sharing) the CPU

Firefox

Word

javac

Firefox

Word

time
Transfer

of control is called a context switch

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

How Do Processes Share the CPU?


The

OS maintains a per-process Process


Control Block (PCB)
Which

On

stores state for non-running processes

a context switch

Save

state of the old process


Restore state of the new process
Old PCB
CPU

New PCB

Whats in the PCB?


The

In

PCB is a data structure with many fields:

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

OS maintains a set of queues that


represent the state of processes in the
system
e.g., ready queue: all runnable processes
e.g., wait queue: processes blocked on some
condition

As

a process changes state, its PCB is


unlinked from one queue, and linked onto
another

State queues
Ready queue header
head ptr
tail ptr

netscape pcb

emacs pcb

cat pcb

netscape pcb

ls pcb

Wait queue header


head ptr
tail ptr

There

may be many wait queues, one for each type of


wait (particular device, timer, message, )

Walking Through a Context Switch

Process A enters the kernel

The kernel scheduler is invoked:

Is it time to context switch?


If so, which is the next process to run?

Assembly routine exchanges hardware state

Due to a system call, interrupt, or exception

Save process As state to its PCB


Load process Bs state from its PCB

(Process B now running)


OS returns control to user mode

The Guts of Context Switching (x86)


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.

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)

UNIX Process API


How

do user programs interact with


processes?
Fork:

create a new process


Exec: run a program
Kill: destroy a process
Wait: wait for a process to exit

UNIX process creation


Via

the fork() system call


Fork essentially clones the parent process

Child receives identical (but separate) address space


Child inherits open files from its parent

The

fork() system call returns twice

Returns the childs PID to the parent


Returns 0 to the child

Fork example
intvalue=5;
intmain(){
pid_tpid;
value=7;

What value is printed to


the screen?

pid=fork();
if(pid==0){/*Child*/
value+=15;
}
else{/*Parent*/
wait(NULL);/*Waitforchildtoterminate*/
printf("PARENT:value=%d\n",value);
}
}

Exec vs. fork


So

how do we start a new program, instead of


just forking the old program?

the exec() system call!


int exec(char *prog, char ** argv)

exec()

stops the current process


loads program prog into the address space
initializes hardware context, args for new program
places PCB onto ready queue
note: does not create a new process!

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);
}
}
}

Das könnte Ihnen auch gefallen