linux system : lecture 7 process bong-soo sohn lecture notes acknowledgement : the design of unix...
TRANSCRIPT
LINUX System : Lecture 7Process
Bong-Soo Sohn
Lecture notes acknowledgement : The design of UNIX Operating System
2
Process Management
What is Process?• Definition
– an instance of a running program (runnable program)– an execution environment of a program– scheduling entity– a control flow and address space– PCB (Process Control Block) : proc. table and U area
• Manipulation of Process– create, destroy – context– state transition
• dispatch (context switch)• sleep, wakeup• swap
Process State Transition user running
kernel running
zombie initial (idle)
fork
ready to run
suspended ready
asleep
suspended asleep
return fromsyscall orinterrupt
syscall,interrupt
swtch sleep, lock
wakeup, unlock
exit wait
swap swap
swtch
(Source : UNIX Internals)
Context
proc table
U area
segment tablepage tablememory
disk
fdfile table
Registers (TSS)eip sp
eflagseax
cs
• context : system context, address (memory) context, H/W context
….
….
swap
Context : system context• System context– proc. Table
• identification: pid, process group id, …• family relation• state• sleep channel: sleep queue• scheduling information : p_cpu, p_pri, p_nice, ..• signal handling information• address (memory) information
– U area (swappable information)• stores hardware context when the process is not running currently• UID, GID• arguments, return values, and error status for system call• signal catch function• file descriptor• usage statistics
Context : address context• fork example
guess what can we get from this program?
int glob = 6;char buf[] = “a write to stdout\n”;
int main(void){ int var;
pid_t pid;
var = 88;write(STDOUT_FILENO, buf, sizeof(buf)-1);printf(“before fork\n”);
if ((pid = fork()) == 0) { /* child */glob++; var++;
} elsesleep(2); /* parent */
printf(“pid = %d, glob = %d, var = %d\n”, getpid(), glob, var);
exit (0);} (Source : Adv. programming in the UNIX Env., pgm 8.1)
Context : address context
• fork internal : compile results
test.c
gcc
header
text
data
bss
stack
user’s perspective (virtual address)
…movl %eax, [glob]addl %eax, 1movl [glob], %eax...
glob, buf
var, pid
textdata
stack
kernel0xffffffff
0xbfffffff
0x0
a.outExecutable and Linking Format
Context : address context• fork internal : before fork (after run
a.out)
cf) we assume that there is no paging mechanism in this figure.
memory
text
stack
data
segment T.proc T.
pid = 11
glob, buf
var, pid
Context : address context
• fork internal : after fork
– address space : basic protection barrier
memory
text
stack
data
segment T.proc T.
pid = 11
segment T.proc T.
pid = 12
stack
data
glob, buf
var, pid
var, pid
glob, buf
Context : address context• fork internal : with COW (Copy on Write) mechanism
after fork with COW after “glob++” operation memory
text
stack
data
segment T.proc T.
pid = 11
segment T.proc T.
pid = 12
text
stack
data
segment T.proc T.
pid = 11
segment T.proc T.
pid = 12
data
12
Context : address context
• execve internal
header
text
data bss
stack
a.out
memory
text
stack
data
segment T.proc T.
pid = 11
stack
data
text
13
Context : hardware context
• time sharing (multitasking)
process 1
process 2
process 3
time quantum
…
Where am I ??
14
Context : hardware context
• brief reminds the 80x86 architecture
ALU
Control Unit
Registers
IN OUT
eip, eflags eax, ebx, ecx, edx, esi, edi, … cs, ds, ss, es, ... cr0, cr1, cr2, cr3, GDTR, TR, ...
15
Context : hardware context
• context swtch
U area
Proc T. TSSeip sp
eflagseax
cs
CPU
U area
Proc T. TSSeip sp
eflagseax
cs
restore context
savecontext
16
Context : hardware context
• context swtch : pseudo-code in UNIX…/* need context swtch */if (save_context()){
/* pick another process to run from ready queue */….restore_context(new process)/* The control does not arrive here, NEVER !!! */
}/* resuming process executes from here !!! */…... (Source : The Design of the UNIX OS)