linux system : lecture 7 process bong-soo sohn lecture notes acknowledgement : the design of unix...

16
LINUX System : Lecture 7 Process Bong-Soo Sohn ecture notes acknowledgement : The design of UNIX Operating System

Upload: peregrine-warner

Post on 17-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: LINUX System : Lecture 7 Process Bong-Soo Sohn Lecture notes acknowledgement : The design of UNIX Operating System

LINUX System : Lecture 7Process

Bong-Soo Sohn

Lecture notes acknowledgement : The design of UNIX Operating System

Page 2: LINUX System : Lecture 7 Process Bong-Soo Sohn Lecture notes acknowledgement : The design of UNIX Operating System

2

Process Management

Page 3: LINUX System : Lecture 7 Process Bong-Soo Sohn Lecture notes acknowledgement : The design of UNIX Operating System

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

Page 4: LINUX System : Lecture 7 Process Bong-Soo Sohn Lecture notes acknowledgement : The design of UNIX Operating System

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)

Page 5: LINUX System : Lecture 7 Process Bong-Soo Sohn Lecture notes acknowledgement : The design of UNIX Operating System

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

Page 6: LINUX System : Lecture 7 Process Bong-Soo Sohn Lecture notes acknowledgement : The design of UNIX Operating System

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

Page 7: LINUX System : Lecture 7 Process Bong-Soo Sohn Lecture notes acknowledgement : The design of UNIX Operating System

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)

Page 8: LINUX System : Lecture 7 Process Bong-Soo Sohn Lecture notes acknowledgement : The design of UNIX Operating System

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

Page 9: LINUX System : Lecture 7 Process Bong-Soo Sohn Lecture notes acknowledgement : The design of UNIX Operating System

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

Page 10: LINUX System : Lecture 7 Process Bong-Soo Sohn Lecture notes acknowledgement : The design of UNIX Operating System

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

Page 11: LINUX System : Lecture 7 Process Bong-Soo Sohn Lecture notes acknowledgement : The design of UNIX Operating System

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

Page 12: LINUX System : Lecture 7 Process Bong-Soo Sohn Lecture notes acknowledgement : The design of UNIX Operating System

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

Page 13: LINUX System : Lecture 7 Process Bong-Soo Sohn Lecture notes acknowledgement : The design of UNIX Operating System

13

Context : hardware context

• time sharing (multitasking)

process 1

process 2

process 3

time quantum

Where am I ??

Page 14: LINUX System : Lecture 7 Process Bong-Soo Sohn Lecture notes acknowledgement : The design of UNIX Operating System

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, ...

Page 15: LINUX System : Lecture 7 Process Bong-Soo Sohn Lecture notes acknowledgement : The design of UNIX Operating System

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

Page 16: LINUX System : Lecture 7 Process Bong-Soo Sohn Lecture notes acknowledgement : The design of UNIX Operating System

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)