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

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

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

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

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

System Calls & Signals. CS449 Spring 2016

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

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

System Programming. Signals I

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

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

ECE 650 Systems Programming & Engineering. Spring 2018

COSC243 Part 2: Operating Systems

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 18

Killing Zombies, Working, Sleeping, and Spawning Children

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

CSE 153 Design of Operating Systems Fall 2018

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

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

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

CS240: Programming in C

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

Process Control. Philipp Koehn. 23 April 2018

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

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

Signals. Joseph Cordina

Lecture 4 Threads. (chapter 4)

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

518 Lecture Notes Week 3

Chapter 4 Multithreaded Programming

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

CSE 451: Operating Systems Winter Processes. Gary Kimura

CS 326: Operating Systems. Process Execution. Lecture 5

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

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

Mon Sep 17, 2007 Lecture 3: Process Management

(In columns, of course.)

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

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 19

Processes. Johan Montelius KTH

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

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

A process. the stack

Process Management! Goals of this Lecture!

Advanced Unix Concepts. Satyajit Rai

Processes. Dr. Yingwu Zhu

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

Unix Processes. What is a Process?

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

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

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

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

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

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

Process Management 1

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

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

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

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

Design Overview of the FreeBSD Kernel CIS 657

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

Chap 4, 5: Process. Dongkun Shin, SKKU

Lecture 4: Process Management

Processes and Threads

10/10/ Gribble, Lazowska, Levy, Zahorjan 2. 10/10/ Gribble, Lazowska, Levy, Zahorjan 4

Operating Systems. Lecture 05

CSE 421: Introduction to Operating Systems

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

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

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

Operating System Structure

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

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

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

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

What s in a traditional process? Concurrency/Parallelism. What s needed? CSE 451: Operating Systems Autumn 2012

CS 111. Operating Systems Peter Reiher

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Chapter 1 Introduction

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

Assignment 1. Teaching Assistant: Michalis Pachilakis (

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

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

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

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

What s in a process?

CS 111. Operating Systems Peter Reiher

THE PROCESS ABSTRACTION. CS124 Operating Systems Winter , Lecture 7

Operating Systemss and Multicore Programming (1DT089)

Most of the work is done in the context of the process rather than handled separately by the kernel

Exceptions and Processes

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

Transcription:

Process management CSE 451: Operating Systems Spring 2012 Module 4 Processes Ed Lazowska lazowska@cs.washington.edu Allen Center 570 This module begins a series of topics on processes, threads, and synchronization this is the most important part of the class there definitely will be several questions on these topics on the midterm In this module: processes and process management What is a process? What s the OS s process name? How are processes represented inside the OS? What are the executing states of a process? How are processes created? How can this be made faster? Shells Signals 2012 Gribble, Lazowska, Levy, Zahorjan 2 What is a process? The process is the OS s abstraction for execution A process is a program in execution Simplest (classic) case: a sequential process An (an abstraction of memory) A single thread of execution (an abstraction of the CPU) A sequential process is: The unit of execution thread The unit of scheduling The dynamic (active) execution context vs. the program static, just a bunch of bytes What s in a process? A process consists of (at least): An, containing the code (instructions) for the running program the data for the running program (static data, heap data, CPU state, consisting of The program counter (PC), indicating the next instruction The stack pointer Other general purpose register values A set of OS resources open files, network connections, sound channels, 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 2012 Gribble, Lazowska, Levy, Zahorjan 3 2012 Gribble, Lazowska, Levy, Zahorjan 4 A process s (idealized) The OS s process name 0xFFFFFFFF 0x00000000 stack (dynamic allocated mem) heap (dynamic allocated mem) static data (data segment) code (text segment) SP PC (Like most things, the particulars depend on the specific OS, but the principles are general) The name for a process is called a process ID (PID) An integer The PID name 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() Operations on processes take PIDs as an argument E.g., kill(), wait(), nice() 2012 Gribble, Lazowska, Levy, Zahorjan 5 2012 Gribble, Lazowska, Levy, Zahorjan 6 1

Representation of processes by the OS The OS maintains a data structure to keep track of a process s state Called the process control block () or process descriptor Identified by the PID OS keeps all of a process s execution state in (or linked from) the when the process isn t running PC, SP, registers, etc. when a process is unscheduled, the state is transferred out of the hardware into the (when a process is running, its state is spread between the and the CPU) Note: It s natural to think that there must be some esoteric techniques being used fancy data structures that you d never think of yourself Wrong! It s pretty much just what you d think of! 2012 Gribble, Lazowska, Levy, Zahorjan 7 The The is a data structure with many, many fields: process ID (PID) parent process ID execution state program counter, stack pointer, registers info UNIX user id, group id scheduling priority accounting info pointers for state queues In Linux: defined in task_struct (include/linux/sched.h) over 95 fields!!! 2012 Gribble, Lazowska, Levy, Zahorjan 8 s and CPU state When a process is running, its CPU state is inside the CPU PC, SP, registers CPU contains current values When the OS gets control because of a Trap: Program executes a syscall Exception: Program does something unexpected (e.g., page fault) Interrupt: A hardware device requests service the OS saves the CPU state of the running process in that process s When the OS returns the process to the running state, it loads the hardware registers with values from that process s general purpose registers, stack pointer, instruction pointer The act of switching the CPU from one process to another is called a context switch 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 2012 Gribble, Lazowska, Levy, Zahorjan 9 2012 Gribble, Lazowska, Levy, Zahorjan 10 The OS kernel is not a process It s just a block of code! (In a microkernel OS, many things that you normally think of as the operating system execute as usermode processes. But the OS kernel is just a block of code.) Process ID Pointer to parent List of children Process state Pointer to descriptor Program counter stack pointer (all) register values uid (user id) gid (group id) euid (effective user id) This is (a simplification of) what each of those s looks like inside! Open file list Scheduling priority Accounting info Pointers for state queues Exit ( return ) code value 2012 Gribble, Lazowska, Levy, Zahorjan 11 2012 Gribble, Lazowska, Levy, Zahorjan 12 2

Process execution states Process states and state transitions Each process has an execution state, which indicates what it s currently doing ready: waiting to be assigned to a CPU could run, but another process has the CPU running: executing on a CPU it s the process that currently controls the CPU waiting (aka blocked ): waiting for an event, e.g., I/O completion, or a message from (or the completion of) another process cannot make progress until the 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? create dispatch / schedule interrupt (I/O complete) running ready blocked interrupt (unschedule) terminate trap or exception (I/O, page fault, etc.) You can create and destroy processes! 2012 Gribble, Lazowska, Levy, Zahorjan 13 2012 Gribble, Lazowska, Levy, Zahorjan 14 State queues State queues These are s! 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 is queued onto a state queue according to the current state of the process it represents as a process changes state, its is unlinked from one queue, and linked onto another Once again, this is just as straightforward as it sounds! The s are moved between queues, which are represented as linked lists. There is no magic! Ready queue header head ptr tail ptr Wait queue header head ptr tail ptr firefox (1365) emacs (948) ls (1470) cat (1468) firefox (1207) There may be many wait queues, one for each type of wait (particular device, timer, message, ) 2012 Gribble, Lazowska, Levy, Zahorjan 15 2012 Gribble, Lazowska, Levy, Zahorjan 16 s and state queues s are data structures dynamically allocated inside OS memory When a process is created: OS allocates a for it OS initializes (OS does other things not related to the ) OS puts on the correct queue As a process computes: OS moves its from queue to queue When a process is terminated: may be retained for a while (to receive signals, etc.) eventually, OS deallocates the 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? 2012 Gribble, Lazowska, Levy, Zahorjan 17 2012 Gribble, Lazowska, Levy, Zahorjan 18 3

Process creation semantics (Depending on the OS) child processes inherit certain attributes of the parent Examples: Open file table: implies stdin/stdout/stderr On some systems, resource allocation to parent may be divided among children (In Unix) when a child is created, the parent may either wait for the child to finish, or continue in parallel 2012 Gribble, Lazowska, Levy, Zahorjan 19 2012 Gribble, Lazowska, Levy, Zahorjan 20 UNIX process creation details UNIX process creation through fork() system call creates and initializes a new initializes kernel resources of new process with resources of parent (e.g., open files) initializes PC, SP to be same as parent creates a new initializes new with a copy of the entire contents of the of the parent places new 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 2012 Gribble, Lazowska, Levy, Zahorjan 21 2012 Gribble, Lazowska, Levy, Zahorjan 22 similar, but different in key ways testparent use of fork( ) #include <sys/types.h> #include <unistd.h> #include <stdio.h> identical copy (with sole exception of PID argument on the top of the address int main(int argc, char **argv) { char *name = argv[0]; int pid = fork(); if (pid == 0) { printf( of %s is %d\n, name, pid); return 0; else { printf( My child is %d\n, pid); return 0; 2012 Gribble, Lazowska, Levy, Zahorjan 23 2012 Gribble, Lazowska, Levy, Zahorjan 24 4

testparent output spinlock% gcc -o testparent testparent.c spinlock%./testparent My child is 486 of testparent is 0 spinlock%./testparent of testparent is 0 My child is 571 exec() vs. fork() 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[]) exec() stops the current process loads program prog into the i.e., over-writes the existing process image initializes hardware context, args for new program places onto ready queue note: does not create a new process! 2012 Gribble, Lazowska, Levy, Zahorjan 25 2012 Gribble, Lazowska, Levy, Zahorjan 26 So, to run a new program: fork() process does an exec() either waits for the child to complete, or not similar, but different in key ways identical copy (with sole exception of PID argument on the top of the address 2012 Gribble, Lazowska, Levy, Zahorjan 27 2012 Gribble, Lazowska, Levy, Zahorjan 28 address Making process creation faster The semantics of fork() say the child s is a copy of the parent s Implementing fork() that way is slow Have to allocate physical memory for the new Have to set up child s page tables to map new address Have to copy parent s contents into child s Which you are likely to immediately blow away with an exec() 2012 Gribble, Lazowska, Levy, Zahorjan 29 2012 Gribble, Lazowska, Levy, Zahorjan 30 5

Method 1: vfork() vfork() is the older (now uncommon) of the two approaches we ll discuss Instead of child s is a copy of the parent s, the semantics are child s is the parent s With a promise that the child won t modify the before doing an execve() Unenforced! You use vfork() at your own peril When execve() is called, a new is created and it s loaded with the new executable is blocked until execve() is executed by child Saves wasted effort of duplicating parent s, just to blow it away similar, but different in key ways Vfork() 2012 Gribble, Lazowska, Levy, Zahorjan 31 2012 Gribble, Lazowska, Levy, Zahorjan 32 Method 2: copy-on-write Retains the original semantics, but copies only what is necessary rather than the entire On fork(): Create a new Initialize page tables with same mappings as the parent s (i.e., they both point to the same physical memory) No copying of contents have occurred at this point with the sole exception of the top page of the stack Set both parent and child page tables to make all pages read-only If either parent or child writes to memory, an exception occurs When exception occurs, OS copies the page, adjusts page tables, etc. 2012 Gribble, Lazowska, Levy, Zahorjan 33 UNIX shells 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); 2012 Gribble, Lazowska, Levy, Zahorjan 34 Truth in advertising In Linux today, clone is replacing fork (and vfork) clone has additional capabilities/options But you need to clearly understand fork as described here In Linux today, exec is not a system call; execve is the only exec-like system call execve knows whether you have done a fork or a vfork by a flag in the But you need to clearly understand exec as described here 2012 Gribble, Lazowska, Levy, Zahorjan 35 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: the shell copies its current stdin/stdout open file entries opens input.txt as stdin and output.txt as stdout fork restore original stdin/stdout 2012 Gribble, Lazowska, Levy, Zahorjan 36 6

Inter-process communication 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 jumps to event handler routine Used to catch exceptions Also used for inter-process (process-to-process) communication A process can trigger an event in another process using signal 2012 Gribble, Lazowska, Levy, Zahorjan 37 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 read 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 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 2012 Gribble, Lazowska, Levy, Zahorjan 38 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 2012 Gribble, Lazowska, Levy, Zahorjan 39 7