22
CSE-451 Processes 1 Applications, Address Spaces, and Processes Separating Units of Computation

Applications, Address Spaces, and Processes

  • Upload
    norris

  • View
    31

  • Download
    0

Embed Size (px)

DESCRIPTION

Applications, Address Spaces, and Processes. Separating Units of Computation. Definitions. User mode when the system is executing with the privileged bit off Kernel mode when the system is executing with the privileged bit on Address space - PowerPoint PPT Presentation

Citation preview

Page 1: Applications, Address Spaces, and Processes

CSE-451 Processes 1

Applications, Address Spaces, and Processes

Separating Units of Computation

Page 2: Applications, Address Spaces, and Processes

CSE-451 Processes 2

Definitions

• User mode– when the system is executing with the privileged bit off

• Kernel mode– when the system is executing with the privileged bit on

• Address space– the range of addresses available for a program to use

• Legal address space– the range of addresses that a program can use right now

Page 3: Applications, Address Spaces, and Processes

CSE-451 Processes 3

User and Kernel Memory

• When the mode bit is set to PRIVILEGED, the kernel can see all of memory– user program, arguments, etc

– User memory is like a big data structure for the kernel

• But, when the mode bit is off, the user program can only see its own memory– the kernel’s address space is OFF LIMITS

– what happens if the user tries?

• Good for the OS, and good for the user program

Page 4: Applications, Address Spaces, and Processes

CSE-451 Processes 4

OS/User Protection

User Program

The OperatingSystem

0x00000000

0x7fffffff

0xffffffff

main() { int fd = open(“/tmp/foo”); close(fd);}

Syscall dispatch

File system

VM system

/* Syscall Dispatcher */// determine requested routine// transfer control to requested routine// return result

Address Space

0x80000000

Page 5: Applications, Address Spaces, and Processes

CSE-451 Processes 5

Privileged Memory Protection

ModeA

High BitB

Fault?C

0 0 0

0 1 1

1 0 0

1 1 0

Address

low bitshigh bits

Mode bit

Protection Fault

Tomemory

C = AB

Page 6: Applications, Address Spaces, and Processes

CSE-451 Processes 6

Inside the User ProgramUser Program

The codewe wrote

Some codewe didn’t write

_open:load $v0, #SyscallOpensyscallcmp $v0, 0jne Errormove $a0, $v0ret

Error:….

Syscall dispatch

syscall_ent:cmp $v0, #0 // check if good system calljlt Errorcmp $v0, #MaxSysCalljgt Errorjsr SaveAllNonLinkRegistersload $v0, $v0(SyscallTable) // if so, get api entry pointjsr $v0 // go theremove $v0, $a0 // result in $a0load $v0, #0RestoreAllNonLinkRegistersretsys

Error:...

Page 7: Applications, Address Spaces, and Processes

CSE-451 Processes 7

What Happens on Syscall?

• Automatic– Hardware MODE bit flips (go from nonpriv to

priv)– Minimal save and restore of context

• SP <- Kernel Syscall SP

• PC <- Kernel Syscall PC

• *SP++ <- User SP

• *SP++ <- User PC

– What happens on retsys?

Page 8: Applications, Address Spaces, and Processes

CSE-451 Processes 8

And then we pick it up...

• Sycall handler checks to make sure we’re asking for a good service

• Control is transferred to the service

• Result is passed back

Page 9: Applications, Address Spaces, and Processes

CSE-451 Processes 9

Understanding the StackUser stack

mainint fdmain+12SP

Kernel stack

0x40040

USP=0x40040UPC=_open+12

SPstack at syscall$a0$a1$a2…syscall_ent+24SPstack at entry to open

New stuff

Old stuff

0x83000000

Page 10: Applications, Address Spaces, and Processes

CSE-451 Processes 10

Concepts So Far

• User programs operate out of a different portion of the address space than the kernel

• There is a context switch that occurs every time we enter the kernel

• Inside the kernel we have expanded privileges

• A combination of hardware and software is responsible for this behavior

Page 11: Applications, Address Spaces, and Processes

CSE-451 Processes 11

Multiple Address Spaces

• Nearly all operating systems support the abstraction of multiple address spaces

Emacs Mail

Kernel mode

User mode

0x80000000

0xffffffff

CC

0x00000000

0x7fffffff

0x00000000

0x7fffffff

0x00000000

0x7fffffff

Page 12: Applications, Address Spaces, and Processes

CSE-451 Processes 12

A Process

• Each address space contains a process– a bunch of text & data– a “thread” in execution

• A thread represents the flow of control that is active inside a program– deterministic change of state prescribed by the

current state and the PC (which is actually part of the current state)

Page 13: Applications, Address Spaces, and Processes

CSE-451 Processes 13

A Process is a Program in Execution

static int z = 5;main(int argc, char **argv){ int x = foo(); printf(“%d\n”, x);}int foo(){ return z=23;}

SourceCodeFile

ExecutableFile (Program)

a.out

cc

text

header: “size, start PC”

Create Process

text

start PC

z=5 static data

heap

stack

0x00000000

0x7fffffff

Process In Memory

1st instruction

thread

Page 14: Applications, Address Spaces, and Processes

CSE-451 Processes 14

The Thread Of Control

static int z = 5;main(int argc, char **argv){ int x = foo(); printf(“%d\n”, x);}int foo(){ return z=23;}

argc, argv are on the stackcall main

call fooset z to 23return 23

set x to 23push xpush “%d\n”call printfreturn

argcargv_exit

main+423

stack

argcargv_exit2323

“%d\n”main+16 Thread

Page 15: Applications, Address Spaces, and Processes

CSE-451 Processes 15

Where do Processes Come From?

• Remember, a process is an address space with some stuff in it and a thread of control

• All operating systems have facilities for creating new processes

• Some of them (eg, NT) are quite simple:– CreateAddressSpace, WriteAddressSpace,

CreateThreadInAddressSpace, StartThread

• Others (eg, UNIX) are more subtle, but quite elegant

Page 16: Applications, Address Spaces, and Processes

CSE-451 Processes 16

Processes Under UNIX

• In Unix, the fork() system call is the only way to create a new process

• int fork() does many things at once:– creates a new address space (called the child)

– copies the parent’s address space into the child’s

– starts a new thread of control in the child’s address space

– parent and child are equivalent -- almost• in parent, fork() returns a non-zero integer

• in child, fork() returns a zero.

• difference allows parent and child to distinguish

• int fork() returns TWICE!

Page 17: Applications, Address Spaces, and Processes

CSE-451 Processes 17

Examplemain(int argc, char **argv){ char *myName = argv[1]; int cpid = fork(); if (cpid == 0) { printf(“The child of %s is %d\n”, myName, getpid()); exit(0); } else { printf(“My child is %d\n”, cpid); exit(0); }}

What does this program print?

Page 18: Applications, Address Spaces, and Processes

CSE-451 Processes 18

Bizarre But Real

lace:tmp<15> cc a.clace:tmp<16> ./a.out foobarThe child of foobar is 23874My child is 23874

Parent

Child

Operating System

fork()

retsys

v0=0v0=23874

Page 19: Applications, Address Spaces, and Processes

CSE-451 Processes 19

Even More Bizarre

lace:tmp<15> cc a.clace:tmp<16> ./a.out foobarThe child of foobar is 23874My child is 23874lace:tmp<17> ./a.out foobarMy child is 24266The child of foobar is 24266lace:tmp<18>

Parent

Child

Operating System

fork()

retsys

v0=0v0=24266Why do we get a different answer??

Page 20: Applications, Address Spaces, and Processes

CSE-451 Processes 20

Fork is half the story

• Fork() gets us a new address space, but not one that’s all that different.– parent and child share EVERYTHING

• memory, operating system state

• int exec(char *programName) completes the picture– throws away the contents of the calling address space

– replaces it with the program named by programName

– starts executing at header.startPC

Page 21: Applications, Address Spaces, and Processes

CSE-451 Processes 21

Starting a new programmain(int argc, char **argv){ char *myName = argv[1]; char *progName = argv[2];

int cpid = fork(); if (cpid == 0) { printf(“The child of %s is %d\n”, myName, getpid()); execl(progName, // executable name

progName, 0); // null terminated argv printf(“OH NO. THEY LIED TO ME!!!\n”); } else { printf(“My child is %d\n”, cpid); exit(0); }}

Page 22: Applications, Address Spaces, and Processes

CSE-451 Processes 22

Extra Credit for Friday

• Write a simple UNIX program to simulate the UNIX shell in a “read/fork/exec” loop– don’t bother with path searches. All commands can be fully qualified

CSE451Shell% /bin/cat /etc/motdDEC OSF/1 V3.2 (Rev. 214); Thu Feb 22 08:48:40 PST 1996 DEC OSF/1 V3.2 Worksystem Software (Rev. 214)

This is an AFS fileserver. Please run long running jobs (hours) or memoryintensive jobs elsewhere.

CSE451Shell% /bin/dateSun Apr 5 22:51:50 PDT 1998