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

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

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

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

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

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

CMPT 300. Introduction to Operating Systems. Operating Systems Processes & Threads

COMPILING OBJECTS AND OTHER LANGUAGE IMPLEMENTATION ISSUES. Credit: Mostly Bryant & O Hallaron

Introduction to Processes

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

Processes. CS3026 Operating Systems Lecture 05

Lecture 4: Process Management

Processes and Non-Preemptive Scheduling. Otto J. Anshus

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

W4118 Operating Systems. Junfeng Yang

Operating System Structure

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

Mon Sep 17, 2007 Lecture 3: Process Management

CSE 153 Design of Operating Systems Fall 2018

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - III Processes. Louisiana State University. Processes. September 1 st, 2009

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

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

Chap 4, 5: Process. Dongkun Shin, SKKU

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

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

Killing Zombies, Working, Sleeping, and Spawning Children

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

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

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

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

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

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

Processes COMPSCI 386

3. Process Management in xv6

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

Computer Systems II. First Two Major Computer System Evolution Steps

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

CS 326: Operating Systems. Process Execution. Lecture 5

! The Process Control Block (PCB) " is included in the context,

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

Processes. Process Scheduling, Process Synchronization, and Deadlock will be discussed further in Chapters 5, 6, and 7, respectively.

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

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

518 Lecture Notes Week 3

CSI Module 2: Processes

SE350: Operating Systems

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

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

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

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.

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

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

The Kernel Abstraction. Chapter 2 OSPP Part I

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CS 201. Processes. Gerson Robboy Portland State University

CSCE 313: Intro to Computer Systems

Today: Process Management. The Big Picture So Far. What's in a Process? Example Process State in Memory

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

CSC 539: Operating Systems Structure and Design. Spring 2006

The Big Picture So Far. Today: Process Management

Operating System Design

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

Processes and Threads

Lecture 4: Memory Management & The Programming Interface

csci3411: Operating Systems

Jan 20, 2005 Lecture 2: Multiprogramming OS

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

Problem Set: Processes

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

Processes. Johan Montelius KTH

Processes. Dr. Yingwu Zhu

Design Overview of the FreeBSD Kernel CIS 657

Problem Set: Processes

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

A process. the stack

Processes. CSE 2431: Introduction to Operating Systems Reading: Chap. 3, [OSC]

CS 3733 Operating Systems

REVIEW OF COMMONLY USED DATA STRUCTURES IN OS

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

Chapter 3: Process Concept

Chapter 3: Process Concept

SMD149 - Operating Systems

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

CS510 Operating System Foundations. Jonathan Walpole

UNIX Processes. by Armin R. Mikler. 1: Introduction

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

Slide 6-1. Processes. Operating Systems: A Modern Perspective, Chapter 6. Copyright 2004 Pearson Education, Inc.

The Kernel. wants to be your friend

Linux Operating System

Process Control. Philipp Koehn. 23 April 2018

Chapter 3: Process Concept

COSC243 Part 2: Operating Systems

Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

Agenda Process Concept Process Scheduling Operations on Processes Interprocess Communication 3.2

MACHINE-LEVEL PROGRAMMING I: BASICS COMPUTER ARCHITECTURE AND ORGANIZATION

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

Operating Systems. II. Processes

Transcription:

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

What is a Process?! A process is a program during execution. Ø Program = static file (image) Ø Process = executing program = program + execution state.! A process is the basic unit of execution in an operating system Ø Each process has a number, its process identifier (pid).! Different processes may run different instances of the same program Ø E.g., my javac and your javac process both run the Java compiler! At a minimum, process execution requires following resources: Ø Memory to contain the program code and data Ø A set of CPU registers to support execution 2

Program to Process! We write a program in e.g., Java.! A compiler turns that program into an instruction list.! The CPU interprets the instruction list (which is more a graph of basic blocks). void X (int b) { if(b == 1) { int main() { } int a = 2; X(a); 3

Process in Memory! Program to process.! What you wrote void X (int b) { if(b == 1) { int main() { } int a = 2; X(a);! What must the OS track for a process?! What is in memory. main; a = 2 X; b = 2 Heap Stack void X (int b) { if(b == 1) { int main() { int a = 2; X(a); } Code 4

Processes and Process Management Details for running a program! A program consists of code and data! On running a program, the loader: Ø reads and interprets the executable file Ø sets up the process s memory to contain the code & data from executable Ø pushes argc, argv on the stack Ø sets the CPU registers properly & calls _start()! Program starts running at _start() _start(args) { initialize_java(); ret = main(args); exit(ret) } we say process is now running, and no longer think of program! When main() returns, OS calls exit() which destroys the process and returns all resources 5

Keeping track of a process! A process has code. Ø OS must track program counter (code location).! A process has a stack. Ø OS must track stack pointer.! OS stores state of processes computation in a process control block (PCB). Ø E.g., each process has an identifier (process identifier, or PID)! Data (program instructions, stack & heap) resides in memory, metadata is in PCB (which is a kernel data structure in memory) 6

Context Switching! The OS periodically switches execution from one process to another! Called a context switch, because the OS saves one execution context and loads another 7

What causes context switches?! Waiting for I/O (disk, network, etc.) Ø Might as well use the CPU for something useful Ø Called a blocked state! Timer interrupt (preemptive multitasking) Ø Even if a process is busy, we need to be fair to other programs! Voluntary yielding (cooperative multitasking)! A few others Ø Synchronization, IPC, etc. 8

Process Life Cycle! Processes are always either executing, waiting to execute or blocked waiting for an event to occur Start Done Ready Running Blocked! A preemptive scheduler will force a transition from running to ready. A non-preemptive scheduler waits. 9

Process Contexts Example: Multiprogramming User Program n... Program 1 OS Program 2 main{ k: read() read{ I/O Device User Program 2 User Program 1 save state startio() schedule() } main{ System Software Operating System Memory k+1: } endio{ schedule() } restore state save state interrupt 10

When a process is waiting for I/O what is its scheduling state? 1. Ready 2. Running 3. Blocked 4. Zombie 5. Exited 11

Scheduling Processes! OS has PCBs for active processes.! OS puts PCB on an appropriate queue. Ø Ready to run queue. Ø Blocked for IO queue (Queue per device). Ø Zombie queue.! Stopping a process and starting another is called a context switch. Ø 100-10,000 per second, so must be fast. 12

Why Use Processes? Consider a Web server get network message (URL) from client fetch URL data from disk compose response send response How well does this web server perform? With many incoming requests? That access data all over the disk? 13

Why Use Processes? Consider a Web server get network message (URL) from client create child process, send it URL Child fetch URL data from disk compose response send response! If server has configuration file open for writing Ø Prevent child from overwriting configuration! How does server know child serviced request? Ø Need return code from child process 14

Where do new processes come from?! Parent/child model! An existing program has to spawn a new one Ø Most OSes have a special init program that launches system services, logon daemons, etc. Ø When you log in (via a terminal or ssh), the login program spawns your shell 15

Approach 1: Windows CreateProcess! In Windows, when you create a new process, you specify a new program Ø And can optionally allow the child to inherit some resources (e.g., an open file handle) 16

Approach 2: Unix fork/exec()! In Unix, a parent makes a copy of itself using fork() Ø Child inherits everything, runs same program Ø Only difference is the return value from fork()! A separate exec() system call loads a new program! Major design trade-off: Ø How easy to inherit Ø Vs. Security (accidentally inheriting something the parent didn t intend) Ø Note that security is a newer concern, and Windows is a newer design 17

The Convenience of separating Fork/Exec! Life with CreateProcess(filename); Ø But I want to close a file in the child. CreateProcess(filename, list of files); Ø And I want to change the child s environment. CreateProcess(filename, CLOSE_FD, new_envp); Ø Etc. (and a very ugly etc.)! fork() = split this process into 2 (new PID) Ø Returns 0 in child Ø Returns pid of child in parent! exec() = overlay this process with new program (PID does not change) 18

The Convenience of Separating Fork/Exec! Decoupling fork and exec lets you do anything to the child s process environment without adding it to the CreateProcess API. int pid = fork(); // create a child If(0 == pid) { // child continues here // Do anything (unmap memory, close net connections ) } exec( program, argc, argv0, argv1, );! fork() creates a child process that inherits: Ø identical copy of all parent s variables & memory Ø identical copy of all parent s CPU registers (except one)! Parent and child execute at the same point after fork() returns: Ø by convention, for the child, fork() returns 0 Ø by convention, for the parent, fork() returns the process identifier of the child Ø fork() return code a convenience, could always use getpid() 19

Program Loading: exec()! The exec() call allows a process to load a different program and start execution at main (actually _start).! It allows a process to specify the number of arguments (argc) and the string argument array (argv).! If the call is successful Ø it is the same process Ø but it runs a different program!!! Code, stack & heap is overwritten Ø Sometimes memory mapped files are preserved. 20

General Purpose Process Creation In the parent process: main() int pid =fork(); // create a child if(0 == pid) { // child continues here exec_status = exec( calc, argc, argv0, argv1, ); printf( Why would I execute? ); } else { // parent continues here } printf( Who s your daddy? ); child_status = wait(pid); 21

A shell forks and then execs a calculator USER OS int pid = fork(); if(pid == 0) { close(.history ); exec( /bin/calc ); } else { wait(pid); pid = 128 127 open files =.history last_cpu = 0 int pid calc_main(){ = fork(); if(pid int q == = 0) 7; { close(.history ); do_init(); exec( /bin/calc ); ln = get_input(); } else exec_in(ln); { wait(pid); Process Control Blocks (PCBs) pid = 128 open files = last_cpu = 0 22

A shell forks and then execs a calculator main; a = 2 Stack Stack USER OS 0xFC0933CA int shell_main() { int a = 2; Code pid = 127 128 open files =.history last_cpu = 0 pid = 128 open files = last_cpu = 0 Heap 0x43178050 Heap int calc_main() { int q = 7; Code Process Control Blocks (PCBs) 23

At what cost, fork()?! Simple implementation of fork(): Ø allocate memory for the child process Ø copy parent s memory and CPU registers to child s Ø Expensive!!! In 99% of the time, we call exec() after calling fork() Ø the memory copying during fork() operation is useless Ø the child process will likely close the open files & connections Ø overhead is therefore high! vfork() Ø a system call that creates a process without creating an identical memory image Ø child process should call exec() almost immediately Ø Unfortunate example of implementation influence on interface Current Linux & BSD 4.4 have it for backwards compatibility Ø Copy-on-write to implement fork avoids need for vfork 24

Orderly Termination: exit()! After the program finishes execution, it calls exit()! This system call: Ø takes the result of the program as an argument Ø closes all open files, connections, etc. Ø deallocates memory Ø deallocates most of the OS structures supporting the process Ø checks if parent is alive: v If so, it holds the result value until parent requests it; in this case, process does not really die, but it enters the zombie/defunct state v If not, it deallocates all data structures, the process is dead Ø cleans up all waiting zombies! Process termination is the ultimate garbage collection (resource reclamation). 25

The wait() System Call! A child program returns a value to the parent, so the parent must arrange to receive that value! The wait() system call serves this purpose Ø it puts the parent to sleep waiting for a child s result Ø when a child calls exit(), the OS unblocks the parent and returns the value passed by exit() as a result of the wait call (along with the pid of the child) Ø if there are no children alive, wait() returns immediately Ø also, if there are zombies waiting for their parents, wait() returns one of the values immediately (and deallocates the zombie) 26

Process Control OS must include calls to enable special control of a process:! Priority manipulation: Ø nice(), which specifies base process priority (initial priority) Ø In UNIX, process priority decays as the process consumes CPU! Debugging support: Ø ptrace(), allows a process to be put under control of another process Ø The other process can set breakpoints, examine registers, etc.! Alarms and time: Ø Sleep puts a process on a timer queue waiting for some number of seconds, supporting an alarm functionality 27

Tying it All Together: The Unix Shell while(! EOF) { read input handle regular expressions int pid = fork(); // create a child if(pid == 0) { // child continues here } exec( program, argc, argv0, argv1, ); else { // parent continues here }! Translates <CTRL-C> to the kill() system call with SIGKILL! Translates <CTRL-Z> to the kill() system call with SIGSTOP! Allows input-output redirections, pipes, and a lot of other stuff that we will see later 28

Summary! Understand what a process is! The high-level idea of context switching and process states! How a process is created! Pros and cons of different creation APIs Ø Intuition of copy-on-write fork and vfork 29