Multiprogramming Overview l Multiprogramming Running multiple programs at















- Slides: 15

Multiprogramming

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

The Process l The process is an OS abstraction for a running program kernel space stack (dynamic allocated mem) SP l Process is associated with an address space l Preview: Thread is a running program without its own address space heap (dynamic allocated mem) static data (data segment) code (text segment) PC

How Do Processes Share the CPU? l The OS maintains a per-process Process Control Block (PCB) ¡Which stores state for non-running processes l On a context switch ¡Save state of the old process ¡Restore state of the new process Old PCB CPU New PCB

What’s in the PCB? l 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 l In linux: ¡ defined in task_struct (include/linux/sched. h) ¡ over 95 fields!!!

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

State queues l 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 l As a process changes state, its PCB is unlinked from one queue, and linked onto another

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

Walking Through a Context Switch l Process A enters the kernel ¡ Due to a system call, interrupt, or exception l The kernel scheduler is invoked: ¡ Is it time to context switch? ¡ If so, which is the next process to run? l Assembly routine exchanges hardware state ¡ Save process A’s state to its PCB ¡ Load process B’s state from its PCB l (Process B now running) l OS returns control to user mode

The Guts of Context Switching (x 86) 1. � define 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. switch_to(prev, next, last) do { unsigned long esi, edi; asm volatile("pushl %%ebpnt" "movl %%esp, %0nt /* save stackptr */ "movl %5, %%espnt /* restore stackptr */ "movl $1 f, %1nt" /* save instr_ptr */ "pushl %6nt" /* restore instr_ptr */ "jmp __switch_ton” /* Return to C */ "1: t" /* 1: is $1 f*/ "popl %%ebpnt" : "=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)

UNIX Process API l 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

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

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

Exec vs. fork l 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) l 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!

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); } } }