1 / 18

Multiprogramming

Multiprogramming. CSE451 Andrew Whitaker. Overview. Multiprogramming: Running multiple programs “at the same time” Requires multiplexing (sharing) the CPU Transfer of control is called a context switch. Firefox. Word. javac. Firefox. Word. time. The Process.

Download Presentation

Multiprogramming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Multiprogramming CSE451 Andrew Whitaker

  2. Overview • Multiprogramming: Running multiple programs “at the same time” • Requires multiplexing (sharing) the CPU • Transfer of control is called a context switch Firefox Word javac Firefox Word time

  3. The Process • The process is the OS abstraction for a running program • Key point: the OS must make sharing transparent • Virtualized CPU (called a thread of control) • Virtualized address space (called virtual memory) Firefox Word javac OS

  4. CPU Sharing • Each process has its own set of registers • OS stores non-active process’ registers in a a Process Control Block (PCB) • On a context switch: • Save state of the old process • Restore state of the new process Old PCB CPU New PCB

  5. What’s in the PCB? • The PCB is a data structure with many fields: • process ID (PID) • execution state (Ready, Running, Blocked) • program counter, stack pointer, registers • memory management info • UNIX username of owner • scheduling priority • accounting info • In linux: • defined in task_struct (include/linux/sched.h) • over 95 fields!!!

  6. States of a process running interrupt (unschedule) dispatch ready blocking I/O interrupt (I/O complete) blocked

  7. State queues: • The OS maintains a set of queues that represent the state of processes in the system • e.g., ready queue: all runnable processes • e.g., wait queue: processes blocked on some condition • As a process changes state, its PCB is unlinked from one queue, and linked onto another

  8. State queues Ready queue header • There may be many wait queues, one for each type of wait (particular device, timer, message, …) netscape pcb emacs pcb ls pcb head ptr tail ptr Wait queue header cat pcb netscape pcb head ptr tail ptr

  9. A Sample Context Switch • Process A is running • A timer interrupt fires • The kernel scheduler is invoked: • Is it time to context switch? • If so, which is the next process to run? • An assembly routine exchanges hardware state • Save process A’s state to its PCB • Load process B’s state from its PCB • OS exits • Process B is running

  10. The Guts of Context Switching (x86) define switch_to(prev,next,last) do { unsigned long esi,edi; asm volatile("pushl %%ebp\n\t" "movl %%esp,%0\n\t /* save stackptr */ "movl %5,%%esp\n\t /* restore stackptr */ "movl $1f,%1\n\t" /* save instr_ptr */ "pushl %6\n\t" /* restore instr_ptr */ "jmp __switch_to\n” /* Return to C */ "1:\t" /* 1: is $1f*/ "popl %%ebp\n\t" :"=m" (prev->thread.esp), /* %0 */ "=m" (prev->thread.eip), /* %1 */ "=a" (last), /* %2 */ "=S" (esi),"=D" (edi) :"m" (next->thread.esp), /* %5 */ "m" (next->thread.eip), /* %6 */ "2" (prev), "d" (next)); } while (0)

  11. Address Spaces • Each process has its own address space • The OS must make memory sharing transparent • Process’s A’s heap cannot interfere with process B’s heap • The mechanism for this is virtual memory kernel space stack (dynamic allocated mem) SP heap (dynamic allocated mem) static data (data segment) code (text segment) PC

  12. Address Space Isolation Program B Program A int main () { int* x = 0x7000000; *x = 3; sleep(3000); printf(“%d\n”,*x); } int main () { int* x = 0x7000000; *x = 5; sleep(3000); printf(“%d\n”,*x); } Always prints “3” Always prints “5”

  13. Virtual Memory • System supports two layers of addressing • Each process has a virtual address space • “Real” memory resides in physical address space • OS controls these mappings Firefox Excel virtual memory physical memory

  14. UNIX Process API • How do user programs interact with processes? • Fork: create a new process • Exec: run a program • Kill: destroy a process • Wait: wait for a process to exit

  15. UNIX process creation • Via the fork() system call • Fork essentially clones the parent process • Child receives identical (but separate) address space • Child inherits open files from its parent • The fork() system call “returns twice” • Returns the child’s PID to the parent • Returns 0 to the child

  16. Fork example int value = 5; int main () { pid_t pid ; value = 7; pid = fork(); if (pid == 0) { /* Child */ value += 15; } else { /* Parent */ wait (NULL); /* Wait for child to terminate */ printf("PARENT: value = %d\n",value ); } } What value is printed to the screen?

  17. Exec vs. fork • So how do we start a new program, instead of just forking the old program? • the exec() system call! • int exec(char *prog, char ** argv) • exec() • stops the current process • loads program ‘prog’ into the address space • initializes hardware context, args for new program • places PCB onto ready queue • note: does not create a new process!

  18. UNIX shells int main(int argc, char **argv) { while (1) { char *cmd = get_next_command(); int child_pid = fork(); if (child_pid == 0) { exec(cmd); panic(“exec failed!”); } else { waitpid(child_pid); } } }

More Related