Operating Systems ECE 344 Processes Ashvin Goel ECE

  • Slides: 16
Download presentation
Operating Systems ECE 344 Processes Ashvin Goel ECE University of Toronto

Operating Systems ECE 344 Processes Ashvin Goel ECE University of Toronto

Overview q Address space q Processes q Comparing threads and processes q OS-level state

Overview q Address space q Processes q Comparing threads and processes q OS-level state for processes 2

Address Space q q The set of (virtual) memory addresses accessible to a program

Address Space q q The set of (virtual) memory addresses accessible to a program is called address space Address space is divided into several regions o Text – the program code (usually read only) Data, heap – static, dynamic variables o Stack – used for function calls o q Each running program has its own address space Program 1 Program 2 stack Address space SP data, heap PC text stack data text SP PC 3

Traditional Process q A program is a static file containing instructions + data o

Traditional Process q A program is a static file containing instructions + data o q Describes how to perform a computation When OS runs a program, it creates Address space, loads instructions and data o Thread, runs instructions, accesses data from address space o q Traditionally: process = address space + thread Address space provides memory protection o Thread enables concurrency o 4

Process-Based Communication q A program can consist of multiple processes q How can processes

Process-Based Communication q A program can consist of multiple processes q How can processes communicate with each other? Problem: they don’t share memory o Processes communicate via system calls o Generate Data Filter Data Process 1 Process 2 Library send user mode receive OS Kernel kernel mode Hardware 5

Modern Process, Thread-Based Communication q q The single-thread process model is limiting Today: process

Modern Process, Thread-Based Communication q q The single-thread process model is limiting Today: process = address space + one or more threads int A[1000]; main() { thread_create(T 1); thread_create(T 2); main_loop(); } T 1() { int v 1; A[0] = v 1; } T 2() { int v 1, v 2; v 2 = A[0]; } int v 1, v 2; int v 1; main() create(T 1); create(T 2); … int A[]; 6

How to Speedup Vector Operation For (k = 0; k < n; k++) a[k]

How to Speedup Vector Operation For (k = 0; k < n; k++) a[k] = b[k] * c[k] + d[k] * e[k]; t 1 q t 2 How would you speedup this program? Using multiple processes o Using threads in a single process o q Would there be any speedup on a single processor? q Would there be any speedup on a multiprocessor? 7

How to Speedup Web Server Program Run in loop: 1. get network message (URL)

How to Speedup Web Server Program Run in loop: 1. get network message (URL) from client 2. get URL data from disk, cache in memory 3. compose response containing webpage 4. send response q How would you speedup this program? Using multiple processes o Using threads in a single process o q Would there be any speedup on a single processor? q Would there be any speedup on a multiprocessor? 8

Threads versus Processes Threads Processes Memory needed Shared, less Not shared, more Communication &

Threads versus Processes Threads Processes Memory needed Shared, less Not shared, more Communication & Synchronization Via shared variables, faster Via system calls, slower Switching Faster Slower Robustness Memory sharing can cause hard-to-detect bugs All communication is explicit, more robust program design 9

Overview q Address space q Processes q Comparing threads and processes q OS-level state

Overview q Address space q Processes q Comparing threads and processes q OS-level state for processes 10

OS-Level State for Processes q When a program is run, OS creates process, and

OS-Level State for Processes q When a program is run, OS creates process, and maintains state for the process o Thread state (for one/more threads) for virtualizing CPU § Holds CPU registers, PC, SP, etc. , when thread is suspended § Thread id, uniquely identifies thread § Various parameters, e. g. , scheduling parameters o Address space state for virtualizing memory § Holds MMU registers, and virtual to physical mappings § Location of text, data, heap, stack regions o Device related state for virtualizing devices § Holds open files, n/w connections, etc. 11

Summary q A thread virtualizes the CPU o q An address space virtualizes memory

Summary q A thread virtualizes the CPU o q An address space virtualizes memory o q q Each address space accesses its own contiguous, private memory, providing memory protection A running program is called a process o q Multiple threads can run concurrently and in parallel Process consists of: address space + one or more threads Programming with threads versus processes OS maintains per-process state for each abstraction it provides to a process o Thread, address space, and device-related state 12

Think Time: Are They Different? q q q What is the difference between a

Think Time: Are They Different? q q q What is the difference between a program and a process? What is the difference between a thread and a process? What is the difference between an address space and a process? 13

Think Time: Threads and Processes q q We saw that the web server threads

Think Time: Threads and Processes q q We saw that the web server threads running concurrently on a single CPU help hide IO latency. Have we seen this idea elsewhere? Suppose you wanted to write a single-threaded web server (that runs on a single CPU). Can you think of a way to hide IO latency? Do you know how the Linux OS allows a program to create multiple processes? Does the OS code run in a separate process? 14

Think Time: Address Space q q We saw that the heap grows up and

Think Time: Address Space q q We saw that the heap grows up and the stack grows down in the process’s address space. What if they collide? Why do threads have their own, private stack regions? Can threads access the stack regions of other threads? Does the OS have an address space? 15

Think Time: Per-Process State q q The OS maintains device state, such as open

Think Time: Per-Process State q q The OS maintains device state, such as open files, network connections, for each process. What other device state must the OS keep? Is there any other state the OS needs to maintain for a process? Can a process modify the per-process state maintained by the OS? Does the OS need a process structure? 16