Carnegie Mellon. Processes. Lecture 12, May 19 th Alexandre David. Credits to Randy Bryant & Dave O Hallaron from Carnegie Mellon

Similar documents
Processes: Introduction. CS 241 February 13, 2012

Exceptional Control Flow: Exceptions and Processes

University of Washington What is a process?

Today. Exceptional Control Flow Processes. Exceptions and Processes. Control Flow. Altering the Control Flow

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Are branches/calls the only way we can get the processor to go somewhere in a program? What is a program? A processor? A process?

Excep&onal Control Flow: Excep&ons and Processes

OS Interaction and Processes

Giving credit where credit is due

Announcement (1) sys.skku.edu is now available

CSC 252: Computer Organization Spring 2018: Lecture 19

Exceptional Control Flow Part I

Q & A (1) Where were string literals stored? Virtual Address. SSE2033: System Software Experiment 2 Spring 2016 Jin-Soo Kim

Exceptional Control Flow Part I September 22, 2008

Processes. CSE 351 Autumn Instructor: Justin Hsia

Exceptional Control Flow Part I Oct. 17, 2002

Excep onal Control Flow: Excep ons and Processes

CSE351 Inaugural Edi7on Spring

Control Flow. Systemprogrammering 2007 Föreläsning 2 Exceptional Control Flow Part I. Exceptional Control Flow. Altering the Control Flow

Today. Introduction to Computer Systems /18 243, Fall th Lecture. Control Flow. Altering the Control Flow.

Program and OS interac0ons: Excep0ons and Processes

Processes. CSE 351 Autumn Instructor: Justin Hsia

Processes, Exceptional

Introduction to Computer Systems , fall th Lecture, Oct. 7 th

Exceptional Control Flow Part I

Exceptional Control Flow Part I Oct. 28, 2009"

Exceptions, Processes and Signals

Are branches/calls the only way we can get the processor to go somewhere in a program? What is a program? A processor? A process?

Altering the Control Flow

Approaches to Concurrency

CS 201. Processes. Gerson Robboy Portland State University

Exceptional Control Flow: Exceptions and Processes

Altering the Control Flow

Excep&onal+Control+Flow:++ Excep&ons+and+Processes+ + 15C213':'Introduc;on'to'Computer'Systems' 14 th 'Lecture,'Oct.'15,'2015'

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Excep&onal Control Flow: Excep&ons and Processes. (Chapter 8)

Foundations of Computer Systems

Foundations of Computer Systems

Processes. CSE 351 Spring Instructor: Ruth Anderson

Processes, Virtual Memory I

PROCESS PROGRAMMING INTERFACE

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

!"#$%&'()*+,'(-.'*+/*'01+!"#$%&'(2+)(3+4.'#$22$2

Operating System. Chapter 3. Process. Lynn Choi School of Electrical Engineering

CMPSCI 230 Computer Systems Principles. Processes

Exceptions and Processes

Virtual Memory I. CSE 351 Autumn Instructor: Justin Hsia

CSC 1600 Unix Processes. Goals of This Lecture

Operating System Structure

fork System-Level Function

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

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

Process Management 1

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

Shell and Signals. Computer Organization 3/17/2015. CSC252 - Spring The World of Multiprogramming or Multitasking. Unix Process Hierarchy

CS3733: Operating Systems

Lecture 24: Multitasking and Signals

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

Operating Systems Components. What is an Operating System? Process Management. Memory Management. Operating Systems 9/7/2018 CSC 256/456 1

Introduction to Processes

CS 261 Fall Mike Lam, Professor. Processes

System Programming. Process Control II

Exceptional Control Flow Part II Nov. 2, 2009"

Processes & Signals. System Runs Many Processes Concurrently. State consists of memory image + register values + program counter

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

Processes, Exceptional Control Flow

Process Control. Philipp Koehn. 23 April 2018

CS240: Programming in C

Giving credit where credit is due

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

CS213. Exceptional Control Flow Part II. Topics Process Hierarchy Signals

Exceptions, Processes and Signals

Unix Processes 1 / 31

CSCB09: Software Tools and Systems Programming. Bianca Schroeder IC 460

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

Processes. CS3026 Operating Systems Lecture 05

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

CS 550 Operating Systems Spring Process III

Exceptional Control Flow Part II

CS 261 Fall Mike Lam, Professor. Exceptional Control Flow and Processes

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

Multitasking. Programmer s model of multitasking. fork() spawns new process. exit() terminates own process

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

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

ECF Exists at All Levels of a System Exceptional Control Flow Part II Oct. 22, 2002 Topics! Process Hierarchy! Shells! Signals!

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

Lecture 4: Process Management

Programming Assignments will be.. All the PAs are continuous 3 major factors that you should consider

518 Lecture Notes Week 3

Exceptional Control Flow Exists at All Levels of a System. Systemprogrammering 2007 Föreläsning 3 Exceptional Control Flow Part II

Operating systems and concurrency - B03

CSCI2467: Systems Programming Concepts

CSE 153 Design of Operating Systems Fall 2018

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

What is an Operating System? Signals, Processes, & Threads. Resource Sharing. Resource Abstraction ... Operating Systems 10/20/2010

COE518 Lecture Notes Week 2 (Sept. 12, 2011)

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

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

Process Management! Goals of this Lecture!

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

Transcription:

Processes Lecture 12, May 19 th 2011. Alexandre David Credits to Randy Bryant & Dave O Hallaron from Carnegie Mellon 1

Processes Defini=on: A process is an instance of a running program. One of the most profound ideas in computer science Not the same as program or processor Process provides each program with two key abstrac=ons: Logical control flow Each program seems to have exclusive use of the CPU Private virtual address space Each program seems to have exclusive use of main memory How are these Illusions maintained? Process execujons interleaved (muljtasking) or run on separate cores Address spaces managed by virtual memory system we ll talk about this in a couple of weeks 2

Concurrent Processes Two processes run concurrently (are concurrent) if their flows overlap in =me Otherwise, they are sequen0al Examples (running on single core): Concurrent: A & B, A & C SequenJal: B & C Process A Process B Process C Time 3

User View of Concurrent Processes Control flows for concurrent processes are physically disjoint in =me However, we can think of concurrent processes are running in parallel with each other Process A Process B Process C Time 4

Context Switching Processes are managed by a shared chunk of OS code called the kernel Important: the kernel is not a separate process, but rather runs as part of some user process Control flow passes from one process to another via a context switch Process A Process B Time user code kernel code user code kernel code user code context switch context switch 5

fork: Crea=ng New Processes int fork(void) creates a new process (child process) that is idenjcal to the calling process (parent process) returns 0 to the child process returns child s pid to the parent process pid_t pid = fork(); if (pid == 0) { printf("hello from child\n"); else { printf("hello from parent\n"); Fork is interes=ng (and oven confusing) because it is called once but returns twice 6

Understanding fork Process n pid_t pid = fork(); if (pid == 0) { printf("hello from child\n"); else { printf("hello from parent\n"); Child Process m pid_t pid = fork(); if (pid == 0) { printf("hello from child\n"); else { printf("hello from parent\n"); pid = m pid_t pid = fork(); if (pid == 0) { printf("hello from child\n"); else { printf("hello from parent\n"); pid = 0 pid_t pid = fork(); if (pid == 0) { printf("hello from child\n"); else { printf("hello from parent\n"); pid_t pid = fork(); if (pid == 0) { printf("hello from child\n"); else { printf("hello from parent\n"); pid_t pid = fork(); if (pid == 0) { printf("hello from child\n"); else { printf("hello from parent\n"); hello from parent Which one is first? hello from child 7

Fork Example #1 Parent and child both run same code DisJnguish parent from child by return value from fork Start with same state, but each has private copy Including shared output file descriptor RelaJve ordering of their print statements undefined void fork1() { int x = 1; pid_t pid = fork(); if (pid == 0) { printf("child has x = %d\n", ++x); else { printf("parent has x = %d\n", --x); printf(" from process %d with x = %d\n", getpid(), x); 8

Fork Example #2 Both parent and child can con=nue forking void fork2() { printf("l0\n"); fork(); printf("l1\n"); fork(); printf("\n"); L1 L0 L1 9

Fork Example #3 Both parent and child can con=nue forking void fork3() { L2 printf("l0\n"); fork(); printf("l1\n"); L1 L2 fork(); printf("l2\n"); L2 fork(); printf("\n"); L0 L1 L2 10

Fork Example #4 Both parent and child can con=nue forking void fork4() { printf("l0\n"); if (fork()!= 0) { printf("l1\n"); if (fork()!= 0) { printf("l2\n"); fork(); printf("\n"); L0 L1 L2 11

Fork Example #5 Both parent and child can con=nue forking void fork5() { printf("l0\n"); if (fork() == 0) { printf("l1\n"); if (fork() == 0) { printf("l2\n"); fork(); printf("\n"); L0 L2 L1 12

exit: Ending a process void exit(int status) exits a process Normally return with status 0 atexit() registers funcjons to be executed upon exit void cleanup(void) { printf("cleaning up\n"); void fork6() { atexit(cleanup); fork(); exit(0); 13

Zombies Idea When process terminates, sjll consumes system resources Various tables maintained by OS Called a zombie Living corpse, half alive and half dead Reaping Performed by parent on terminated child Parent is given exit status informajon Kernel discards process What if parent doesn t reap? If any parent terminates without reaping a child, then child will be reaped by init process So, only need explicit reaping in long- running processes e.g., shells and servers 14

Zombie Example linux>./forks 7 & [1] 6639 Running Parent, PID = 6639 Terminating Child, PID = 6640 linux> ps PID TTY TIME CMD 6585 ttyp9 00:00:00 tcsh 6639 ttyp9 00:00:03 forks 6640 ttyp9 00:00:00 forks <defunct> 6641 ttyp9 00:00:00 ps linux> kill 6639 [1] Terminated linux> ps PID TTY TIME CMD 6585 ttyp9 00:00:00 tcsh 6642 ttyp9 00:00:00 ps void fork7() { if (fork() == 0) { /* Child */ printf("terminating Child, PID = %d\n", getpid()); exit(0); else { printf("running Parent, PID = %d\n", getpid()); while (1) ; /* Infinite loop */ ps shows child process as defunct Killing parent allows child to be reaped by init 15

Nontermina=ng Child Example linux>./forks 8 Terminating Parent, PID = 6675 Running Child, PID = 6676 linux> ps PID TTY TIME CMD 6585 ttyp9 00:00:00 tcsh 6676 ttyp9 00:00:06 forks 6677 ttyp9 00:00:00 ps linux> kill 6676 linux> ps PID TTY TIME CMD 6585 ttyp9 00:00:00 tcsh 6678 ttyp9 00:00:00 ps void fork8() { if (fork() == 0) { /* Child */ printf("running Child, PID = %d\n", getpid()); while (1) ; /* Infinite loop */ else { printf("terminating Parent, PID = %d\n", getpid()); exit(0); Child process sjll acjve even though parent has terminated Must kill explicitly, or else will keep running indefinitely 16

wait: Synchronizing with Children int wait(int *child_status) suspends current process unjl one of its children terminates return value is the pid of the child process that terminated if child_status!= NULL, then the object it points to will be set to a status indicajng why the child process terminated 17

wait: Synchronizing with Children void fork9() { int child_status; if (fork() == 0) { printf("hc: hello from child\n"); else { printf("hp: hello from parent\n"); wait(&child_status); printf("ct: child has terminated\n"); printf("\n"); exit(); HC HP CT 18

execve: Loading and Running Programs int execve( char *filename, char *argv[], char *envp[] ) Loads and runs in current process: Executable filename With argument list argv And environment variable list envp Does not return (unless error) Overwrites code, data, and stack keeps pid, open files and signal context Environment variables: name=value strings getenv and putenv Null- terminated env var strings Null- terminated cmd line arg strings unused envp[n] == NULL envp[n- 1] envp[0] argv[argc] == NULL argv[argc- 1] argv[0] Linker vars envp argv argc Stack frame for main Stack boaom Stack top environ 19

execve Example if ((pid = Fork()) == 0) { /* Child runs user job */ if (execve(argv[0], argv, environ) < 0) { printf("%s: Command not found.\n", argv[0]); exit(0); argv argv[argc] = NULL argv[argc- 1] argv[0] /usr/include -lt ls environ envp[n] = NULL envp[n- 1] envp[0] PWD=/usr/droh PRINTER=iron USER=droh 20

Summary Processes At any given Jme, system has muljple acjve processes Only one can execute at a Jme on a single core, though Each process appears to have total control of processor + private memory space 21

Summary (cont.) Spawning processes Call fork One call, two returns Process comple=on Call exit One call, no return Reaping and wai=ng for Processes Call wait or waitpid Loading and running Programs Call execve (or variant) One call, (normally) no return 22