CS 550 Operating Systems Spring 2018 Process I 1
Process Informal definition: A process is a program in execution. Process is not the same as a program. Program is a passive entity stored in the disk Process is an actively executing entity Program is just one component of a process. 2
Programs and Processes Process The running instantiation of a program, stored in RAM Program An executable file in long-term storage One-to-many relationship between program and processes 3
Programs How is a program (i.e., an executable binary file) formatted? main.c int buf[2] = {1, 2}; int main() { swap(); return 0; } swap.c extern int buf[]; int *bufp0 = &buf[0]; static int *bufp1; void swap() { int temp; bufp1 = &buf[1]; temp = *bufp0; *bufp0 = *bufp1; *bufp1 = temp; } 4
Programs How is a program (i.e., an executable binary file) formatted? The compiler compiles source code into relocatable object files. The linker links all necessary relocatable object files into an executable object file (i.e., the program). 5
Static Linking Programs are translated and linked using a compiler driver: unix> gcc -O2 -g -o p main.c swap.c unix>./p main.c swap.c Source files Translators (cpp, cc1, as) main.o Linker (ld) Translators (cpp, cc1, as) swap.o Separately compiled (relocatable) object files p Fully linked program (executable object file) (contains code and data for all functions defined in main.c and swap.c) 6
Executable Object files Object file formats vary from system to system DOS: MZ executables (*.exe) Named after Mark Zbikowski, a DOS developer Windows Portable Executable (PE, PE32+) (*.exe) Modified version of Unix COFF executable format Mac OSX: Mach object file format (Mach-O) Unix/Linux: Executable and Linkable Format (ELF) 7
Executable and Linkable Format (ELF) Standard binary format for object files for Unix and Unix-like systems on x86. Originally proposed by AT&T System V Unix Later adopted by BSD Unix variants and Linux One unified format for Relocatable object files (.o), Executable object files (a.out) Shared object files (.so) Generic name: ELF binaries 8
ELF Object File Format Elf header Word size, byte ordering, file type (.o, exec,.so), machine type, etc. Segment header table Page size, virtual addresses memory segments (sections), segment sizes..text section Code.rodata section Read only data.data section Initialized global variables.bss section Uninitialized global variables Block Started by Symbol Better Save Space Has section header but occupies no space ELF header Segment header table (required for executables).text section.rodata section.data section.bss section.symtab section.rel.txt section.rel.data section.debug section Section header table 9 0
ELF Header oot@huicomputer1:/home/huilu/case1# readelf -h p ELF Header: Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 Class: ELF64 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: EXEC (Executable file) Machine: Advanced Micro Devices X86-64 Version: 0x1 Entry point address: 0x400430 Start of program headers: 64 (bytes into file) Start of section headers: 6784 (bytes into file) Flags: 0x0 Size of this header: 64 (bytes) Size of program headers: 56 (bytes) Number of program headers: 9 Size of section headers: 64 (bytes) Number of section headers: 31 Section header string table index: 28 10
More Sources (ELF) ELF Wiki https://en.wikipedia.org/wiki/executable_and_link able_format ELF 101: https://linux-audit.com/elf-binaries-onlinux-understanding-and-analysis/ 11
Programs When you double-click on a program (or use command line), how does the OS turn the file on disk into a process? Load time The loader creates a process for the program, loads the program to RAM, or more precisely, to the address space of the process, and starts executing the first instruction of the program. 12
Loading executable object files p is not a built-in shell command shell assumes p is an executable object file shell invokes the loader (some memory-resident OS code) to run the program p. Unix/Linux programs can invoke loader by calling the execve function (introduced later). The loader copies the code and data in the executable object file from disk into memory, and then runs the program by jumping to its first instruction, or the entry point. This process of copying the program into memory and then running it is known as loading. 13
So what else constitutes a process? Memory space (static, dynamic) Procedure call stack Registers and counters : Program counter, Stack pointer, General purpose registers Open files, connections 14
References Chapter 4 of OSTEP book Chapter 2 of the Tanenbaum s book Man pages for different system calls Try man 2 <syscall_name> E.g. man 2 exec Syscalls are normally listed in section 2 of the man page Google for Linux source code 15
Backups 16