Chapter 3: Processes
Themes (so far) Computers solve problems Automate computation, scientific models OS evolved to solve problems Common I/O, shared HW, multitasking, protection Implementations vary, but concepts persist
Process Program in execution Instance of a running program Entity scheduled for CPU time Combination of code, state, and resources
How is this represented?
Process creation New batch job Interactive logon Provide OS service Spawning
Process termination Normal completion Time limit exceeded Memory unavailable Bounds violation Protection error Arithmetic error Time overrun I/O failure Invalid instruction Privileged instruction Data misuse Intervention Parent termination Parent request
New Admit Ready Dispatch Running Release Exit Timeout Event Occurs Event Wait Blocked Figure 3.6 Five-State Process Model
0xffffffff 0xc0000000 kernel 0xffffffff 0xc0000000 kernel 0xffffffff 0xc0000000 kernel code+data code+data code+data 0x00000000 0x00000000 0x00000000
0xffffffff 0xc0000000 kernel 0xffffffff 0xc0000000 kernel 0xffffffff 0xc0000000 kernel code+data code+data code+data 0x00000000 0x00000000 0x00000000 ready kernel blocked
0xffffffff 0xc0000000 kernel 0xffffffff 0xc0000000 kernel 0xffffffff 0xc0000000 kernel code+data code+data code+data 0x00000000 0x00000000 0x00000000 ready kernel blocked
0xffffffff 0xc0000000 kernel 0xffffffff 0xc0000000 kernel 0xffffffff 0xc0000000 kernel code+data code+data code+data 0x00000000 0x00000000 0x00000000 ready kernel blocked
Process A Process B Process C Dispatcher 0 5 10 15 20 25 30 35 40 45 50 = Running = Ready = Blocked Figure 3.7 Process States for Trace of Figure 3.4
ready ready blocked new blocked ready
ready ready new blocked blocked ready
ready ready ready blocked blocked ready
ready ready ready blocked suspended ready
Suspended process Not immediately available for execution May or may not be waiting Suspended by an agent Agent must explicitly un-suspend
Process representation Code Data Associated resources
Code: (program.c) #include <stdio.h> #include <fcntl.h> int main() { int f; size_t s; char c[10]; f = open("program.c",o_rdonly); printf ("f = %d\n",f); } s = read(f, c, 9); c[9] = '\0'; printf("c is '%s'\n",c); close(f); return 0;
Code: (program.c) #include <stdio.h> #include <fcntl.h> Output: f = 3 c is #include int main() { int f; size_t s; char c[10]; f = open("program.c",o_rdonly); printf ("f = %d\n",f); } s = read(f, c, 9); c[9] = '\0'; printf("c is '%s'\n",c); close(f); return 0;
Code: (program.c) #include <stdio.h> #include <fcntl.h> int main() { int f; size_t s; char c[10]; f = open("program.c",o_rdonly); printf ("f = %d\n",f); Output: f = 3 c is #include 0 = stdin 1 = stdout 2 = stderr } s = read(f, c, 9); c[9] = '\0'; printf("c is '%s'\n",c); close(f); return 0;
Process location
Process location 0xffffffff +----------------------------------+ kernel 0xc0000000 +----------------------------------+ user V grows downward grows upward ^ user heap +----------------------------------+ uninitialized data segment (BSS) +----------------------------------+ initialized data segment +----------------------------------+ code segment 0x08084000 +----------------------------------+ 0 +----------------------------------+
Process location code + data + 0xffffffff +----------------------------------+ kernel 0xc0000000 +----------------------------------+ user V grows downward grows upward ^ user heap +----------------------------------+ uninitialized data segment (BSS) +----------------------------------+ initialized data segment +----------------------------------+ code segment 0x08084000 +----------------------------------+ 0 +----------------------------------+
Process location attributes () code + data + 0xffffffff +----------------------------------+ kernel 0xc0000000 +----------------------------------+ user V grows downward grows upward ^ user heap +----------------------------------+ uninitialized data segment (BSS) +----------------------------------+ initialized data segment +----------------------------------+ code segment 0x08084000 +----------------------------------+ 0 +----------------------------------+
Process location Memory Storage 0xffffffff +----------------------------------+ kernel 0xc0000000 +----------------------------------+ user V grows downward grows upward ^ user heap +----------------------------------+ uninitialized data segment (BSS) +----------------------------------+ initialized data segment +----------------------------------+ code segment 0x08084000 +----------------------------------+ 0 +----------------------------------+
Process location Memory Storage 0xffffffff +----------------------------------+ kernel 0xc0000000 +----------------------------------+ user V grows downward grows upward ^ user heap +----------------------------------+ uninitialized data segment (BSS) +----------------------------------+ initialized data segment +----------------------------------+ code segment 0x08084000 +----------------------------------+ 0 +----------------------------------+
Process location Memory Storage 0xffffffff +----------------------------------+ kernel 0xc0000000 +----------------------------------+ user V grows downward grows upward ^ user heap +----------------------------------+ uninitialized data segment (BSS) +----------------------------------+ initialized data segment +----------------------------------+ code segment 0x08084000 +----------------------------------+ 0 +----------------------------------+
Process attributes Process identification PID, PPID, UID Processor state information User-visible registers, control/status registers (PC, etc.), pointers Process control information Scheduling (state, priority, events), pointers to other, IPC, privileges, memory mgmt., resources
proc int
int hdlr proc int
int hdlr kernel proc int irt
int hdlr kernel proc proc int irt int
int hdlr kernel int hdlr proc proc int irt int int
Process creation 1. Assign unique PID 2. Allocate space 3. Initialize 4. Set linkages 5. Create/expand data structures
Process switching When? Interrupt, trap, supervisor (system) call How? Mode switch vs. context switch
kernel kernel code code gen reg CPU ctl reg vm reg pc code+data code+data
kernel kernel code code gen reg CPU ctl reg vm reg pc code+data code+data
kernel code INT ==> Mode switch kernel code gen reg CPU ctl reg vm reg pc code+data code+data
kernel kernel code code gen reg CPU ctl reg vm reg pc code+data code+data
kernel kernel code code gen reg CPU ctl reg vm reg pc code+data code+data
kernel kernel code code gen reg CPU ctl reg vm reg pc code+data code+data
kernel kernel code code gen reg CPU ctl reg vm reg pc code+data code+data
kernel code Control registers PC Kernel pointer User pointer PSW (x86 EFLAGS) CR0, CR1, CR2, CR3 (for VM) kernel code gen reg CPU ctl reg vm reg pc code+data code+data
Context switch 1. Save CPU context 2. Update of running process 3. Move to queue 4. Select next process 5. Update of new process 6. Update memory management 7. Restore context of selected process