Processes: Introduction. CS 241 February 13, 2012

Similar documents
Exceptional Control Flow: Exceptions and Processes

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

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

University of Washington What is a process?

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?

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

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

Exceptional Control Flow Part I Oct. 17, 2002

Exceptional Control Flow Part I September 22, 2008

Processes. CSE 351 Autumn Instructor: Justin Hsia

CSC 252: Computer Organization Spring 2018: Lecture 19

Giving credit where credit is due

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.

Processes. CSE 351 Autumn Instructor: Justin Hsia

Processes, Exceptional

Exceptional Control Flow Part I

Excep&onal Control Flow: Excep&ons and Processes

Exceptional Control Flow Part I Oct. 28, 2009"

OS Interaction and Processes

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

Excep onal Control Flow: Excep ons and Processes

Program and OS interac0ons: Excep0ons and Processes

CSE351 Inaugural Edi7on Spring

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?

Approaches to Concurrency

Exceptions, Processes and Signals

Exceptional Control Flow: Exceptions and Processes

Altering the Control Flow

CS 201. Processes. Gerson Robboy Portland State University

Altering the Control Flow

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

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

Processes. CSE 351 Spring Instructor: Ruth Anderson

Processes, Virtual Memory I

Foundations of Computer Systems

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

Foundations of Computer Systems

Virtual Memory I. CSE 351 Autumn Instructor: Justin Hsia

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

CS3733: Operating Systems

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

PROCESS PROGRAMMING INTERFACE

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

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

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

Exceptional Control Flow Part II Nov. 2, 2009"

Exceptions and Processes

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

Lecture 24: Multitasking and Signals

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

CMPSCI 230 Computer Systems Principles. Processes

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

Processes, Exceptional Control Flow

Signals. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

Exceptions, Processes and Signals

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

CSC 1600 Unix Processes. Goals of This Lecture

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

Operating System Structure

Exceptional Control Flow Part II

Giving credit where credit is due

fork System-Level Function

System Programming. Process Control II

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

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

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

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

Process Management. Outline. Process vs. Program. Address Space. How do we run a program? What are steps to create a process?

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

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

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

Unix Processes 1 / 31

CS 261 Fall Mike Lam, Professor. Processes

CS 550 Operating Systems Spring Process III

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

Process Management 1

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

CSCI2467: Systems Programming Concepts

Operating systems and concurrency - B03

Process Control. Philipp Koehn. 23 April 2018

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

CS240: Programming in C

Exceptional Control Flow: Signals and Nonlocal Jumps

Introduction to Processes

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

Excep&onal Control Flow

Processes. CS3026 Operating Systems Lecture 05

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

CS 3733 Operating Systems

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

Lesson 2. process id = 1000 text data i = 5 pid = 1200

EECE.4810/EECE.5730: Operating Systems Spring 2017 Homework 1 Solution Due 3:15 PM, Wednesday, 2/1/17

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

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

Unix-Linux 2. Unix is supposed to leave room in the process table for a superuser process that could be used to kill errant processes.

Last Class. System Calls, Kernel Mode, and Process Implementation. Processes. Today. Operating Systems 9/11/2018 CSC 256/456 1

Transcription:

Processes: Introduction CS 241 February 13, 2012 1

Announcements MP2 due tomorrow Deadline and contest cutoff 11:59 p.m. Fabulous prizes on Wednesday MP3 out Wednesday: Shell (1 week) Code from this lecture posted after class 2

Processes Definition: 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 abstractions: 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 executions interleaved (multitasking) or run on separate cores Address spaces managed by virtual memory system 3

Concurrent Processes Two processes run concurrently (are concurrent) if their flows overlap in time Otherwise, they are sequential Examples (running on single core): Concurrent: A & B, A & C Sequential: B & C Process A Process B Process C Time 4

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

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 user code Time kernel code user code kernel code user code context switch context switch 6

fork: Creating New Processes int fork(void) creates a new process (child process) that is identical to the calling process (parent process) returns 0 to the child process returns child s pid (process id) to the parent process pid_t pid = fork(); if (pid == 0) { printf("hello from child\n"); else { printf("hello from parent\n"); Fork is interesting (and often confusing) because it is called once but returns twice 7

Understanding fork Process n Child Process m 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 = 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 8

Fork Example #1 Parent and child both run same code Distinguish parent from child by return value from fork Start with same state, but each has private copy Including shared output file descriptor Relative 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); 9

Fork Example #2 #define bork fork void fork3() { bork(); bork(); bork(); printf( borked\n"); 13

Fork Example #2 Three consecutive forks #define bork fork void fork3() { bork(); bork(); bork(); printf( borked\n"); borked borked borked borked borked borked borked borked 14

Fork Example #3 Three consecutive forks void fork3() { L2 printf("l0\n"); fork(); printf("l1\n"); L1 L2 fork(); printf("l2\n"); L2 fork(); printf("\n"); L0 L1 L2 15

Fork Example #4 Nested forks in parent void fork4() { printf("l0\n"); if (fork()!= 0) { printf("l1\n"); if (fork()!= 0) { printf("l2\n"); fork(); printf("\n"); L0 L1 L2 16

Fork Example #5 Nested forks in children void fork5() { printf("l0\n"); if (fork() == 0) { printf("l1\n"); if (fork() == 0) { printf("l2\n"); fork(); printf("\n"); L0 L2 L1 17

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

Zombies What happens on termination? When process terminates, still consumes system resources Various tables & info maintained by OS Called a zombie Living corpse, half alive and half dead 19

Zombies What happens on termination? When process terminates, still consumes system resources Various tables & info maintained by OS Called a zombie Living corpse, half alive and half dead Reaping Performed by parent on terminated child (using wait or waitpid) Parent is given exit status information 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 (pid == 1) So, only need explicit reaping in long-running processes e.g., shells and servers 20

Zombie Example Fig. 1. Exemplary Zombies. 21

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 22

Nonterminating 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 still active even though parent has terminated Child is an orphan Must kill explicitly, or else will keep running indefinitely 23

wait: synchronizing with children Parent reaps child by calling the wait function int wait(int *child_status) suspends current process until 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 indicating why the child process terminated Professional code uses signal handler (CS241 later lecture) for signal SIGCHLD which issues a wait() call 24

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 25

wait() Example If multiple children completed, will take in arbitrary order Can use macros WIFEXITED and WEXITSTATUS to get information about exit status void fork10() { pid_t pid[n]; int i; int child_status; for (i = 0; i < N; i++) if ((pid[i] = fork()) == 0) exit(100+i); /* Child */ for (i = 0; i < N; i++) { pid_t wpid = wait(&child_status); if (WIFEXITED(child_status)) printf("child %d terminated with exit status %d\n", wpid, WEXITSTATUS(child_status)); else printf("child %d terminate abnormally\n", wpid); 26

waitpid(): Waiting for a Specific Process waitpid(pid, &status, options) suspends current process until specific process terminates various options (see man page or textbook) void fork11() { pid_t pid[n]; int i; int child_status; for (i = 0; i < N; i++) if ((pid[i] = fork()) == 0) exit(100+i); /* Child */ for (i = N-1; i >= 0; i--) { pid_t wpid = waitpid(pid[i], &child_status, 0); if (WIFEXITED(child_status)) printf("child %d terminated with exit status %d\n", wpid, WEXITSTATUS(child_status)); else printf("child %d terminated abnormally\n", wpid); 27

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 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 bo;om Stack top environ 28

execve: Loading and Running Programs int execve( char *filename, char *argv[], char *envp[] ) Environment variables: name=value strings getenv and putenv Simpler variants: execlp, execv,... 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 bo;om Stack top environ 29

execve Example if ((pid = fork()) == 0) { /* Child runs user job */ if (execve(argv[0], argv, env) < 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=/Users/pbg SHELL=/bin/bash USER=pbg 30

Summary Processes At any given time, system has multiple active processes But only one can execute at a time on a single core Each process appears to have total control of processor + private memory space 31

Summary (cont.) Spawning processes Call fork One call, two returns Process completion Call exit One call, no return Reaping and waiting for processes Call wait or waitpid Loading and running programs Call execve (or front-end variant) One call, (normally) no return 32