CSE 153 Design of Operating Systems Fall 2018

Similar documents
CS153: Process 2. Chengyu Song. Slides modified from Harsha Madhyvasta, Nael Abu-Ghazaleh, and Zhiyun Qian

CSE 120 Principles of Operating Systems

CSE 120 Principles of Operating Systems

CSE 120 Principles of Operating Systems

Lecture 4: Threads; weaving control flow

CS 153 Design of Operating Systems Spring 18

CS 153 Design of Operating Systems Winter 2016

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

Lecture 2: Processes. CSE 120: Principles of Opera9ng Systems. UC San Diego: Summer Session I, 2009 Frank Uyeda

CS 318 Principles of Operating Systems

Reading Assignment 4. n Chapter 4 Threads, due 2/7. 1/31/13 CSE325 - Processes 1

CS 318 Principles of Operating Systems

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

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

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

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

Lecture 4: Process Management

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

CSCC69H3. Operating Systems Sina Meraji U of T

THREADS. Jo, Heeseung

Computer Systems Laboratory Sungkyunkwan University

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

W4118 Operating Systems. Junfeng Yang

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSCE 313: Intro to Computer Systems

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

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

Processes & Threads. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! More of the same J

CSE 153 Design of Operating Systems Fall 2018

Operating System Structure

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS510 Operating System Foundations. Jonathan Walpole

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

Threads. Computer Systems. 5/12/2009 cse threads Perkins, DW Johnson and University of Washington 1

CS 326: Operating Systems. Process Execution. Lecture 5

COMP 3100 Operating Systems

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

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

CS 537 Lecture 2 - Processes

Processes and Threads

Processes and Threads

Processes. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! Scheduling processes

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

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

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

csci3411: Operating Systems

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

Computer Systems Assignment 2: Fork and Threads Package

SE350: Operating Systems

From Processes to Threads

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

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

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

Windows architecture. user. mode. Env. subsystems. Executive. Device drivers Kernel. kernel. mode HAL. Hardware. Process B. Process C.

Chapter 3 Process Description and Control

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

Multithreaded Programming

fork System-Level Function

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

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

Prepared by Prof. Hui Jiang Process. Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University

Process. Prepared by Prof. Hui Jiang Dept. of EECS, York Univ. 1. Process in Memory (I) PROCESS. Process. How OS manages CPU usage? No.

What Is A Process? Process States. Process Concept. Process Control Block (PCB) Process State Transition Diagram 9/6/2013. Process Fundamentals

CSC 539: Operating Systems Structure and Design. Spring 2006

Processes & Threads. (Chapter 3) CS 4410 Operating Systems. [R. Agarwal, L. Alvisi, A. Bracy, M. George, E. Sirer, R. Van Renesse]

Princeton University Computer Science 217: Introduction to Programming Systems. Process Management

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

CSE 4/521 Introduction to Operating Systems

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

Chapter 3: Process Concept

Chapter 3: Process Concept

Processes, Threads and Processors

CISC2200 Threads Spring 2015

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

Killing Zombies, Working, Sleeping, and Spawning Children

Process Description and Control

Agenda Process Concept Process Scheduling Operations on Processes Interprocess Communication 3.2

Disciplina Sistemas de Computação

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

Processes. Process Concept

Operating System Design

Chapter 4: Processes. Process Concept

Lecture 2 Process Management

CSE 153 Design of Operating Systems

Chapter 4: Processes

CSci 4061 Introduction to Operating Systems. Processes in C/Unix

CMPSC 311- Introduction to Systems Programming Module: Concurrency

Notes. CS 537 Lecture 5 Threads and Cooperation. Questions answered in this lecture: What s in a process?

Princeton University. Computer Science 217: Introduction to Programming Systems. Process Management

Page 1. Analogy: Problems: Operating Systems Lecture 7. Operating Systems Lecture 7

Part Two - Process Management. Chapter 3: Processes

Process Management! Goals of this Lecture!

Chapter 3: Process Concept

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

CS 3305 Intro to Threads. Lecture 6

518 Lecture Notes Week 3

CS Advanced Operating Systems Structures and Implementation Lecture 5. Processes. Goals for Today

For use by students enrolled in #71251 CSE430 Fall 2012 at Arizona State University. Do not use if not enrolled.

Threads, SMP, and Microkernels

Transcription:

CSE 153 Design of Operating Systems Fall 2018 Lecture 4: Processes (2) Threads

Process Creation: Unix In Unix, processes are created using fork() int fork() fork() Creates and initializes a new PCB Creates a new address space Initializes the address space with a copy of the entire contents of the address space of the parent Initializes the kernel resources to point to the resources used by parent (e.g., open files) Places the PCB on the ready queue Fork returns twice Returns the child s PID to the parent, 0 to the child CSE 153 Lecture 5 Processes (II) 2

fork() int main(int argc, char *argv[]) { char *name = argv[0]; int child_pid = fork(); if (child_pid == 0) { printf( Child of %s is %d\n, name, getpid()); return 0; else { printf( My child is %d\n, child_pid); return 0; What does this program print? CSE 153 Lecture 5 Processes (II) 3

Example Output [well ~]$ gcc t.c [well ~]$./a.out My child is 486 Child of a.out is 486 CSE 153 Lecture 5 Processes (II) 4

Duplicating Address Spaces child_pid = 486 child_pid = 0 PC child_pid = fork(); if (child_pid == 0) { child_pid = fork(); if (child_pid == 0) { PC printf( child ); printf( child ); else { else { printf( parent ); printf( parent ); Parent Child CSE 153 Lecture 5 Processes (II) 5

Divergence child_pid = 486 child_pid = 0 child_pid = fork(); child_pid = fork(); if (child_pid == 0) { if (child_pid == 0) { printf( child ); printf( child ); PC else { else { PC printf( parent ); printf( parent ); Parent Child CSE 153 Lecture 5 Processes (II) 6

Example Continued [well ~]$ gcc t.c [well ~]$./a.out My child is 486 Child of a.out is 486 [well ~]$./a.out Child of a.out is 498 My child is 498 Why is the output in a different order? CSE 153 Lecture 5 Processes (II) 7

Why fork()? Very useful when the child Is cooperating with the parent Relies upon the parent s data to accomplish its task Example: Web server while (1) { int sock = accept(); if ((child_pid = fork()) == 0) { Handle client request else { Close socket CSE 153 Lecture 5 Processes (II) 8

Process Creation: Unix (2) Wait a second. How do we actually start a new program? int exec(char *prog, char *argv[]) exec() Stops the current process Loads the program prog into the process address space Initializes hardware context and args for the new program Places the PCB onto the ready queue Note: It does not create a new process What does it mean for exec to return? What does it mean for exec to return with an error? CSE 153 Lecture 5 Processes (II) 9

Process Creation: Unix (3) fork() is used to create a new process, exec is used to load a program into the address space What happens if you run exec csh in your shell? What happens if you run exec ls in your shell? Try it. fork() can return an error. Why might this happen? CSE 153 Lecture 5 Processes (II) 10

Process Termination All good processes must come to an end. But how? Unix: exit(int status), NT: ExitProcess(int status) Essentially, free resources and terminate Terminate all threads (next lecture) Close open files, network connections Allocated memory (and VM pages out on disk) Remove PCB from kernel data structures, delete Note that a process does not need to clean up itself OS will handle this on its behalf CSE 153 Lecture 5 Processes (II) 11

wait() a second Often it is convenient to pause until a child process has finished Think of executing commands in a shell Use wait() (WaitForSingleObject) Suspends the current process until a child process ends waitpid() suspends until the specified child process ends Wait has a return value what is it? Unix: Every process must be reaped by a parent What happens if a parent process exits before a child? What do you think is a zombie process? CSE 153 Lecture 5 Processes (II) 12

Unix Shells while (1) { char *cmd = read_command(); int child_pid = fork(); if (child_pid == 0) { Manipulate STDIN/OUT/ERR file descriptors for pipes, redirection, etc. exec(cmd); panic( exec failed ); else { if (!(run_in_background)) waitpid(child_pid); CSE 153 Lecture 5 Processes (II) 13

Processes: check your understanding What are the units of execution? Processes How are those units of execution represented? Process Control Blocks (PCBs) How is work scheduled in the CPU? Process states, process queues, context switches What are the possible execution states of a process? Running, ready, waiting, How does a process move from one state to another? Scheduling, I/O, creation, termination How are processes created? CreateProcess (NT), fork/exec (Unix) CSE 153 Lecture 5 Processes (II) 14

Processes P1 P2 Recall that A process includes:» An address space (defining all the code and data pages)» OS resources (e.g., open files) and accounting info» Execution state (PC, SP, regs, etc.)» PCB to keep track of everything OS Processes are completely isolated from each other CSE 153 Lecture 6 Threads 15

Some issues with processes Creating a new process is costly because of new address space and data structures that must be allocated and initialized Recall struct proc in xv6 or Solaris Communicating between processes is costly because most communication goes through the OS Inter Process Communication (IPC) we will discuss later Overhead of system calls and copying data CSE 153 Lecture 6 Threads 16

Parallel Programs l Also recall our Web server example that forks off copies of itself to handle multiple simultaneous requests l To execute these programs we need to Create several processes that execute in parallel Cause each to map to the same address space to share data» They are all part of the same computation Have the OS schedule these processes in parallel l This situation is very inefficient (CoW helps) Space: PCB, page tables, etc. Time: create data structures, fork and copy addr space, etc. CSE 153 Lecture 6 Threads 17

Rethinking Processes What is similar in these cooperating processes? They all share the same code and data (address space) They all share the same privileges They all share the same resources (files, sockets, etc.) What don t they share? Each has its own execution state: PC, SP, and registers Key idea: Separate resources from execution state Exec state also called thread of control, or thread CSE 153 Lecture 6 Threads 18

Recap: Process Components Per- Process State Per- Thread State A process is named using its process ID (PID) A process contains all of the state for a program in execution An address space The code for the executing program The data for the executing program A set of operating system resources» Open files, network connections, etc. An execution stack encapsulating the state of procedure calls The program counter (PC) indicating the next instruction A set of general-purpose registers with current values Current execution state (Ready/Running/Waiting) CSE 153 Lecture 6 Threads 19

Threads l Separate execution and resource container roles The thread defines a sequential execution stream within a process (PC, SP, registers) The process defines the address space, resources, and general process attributes (everything but threads) l Threads become the unit of scheduling Processes are now the containers in which threads execute Processes become static, threads are the dynamic entities CSE 153 Lecture 6 Threads 20

Recap: Process Address Space 0xFFFFFFFF Stack SP Address Space Heap (Dynamic Memory Alloc) Static Data (Data Segment) Code (Text Segment) PC 0x00000000 CSE 153 Lecture 6 Threads 21

Threads in a Process Stack (T1) Thread 1 Thread 2 Stack (T2) Stack (T3) Thread 3 PC (T2) Heap Static Data Code PC (T3) PC (T1) CSE 153 Lecture 6 Threads 22

Thread Design Space Address Space One Thread/Process One Address Space (MSDOS) One Thread/Process Many Address Spaces (Early Unix) Thread Many Threads/Process One Address Space (Pilot, Java) Many Threads/Process Many Address Spaces (Mac OS, Unix, Windows) CSE 153 Lecture 6 Threads 23

Process/Thread Separation Separating threads and processes makes it easier to support multithreaded applications Concurrency does not require creating new processes Concurrency (multithreading) can be very useful Improving program structure Handling concurrent events (e.g., Web requests) Writing parallel programs So multithreading is even useful on a uniprocessor CSE 153 Lecture 6 Threads 24

Threads: Concurrent Servers Using fork() to create new processes to handle requests in parallel is overkill for such a simple task Recall our forking Web server: while (1) { int sock = accept(); if ((child_pid = fork()) == 0) { Handle client request Close socket and exit else { Close socket CSE 153 Lecture 6 Threads 25

Threads: Concurrent Servers Instead, we can create a new thread for each request web_server() { while (1) { int sock = accept(); thread_fork(handle_request, sock); handle_request(int sock) { Process request close(sock); CSE 153 Lecture 6 Threads 26

Implementing threads l Kernel Level Threads l All thread operations are implemented in the kernel The OS schedules all of the threads in the system Don t have to separate from processes l OS-managed threads are called kernel-level threads or lightweight processes Windows: threads Solaris: lightweight processes (LWP) POSIX Threads (pthreads): PTHREAD_SCOPE_SYSTEM CSE 153 Lecture 6 Threads 27

Kernel Thread (KLT) Limitations l KLTs make concurrency cheaper than processes u Much less state to allocate and initialize l However, there are a couple of issues u u Issue 1: KLT overhead still high» Thread operations still require system calls» Ideally, want thread operations to be as fast as a procedure call Issue 2: KLTs are general; unaware of application needs l Alternative: User-level threads (ULT) CSE 153 Lecture 6 Threads 28

Alternative: User-Level Threads Implement threads using user-level library ULTs are small and fast A thread is simply represented by a PC, registers, stack, and small thread control block (TCB) Creating a new thread, switching between threads, and synchronizing threads are done via procedure call» No kernel involvement User-level thread operations 100x faster than kernel threads pthreads: PTHREAD_SCOPE_PROCESS CSE 153 Lecture 6 Threads 29