CS 326: Operating Systems Process Execution Lecture 5
Today s Schedule Process Creation Threads Limited Direct Execution Basic Scheduling 2/5/18 CS 326: Operating Systems 2
Today s Schedule Process Creation Threads Limited Direct Execution Basic Scheduling 2/5/18 CS 326: Operating Systems 3
Creating a Process As we discussed previously, we can create processes with the fork function This creates a clone of an existing process After creating the clone, we know two things: Which process is the parent Which process is the child Logic branches from here, allowing the two processes to do different work 2/5/18 CS 326: Operating Systems 4
Dealing with Clones The cloning approach is particularly nice if you want to make your application work on multiple CPUs Example: MPI It doesn t quite help us if we want to launch a completely different process, though For instance, our program wants to start the top command There is another function to accomplish this: exec 2/5/18 CS 326: Operating Systems 5
exec The exec family of functions allows us to launch other applications exec replaces the memory space of a clone with a new program and begins its execution After fork(): copy of my_program After exec(): separate process running top 2/5/18 CS 326: Operating Systems 6
Why fork + exec? Why not just have a nice C function called launch_program (or something like that) instead? Or in other words: why does this need to be broken into two steps? Advantages of operating this way: While the new process is still a clone, it can set up the target environment for the new application No restriction on which process will be replaced (could be the parent or child usually child) 2/5/18 CS 326: Operating Systems 7
Setting up the Environment The new process can inherit several aspects of its predecessor Environment variables: the system path, current working directory, global program options Redirection: the new process may be set up to receive input on its stdin stream from the parent process Pipes in the shell 2/5/18 CS 326: Operating Systems 8
Termination There are three ways a process can terminate: 1. Clean exit 2. Error exit 3. Involuntary exit Regardless of the exit type, the OS: Deallocates the memory assigned to the process Closes any open files Releases locks Cancels any callbacks/timers 2/5/18 CS 326: Operating Systems 9
Clean Exit In the most common case, programs terminate cleanly Generally this is indicated by exit code 0 (or: why our C programs end with return 0) The exit code is returned to the parent process For example, in your shell Demo: running ls on known good file, echo ${?} 2/5/18 CS 326: Operating Systems 10
Error Exit In some cases a program exits voluntarily, but termination was caused by some type of error Generally this is indicated by a nonzero exit code (or: why our C programs indicate a failure with return EXIT_FAILURE) Once again, the exit code is returned to the parent process Demo: running ls on a missing file, echo ${?} 2/5/18 CS 326: Operating Systems 11
Involuntary Exit Sometimes a bug causes the process to exit involuntarily Segfault: program tried to access out-of-bounds memory OS sends SIGSEGV signal and terminates the program Or the process is killed by another: OS needs to deallocate resources, is shutting down, etc kill -s SIGTERM ; kill -s SIGKILL 2/5/18 CS 326: Operating Systems 12
Demo: exec 2/5/18 CS 326: Operating Systems 13
Today s Schedule Process Creation Threads Limited Direct Execution Basic Scheduling 2/5/18 CS 326: Operating Systems 14
Threads Threads are essentially lightweight processes Created by processes to do some subset of the work A single process can manage multiple threads Threads use shared memory for coordination All the threads have access to the program heap In our class, we ll use the pthreads (POSIX Threads) library to build multithreaded applications POSIX - Portable Operating System Interface 2/5/18 CS 326: Operating Systems 15
Process Contents Each process has: Binary instructions, data, memory File descriptors, permissions Stack, heap, registers Threads are very similar, but they share almost everything with their parent process except for: Stack Registers Program counter 2/5/18 CS 326: Operating Systems 16
Memory Allocation with Threads 2/5/18 CS 326: Operating Systems 17
Sharing Data Since threads share the heap with their parent process, we can share pointers to memory locations A thread can read and write data set up by its parent process Sharing these resources also means that it s faster to create threads No need to allocate a new heap, set up permissions, etc. 2/5/18 CS 326: Operating Systems 18
Creating a Thread pthreads int pthread_create( pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg); 2/5/18 CS 326: Operating Systems 19
pthread_create 1. pthread_t, is considered an opaque type, defined internally by the library It s often just an integer that uniquely identifies the thread 2. pthread attributes Include stack size and scheduling policies 3. Function pointer to execute 4. Args (pointer to anything void *) 2/5/18 CS 326: Operating Systems 20
pthread_join int pthread_join(pthread_t thread, void **value_ptr); The pthread_join function waits for a pthread to finish execution (by calling return) The return value of the thread is stored in value_ptr Commonly used to coordinate shutting down the threads, waiting for their results, etc Similar to wait() 2/5/18 CS 326: Operating Systems 21
Multi-Process or Multi-Thread? In general, using multiple processes is simpler and easier to implement Split up the problem, have the processes work on it independently However, if the algorithm you are implementing requires lots of communication or shared state, threads are a better choice They are also faster to create and require fewer system resources 2/5/18 CS 326: Operating Systems 22
Speed Difference: Creation On Solaris, creating a process is 30x slower than creating a thread, and context switches are 5x slower Each operating system has different overhead associated with processes/threads Windows: huge process overhead. Use threads where possible Linux: relatively low process creation overhead These speed differentials impact how the OS is built! 2/5/18 CS 326: Operating Systems 23
Scalability Remember: single-threaded (or single-process) applications cannot run on more than one CPU This impacts scalability: the ability of your algorithm to run faster when given more resources Threads are a great way to take advantage of more cores to carry out background tasks and make applications more responsive 2/5/18 CS 326: Operating Systems 24
Today s Schedule Process Creation Threads Limited Direct Execution Basic Scheduling 2/5/18 CS 326: Operating Systems 25
CPU Virtualization Recall: the OS virtualizes the CPU to allow multiple programs to run concurrently or at least with the illusion of concurrency How does this work at the OS level? One approach: full virtualization Trap and translate all instructions to make the process believe it s running on some other platform Inefficient! Roughly doubles the amount of instructions needed 2/5/18 CS 326: Operating Systems 26
Direct Execution The opposite of full virtualization is direct execution This allows the process to run directly on the hardware While doing so, it has complete control This is the old DOS/Win 3.1 approach Unfortunately, direct execution has some downsides 2/5/18 CS 326: Operating Systems 27
Issues with Direct Execution If you give a process full control of the hardware, it can do whatever it wants Access other processes memory, for instance Manipulate I/O devices directly Who cares about disk permissions when you can just make the device read the files you want? So while the performance of direct execution is great, it poses security and reliability risks 2/5/18 CS 326: Operating Systems 28
Limited Direct Execution OS designers came to a compromise between full virtualization and direct execution Limited direct execution For certain (safe) operations, processes are given full access to the CPU/hardware Some privileged operations are not allowed, however In this case, the process must ask the kernel for help 2/5/18 CS 326: Operating Systems 29
System Calls These privileged operations are system calls System calls include performing I/O, setting the current time, or launching other processes (fork!) This is where we derive the division between two halves of the OS: User space Kernel space 2/5/18 CS 326: Operating Systems 30
Executing a System Call When you call fork, you aren t directly calling into kernel space The C library is responsible for interfacing with the kernel Sets up parameters and return address for the call Jumps into a predefined memory address in kernel space In doing so, execution is transferred to the kernel 2/5/18 CS 326: Operating Systems 31
System Calls 2/5/18 CS 326: Operating Systems 32
Protecting System Calls In the early days, operating systems allowed processes to call OS routines directly This approach has security issues! CPU Protection Rings enable a separation between system-level functions and user space applications Ring 0: highest level of privilege. OS functionality Rings 1-2: drivers, VM Hypervisors, or simply unused Ring 3: user applications Instructions are flagged with a permission level 2/5/18 CS 326: Operating Systems 33
Protection Rings: x86 2/5/18 CS 326: Operating Systems 34
Transferring Control When the jump occurs between user and kernel space, we must adjust the privilege level to transition successfully This is called a trap, which moves the execution pointer and changes the privilege level Once the the operation is complete, a return-fromtrap is executed Privilege level is dropped, and control is returned to process routines 2/5/18 CS 326: Operating Systems 35
Securing Kernel Space During boot, the OS has exclusive control of the hardware and sets up the trap table This defines protected regions that may be used for programs and hardware to interface with kernel space Examples: Where to jump to execute a system call How to handle hardware interrupts 2/5/18 CS 326: Operating Systems 36
Interrupts An interrupt is a signal to the processor that some type of event needs to be handled Software interrupt: trap Hardware interrupts inform the OS of a key being pressed, I/O completing, timer ticks, etc. Much better than polling: actively checking the state of all the devices continuously Note: what happens when too many interrupts are produced? 2/5/18 CS 326: Operating Systems 37
Security Protection rings are one line of defense between malicious programs and the kernel However, we still do not use full virtualization We don t inspect every operation before it runs This would cause a noticeable decrease in performance This does open up the OS to vulnerabilities in both the software and hardware 2/5/18 CS 326: Operating Systems 38
Overhead Using the kernel as an intermediary does have downsides Still slower than direct execution This cost is called overhead, the amount of extra time spent in kernel space Many privileged operations will be executed twice, once in each context 2/5/18 CS 326: Operating Systems 39
Today s Schedule Process Creation Threads Limited Direct Execution Basic Scheduling 2/5/18 CS 326: Operating Systems 40
Context Switching As discussed previously, switching execution between processes requires a context switch Saving and restoring the process control block You may wonder if transitioning between user and kernel space requires a context switch In general, no Only a privilege change occurs 2/5/18 CS 326: Operating Systems 41
Process Control Block (Isolated from other processes) Call Stack Heap Permissions, ownership Isolate file access Environment Variables Register States Stack pointer, program counter Memory Addressing 2/5/18 CS 326: Operating Systems 42
Basic Scheduling We mentioned earlier that the OS can preempt running processes This is accomplished via interrupts The OS configures a hardware timer to fire on a set interval: Interrupt CPU in X ms 2/5/18 CS 326: Operating Systems 43
Interrupt Handling The trap table is used to determine what to do when the hardware timer fires Ultimately moves us back into kernel space When this occurs, save the running PCB and replace with the next in the run queue 2/5/18 CS 326: Operating Systems 44
Advanced Scheduling How do we decide what to run next? Several scheduling algorithms exist, all with different run time properties Our next subject: scheduling in depth 2/5/18 CS 326: Operating Systems 45