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

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

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

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

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

CS 537 Lecture 2 - Processes

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

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

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

System Calls & Signals. CS449 Spring 2016

System Calls and Signals: Communication with the OS. System Call. strace./hello. Kernel. Context Switch

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

ECE 650 Systems Programming & Engineering. Spring 2018

CSE 153 Design of Operating Systems Fall 2018

Killing Zombies, Working, Sleeping, and Spawning Children

System Programming. Signals I

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

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

COSC243 Part 2: Operating Systems

Lecture 4 Threads. (chapter 4)

Processes. q Process concept q Process model and implementation q Multiprocessing once again q Next Time: Scheduling

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

Lesson 3. The func procedure allows a user to choose the action upon receipt of a signal.

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 18

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

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

Chapter 4 Multithreaded Programming

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

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

Signals. Joseph Cordina

CS240: Programming in C

CSE 451: Operating Systems Winter Processes. Gary Kimura

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

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

Operating Systemss and Multicore Programming (1DT089)

Processes. Sanzheng Qiao. December, Department of Computing and Software

Process Management! Goals of this Lecture!

Unix Processes. What is a Process?

Process Control. Philipp Koehn. 23 April 2018

Processes in linux. What s s a process? process? A dynamically executing instance of a program. David Morgan. David Morgan

518 Lecture Notes Week 3

CS 326: Operating Systems. Process Execution. Lecture 5

CS 355 Operating Systems. Keeping Track of Processes. When are processes created? Process States 1/26/18. Processes, Unix Processes and System Calls

Chapter 1 Introduction

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

Process Management 1

Mon Sep 17, 2007 Lecture 3: Process Management

Operating Systems. Threads and Signals. Amir Ghavam Winter Winter Amir Ghavam

Advanced Unix Concepts. Satyajit Rai

* What are the different states for a task in an OS?

Processes. Johan Montelius KTH

Computer Science 330 Operating Systems Siena College Spring Lab 5: Unix Systems Programming Due: 4:00 PM, Wednesday, February 29, 2012

A process. the stack

APPLIED INFORMATICS Processes. Bash characteristics. Command type. Aliases.

Operating Systemss and Multicore Programming (1DT089)

CS 350 : COMPUTER SYSTEM CONCEPTS SAMPLE TEST 2 (OPERATING SYSTEMS PART) Student s Name: MAXIMUM MARK: 100 Time allowed: 70 minutes

Assignment 1. Teaching Assistant: Michalis Pachilakis (

CSE 421: Introduction to Operating Systems

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

Lecture 4: Process Management

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.

Chap 4, 5: Process. Dongkun Shin, SKKU

KING FAHD UNIVERSITY OF PETROLEUM AND MINERALS Information and Computer Science Department ICS 431 Operating Systems Lab # 6

Processes and Threads

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

Operating Systems. Lecture 05

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 19

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430

Processes. What s s a process? process? A dynamically executing instance of a program. David Morgan

CS 111. Operating Systems Peter Reiher

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

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

Processes. Process Management Chapter 3. When does a process gets created? When does a process gets terminated?

PROCESS CONCEPTS. Process Concept Relationship to a Program What is a Process? Process Lifecycle Process Management Inter-Process Communication 2.

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

CS 111. Operating Systems Peter Reiher

Universidad Carlos III de Madrid Computer Science and Engineering Department Operating Systems Course

SE350: Operating Systems

Processes. Dr. Yingwu Zhu

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

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

System Programming. Introduction to Unix

Processes and Non-Preemptive Scheduling. Otto J. Anshus

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

Operating System Design

CSC209 Fall Karen Reid 1

Process. Signal #8. Signals are software interrupts from unexpected events. a power failure. an alarm clock. the death of a child process

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

Process Management! Goals of this Lecture!

Operating System Structure

Lecture 4: Memory Management & The Programming Interface

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

CSci 4061 Introduction to Operating Systems. (Advanced Control Signals)

CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras

Transcription:

CSE 410: Computer Systems Spring 2018 Processes John Zahorjan zahorjan@cs.washington.edu Allen Center 534

1. What is a process? Processes 2. What's the process namespace? 3. How are processes represented inside the OS? 4. The execution states of a process? 5. How are they created? 6. Making creation fast(er) 7. Shells 8. An example of process-process communication: signals 2018 Gribble, Lazowska, Levy, Zahorjan 2

1. What is a process? The process is the OS s abstraction for execution A process is a program in execution It's the OS-provided higher level abstraction for the hardware CPU and main memory resources E.g., notions of real time are simplified to sequential execution of successive instructions The simplest (classic) case is the sequential process An address space (abstraction of memory) A single thread (abstraction of the CPU) A sequential process is: the unit of execution the unit of scheduling the dynamic (active) execution context compared with program: static, just a bunch of bytes thread address space 2018 Gribble, Lazowska, Levy, Zahorjan 3

What s in a process? A process consists of (at least): an address space, containing the code (instructions) for the running program the data for the running program thread state, consisting of: the program counter (PC), indicating the next instruction the stack pointer register (implying the stack it points to) Other general purpose register values a set of OS resources open files, network connections, sound channels, other process metadata e.g., signal handlers In other words, it s all the stuff you need to run the program or to re-start it, if it s interrupted at some point 2018 Gribble, Lazowska, Levy, Zahorjan 4

: A process s address space (idealized) 0xFFFFFFFF stack (dynamic allocated mem) SP address space 0x00000000 heap (dynamic allocated mem) static data (data segment) code (text segment) PC 2018 Gribble, Lazowska, Levy, Zahorjan 5

2. The process namespace (Like most everything, the particulars on the particular OS) The name for a process is called a process ID (PID) An integer The PID namespace is global to the system Only one process at a time has a particular PID Operations that create processes return a PID e.g., fork(), clone(), exec() Operations on processes take PIDs as an argument e.g., kill(), wait(), nice() 2018 Gribble, Lazowska, Levy, Zahorjan 6

The kernel maintains a data structure to keep track of process state Called the process control block (PCB) OS keeps all of a process s hardware execution state in the PCB when the process isn t running PC, SP, registers, etc. 3. Processes in the OS When a process is unscheduled, the state is transferred out of the hardware into the PCB (when a process is running, its state is spread between the PCB and the CPU) Note: It s natural to think that there must be some esoteric techniques being used fancy data structures that d you d never think of yourself Wrong! It s pretty much just what you d think of! 2018 Gribble, Lazowska, Levy, Zahorjan 7

The PCB The PCB is a data structure with many, many fields: process ID (PID) parent process ID execution state program counter, stack pointer, registers address space info user id (uid) group id (gid) scheduling priority accounting info pointers for use in state queues In Linux: defined in task_struct (include/linux/sched.h) over 95 fields!!! 2018 Gribble, Lazowska, Levy, Zahorjan 8

PCBs and hardware state When a process is running, its hardware state is loaded on a CPU PC, SP, registers CPU contains current values When a process is transitioned to the waiting state, the OS saves the register values in the PCB when the OS returns the process to the running state, it loads the hardware registers from the values in that process s PCB The act of switching a CPU from one process to another is called a context switch timesharing systems may do 100s or 1000s of switches/sec. takes a few microseconds on today s hardware Choosing which process to run next is called scheduling 2018 Gribble, Lazowska, Levy, Zahorjan 9

Process ID Pointer to parent List of children This is (a simplification of) what each of those PCBs looks like inside! Process state Pointer to address space descriptor Program counter stack pointer (all) register values uid (user id) gid (group id) euid (effective user id) Open file list Scheduling priority Accounting info Pointers for state queues Exit ( return ) code value 2018 Gribble, Lazowska, Levy, Zahorjan 10

4. Process execution states Each process has an execution state, which indicates what it is currently doing Ready: waiting to be assigned to a CPU could run, but another process has the CPU Running: executing on a CPU is the process that currently controls the CPU pop quiz: how many processes can be running simultaneously? Waiting (aka blocked ): waiting for an event, e.g., I/O completion cannot make progress until event happens As a process executes, it moves from state to state UNIX: run ps, STAT column shows current state which state is a process in most of the time? 2018 Gribble, Lazowska, Levy, Zahorjan 11

States of a process (slightly simplified) running dispatch / schedule interrupt (unschedule) ready interrupt (I/O complete) exception (I/O, page fault, etc.) blocked 2018 Gribble, Lazowska, Levy, Zahorjan 12

State queues The OS maintains a collection of queues that represent the state of all processes in the system typically one queue for each state e.g., ready, waiting, Each PCB is queued onto a state queue according to the current state of the process it represents as a process changes state, its PCB is unlinked from one queue, and linked onto another Once again, this is just as straightforward as it sounds! The PCBs are moved among queues, which are represented as linked lists. There is no magic! 2018 Gribble, Lazowska, Levy, Zahorjan 13

State queues These are PCBs! Ready queue header head ptr tail ptr chrome (1365) emacs (948) ls (1470) Wait queue header head ptr tail ptr cat (1468) chrome (1207) There may be many wait queues, one for each type of wait (particular device, timer, message, ) 2018 Gribble, Lazowska, Levy, Zahorjan 14

PCBs and state queues PCBs are data structures dynamically allocated inside OS memory When a process is created: OS allocates a PCB for it OS initializes PCB OS puts PCB on the correct queue As a process computes: OS moves its PCB from queue to queue When a process is terminated: PCB may hang around for a while (exit code ) What is the process state? Eventually, OS deallocates its PCB 2018 Gribble, Lazowska, Levy, Zahorjan 15

5. Process creation New processes are created by existing processes creator is called the parent created process is called the child UNIX: do ps, look for PPID field what creates the first process, and when? 2018 Gribble, Lazowska, Levy, Zahorjan 16

2018 Gribble, Lazowska, Levy, Zahorjan 17

Process Creation Semantics (Depending on the OS) child processes inherit certain attributes of the parent Examples: pid/gid: implies authorization of child Open file table: implies stdin/stdout/stderr Environment variables other metadata On some systems, resource allocation to parent may be divided among children Hierarchical resource allocation limits impact of your activity on mine 2018 Gribble, Lazowska, Levy, Zahorjan 18

UNIX process creation details UNIX process creation through fork() system call creates and initializes a new PCB creates a new address space initializes new address space with a copy of the entire contents of the address space of the parent initializes kernel resources of new process with resources of parent (e.g., open files) places new PCB on the ready queue the fork() system call returns twice once into the parent, and once into the child returns the child s PID to the parent returns 0 to the child fork() = clone me (We'll see why in a minute...) 2018 Gribble, Lazowska, Levy, Zahorjan 19

testparent use of fork( ) #include <sys/types.h> #include <unistd.h> #include <stdio.h> int main(int argc, char **argv) { char *name = argv[0]; int pid = fork(); if (pid == 0) { printf( Child of %s is %d\n, name, pid); return 0; } else { printf( My child is %d\n, pid); return 0; } } 2018 Gribble, Lazowska, Levy, Zahorjan 20

testparent output spinlock% gcc -o testparent testparent.c spinlock%./testparent My child is 486 Child of testparent is 0 spinlock%./testparent Child of testparent is 0 My child is 571 2018 Gribble, Lazowska, Levy, Zahorjan 21

fork() exec() Q: So how do we start a new program, instead of just forking the old program? A: first fork, then exec int exec(char * prog, char * argv[]) (actually, there are many flavors of exec) stops the current process loads program prog into the address space i.e., overwrites existing process image initializes hardware context, args for new program places PCB onto ready queue note: does not create a new process! To run a new program: fork() Child process does an exec() (parent either waits for child to complete, or not) 2018 Gribble, Lazowska, Levy, Zahorjan 22

6. Making Creation Fast(er) The semantics of fork() say the child's address space is a copy of the parent's Implementing fork() that way is slow: Have to allocate physical memory for the new address space Have to copy parent's address space contents into child's address space Have to set up child's page tables to map new address space We can speed this up... 2018 Gribble, Lazowska, Levy, Zahorjan 23

Method 1: vfork() vfork() is the older of the two approaches talked about here It's an instance of changing the problem definition into something we can implement efficiently Instead of child address space is a copy of parent's, the semantics are child address space is the parent's With a promise that the child won't modify the address space before doing an exec() This is unenforced. You use vfork() at your own peril. When exec() is called, a new address space is created, new page tables set up for it, and it's loaded with the new executable This saves the wasted effort of duplicating the parent's address space (setting up page tables and copying contents) when the child is just going to exec() anyway (which is common) 2018 Gribble, Lazowska, Levy, Zahorjan 24

Method 2: copy-on-write This approach retains the original semantics, but copies only what is necessary, rather than the entire address space On fork(): Create a new address space Initialize its page tables to the same mappings as the parent's (i.e., they both point to the same physical memory) No copying of address space contents have occurred to this point Set both parent and child page tables to make all pages read-only If either the parent or child writes to memory, a protection fault occurs When the fault occurs: Allocate a new physical frame for the child, and point its page table entry at it Copy the current contents of the parent address space to that frame Mark the entries in both the parent's and child's address space writable for that page Restart the process doing the write, re-executing the write instruction The result: only pages modified by the parent or child ever end up being copied 2018 Gribble, Lazowska, Levy, Zahorjan 25

7. UNIX shells $./myprog int main(int argc, char **argv) { } while (1) { printf( $ ); } char *cmd = get_next_command(); int pid = fork(); if (pid == 0) { exec(cmd); panic( exec failed! ); } else { } wait(pid); 2018 Gribble, Lazowska, Levy, Zahorjan 26

Input/Output Redirection $./myprog <input.txt >output.txt # UNIX Each process has an open file table by (universal) convention: 0: stdin 1: stdout 2: stderr a child process inherits the parent s open file table Redirection: open files before executing child process code 2018 Gribble, Lazowska, Levy, Zahorjan 27

UNIX shells: input/output redirection $ foo myfile.txt <input.txt >output.txt int main(int argc, char **argv) { } while (1) { printf( $ ); } char *cmd = get_next_command(); int pid = fork(); if (pid == 0) { manipulate stdin/stdout/stderr exec(cmd); panic( exec failed! ); } else { } wait(pid); 2018 Gribble, Lazowska, Levy, Zahorjan 28

More Note that redirection is completely transparent to the child process What about $./myprog >>output.txt $./myprog >output.txt 2>&1 $./myprog less $./myprog & 2018 Gribble, Lazowska, Levy, Zahorjan 29

8. Process-process communcation via signals Processes can register event handlers Feels a lot like event handlers in Java, which... Feel sort of like catch blocks in Java programs When the event occurs, process asynchronous jumps to event handler routine Used to catch exceptions Also used for inter-process communication (IPC): a process can trigger an event in another one using signal 2018 Gribble, Lazowska, Levy, Zahorjan 30

Signals Signal Value Action Comment ------------------------------------------------------------------------ SIGHUP 1 Term Hangup detected on controlling terminal or death of controlling process SIGINT 2 Term Interrupt from keyboard SIGQUIT 3 Core Quit from keyboard SIGILL 4 Core Illegal Instruction SIGABRT 6 Core Abort signal from abort(3) SIGFPE 8 Core Floating point exception SIGKILL 9 Term Kill signal SIGSEGV 11 Core Invalid memory reference SIGPIPE 13 Term Broken pipe: write to pipe with no readers SIGALRM 14 Term Timer signal from alarm(2) SIGTERM 15 Term Termination signal SIGUSR1 30,10,16 Term User-defined signal 1 SIGUSR2 31,12,17 Term User-defined signal 2 SIGCHLD 20,17,18 Ign Child stopped or terminated SIGCONT 19,18,25 Continue if stopped SIGSTOP 17,19,23 Stop Stop process SIGTSTP 18,20,24 Stop Stop typed at tty SIGTTIN 21,21,26 Stop tty input for background process SIGTTOU 22,22,27 Stop tty output for background process 2018 Gribble, Lazowska, Levy, Zahorjan 31

Example Use You're implementing Apache, a web server Apache reads a configuration file when it is launched Controls things like what the root directory of the web files is, what permissions there are on pieces of it, etc. Suppose you want to change the configuration while Apache is running If you restart the currently running Apache, you drop some unknown number of user connections Solution: send the running Apache process a signal It has registered an signal handler that gracefully re-reads the configuration file 2018 Gribble, Lazowska, Levy, Zahorjan 32