Process Management! Goals of this Lecture!

Similar documents
Process Management 1

Process Management! Goals of this Lecture!

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

fork System-Level Function

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

CSC 1600 Unix Processes. Goals of This Lecture

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

Pipes and FIFOs. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

The Shell, System Calls, Processes, and Basic Inter-Process Communication

Pipelines, Forks, and Shell

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

518 Lecture Notes Week 3

Contents. PA1 review and introduction to PA2. IPC (Inter-Process Communication) Exercise. I/O redirection Pipes FIFOs

Operating systems and concurrency - B03

Operating System Structure

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

Contents. IPC (Inter-Process Communication) Representation of open files in kernel I/O redirection Anonymous Pipe Named Pipe (FIFO)

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

The Classical OS Model in Unix

Operating Systems. Lecture 05

Operating System Labs. Yuanbin Wu

CS240: Programming in C

CSC209 Fall Karen Reid 1

Introduction to Processes

UNIX Kernel. UNIX History

628 Lecture Notes Week 4

Process Creation in UNIX

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

Unix Processes. What is a Process?

Creating a Shell or Command Interperter Program CSCI411 Lab

Maria Hybinette, UGA. ! One easy way to communicate is to use files. ! File descriptors. 3 Maria Hybinette, UGA. ! Simple example: who sort

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

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

Processes COMPSCI 386

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

Outline. OS Interface to Devices. System Input/Output. CSCI 4061 Introduction to Operating Systems. System I/O and Files. Instructor: Abhishek Chandra

Operating Systems Lab

Lecture 5. Systems Programming: Unix Processes Creation: fork & exec Process Communication: Pipes

Assignment 1. Teaching Assistant: Michalis Pachilakis (

Fall 2015 COMP Operating Systems. Lab #3

Processes. Processes (cont d)

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

CS 201. Processes. Gerson Robboy Portland State University

CSE 153 Design of Operating Systems Fall 2018

SE350: Operating Systems

Processes, Threads, SMP, and Microkernels

SOFTWARE ARCHITECTURE 3. SHELL

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

Inter-Process Communication

Princeton University. Computer Science 217: Introduction to Programming Systems. I/O Management

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

Processes. CS3026 Operating Systems Lecture 05

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

CS 25200: Systems Programming. Lecture 14: Files, Fork, and Pipes

OS COMPONENTS OVERVIEW OF UNIX FILE I/O. CS124 Operating Systems Fall , Lecture 2

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

CS240: Programming in C

Princeton University Computer Science 217: Introduction to Programming Systems. I/O Management

Processes. Process Concept

Operating Systemss and Multicore Programming (1DT089)

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

Layers in a UNIX System. Create a new process. Processes in UNIX. fildescriptors streams pipe(2) labinstructions

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

Operating Systems. Lecture 07. System Calls, Input/Output and Error Redirection, Inter-process Communication in Unix/Linux

Processes. Dr. Yingwu Zhu

CS61 Scribe Notes Date: Topic: Fork, Advanced Virtual Memory. Scribes: Mitchel Cole Emily Lawton Jefferson Lee Wentao Xu

Parents and Children

Interrupts, Fork, I/O Basics

CSC209 Review. Yeah! We made it!

OS Lab Tutorial 1. Spawning processes Shared memory

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

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

What is a Process. Processes. Programs and Processes. System Classification 3/5/2013. Process: An execution stream and its associated state

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

Lecture 4: Process Management

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

Lecture 23: System-Level I/O

Lecture 3 Process API in UNIX systems

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

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

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

System Programming. Introduction to Unix

EXPERIMENT NO : M/C Lenovo Think center M700 Ci3,6100,6th Gen. H81, 4GB RAM,500GB HDD

Review of Fundamentals

Design Overview of the FreeBSD Kernel CIS 657

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

Princeton University Computer Science 217: Introduction to Programming Systems I/O Management

API Interlude: Process Creation (DRAFT)

UNIX System Programming. Overview. 1. A UNIX System. 2. Processes (review) 2.1. Context. Pipes/FIFOs

Concurrency. Stefan D. Bruda. Winter 2018

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

Lecture Topics. Announcements. Today: Threads (Stallings, chapter , 4.6) Next: Concurrency (Stallings, chapter , 5.

COMP 2355 Introduction to Systems Programming

COE518 Lecture Notes Week 4 (Sept. 26, 2011)

Transcription:

Process Management! 1 Goals of this Lecture! Help you learn about:" Creating new processes" Programmatically redirecting stdin, stdout, and stderr" (Appendix) communication between processes via pipes" Why?" Creating processes and programmatic redirection are fundamental tasks of a Unix shell (see Assignment 7)" A power programmer knows about Unix shells, and thus about creating new processes and programmatic redirection" 2 1

Why Create a New Process?! Run a new program" E.g., shell executing a program entered at command line" Or, even running an entire pipeline of commands" Such as wc l * sort uniq -c sort nr " Run a new thread of control for the same program" E.g., a Web server handling a new Web request" While continuing to allow more requests to arrive" Essentially time sharing the computer" Underlying mechanism" A process executes fork() to create a child process" (Optionally) child process does exec() of a new program" 3 Creating a New Process! Cloning an existing process" Parent process creates a new child process" The two processes then run concurrently " Child process inherits state from parent " Identical (but separate) copy of virtual address space" Copy of the parent s open file descriptors" Parent and child share access to open files" Child then runs independently" Executing independently, including invoking a new program" Reading and writing its own address space" parent child 4 2

Fork System-Level Function! fork() is called once" But returns twice, once in each process" Telling which process is which " Parent: fork() returns the child s process ID" Child: fork() returns if (pid!= 0) { else { 5 Fork and Process State! Inherited" User and group IDs" Signal handling settings" Stdio" File pointers" Root directory" File mode creation mask" Resource limits" Controlling terminal" All machine register states" Control register(s)" " Separate in child" Process ID" Address space (memory)" File descriptors" Parent process ID" Pending signals" Time signal reset times" " 6 3

Example: What Output?! int main(void) { pid_t pid; int x = 1; if (pid!= 0) { printf("parent: x = %d\n", --x); exit(0); else { printf("child: x = %d\n", ++x); exit(0); 7 Executing a New Program! fork() copies the state of the parent process" Child continues running the parent program" with a copy of the process memory and registers" Need a way to invoke a new program" In the context of the newly-created child process" Example" program" NULL-terminated array" Contains command-line arguments (to become argv[] of ls)" execvp("ls", argv); 8 4

Waiting for the Child to Finish! Parent should wait for children to finish" Example: a shell waiting for operations to complete" Waiting for a child to terminate: wait() Blocks until some child terminates" Returns the process ID of the child process" Or returns -1 if no children exist (i.e., already exited)" Waiting for specific child to terminate: waitpid() Blocks till a child with particular process ID terminates" #include <sys/types.h> #include <sys/wait.h> pid_t wait(int *status); pid_t waitpid(pid_t pid, int *status, int options); 9 Example: A Simple Shell! Shell is the parent process" E.g., bash" Parses command line" E.g., ls l " Invokes child process" fork(), execvp() Waits for child" wait() child" execvp" ls" bash" fork" parent" wait" 10 5

Simple Shell Code! Parse command line Assign values to somepgm, someargv Repeat the previous 11 Simple Shell Trace (1)! Parse command line Assign values to somepgm, someargv Repeat the previous Parent reads and parses command line" Parent assigns values to somepgm and someargv 12 6

Simple Shell Trace (2)! Parse command line Assign values to somepgm, someargv Repeat the previous executing" concurrently" Parse command line Assign values to somefile, someargv Repeat the previous fork() creates child process" Which process gets the CPU first? Let s assume the parent" 13 Simple Shell Trace (3)! Parse command line Assign values to somepgm, someargv Repeat the previous child s pid" executing" concurrently" Parse command line Assign values to somefile, someargv Repeat the previous In parent, pid!= 0; parent waits; OS gives CPU to child" 14 7

Simple Shell Trace (4)! Parse command line Assign values to somepgm, someargv Repeat the previous executing" concurrently" Parse command line Assign values to somefile, someargv Repeat the previous In child, pid == 0; child calls execvp() 15 Simple Shell Trace (5)! Parse command line Assign values to somepgm, someargv Repeat the previous executing" concurrently" somepgm In child, somepgm overwrites shell program;" main() is called with someargv as argv parameter" 16 8

Simple Shell Trace (6)! Parse command line Assign values to somepgm, someargv Repeat the previous executing" concurrently" somepgm Somepgm executes in child, and eventually exits" 17 Simple Shell Trace (7)! Parse command line Assign values to somepgm, someargv Repeat the previous Parent returns from wait() and proceeds" 18 9

Combined Fork/Exec/Wait! Common combination of operations" fork() to create a new child process" exec() to invoke new program in child process" wait() in the parent process for the child to complete" Single call that combines all three" int system(const char *cmd); Example" int main(void) { system("echo Hello world"); return 0; 19 Fork and Virtual Memory! Incidentally" Question:" fork() duplicates an entire process (text, bss, data, rodata, stack, heap sections)" Isn t that very inefficient???!!!" Answer:" Using virtual memory, not really!" Upon fork(), OS creates virtual pages for child process" Each child virtual page points to real page (in memory or on disk) of parent" OS duplicates real pages incrementally, and only if/when write occurs" 20 10

Redirection! Unix allows programmatic redirection of stdin, stdout, or stderr" How?" Use open(), creat(), and close() system calls" Described in I/O Management lecture" Use dup() system call" int dup(int oldfd); Create a copy of the file descriptor oldfd. After a successful return from dup() or dup2(), the old and new file descriptors may be used interchangeably. They refer to the same open file description and thus share file offset and file status flags. Uses the lowest-numbered unused descriptor for the new descriptor. Return the new descriptor, or -1 if an error occurred." 21 Redirection Example! How does shell implement somepgm > somefile?" " 22 11

Redirection Example Trace (1)! /dev/tty" Parent has file descriptor table; first three point to terminal " 23 Redirection Example Trace (2)! /dev/tty" Parent forks child; child has identical file descriptor 24 12

Redirection Example Trace (3)! /dev/tty" Let s say parent gets CPU first; parent waits" 25 Redirection Example Trace (4)! /dev/tty" somefile" Child gets CPU; child creates somefile" 26 13

Redirection Example Trace (5)! /dev/tty" somefile" Child closes file descriptor 1 (stdout)" 27 Redirection Example Trace (6)! /dev/tty" somefile" Child duplicates file descriptor 3 into first unused spot" 28 14

Redirection Example Trace (7)! /dev/tty" somefile" Child closes file descriptor 29 Redirection Example Trace (8)! /dev/tty" somefile" Child calls execvp()" 30 15

Redirection Example Trace (9)! /dev/tty" somefile" execvp(somepfm, someargv); somepgm Somepgm executes with stdout redirected to somefile" 31 Redirection Example Trace (10)! /dev/tty" execvp(somefile, someargv); Somepgm exits; parent returns from wait() and proceeds" 32 16

The Beginnings of a Unix Shell! A shell is mostly a big loop" Parse command line from stdin" Expand wildcards ( * )" Interpret redirections ( <, and > )" fork(), dup(), exec(), and wait(), as necessary" Start from the code in earlier slides" And edit till it becomes a Unix shell" This is the heart of the last programming assignment" 33 Summary! System-level functions for creating processes" fork(): process creates a new child process" wait(): parent waits for child process to complete" exec(): child starts running a new program" system(): combines fork, wait, and exec all in one" System-level functions for redirection" open() / creat(): to open a file close(): to close a file dup(): to duplicate a file 34 17

Appendix! " " Inter-Process Communication (IPC)" 35 IPC! different machines same machine 36 18

IPC Mechanisms! Pipes" Processes on the same machine" Allows parent process to communicate with child process" Allows two sibling processes to communicate" Used mostly for a pipeline of filters" Sockets" Processes on any machines" Processes created independently" Used for client/server communication (e.g., Web)" Both provide abstraction of an ordered stream of bytes 37 Pipes! 38 19

Example Use of Pipes! Compute a histogram of content types in my e-mail" Many e-mail messages, consisting of many lines" Lines like Content-Type: image/jpeg indicate the type" Pipeline of Unix commands" Identifying content type: grep -i Content-Type * " Extracting just the type: cut -d" " -f2 Sorting the list of types: sort Counting the unique types: uniq -c " Sorting the counts: sort nr Simply running this at the shell prompt:" grep -i Content-Type * cut -d" " -f2 sort uniq -c sort nr" 39 Creating a Pipe! 40 20

Pipe Example! child parent 41 Pipes and Stdio! child makes stdin (0) the read side of the pipe parent makes stdout (1) the write side of the pipe 42 21

Pipes and Exec! child process invokes a new program 43 22