Top Banner
Forking & Process Scheduling Vivek Pai / Kai Li Princeton University
24

Forking & Process Scheduling Vivek Pai / Kai Li Princeton University.

Jan 15, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Forking & Process Scheduling Vivek Pai / Kai Li Princeton University.

Forking & Process Scheduling

Vivek Pai / Kai Li

Princeton University

Page 2: Forking & Process Scheduling Vivek Pai / Kai Li Princeton University.

2

Mechanics

Exams not graded– Hopefully, this week

Filesystem project– Extension + bonus?– This wasn’t supposed to happen

Today: forking + scheduling

Page 3: Forking & Process Scheduling Vivek Pai / Kai Li Princeton University.

3

A Quick Review

What have we covered– How to store data via files– How to virtualize memory

• Every process gets uniform address space

• Lots of tricks can be played to share memory

What’s left– How to share/virtualize the processor– Having processes communicate/cooperate

Page 4: Forking & Process Scheduling Vivek Pai / Kai Li Princeton University.

4

How To Launch a New Process?

Obvious choice: “start process” system callBut not all processes start the same

– “testprogram” versus “testprogram >& outfile” versus “testprogram arg1 arg2 >& outfile”

The “parent” process wants to specify various aspects of the child’s “environment”– Next step: add more parameters to specify

environment

Page 5: Forking & Process Scheduling Vivek Pai / Kai Li Princeton University.

5

Can We Generalize?

What happens as more information gets added to the process’s “environment” – more parameters? New system calls? This gets ugly

What’s the most general way of setting up all of the environment?

So, why not allow process setup at any point?– This is the exec( ) system call (and its variants)

Page 6: Forking & Process Scheduling Vivek Pai / Kai Li Princeton University.

6

But We Want a Parent and a Child

The exec call “destroys” the current processSo, instead, destroy a copy of the process

– The fork( ) call duplicates the current process

– Better yet, don’t tightly couple fork and exec• This way, you can customize the child’s environment

So what does fork( ) entail?– Making a copy of everything about the process

– Ouch!

Page 7: Forking & Process Scheduling Vivek Pai / Kai Li Princeton University.

7

What Gets Copied

So far, we’ve covered the following:– VM system

– File system

– Signals

How do we go about copying this information?What parts are easy to copy, and what’s hard?What’s the common case with fork/exec?

– What needs to get preserved in this scenario?

Page 8: Forking & Process Scheduling Vivek Pai / Kai Li Princeton University.

8

Shared Memory

How to destroy a virtual address space?– Link all PTEs

– Reference count

How to swap out/in?– Link all PTEs

– Operation on all entries

How to pin/unpin?– Link all PTEs

– Reference count

.

.

.

.

.

.

...

.

.

.

Process 1

Process 2

w

...

w

Page table

Page table

Physicalpages

Page 9: Forking & Process Scheduling Vivek Pai / Kai Li Princeton University.

9

.

.

.

.

.

.

...

.

.

.

Copy-On-Write

Child’s virtual address space uses the same page mapping as parent’s

Make all pages read-only Make child process ready On a read, nothing

happens On a write, generates an

access fault– map to a new page frame

– copy the page over

– restart the instruction

Parent process

Child process

rr

...

rr

Page table

Page table

Physicalpages

Page 10: Forking & Process Scheduling Vivek Pai / Kai Li Princeton University.

10

Issues of Copy-On-Write

How to destroy an address space– Same as shared memory case?

How to swap in/out?– Same as shared memory

How to pin/unpin– Same as shared memory

Page 11: Forking & Process Scheduling Vivek Pai / Kai Li Princeton University.

11

Process Creation & Termination

Four primitives:Fork – create a copy of this processExec – replace this process with this programWait – wait for child process to finishKill – (potentially) end a running process

Processes form a tree – what happens when parent disappears?

Page 12: Forking & Process Scheduling Vivek Pai / Kai Li Princeton University.

12

Signals

Asynchronous event delivery mechanism

Examples – FPE, segv, ctrl-c, hang up, resume

Default actions – ignore, abort, core dump

Handler – program-specified routine for signal

Page 13: Forking & Process Scheduling Vivek Pai / Kai Li Princeton University.

13

A Signaling Sidebar

What’s wrong with this program:int randVal;void SigHand(void){

printf(“your rand val is %d\n”, randVal);}int main(int argc, char *argv[]){

set up ctrl-c handler;while (1) {

randVal = 0;randVal = 1;…randVal = 9;

}}

Page 14: Forking & Process Scheduling Vivek Pai / Kai Li Princeton University.

14

Scheduling Primitives

Block – wait on some event/resource– Network packet arrival– Keyboard, mouse input– Disk activity completion

Yield – give up running for now– Directed (let my friend run)– Undirected (let any process run)

Synchronization– We will talk about this later

Page 15: Forking & Process Scheduling Vivek Pai / Kai Li Princeton University.

15

Our Friend, The Transition Diagram

Running

BlockedReady

Sche

duler

disp

atch W

ait for

resource

Resource becomesavailable

Createa process

terminate

Page 16: Forking & Process Scheduling Vivek Pai / Kai Li Princeton University.

16

Process State Transition ofNon-Preemptive Scheduling

Running

BlockedReady

Resource becomes available(move to ready queue)

Createa process

Terminate(call scheduler)

Yield(call scheduler)

Block for resource(call scheduler)

Schedulerdispatch

Page 17: Forking & Process Scheduling Vivek Pai / Kai Li Princeton University.

17

Who’s Happy Right Now

Page 18: Forking & Process Scheduling Vivek Pai / Kai Li Princeton University.

Scheduler

A non-preemptive scheduler invoked by explicit block or yield calls

The simplest form

Scheduler:

save current process state (into PCB)

choose next process to run

dispatch (load PCB and run) Does this work?

Page 19: Forking & Process Scheduling Vivek Pai / Kai Li Princeton University.

More on Scheduler

Should the scheduler use a special stack?– Yes, because a user process can overflow and it would

require another stack to deal with stack overflow

Should the scheduler simply be a kernel process?– You can view it that way because it has a stack, code

and its data structure

– This process always runs when there is no user process

Page 20: Forking & Process Scheduling Vivek Pai / Kai Li Princeton University.

20

Where Should PCB Be Saved?

Save the PCB on its user stack– Many processors have a special instruction to do it

“efficiently”

– But, need to deal with the overflow problem

– When the process terminates, the PCB vanishes

Save the PCB on the kernel heap data structure– May not be as efficient as saving it on stack

– But, it is very flexible and no other problems

Page 21: Forking & Process Scheduling Vivek Pai / Kai Li Princeton University.

21

Physical Memory & Multiprogramming

Memory is a scarce resourceWant to run many programsPrograms need memory to run

What happens whenM(a) + M(b) + M(c) > physical mem?

Answer: paging. But what if no paging?

Page 22: Forking & Process Scheduling Vivek Pai / Kai Li Princeton University.

22

Job Swapping

Partially executedswapped-out processes

Ready Queue CPU

I/O Waitingqueues

I/O

Terminate

Swap outSwap in

Page 23: Forking & Process Scheduling Vivek Pai / Kai Li Princeton University.

23

Add Job Swapping toState Transition Diagram

Running

BlockedReady

Resource becomes available(move to ready queue)

Createa process

Terminate(call scheduler)

Yield(call scheduler)

Block for resource(call scheduler)

Schedulerdispatch

Swap out

Swap in

Swap

Page 24: Forking & Process Scheduling Vivek Pai / Kai Li Princeton University.

24

Think About Swapping

Is swappingNecessaryDesirableGood Ideal

Things to considerPerformanceComplexityEfficiency

Moreover, what decides swapping versus paging?Is each appropriate somewhere?