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. 2. 3. 4. 5. CSE 361 Review: Signals Overview CSE 361 Review: Resource Virtualization Linux Processes and Threads Creating a New Process Termination CSE 422 S – Operating Systems Organization 2
Signals Introduction • We will be covering signals in detail later in the semester • Understanding process management requires a basic understanding of signals • If you took CSE 361 (undergraduates must have), the following material will be review CSE 422 S – Operating Systems Organization 3
What Are Signals • Sometimes called software interrupts • Asynchronous notification to a process • Each signal has default receipt behavior (terminate, suspend, ignore, abort) • A process may register a signal handler, a function replacing the default receipt behavior for a specified signal • Each signal defined as an integer with corresponding manifest constant CSE 422 S – Operating Systems Organization 4
Common Signals Name Description Default Behavior SIGTSTP Stop process with CTRL+Z Stop SIGCONT Resume stopped process, ignored if process not currently stopped Ignore SIGINT Kill process with CTRL+C Terminate SIGQUIT Kill process with CTRL+, dump core Abort SIGKILL Kill process, cannot be blocked or overridden Terminate SIGSTOP Stop process, cannot be blocked or overridden Stop SIGCHLD Delivered to parent process when child terminated or stopped Ignore CSE 422 S – Operating Systems Organization 5
Overview 1. 2. 3. 4. 5. CSE 361 Review: Signals Overview CSE 361 Review: Resource Virtualization Linux Processes and Threads Creating a New Process Termination CSE 422 S – Operating Systems Organization 6
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 7
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 8
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 Memory Virtualization: • Translate private memory addresses to physical addresses • Virtual memory CSE 422 S – Operating Systems Organization 9
Overview 1. 2. 3. 4. 5. CSE 361 Review: Signals Overview CSE 361 Review: Resource Virtualization Linux Processes and Threads Creating a New Process Termination CSE 422 S – Operating Systems Organization 10
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 11
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 12
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 13
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 14
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 15
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 16
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 17
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 18
Overview 1. 2. 3. 4. 5. CSE 361 Review: Signals Overview CSE 361 Review: Resource Virtualization Linux Processes and Threads Creating a New Process Termination CSE 422 S – Operating Systems Organization 19
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)* • fork() is a library function that passes appropriate flags to clone() system call *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 20
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 • In Linux terminology: Threads are Processes that share parent address space 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 21
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 22
Overview 1. 2. 3. 4. 5. CSE 361 Review: Signals Overview CSE 361 Review: Resource Virtualization Linux Processes and Threads Creating a New Process Termination CSE 422 S – Operating Systems Organization 23
The exit(int status) Library Call • • Calls exit handlers Flushes stdio buffers Calls __exit(int status) system call Automatically linked into your program int main(int argc, char * argv[]) { //Do something return status; } Equivalent int main(int argc, char * argv[]) { //Do something exit(status); } • Do not use exit() from inside a function rather than providing a clean termination path CSE 422 S – Operating Systems Organization 24
The __exit(int status) System Call • Performs process termination and cleanup • Does not free process task_struct • This allows parent process to obtain status via wait*() functions CSE 422 S – Operating Systems Organization 25
Exit Handlers • Run whenever exit() is called • E. g. , any return from main() • Useful for ensuring resource cleanup along all termination paths – Free dynamically allocated memory – Close files and sockets • Two methods to register: – int atexit(void (*func)(void)); Simple, portable – int on_exit(void (*func)(int, void*), void *arg); Receives exit status and additional argument CSE 422 S – Operating Systems Organization 26
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 27
- Slides: 27