15-410, s’04 - 1 - the process jan. 21, 2004 dave eckhardt bruce maggs l05_process 15-410...

35
15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Dave Eckhardt Bruce Maggs Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

Upload: diana-jefferson

Post on 18-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

The ProcessJan. 21, 2004

Dave EckhardtDave Eckhardt

Bruce MaggsBruce Maggs

L05_Process

15-410“System call abuse for fun & profit”

Page 2: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

Synchronization

Project 0 due at midnightProject 0 due at midnight Please go through the hand-in page now

Anybody reading comp.risks?Anybody reading comp.risks?

TodayToday– Chapter 4, but not exactly!

Page 3: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

Outline

Process as pseudo-machineProcess as pseudo-machine– (that's all there is)

Process life cycleProcess life cycle

Process kernel statesProcess kernel states

Process kernel stateProcess kernel state

Page 4: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

The Computer

Stack

Program

Registers

Keyboard

Screen

Timer

Page 5: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

The Process

Stack

CodeDataHeap

Registers

stdin

stdout

timer

Page 6: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

Process life cycle

BirthBirth– (or, well, fission)

SchoolSchool

WorkWork

DeathDeath

(Nomenclature courtesy of The Godfathers)(Nomenclature courtesy of The Godfathers)

Page 7: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

Birth

Where do new processes come from?Where do new processes come from?– (Not: under a cabbage leaf, by stork, ...)

What do we need?What do we need?– Memory contents

● Text, data, stack– CPU register contents (N of them)– "I/O ports"

● File descriptors, e.g., stdin/stdout/stderr– Hidden “stuff”

● timer state, current directory, umask

Page 8: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

Birth

Intimidating?Intimidating?

How to specify all of that stuff?How to specify all of that stuff?– What is your {name,quest,favorite_color}?

Gee, we already have Gee, we already have oneone process we like... process we like...

Page 9: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

Birth – fork() - 1

MemoryMemory– Copy all of it– Maybe using VM tricks so it' s cheaper

RegistersRegisters– Copy all of them

● All but one: parent learns child's process ID, child gets 0

Page 10: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

Birth – fork() - 2

File descriptorsFile descriptors– Copy all of them– Can't copy the files!– Copy references to open-file state

Hidden stuffHidden stuff– Do whatever is "obvious"

ResultResult– Original, “parent”, process– Fully-specified “child” process, with 0 fork() parameters

Page 11: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

Now what?

Two copies of the same process is Two copies of the same process is boringboring

Transplant surgery!Transplant surgery!– Implant new memory!

● New program text– Implant new registers!

● Old ones don't point well into the new memory– Keep (most) file descriptors

● Good for cooperation/delegation– Hidden state?

● Do what's “obvious”

Page 12: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

Original Process

Stack

/bin/shDataHeap

Registers

stdin

stdout

timer t=4

Page 13: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

Toss Heap, Data

Stack

/bin/sh

Registers

stdin

stdout

timer t=4

Page 14: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

Load New Code, Data From File

Stack

/u/b/gccData

Registers

stdin

stdout

timer t=4

Page 15: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

Reset Stack, Heap

Stack

/u/b/gccData

Registers

stdin

stdout

timer t=4[Heap]

Page 16: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

Fix “Stuff”

Stack

/u/b/gccData

Registers

stdin

stdout

timer off[Heap]

Page 17: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

Initialize Registers

Stack

/u/b/gccData

Registers

stdin

stdout

timer off[Heap]

Page 18: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

Begin Execution

Stack

/u/b/gccData

Registers

stdin

stdout

timer offHeap

Page 19: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

What's This Procedure Called?

int execve( char *path, char *argv[ ], char *envp[ ])

Page 20: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

Birth - other ways

There is another wayThere is another way– Well, two

spawn()spawn()– Carefully specify all features of new process– Don't need to copy stuff you will immediately toss

Plan 9 rfork() / Linux clone()Plan 9 rfork() / Linux clone()– Build new process from old one– Specify which things get shared vs. copied

Page 21: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

School

Old process calledOld process called

execve(char *path,char *argv[ ],char

*envp[ ]);

Result isResult ischar **environ;main(int argc, char *argv[ ]){ ...}

Page 22: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

School

How does the magic work?How does the magic work?– 15-410 motto: No magic

Kernel process setup: we saw...Kernel process setup: we saw...– Toss old data memory– Toss old stack memory– Load executable file

Also...Also...

Page 23: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

The Stack!

Kernel builds stack for new processKernel builds stack for new process– Transfers argv[] and envp[] to top of new process stack– Hand-crafts stack frame for __main()– Sets registers

● Stack pointer (to top frame)● Program counter (to start of __main())

Page 24: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

Work

Process statesProcess states– Running

● User mode● Kernel mode

– Runnable● User mode● Kernel mode

– Sleeping● In condition_wait(), more or less

Page 25: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

Work

Other process statesOther process states– Forking– Zombie

““Exercise for the reader”Exercise for the reader”– Draw the state transition diagram

Page 26: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

Death

VoluntaryVoluntary

void exit(int reason);

Software exceptionSoftware exception– SIGXCPU – used "too much" CPU time

Hardware exceptionHardware exception– SIGSEGV - no memory there for you!

Page 27: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

Death

kill(pid, sig);kill(pid, sig);

^C kill(getpid(), SIGINT);

Start loggingStart logging

kill(daemon_pid, SIGUSR1);% kill -USR1 33

Lost in SpaceLost in Spacekill(Will_Robinson, SIGDANGER);

– I apologize to IBM for lampooning their serious signal● No, I apologize for that apology...

Page 28: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

Process cleanup

Resource releaseResource release– Open files: close()

● TCP: 2 minutes (or more)● Solaris disk offline - forever (“None shall pass!”)

– Memory: release

AccountingAccounting– Record resource usage in a magic file

Gone?Gone?

Page 29: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

“All You Zombies...”

Zombie processZombie process– Process state reduced to exit code– Wait around until parent calls wait()

● Copy exit code to parent memory● Delete PCB

Page 30: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

Kernel process state

The dreaded "PCB"The dreaded "PCB"– (polychlorinated biphenol?)

Process Control BlockProcess Control Block– “Everything without a memory address”

● Kernel management information● Scheduler state● The “stuff”

Page 31: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

Sample PCB contents

CPU register save areaCPU register save area

Process number, parent process numberProcess number, parent process number

Countdown timer valueCountdown timer value

Memory segment infoMemory segment info– User memory segment list– Kernel stack reference

Scheduler infoScheduler info– linked list slot, priority, “sleep channel”

Page 32: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

Conceptual Memory Layout

Stack

Program

k-stackk-stack

k-stackk-stack

Kernel Data

Kernel Program

Stack

Program

Stack

Program

Stack

Program

Page 33: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

Physical Memory Layout

Kernel Memory

User Memory

16 MB

256 MB

Page 34: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

Ready to Implement All This?

Not so complicated...Not so complicated...– getpid()– fork()– exec()– wait()– exit()

What could possibly go wrong?What could possibly go wrong?

Page 35: 15-410, S’04 - 1 - The Process Jan. 21, 2004 Dave Eckhardt Bruce Maggs L05_Process 15-410 “System call abuse for fun & profit”

15-410, S’04- 1 -

Summary

Parts of a ProcessParts of a Process Virtual – Memory regions, registers, I/O “ports” Physical – Memory pages, registers, I/O devices

Birth, School, Work, DeathBirth, School, Work, Death

““Big Picture” of memory – both of themBig Picture” of memory – both of them (Numbers & arrangement are 15-410–specific) (Numbers & arrangement are 15-410–specific)