Processes David Ferry Chris Gill Brian Kocoloski Marion
Processes David Ferry, Chris Gill, Brian Kocoloski, Marion Sudvarg CSE 422 S - Operating Systems Organization Washington University in St. Louis, MO 63143
Overview 1. CSE 361 Review: Resource Virtualization 2. Linux Processes and Threads 3. Creating a New Process CSE 422 S – Operating Systems Organization 2
Resource Virtualization • What resources do you use when you write programs? – – The processor(s) Memory Files Data (global variables, things like hardcoded strings) • What do you not have to worry about generally? – Existence of other processes – State of hardware resources CSE 422 S – Operating Systems Organization 3
Resource Virtualization • The operating system virtualizes physical hardware resources into virtual resources in the form of processes Physical Memory Physical Processors CSE 422 S – Operating Systems Organization 4
Resource Virtualization Process 1 Process 2 Physical Memory Physical Processors Processor Virtualization: • Two (or more) processes may share the same set of physical CPUs • How to share them? • CPU scheduling (next week) Memory Virtualization: • Translate private memory addresses to physical addresses • Virtual memory (in a few weeks) CSE 422 S – Operating Systems Organization 5
Overview 1. CSE 361 Review: Resource Virtualization 2. Linux Processes and Threads 3. Creating a New Process CSE 422 S – Operating Systems Organization 6
Processes in Linux Fundamental process abstraction: – Original: Each process behaves as though it has total control over the system – Modern: Threads still execute as though they monopolize the system, but the process abstraction also facilitates multi-threading, signals, and interprocess communication More features: – – – Scheduling Virtual Memory Process accounting Signals Process groups Synchronization – – Multi-threading Shared memory Sockets etc. CSE 422 S – Operating Systems Organization 7
Tasks in Linux A task/process is an execution stack together with data that describes the process state. (also describes threads) User side: Kernel side: Two stacks: – Kernel mode – User mode User Stack . bss Kernel stack thread_info . data Data: . text task_struct – thread_info: small, efficient – task_struct: large, statically allocated by slab allocator, points to other data structs CSE 422 S – Operating Systems Organization 8
One Size Fits All Processes, threads, and kernel threads are all implemented with the same data structures (thread_info and task_struct) and functions. Threads are like user processes but they share their parent’s address space. Kernel threads don’t have a user-side address space (i. e. mm_struct pointer is NULL) and not all data values are used. CSE 422 S – Operating Systems Organization 9
Threads, Processes, and Kernel Threads Address space thread_info Kernel level User level Kernel stack. bss (static variables). text (executable code). data (heap) User stack CSE 422 S – Operating Systems Organization 10
Threads, Processes, and Kernel Threads <set stack pointer to kernel address> <set page table to kernel memory> Address space thread_info Kernel level main() syscall() User level Kernel stack. bss (static variables). text (executable code). data (heap) User stack CSE 422 S – Operating Systems Organization 11
Threads, Processes, and Kernel Threads // current is a user-level process struct task_struct * task = current; struct mm_struct * mm = current->mm; Kernel level User level Address space thread_info Kernel stack. bss (static variables). text (executable code). data (heap) User stack CSE 422 S – Operating Systems Organization 12
Threads, Processes, and Kernel Threads // current is a kernel thread // (recall, look for processes named // “k …” in ‘ps –aux’) struct task_struct * task = current; struct mm_struct * mm = current->mm; Address space thread_info Kernel level Kernel stack User level NULL CSE 422 S – Operating Systems Organization 13
Threads, Processes, and Kernel Threads struct task_struct * task 1; struct task_struct * task 2; // fetch task_structs for 2 threads of the same process task 1 ->mm; task 2 ->mm Task 2 Task 1. bss (static variables). text (executable code). data (heap) User level stack Stacks are specific per-thread CSE 422 S – Operating Systems Organization 14
Overview 1. CSE 361 Review: Resource Virtualization 2. Linux Processes and Threads 3. Creating a New Process CSE 422 S – Operating Systems Organization 15
Creating a New Process: fork() • Creates a new process by duplicating the calling process • Initialize per-PID data, e. g. : PID, real_parent, statistics • Copies file handles, signal handlers, process address space, etc. • Resources shared via page-level lazy copy-on-write (COW)* *E. g. Every process in the system shares the same “copy” of the C library Kernel stack thread_info task_struct ptr* 0 x 1 ptr* 0 x 2 ptr* 0 x 3 User Stack . bss . data . text CSE 422 S – Operating Systems Organization 16
Creating a New Thread: clone() • Like fork() but with more control • Can be used to create new threads • Duplicates the calling process and initializes per-PID data • Copying of file handles, signal handlers, process address space, etc. is optional Kernel stack thread_info task_struct ptr* 0 x 1 ptr* 0 x 2 ptr* 0 x 3 User Stack . bss. data. text CSE 422 S – Operating Systems Organization 17
Kernel Source • Process creation – _do_fork() – get_task_pid() (kernel/fork. c line 2018) (kernel/pid. c line 465) • Kernel thread creation – kernel_thread() (kernel/fork. c line 2107) • Process copying – copy_process() (kernel/fork. c line 1537) CSE 422 S – Operating Systems Organization 18
Today’s Studio • Explore user-space utilities for process discovery and creation • Explore kernel-level data structures used for process management – struct task_struct • More experience with kernel modules CSE 422 S – Operating Systems Organization 19
- Slides: 19