Processes and Threads

Similar documents
Lecture 4: Memory Management & The Programming Interface

CSE 153 Design of Operating Systems Fall 2018

OS lpr. www. nfsd gcc emacs ls 1/27/09. Process Management. CS 537 Lecture 3: Processes. Example OS in operation. Why Processes? Simplicity + Speed

Operating Systems Structure and Processes Lars Ailo Bongo Spring 2017 (using slides by Otto J. Anshus University of Tromsø/Oslo)

Arvind Krishnamurthy Spring Threads, synchronization, scheduling Virtual memory File systems Networking

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

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

Architectural Support for Operating Systems. Jinkyu Jeong ( Computer Systems Laboratory Sungkyunkwan University

W4118 Operating Systems. Junfeng Yang

Threads Implementation. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Motivation. Threads. Multithreaded Server Architecture. Thread of execution. Chapter 4

www nfsd emacs lpr Process Management CS 537 Lecture 4: Processes Example OS in operation Why Processes? Simplicity + Speed

Processes. Dr. Yingwu Zhu

Chapter 4: Multithreaded Programming

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

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

Lecture 4: Threads; weaving control flow

Process. Heechul Yun. Disclaimer: some slides are adopted from the book authors slides with permission

Process Description and Control

Threads Implementation. Jo, Heeseung

Chapter 5: Threads. Outline

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

CS510 Operating System Foundations. Jonathan Walpole

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

Processes. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University

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

ECE 550D Fundamentals of Computer Systems and Engineering. Fall 2017

Process. Heechul Yun. Disclaimer: some slides are adopted from the book authors slides with permission 1

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

CS 326: Operating Systems. Process Execution. Lecture 5

OPERATING SYSTEM. Chapter 4: Threads

Computer Systems Laboratory Sungkyunkwan University

CS 450 Operating System Week 4 Lecture Notes

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

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

Process a program in execution; process execution must progress in sequential fashion. Operating Systems

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

Chapter 4: Multithreaded

CSE 120 Principles of Operating Systems

Announcement. Exercise #2 will be out today. Due date is next Monday

Agenda Process Concept Process Scheduling Operations on Processes Interprocess Communication 3.2

COS 318: Operating Systems

Design Overview of the FreeBSD Kernel CIS 657

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

Background: Operating Systems

High Performance Computing Lecture 11. Matthew Jacob Indian Institute of Science

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

Announcements Processes: Part II. Operating Systems. Autumn CS4023

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

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

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

CSE 120 Principles of Operating Systems

COS 318: Operating Systems. Overview. Andy Bavier Computer Science Department Princeton University

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

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

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

Processes & Threads. Recap of the Last Class. Layered Structure. Virtual Machines. CS 256/456 Dept. of Computer Science, University of Rochester

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

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

CSE 410: Computer Systems Spring Processes. John Zahorjan Allen Center 534

Operating System Design

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Processes. Process Concept

CSE 4/521 Introduction to Operating Systems

Last class: Today: Thread Background. Thread Systems

Disciplina Sistemas de Computação

Chapter 4: Threads. Chapter 4: Threads

OS lpr. www. nfsd gcc emacs ls 9/18/11. Process Management. CS 537 Lecture 4: Processes. The Process. Why Processes? Simplicity + Speed

The Kernel Abstraction. Chapter 2 OSPP Part I

THREADS. Jo, Heeseung

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

CSE 120 Principles of Operating Systems

CSC 4320 Test 1 Spring 2017

Processes. CS3026 Operating Systems Lecture 05

Chapter 3: Processes. Operating System Concepts 8 th Edition,

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

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

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

Kernel Types Simple OS Examples System Calls. Operating Systems. Autumn CS4023

CSE 4/521 Introduction to Operating Systems

Processes & Threads. Process Management. Managing Concurrency in Computer Systems. The Process. What s in a Process?

Chap 4, 5: Process. Dongkun Shin, SKKU

Chapter 4: Threads. Chapter 4: Threads

Chapter 4: Multi-Threaded Programming

Processes and Threads

What s in a process?

Lec 26: Parallel Processing. Announcements

Threads. Thread Concept Multithreading Models User & Kernel Threads Pthreads Threads in Solaris, Linux, Windows. 2/13/11 CSE325 - Threads 1

Process Concepts. CSC400 - Operating Systems. 3. Process Concepts. J. Sumey

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

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

A process. the stack

Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

Process Time. Steven M. Bellovin January 25,

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

Processes and More. CSCI 315 Operating Systems Design Department of Computer Science

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

Lecture 2 Fundamental OS Concepts. Bo 2018, Spring

Concurrency, Thread. Dongkun Shin, SKKU

Transcription:

COS 318: Operating Systems Processes and Threads Kai Li and Andy Bavier Computer Science Department Princeton University http://www.cs.princeton.edu/courses/archive/fall13/cos318

Today s Topics u Concurrency u Processes u Threads u Reminder: l Hope you re all busy implementing Project 1 2

Development Models Linear Iterative Read docs Read docs Design Vs. Design Implement Implement Debug Debug 3

Development Models Linear Iterative Read docs Read docs Design Vs. Design Implement Implement Debug Debug 4

Concurrency and Process u Concurrency l l l Hundreds of jobs going on in a system CPU is shared, as are I/O devices Each job would like to have its own computer u Process concurrency l l l l Decompose complex problems into simple ones Make each simple one a process Deal with one at a time Each process feels like having its own computer u Example: gcc (via gcc pipe v ) launches l /usr/libexec/cpp /usr/libexec/cc1 /usr/libexec/as /usr/libexec/elf/ld l Each instance is a process 5

Process Parallelism u Virtualization l Each process run for a while l Make a CPU into many l Each virtually has its own CPU u I/O parallelism l CPU job overlaps with I/O l Each runs almost as fast as if it has its own computer l Reduce total completion time u CPU parallelism l Multiple CPUs (such as SMP) l Processes running in parallel l Speedup emacs gcc emacs CPU I/O CPU 3s 2s 3s CPU 3s CPU 3s CPU 3s 3s I/O 2s 9s 6

More on Process Parallelism u Process parallelism is common in real life l Each sales person sell $1M annually l Hire 100 sales people to generate $100M revenue u Speedup l Ideal speedup is factor of N l Reality: bottlenecks + coordination overhead u Question l Can you speedup by working with a partner? l Can you speedup by working with 20 partners? l Can you get super-linear (more than a factor of N) speedup? 7

Simplest Process u Sequential execution l No concurrency inside a process l Everything happens sequentially l Some coordination may be required u Process state l Registers l Main memory l I/O devices l File system Communication ports 8

Program and Process main() {... foo()... } main() {... foo()... } heap stack bar() {... } Program bar() {... } Process registers PC 9

Process vs. Program u Process > program l Program is just part of process state l Example: many users can run the same program Each process has its own address space, i.e., even though program has single set of variable names, each process will have different values u Process < program l A program can invoke more than one process l Example: Fork off processes 10

Process Control Block (PCB) u Process management info l State Ready: ready to run Running: currently running Blocked: waiting for resources l Registers, EFLAGS, and other CPU state l Stack, code and data segment l Parents, etc u Memory management info l Segments, page table, stats, etc u I/O and file management l Communication ports, directories, file descriptors, etc. u How OS takes care of processes l Resource allocation and process state transition 11

Primitives of Processes u Creation and termination l Exec, Fork, Wait, Kill u Signals l Action, Return, Handler u Operations l Block, Yield u Synchronization l We will talk about this later 12

Make A Process u Creation l Load code and data into memory l Create an empty call stack l Initialize state to same as after a process switch l Make the process ready to run u Clone l Stop current process and save state l Make copy of current code, data, stack and OS state l Make the process ready to run 13

Example: Unix u How to make processes: l fork clones a process l exec overlays the current process pid = fork() if (pid == 0) { /* child process */ exec( foo ); /* does not return */ } else { /* parent */ wait(pid); /* wait for child to die */ } 14

Fork and Exec in Unix pid = fork() if (pid == 0) exec( foo ); else wait(pid); pid = fork() if (pid == 0) exec( foo ); else wait(pid); pid = fork() if (pid == 0) exec( foo ); else wait(pid); foo: Main() { } Wait 15

More on Fork u Parent process has a PCB and an address space u Create and initialize PCB u Create an address space u Copy the contents of the parent address space to the new address space u Inherit the execution context of the parent u New process is ready New address space PCB Parent address space PCB 16

Process Context Switch u Save a context (everything that a process may damage) l All registers (general purpose and floating point) l All co-processor state l Save all memory to disk? l What about cache and TLB stuff? u Start a context l Does the reverse u Challenge l OS code must save state without changing any state l How to run without touching any registers? CISC machines have a special instruction to save and restore all registers on stack RISC: reserve registers for kernel or have way to carefully save one and then continue 17

Process State Transition Terminate Running Create Ready Blocked Resource becomes available 18

Today s Topics u Concurrency u Processes u Threads 19

Interlude I think there is a profound and enduring beauty in simplicity, in clarity, in efficiency. True simplicity is derived from so much more than just the absence of clutter and ornamentation. It s about bringing order to complexity. -- Sir Jony Ive 20

From Last Time u Q: It says on the slide that Mac OS X is a microkernel, and microkernels are supposed to be more robust, so why does my Mac OS X crash? u Microkernels can still crash l Bugs in the kernel l Cannot recover from a critical OS service crashing u Mac OS X uses XNU hybrid kernel: Mach + BSD l Many claim it s not a true microkernel 21

Threads u Thread l A sequential execution stream within a process (also called lightweight process) l Threads in a process share the same address space u Thread concurrency l Easier to program I/O overlapping with threads than signals l Responsive user interface l Run some program activities in the background l Multiple CPUs sharing the same memory 22

Thread Control Block (TCB) l State Ready: ready to run Running: currently running Blocked: waiting for resources l Registers l Status (EFLAGS) l Program counter (EIP) l Stack l Code 23

Typical Thread API u Creation l Create, Join, Exit u Mutual exclusion l Acquire (lock), Release (unlock) u Condition variables l Wait, Signal, Broadcast 24

Revisit Process u Process l Threads l Address space l Environment for the threads to run on OS (open files, etc) u Simplest process has 1 thread Process 25

Thread Context Switch u Save a context (everything that a thread may damage) l All registers (general purpose and floating point) l All co-processor state l Need to save stack? l What about cache and TLB stuff? u Start a context l Does the reverse u May trigger a process context switch 26

Procedure Call u Caller or callee save some context (same stack) u Caller saved example: save active caller registers call foo foo() { do stuff } restore caller regs 27

Threads vs. Procedures u Threads may resume out of order l Cannot use LIFO stack to save state l Each thread has its own stack u Threads switch less often l Do not partition registers l Each thread has its own CPU u Threads can be asynchronous l Procedure call can use compiler to save state synchronously l Threads can run asynchronously u Multiple threads l Multiple threads can run on multiple CPUs in parallel l Procedure calls are sequential 28

Process vs. Threads u Address space l Processes do not usually share memory l Process context switch changes page table and other memory mechanisms l Threads in a process share the entire address space u Privileges l Processes have their own privileges (file accesses, e.g.) l Threads in a process share all privileges u Question l Do you really want to share the entire address space? 29

Real Operating Systems u One or many address spaces u One or many threads per address space 1 address space Many address spaces 1 thread per address space MSDOS Macintosh Traditional Unix Many threads per address spaces Embedded OS, Pilot VMS, Mach (OS-X), OS/2, Windows NT/XP/Vista, Solaris, HP-UX, Linux 30

Summary u Concurrency l CPU and I/O l Among applications l Within an application u Processes l Abstraction for application concurrency u Threads l Abstraction for concurrency within an application 31