Applications Address Spaces and Processes Separating Units of























![Example main(int argc, char **argv) { char *my. Name = argv[1]; int cpid = Example main(int argc, char **argv) { char *my. Name = argv[1]; int cpid =](https://slidetodoc.com/presentation_image_h2/485f90d0ba38ff45ff3c6032c5b1f0ef/image-24.jpg)



![Starting a new program main(int argc, char **argv) { char *my. Name = argv[1]; Starting a new program main(int argc, char **argv) { char *my. Name = argv[1];](https://slidetodoc.com/presentation_image_h2/485f90d0ba38ff45ff3c6032c5b1f0ef/image-28.jpg)

- Slides: 29
Applications, Address Spaces, and Processes Separating Units of Computation CSE-451 Processes 1
Understanding Processes • A Computer System has lots of nasty details in it. – CPU, Memory, Stack, Address space, Privileged execution, Exceptions, System calls, I/O Control & Interrupts • A PROCESS is the operating system’s ABSTRACTION for all of this junk. • Programs execute using the PROCESS abstraction. • Basis for – – memory management sharing & isolation concurrency scheduling 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 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 CSE-451 Processes 4
OS/User Protection Address Space 0 x 0000 main() { int fd = open(“/tmp/foo”); close(fd); } /* Syscall Dispatcher */ // determine requested routine // transfer control to requested routine // return result The Operating System CSE-451 Processes User Program Syscall dispatch 0 x 7 fffffff 0 x 80000000 File system VM system 0 xffff 5
Privileged Memory Protection Address high bits low bits Mode bit To memory Protection Fault C = AB CSE-451 Processes 6
Inside the User Program _open: load $v 0, #Syscall. Open syscall cmp $v 0, 0 jne Error move $a 0, $v 0 ret The code we wrote Error: …. Some code we didn’t write syscall_ent: Syscall dispatch Error: . . . CSE-451 Processes cmp $v 0, #0 // check if good system call jlt Error cmp $v 0, #Max. Sys. Call jgt Error jsr Save. All. Non. Link. Registers load $v 0, $v 0(Syscall. Table) // if so, get api entry point jsr $v 0 // go there move $v 0, $a 0 // result in $a 0 load $v 0, #0 Restore. All. Non. Link. Registers 7 retsys
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? 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 CSE-451 Processes 9
The Stack • Push down record of execution state maintained as a series of frames typedef struct Stack. Frame { int registers[NUM_REGS]; int pc; Stack. Frame *prev; }; CSE-451 Processes 10
The Stack • Push down record of execution state r 1 r 2 . . . 100 P 1 23 45 200 P 2 11 0 300 P 3 15 1 Program Registers CSE-451 Processes sp 600 616 620 23, 45, 100, - Older 11, 0, 200, 600 15, 1, 300, 616 Newer Stack (array of words) 11
Understanding the Stack Old stuff SP User stack main int fd main+12 0 x 40040 New stuff stack at syscall stack at entry to open SP SP CSE-451 Processes USP=0 x 40040 UPC=_open+12 0 x 83000000 Kernel stack $a 0 $a 1 $a 2… syscall_ent+24 12
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 CSE-451 Processes 13
Multiple Address Spaces • Nearly all operating systems support the abstraction of multiple address spaces 0 x 00000000 Emacs 0 x 7 fffffff 0 x 0000 CC 0 x 7 fffffff Mail User mode 0 x 7 fffffff 0 x 80000000 Kernel mode 0 xffff CSE-451 Processes 14
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) CSE-451 Processes 15
A Process is a Program in Execution Source Code File Process In Memory static int z = 5; main(int argc, char **argv) { int x = foo(); printf(“%dn”, x); } int foo() { return z=23; } start PC 0 x 0000 thread 1 st instruction z=5 cc text static data stack header: “size, start PC” Executable File (Program) 0 x 7 fffffff heap text Create Process a. out CSE-451 Processes 16
The Thread Of Control static int z = 5; main(int argc, char **argv) { int x = foo(); printf(“%dn”, x); } int foo() { return z=23; } argc argv _exit 23 23 “%dn” main+16 CSE-451 Processes argc, argv are on the stack call main call foo argc set z to 23 argv _exit return 23 main+4 set x to 23 23 push x push “%dn” call printf stack return Thread 17
Where do Processes Come From? • 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: – Create. Address. Space, Write. Address. Space, Create. Thread. In. Address. Space, Start. Thread • Others (eg, UNIX) are more subtle, but quite elegant CSE-451 Processes 18
Process Creation: The Directed Approach new address space 1. h = Create. Address. Space() Operating System CSE-451 Processes 19
Process Creation: The Directed Approach (2) new address space 1. h = Create. Address. Space(); 2. Write. Address. Space(h, program. Image); Operating System CSE-451 Processes 20
Process Creation: The Directed Approach (3) new address space 1. h = Create. Address. Space() 2. Write. Address. Space(h, program. Image) 3. Start. Thread. In. Address. Space(h, 0); Operating System Process. Table CSE-451 Processes 21
Tying it Together with a User Level Routine spawn(char *program. Name); { Address. Space a; char *program. Image; int fd = open(program. Name); program. Image = (char*)malloc(filesize(fd)); read(fd, program. Image, filesize(fd)); a = Create. Address. Space(); Write. Address. Space(a, program. Image, filesize(fd)); Start. Thread. In. Address. Space(a, 0); } What’s the significance of this being a user level routine? CSE-451 Processes 22
Processes Under UNIX • In Unix, the fork() system call is the only way to create a new process – attractive when new and old process share a lot (the original Unix model) • 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! CSE-451 Processes 23
Example main(int argc, char **argv) { char *my. Name = argv[1]; int cpid = fork(); // cpid is a handle. if (cpid == 0) { printf(“The child of %s is %dn”, my. Name, getpid()); exit(0); } else { printf(“My child is %dn”, cpid); exit(0); } } What does this program print? CSE-451 Processes 24
Bizarre But Real lace: tmp<15> cc a. c lace: tmp<16>. /a. out foobar The child of foobar is 23874 My child is 23874 Parent Child fork() retsys v 0=23874 v 0=0 Operating System CSE-451 Processes 25
Even More Bizarre lace: tmp<15> cc a. c lace: tmp<16>. /a. out foobar The child of foobar is 23874 My child is 23874 lace: tmp<17>. /a. out foobar My child is 24266 The child of foobar is 24266 lace: tmp<18> Parent Child fork() retsys Why do we get a different answer? ? CSE-451 Processes v 0=24266 v 0=0 Operating System 26
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 *program. Name) completes the picture – throws away the contents of the calling address space – replaces it with the program named by program. Name – starts executing at header. start. PC CSE-451 Processes 27
Starting a new program main(int argc, char **argv) { char *my. Name = argv[1]; char *prog. Name = argv[2]; int cpid = fork(); if (cpid == 0) { printf(“The child of %s is %dn”, my. Name, getpid()); execl(prog. Name, // executable name prog. Name, 0); // null terminated argv printf(“OH NO. THEY LIED TO ME!!!n”); } else { printf(“My child is %dn”, cpid); exit(0); } } CSE-451 Processes 28
HW 1 • 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 CSE 451 Shell% /bin/cat /etc/motd DEC OSF/1 V 3. 2 (Rev. 214); Thu Feb 22 08: 40 PST 1996 DEC OSF/1 V 3. 2 Worksystem Software (Rev. 214) This is an AFS fileserver. Please run long running jobs (hours) or memory intensive jobs elsewhere. CSE 451 Shell% /bin/date Sun Apr 5 22: 51: 50 PDT 1998 CSE-451 Processes 29