Process Environment Pradipta De pradipta.de@sunykorea.ac.kr
Today s Topic Program to process How is a program loaded by the kernel How does kernel set up the process Outline Review of linking and loading Look at ELF files Process memory allocation CSE506: Process Environment 2
What is a process? A process is an instance of a program in execution Same program, multiple instances multiple processes Needs several data structures for managing resources (CPU time, mem, access to I/O ports), as well as, remember current state (initialized, running, suspended, etc.) Lightweight process A process sharing threads of execution Supported by POSIX pthread library
Process Descriptor In Linux, Ln: 1238 struct task_struct http://lxr.free-electrons.com/source/include/linux/sched.h Task_struct contains, Process state identifier, parent id, usage flags, and pointers to other structs We will focus on mm_struct
mm_struct: overview Task_struct *mm Mm_struct *mmap *mm_rb Linked List vm_area_struct vm_area_struct Red-black Tree vm_area_struct null
Executable program? Basics: compiler, linker, loader Files a.c and b.c Compiled to generate a.o and b.o Preprocessed gcc E *.c o *.i Compiled gcc S *.c Assembled gcc c test.o test.c Linker combines the *.o to generate executable Ld <options> a.o b.o o prog Shell invokes loader to load prog (code and data) into memory Execve acts as the loader function
Object files Relocatable: code and data can be combined with other relocatable object files Executable: can be directly loaded into memory and instructions can be executed Shared: special relocatable file, which can be loaded into memory dynamically
File type: Relocatable Entry point not set File type: Executable Entry point set
Relocatable File Executable file
ELF Sections Multiple sections in a ELF file.text : code.data: initialized data.bss: uninitialized data.stack: stack segment Several more not all relevant to us now Coming up.plt,.got
Stack address is assigned during loading. Stack grows downwards in memory
Library calls Static linking Dynamic linking Sections are merged to create a block of sections for each type (.text,.code, Linked at runtime
How functions are referenced Using Procedure Linkage Table.plt section has pointer to the function names Think jump table Address of the functions resolved at runtime
Process Address Space
Per process address space Each process sees a flat 32bit or 64bit address space Not absolutely true user space and kernel space Kernel mapped to high mem @0xF0100000 (3GB) Why VMA and LMA different in kernel image? Memory area contains, executable file's code, _text section_ initialized global variable, _data section_ zero page for uninitialized global variable, _bss section_ zero page for process's user-space stack additional text, data, bss section for each shared library, such as C library and dynamic linker, loaded into process address space any memory mapped file shared memory segments - any anonymous memory mappings Check memory layout of any program ldd /usr/bin/xterm Note: ld-linux-x86-64.so.2 always mapped
vm_area_struct Start and end address Memory regions are page aligned Protection flag (read, write, execute) Code (text) : no write, execute Data: write Why two representations for vm_area_struct list? Single linked list Good for processes with very few memory regions RB tree Efficient in terms of search, retrieval, insertion Tree traversal O(log n)
Sharing memory area On creating a child process (fork), Can copy each page of the parent process to the child process do we really need all the pages? Mark the pages Copy-on-Write (COW) Will make new page allocation on write
Putting It Together What is process address space? ELF Section headers loader maps it to process address space Shared libraries Address space data structures Shared address space lazy allocation CSE506: Process Environment 19