CS 326: Operating Systems. Process Execution. Lecture 5

Similar documents
Introduction to pthreads

EPL372 Lab Exercise 2: Threads and pthreads. Εργαστήριο 2. Πέτρος Παναγή

CSE 153 Design of Operating Systems Fall 2018

Thread Concept. Thread. No. 3. Multiple single-threaded Process. One single-threaded Process. Process vs. Thread. One multi-threaded Process

Threads. CS3026 Operating Systems Lecture 06

Lecture Topics. Announcements. Today: Threads (Stallings, chapter , 4.6) Next: Concurrency (Stallings, chapter , 5.

Introduction to PThreads and Basic Synchronization

pthreads CS449 Fall 2017

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: Processes and Threads

Multithreaded Programming

THREADS. Jo, Heeseung

CS 3305 Intro to Threads. Lecture 6

Processes & Threads. Recap of the Last Class. Microkernel System Architecture. Layered Structure

Threads. Threads (continued)

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Processes and threads

W4118 Operating Systems. Junfeng Yang

Questions answered in this lecture: CS 537 Lecture 19 Threads and Cooperation. What s in a process? Organizing a Process

Processes. Dr. Yingwu Zhu

Last class: Today: Thread Background. Thread Systems

Processes and Threads

Outline. Threads. Single and Multithreaded Processes. Benefits of Threads. Eike Ritter 1. Modified: October 16, 2012

Lecture 4 Threads. (chapter 4)

CS 326: Operating Systems. CPU Scheduling. Lecture 6

Threads. CS-3013 Operating Systems Hugh C. Lauer. CS-3013, C-Term 2012 Threads 1

Processes. CS3026 Operating Systems Lecture 05

Processes. CS439: Principles of Computer Systems January 30, 2019

LSN 13 Linux Concurrency Mechanisms

CS 475. Process = Address space + one thread of control Concurrent program = multiple threads of control

Getting to know you. Anatomy of a Process. Processes. Of Programs and Processes

Introduction to OS Processes in Unix, Linux, and Windows MOS 2.1 Mahmoud El-Gayyar

CS510 Operating System Foundations. Jonathan Walpole

CSE 333 SECTION 9. Threads

Thread. Disclaimer: some slides are adopted from the book authors slides with permission 1

Process Time. Steven M. Bellovin January 25,

Creating Threads COMP755

Processes. Operating System CS 217. Supports virtual machines. Provides services: User Process. User Process. OS Kernel. Hardware

CSC369 Lecture 2. Larry Zhang, September 21, 2015

Announcements Processes: Part II. Operating Systems. Autumn CS4023

Exercise (could be a quiz) Solution. Concurrent Programming. Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - IV Threads

Processes and Threads. Processes and Threads. Processes (2) Processes (1)

Processes. CS439: Principles of Computer Systems January 24, 2018

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

System Call. Preview. System Call. System Call. System Call 9/7/2018

Lecture 17: Threads and Scheduling. Thursday, 05 Nov 2009

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Processes. Process Concept

CPSC 341 OS & Networks. Processes. Dr. Yingwu Zhu

Chapter 4: Multithreaded Programming

Killing Zombies, Working, Sleeping, and Spawning Children

OS Structure, Processes & Process Management. Don Porter Portions courtesy Emmett Witchel

CSC369 Lecture 2. Larry Zhang

ECE 598 Advanced Operating Systems Lecture 23

Processes. OS Structure. OS Structure. Modes of Execution. Typical Functions of an OS Kernel. Non-Kernel OS. COMP755 Advanced Operating Systems

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Concurrency, Thread. Dongkun Shin, SKKU

CS 31: Intro to Systems Processes. Kevin Webb Swarthmore College March 31, 2016

What is a Process? Processes and Process Management Details for running a program

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: Processes and Threads

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSCE 313: Intro to Computer Systems

CS 104 Computer Organization and Design

Computer Systems Laboratory Sungkyunkwan University

Chapter 4: Threads. Operating System Concepts. Silberschatz, Galvin and Gagne

Chapter 4: Threads. Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

CSE 380 Computer Operating Systems. Instructor: Insup Lee. University of Pennsylvania Fall 2003

Process Description and Control

3. Process Management in xv6

Chapter 4 Multithreaded Programming

REVIEW OF COMMONLY USED DATA STRUCTURES IN OS

Threads. Raju Pandey Department of Computer Sciences University of California, Davis Spring 2011

CSE 451: Operating Systems Winter Module 4 Processes. Mark Zbikowski Allen Center 476

Processes. Johan Montelius KTH

Threads. What is a thread? Motivation. Single and Multithreaded Processes. Benefits

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

A process. the stack

ECE 574 Cluster Computing Lecture 8

Threads and Too Much Milk! CS439: Principles of Computer Systems February 6, 2019

Operating Systems & Concurrency: Process Concepts

CPS221 Lecture: Threads

Process and Its Image An operating system executes a variety of programs: A program that browses the Web A program that serves Web requests

Design Overview of the FreeBSD Kernel CIS 657

Processes Prof. James L. Frankel Harvard University. Version of 6:16 PM 10-Feb-2017 Copyright 2017, 2015 James L. Frankel. All rights reserved.

Threads. studykorner.org

Threads and Too Much Milk! CS439: Principles of Computer Systems January 31, 2018

Design Overview of the FreeBSD Kernel. Organization of the Kernel. What Code is Machine Independent?

Processes, Context Switching, and Scheduling. Kevin Webb Swarthmore College January 30, 2018

Outline. CS4254 Computer Network Architecture and Programming. Introduction 2/4. Introduction 1/4. Dr. Ayman A. Abdel-Hamid.

Lecture 4: Mechanism of process execution. Mythili Vutukuru IIT Bombay

Processes and Non-Preemptive Scheduling. Otto J. Anshus

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Computer Systems Engineering: Spring Quiz I Solutions

Fall 2017 :: CSE 306. Process Abstraction. Nima Honarmand

CSCE Operating Systems Interrupts, Exceptions, and Signals. Qiang Zeng, Ph.D. Fall 2018

Processes and Threads

2 Processes. 2 Processes. 2 Processes. 2.1 The Process Model. 2.1 The Process Model PROCESSES OPERATING SYSTEMS

CSE 306/506 Operating Systems Threads. YoungMin Kwon

COSC243 Part 2: Operating Systems

CSE 120 Principles of Operating Systems

Process management. What s in a process? What is a process? The OS s process namespace. A process s address space (idealized)

Transcription:

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