System Programming Lecture Note 4 Process Structure September

System Programming Lecture Note 4. Process Structure September 26, 2019 Jongmoo Choi Dept. of Software Dankook University choijm@dankook. ac. kr http: //embedded. dankook. ac. kr/~choijm

Objectives Understand the definition of a process (task) Explore the process structure Discuss the relation between program and process structure Grasp the details of stack Refer to Chapter 6 in the LPI and Chapter 8, 9 in the CSAPP 2

Process Definition (1/2) What is a process (task)? ü ü ü Program in execution Having its own memory space and CPU registers Scheduling entity Conflict each other for resource allocation Parent-child relation (family) Process (task) program (Source: computer systems: a programmer perspective) 3

Process Definition (2/2) Related terminology ü Load § § ü from disk into main memory disk: file system(chapter 3) main memory: virtual memory carried out by OS (e. g. page fault mechanism) Fetch § From memory into CPU § instruction fetch and data fetch § carried out by hardware ü Multiple tasks related § § task structure per each task (inode per each file) context switch during scheduling synchronization memory sharing/protection 4

Process Structure (1/6) Conceptual structure ü text, data, heap, stack 5

Process Structure (2/6) Process structure in C program: function pointer /* f_pointer. c: for function pointer exercise, by choijm, choijm@dku. edu */ #include <stdio. h> int a = 10; int func 1(int arg 1) { printf("In func 1: arg 1 = %dn", arg 1); } main() { int *pa; int (*func_ptr)(int); pa = &a; printf("pa = %p, *pa = %dn", pa, *pa); func 1(3); func_ptr = func 1; func_ptr(5); } printf("Bye. . ^^n"); 6

Process Structure (3/6) Process structure in C program: address printing /* task_struct. c: display addresses of variables and functions, choijm@dku. edu */ #include <stdlib. h> #include <stdio. h> int glob 1, glob 2; int func 2() { int f 2_local 1, f 2_local 2; } printf("func 2 local: nt%p, nt%pn", &f 2_local 1, &f 2_local 2); int func 1() { int f 1_local 1, f 1_local 2; } printf("func 1 local: nt%p, nt%pn", &f 1_local 1, &f 1_local 2); func 2(); main(){ int m_local 1, m_local 2; int *dynamic_addr; printf("main local: nt%p, nt%pn", &m_local 1, &m_local 2); func 1(); } dynamic_addr = malloc(16); printf("dynamic: nt%pn", dynamic_addr); printf("global: nt%p, nt%pn", &glob 1, &glob 2); printf("functions: nt%p, nt%pn", main, func 1, func 2); 7

Process Structure (4/6) Process structure in C program: address printing 0 xffaaca 84 0 xffaaca 80 0 xffaaca 44 0 xffaaca 40 0 xffaaca 24 0 xffaaca 20 0 x 0956 b 410 0 x 080497 c 4 stack for main stack for func 1 stack for func 2 heap data text for main 0 x 0804840 b 0 x 080483 e 4 0 x 080483 c 2 Addresses can be 8 different based on Compiler and OS text for func 1 text for func 2

Process Structure (5/6) Summary ü Process: consist of four regions, text, data, stack and heap Also called as segment or vm_object ü Text § Program code (assembly language) § Go up to the higher address according to coding order ü Data § Global variable § Initialized and uninitialized data are managed separately (for the performance reason) ü Stack § Local variable, argument, return address § Go down to the lower address as functions invoked ü Heap § Dynamic allocation area (malloc(), calloc(), …) § Go up to the higher address as allocated 9

Process Structure (6/6) Relation btw program and process data text stack 10

Process Structure in CSAPP Another viewpoint for process structure ü text, data, heap, stack + shared region, kernel 11

Stack Details (1/4) Stack in Intel architecture ü a contiguous array of memory locations with LIFO property § push: decrement the ESP and write data at the top of stack (down) § pop: read data from the top and increment the ESP (up) ü local variable, argument, return address, … (Source: Intel 64 and IA-32 Architectures Software Developer’s Manual) 12

Stack Details (2/4) Stack in Linux int func 2(int x, int y) { int f 2_local 1 = 21, f 2_local 2 = 22; int *pointer, i; } arguments, return address, local variables. . . void func 1() { int ret_val; int f 1_local 1 = 11, f 1_local 2 = 12; } stack frame for func 1 argument 2 argument 1 return address saved ebp . . . ret_val = func 2(111, 112); . . . int main() {. . . func 1(); . . . } stack frame for main stack frame for func 2 local variable 1 local variable 2. . . Compiler (and version) dependent Especially, recent compiler makes use of obfuscation, where the locations of local variables are changed according to program contents (gcc 4. *). But, gcc 3. * version comply with the Intel’s suggestion (like this figure) For lecturing purpose, gcc 3. * is more effective (Use 3. 4 in this lecture note) 13

Stack Details (3/4) Stack example 1 /* stack_struct. c: stack structure analysis, by choijm@dku. edu */ #include <stdio. h> int func 2(int x, int y) { int f 2_local 1 = 21, f 2_local 2 = 22; int *pointer; printf("func 2 local: t%p, t%pn", &f 2_local 1, &f 2_local 2, &pointer); pointer = &f 2_local 1; printf("t%p t%dn", (pointer), *(pointer)); printf("t%p t%dn", (pointer-1), *(pointer-1)); printf("t%p t%dn", (pointer+3), *(pointer+3)); } *(pointer+4) = 333; printf("ty = %dn", y); return 222; void func 1() { int ret_val, f 1_local 1 = 11, f 1_local 2 = 12; } ret_val = func 2(111, 112); main() { func 1(); } 14

Stack Details (4/4) Stack example 2 /* stack_destroy. c: 스택 구조 분석 2, 9월 19일, choijm@dku. edu */ #include <stdio. h> void f 1() { int i; printf("In func 1n"); } void f 2() { int j, *ptr; printf("f 2 local: t%p, t%pn", &j, &ptr); printf("In func 2 n"); ptr = &j; *(ptr+2) = f 1; } void f 3() { printf("Before invoke f 2()n"); f 2(); printf("After invoke f 2()n"); } main() { f 3(); } 15

Summary Understand the differences between process and program Discuss the differences among text, data, heap and stack Find out the details of stack structure ü ü Argument passing, Return address, Local variables Stack overflow Homework 4: Exercise the stack example 2 (slide 15 pages) for yourself Requirements - shows student’s ID and date (using whoami and date) - overcome the segmentation fault problem - hand out the report that includes a snapshot and discussion 16

Appendix 1 Snapshot for the Homework 4 17

Appendix 2 Function pointer practice /**************/ /* f_ptr_exam. c : 함수 포인터 예, */ /* 10월 4일, choijm@dku. edu */ /*************/ #include <stdio. h> main() { int (*ALU)(int x, int y); int a, b; char c; int add(int a, int b) { return a+b; } printf("USAGE: number operator number ==>"); scanf("%d %c %d", &a, &c, &b); switch (c) { case '+': ALU = add; break; case '-': ALU = substract; break; case '*': ALU = multiply; break; case '/': ALU = divide; break; } int substract(int a, int b) { return a-b; } int multiply(int a, int b) { return a*b; } int divide(int a, int b) { return a/b; } }18 printf("n%d %c %d = %dn", a, c, b, ALU(a, b));
- Slides: 18