230 likes | 403 Views
PROCESSES. Concurrency and Process. What is concurrency? Process concurrency Decompose complex problems into simple ones The result: pseudoparallelism How do you check what processes are currently running at the computer?. Process Parallelism. Virtualization I/O parallelism
E N D
Concurrency and Process • What is concurrency? • Process concurrency • Decompose complex problems into simple ones • The result: pseudoparallelism • How do you check what processes are currently running at the computer?
Process Parallelism • Virtualization • I/O parallelism • CPU parallelism
More on Process Parallelism • Process parallelism is common in real life • Each sales person sell $1M annually • Hire 100 sales people to generate $100M • Speedup • Ideal speedup is ? • Reality ? • Question: • Can you speed up by working with a partner? • Can you speed up by working with 20 partners? • Can you get super-linear (factor of N or more) speedup?
Process • What is a process?
Simplest Process • Sequential execution • Process state • Registers • Main memory • I/O devices • File system • Communication ports
Program • What is a program? • What does a program consists of? • A process is an executing program
Process vs. Program • Process > program • Process < program
Process Control Block (PCB) • Process management info • State • Ready: ready to run • Running: currently running • Blocked: waiting for resources (e.g., I/O) • Registers (including PC), CPU accounting info, etc. • Parent, etc. • Memory management info • Segments, page table, stats, etc. • I/O and file management • Communication ports, directories, file descriptors, etc. • How OS takes care of processes • Resource allocation and process state transition
Process Data Structure • OS represents a process using a PCB • Has all the details about the process
Primitives of Processes • Creation and termination • Signals • Operations • Synchronization
When is a Process Created? • At system initialization: • When OS is booted daemon processes • A running process might create a child process • A user request to create a new process • In user-interactive systems • Initiation of a batch system
Make a Process • Creation • Load code and data into memory • Create an empty call stack • Initialize state to same as after a process switch • Make the process ready to run • Clone • Stop current process and save state • Make copy of current code, data, stack and OS state • Make the process ready to run
Example: Unix • How to make processes: • Fork clones a process • Exec overlays the current process if ((pid = fork()) == 0) { /* child process */ exec(“foo”); /* does not return */ else /* parent */ wait(pid); /* wait for child to die */
Shared Memory: for Efficiency • fork() actually shares memory between parent, child! • But if a page is modified, by either, fork duplicates it • Called “copy on write” sharing • Also shares open files, pipes, even stack and registers • In fact, duplicates everything except the fork() return value • If you call exec() this page-level sharing ends • Unix (Linux) and Windows also provide system calls to let processes share memory by “mapping” a file into memory • You tell it where, or let it pick an address range • Mapped files are limited to one writer. Can have many readers
Fork() Creates Parallelism • Initially, child is a clone of parent except for “pid” • Even share file descriptors for files parent had open! • Linux: includes stdin, stdout, stderr • Plus files the parent explicitly opened. • Confusing: these shared files have a single “seek pointer”. If parent and child both do I/O, they “contend” for access.
Process Termination • Exit: normal exit. What does it mean? • Abort: forced exit. Why?
Process Context Switch • For a running process • All registers are loaded in CPU and modified • E.g. • When process relinquishes the CPU, the OS • Saves register values to the PCB of that process • To execute another process, the OS • Loads register values from PCB of that process • Context Switch • Process of switching CPU from one process to another • Very machine dependent for types of registers
Details of Context Switching • Very tricky to implement • OS must save state without changing state • Should run without touching any registers • CISC: single instruction saves all state • RISC: reserve registers for kernel • Or way to save a register and then continue • Overheads: CPU is idle during a context switch • Explicit: • Direct cost of loading/storing registers to/from main memory • Implicit: • Opportunity cost of flushing useful caches (cache, TLB, etc.) • Wait for pipeline to drain in pipelined processors
Context Switching is Costly! • In systems that do excessive amounts of context switching, it balloons into a big overhead • This is often ignored by application developers • But if you split an application into multiple processes need to keep it in mind • Make sure that each process does big chunks of work • Think about conditions under which context switching could occur and make sure they are reasonably rare