Process Management! Goals of this Lecture!

Similar documents
Process Management! Goals of this Lecture!

Process Management 1

Goals of this Lecture

I/O Management! Goals of this Lecture!

I/O 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

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

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

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

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

CSC 1600 Unix Processes. Goals of This Lecture

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

What Is Operating System? Operating Systems, System Calls, and Buffered I/O. Academic Computers in 1983 and Operating System

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

Process Creation in UNIX

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

Processes often need to communicate. CSCB09: Software Tools and Systems Programming. Solution: Pipes. Recall: I/O mechanisms in C

CMPS 105 Systems Programming. Prof. Darrell Long E2.371

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

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

628 Lecture Notes Week 4

Systems Programming. COSC Software Tools. Systems Programming. High-Level vs. Low-Level. High-Level vs. Low-Level.

UNIX System Calls. Sys Calls versus Library Func

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

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

Introduction to Processes

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

Lecture 23: System-Level I/O

UNIX Kernel. UNIX History

CSE 333 SECTION 3. POSIX I/O Functions

Processes COMPSCI 386

File Access. FILE * fopen(const char *name, const char * mode);

2009 S2 COMP File Operations

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

File Descriptors and Piping

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

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

System- Level I/O. Andrew Case. Slides adapted from Jinyang Li, Randy Bryant and Dave O Hallaron

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

Recitation 8: Tshlab + VM

Unix Processes. What is a Process?

Basic OS Progamming Abstrac7ons

The Classical OS Model in Unix

Basic OS Progamming Abstrac2ons

Operating Systems CS 217. Provides each process with a virtual machine. Promises each program the illusion of having whole machine to itself

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

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

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

Operating System Labs. Yuanbin Wu

Operating System Structure

Operating systems and concurrency - B03

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Preview. Interprocess Communication with Pipe. Pipe from the Parent to the child Pipe from the child to the parent FIFO popen() with r Popen() with w

Inter-Process Communication

Lecture 7: file I/O, more Unix

Naked C Lecture 6. File Operations and System Calls

Lecture 8: Structs & File I/O

CSC209 Fall Karen Reid 1

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.

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

Assignment 1. Teaching Assistant: Michalis Pachilakis (

CS240: Programming in C

Input and Output System Calls

CSE 410: Systems Programming

CS240: Programming in C

Fall 2017 :: CSE 306. File Systems Basics. Nima Honarmand

Pipelines, Forks, and Shell

UNIX System Programming

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

UNIX I/O. Computer Systems: A Programmer's Perspective, Randal E. Bryant and David R. O'Hallaron Prentice Hall, 3 rd edition, 2016, Chapter 10

Operating Systems. Lecture 05

UNIX input and output

Creating a Shell or Command Interperter Program CSCI411 Lab

Operating Systems. Lecture 06. System Calls (Exec, Open, Read, Write) Inter-process Communication in Unix/Linux (PIPE), Use of PIPE on command line

How do we define pointers? Memory allocation. Syntax. Notes. Pointers to variables. Pointers to structures. Pointers to functions. Notes.

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #47. File Handling

CSE 333 SECTION 3. POSIX I/O Functions

Processes. Processes (cont d)

everything is a file main.c a.out /dev/sda1 /dev/tty2 /proc/cpuinfo file descriptor int

W4118: OS Overview. Junfeng Yang

Lecture files in /home/hwang/cs375/lecture05 on csserver.

CSE 410: Systems Programming

Interrupts, Fork, I/O Basics

CSC209 Review. Yeah! We made it!

Files. Eric McCreath

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

518 Lecture Notes Week 3

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

Sistemas Operativos /2016 Support Document N o 1. Files, Pipes, FIFOs, I/O Redirection, and Unix Sockets

CS240: Programming in C

CSC209H Lecture 7. Dan Zingaro. February 25, 2015

Summer June 15, 2010

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

Transcription:

Process Management! 1 Goals of this Lecture! Help you learn about:" Creating new processes" Programmatically redirecting stdin, stdout, and stderr" Unix system-level functions for I/O" The Unix stream concept" Standard C I/O functions and their use of Unix functions" (Appendix) communication between processes via pipes" Why?" Creating new processes and programmatic redirection are fundamental tasks of a Unix shell (Assignment 7)" A power programmer knows these things as well as I/O" Streams are a beautiful Unix abstraction" 2 1

Why a Process Creates a New One! 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" Underlying mechanism" A process executes fork() to create a child process" (Optionally) child process does exec() of 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 some resources, like open files" Child then runs independently" Including perhaps 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 control twice, once in ( to ) each process" Returns different values to the two processes" How to tell which process is which? " Parent: fork() returns the child s process ID" Child: fork() returns if (pid!= 0) { else { 5 Fork and Process State! Shared with parent" 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ʼs the 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" What if child process wants to run a new program" Solution: child does exec()" Note: exec() does not return. If it does, it failed." Example" Program" to run" 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 (any) child of this process terminates" Returns the process ID of that 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() Child runs ls program" execvp() Parent waits for child" wait() ls" child" execvp" bash" fork" parent" wait" 10 5

Simple Shell Code! Parse command line Assign values to somepgm, someargv Repeat the above 11 Simple Shell Trace (1)! Parse command line Assign values to somepgm, someargv Repeat the above 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 Child Process" 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 Child Process" 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 Child Process" Repeat the previous In child, pid == 0; child calls execvp() 15 Simple Shell Trace (5)! Child Process" 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)! Child Process" 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! 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

Fork and I/O! Child process gets a copy of parentʼs file descriptors" File descriptor: integer that uniquely represents an open file" Passed to and returned by system-level I/O functions" A file is a stream" An ordered sequence of characters" Can read or write a stream; can read while someone is writing, " A beautiful abstraction for I/O" Can create, open, close, read, write or seek into a file" Using file 21 Streams! Each stream has an associated file position" Starts at beginning of file, if file opened to read or write" Or, starts at end of file, if file opened to append" " file file Read/write operations advance the file position" Allows sequencing through the file in sequential manner" Support for random access to the stream" Functions to learn current position and seek to new one" 22 11

System-Level Functions for I/O! int creat(char *pathname, mode_t mode); Create a new file named pathname, and return a file int open(char *pathname, int flags, mode_t mode); Open the file pathname and return a file int close(int fd); Close fd int read(int fd, void *buf, int count); Read up to count bytes from fd into the buffer at buf "" int write(int fd, void *buf, int count); Writes up to count bytes into fd from the buffer at buf int lseek(int fd, int offset, int whence); Assigns the file pointer of fd to a new value by applying an offset" 23 Example: open() Takes a path name, returns file int open(const char *pathname, int flags, mode_t mode); Arguments" Pathname: name of the file" Flags: bit flags for O_RDONLY, O_WRONLY, O_RDWR Mode: permissions to set if file must be created" Returns" File descriptor (or a -1 if an error)" Performs a variety of checks" E.g., whether the process is entitled to access the file" Underlies fopen() call in C stdio library" 24 12

Example: read() Reads bytes from a file int read(int fd, void *buf, int count); Arguments" File descriptor: integer descriptor returned by open() Buffer: pointer to memory to store the bytes it reads" Count: maximum number of bytes to read Returns" Number of bytes read" 0 if nothing more to read" -1 if error occurred" Performs a variety of checks" Whether file has been opened, whether reading is okay" Underlies getchar(), fgets(), scanf(), etc." 25 Stream Abstraction and C stdio! Built on top of system-level functions" Stream: any source of input or destination for output" keyboard, screen, files on disk/cd, network/printer port " Accessed in C programs through file pointers" E.g., FILE *fp1 = fopen("myfile.txt", "r"); Three streams provided by stdio.h" Streams stdin, stdout, and stderr! Typically map to keyboard, screen, and screen" Can redirect to correspond to other streams" E.g., stdin can be named file or output of another pgm" E.g., stdout can be named file or input to another pgm" 26 13

Redirection! Unix allows programmatic redirection of stdin, stdout, stderr" How?" Use open(), creat(), and close() system calls" 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. dup() uses the lowest-numbered unused descriptor for the new descriptor. Return the new descriptor, or -1 if an error occurred." 27 Redirection Example! How does shell implement somepgm > somefile?" " 28 14

Redirection Example Trace (1)! /dev/tty" Parent has file descriptor table; first three point to terminal " 29 Redirection Example Trace (2)! /dev/tty" Child Process" Parent forks child; child has identical file descriptor 30 15

Redirection Example Trace (3)! /dev/tty" Child Process" Let s say parent gets CPU first; parent waits" 31 Redirection Example Trace (4)! /dev/tty" Child Process" somefile" Child gets CPU; child creates somefile" 32 16

Redirection Example Trace (5)! /dev/tty" Child Process" somefile" Child closes file descriptor 1 (stdout)" 33 Redirection Example Trace (6)! /dev/tty" Child Process" somefile" Child duplicates file descriptor 3 into first unused spot" 34 17

Redirection Example Trace (7)! /dev/tty" Child Process" somefile" Child closes file descriptor 35 Redirection Example Trace (8)! /dev/tty" Child Process" somefile" Child calls execvp()" 36 18

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

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" 39 More on C Standard I/O Functions! Portability" Generic I/O support for C programs" Specific implementations for various host OSes" Invokes the OS-specific system calls for I/O" Abstractions for C programs" Streams" Line-by-line input" Formatted output " Additional optimizations" Buffered I/O" Safe writing" user OS Appl Prog Stdio Library File System 40 20

Example: Opening a File! FILE *fopen("myfile.txt", "r") Open the named file and return a stream" Includes a mode, such as r for read or w for write" Creates a FILE data structure for the file" Mode, status, buffer, " Assigns fields and returns a pointer" Opens or creates the file, based on the mode" Write ( w ): create file with default permissions" Read ( r ): open the file as read-only" Append ( a ): open or create file, and seek to the end" Uses underlying system calls supported by OS" 41 Example: Formatted I/O! int fprintf(fp1, "Number: %d\n", i)" Convert and write output to stream in specified format" int fscanf(fp1, "FooBar: %d", &i) Read from stream in format and assign converted values" Specialized versions" printf() is just fprintf(stdout, ) scanf() is just fscanf(stdin, ) Use underlying syscalls: read, write" 42 21

Abstraction: stdio built on syscalls! User" process" Operating" System" Appl Pgm! Stdio Library! File System! Storage! Driver! FILE * stream" int fd! hierarchical file system" variable-length segments" disk blocks" File descriptor:" An integer that" uniquely identifies" an open file" Disk! stdio functions use create, open, close, read, write system calls 43 Example: A Simple getchar() int getchar(void) { char c; if (read(0, &c, 1) == 1) return c; else return EOF; Read one character from stdin File descriptor 0 is stdin &c points to the buffer to read into" 1 is the number of bytes to read" Read returns the number of bytes read " In this case, 1 byte means success" 44 22

Making getchar() More Efficient! Poor performance reading one byte at a time" Read system call is accessing the device (e.g., a disk)" Reading one byte from disk is very time consuming" Better to read and write in larger chunks! Buffered I/O" Library reads large chunk from disk into a memory buffer" Doles out bytes to the user process as requested" Discard buffer contents when the stream is closed" Similarly, write individual bytes to a buffer in memory" And write to disk when full, or when stream is closed" Known as flushing the buffer" 45 Better getchar() with Buffered I/O! " int getchar(void) { static char base[1024]; static char *ptr; static int cnt = 0; if (cnt--) return *ptr++; persistent variables for the buffer base cnt = read(0, base, sizeof(base)); if (cnt <= 0) return EOF; ptr = base; ptr return getchar(); But, many functions may read (or write) the stream 46 23

Details of FILE in stdio.h (K&R 8.5)! #define OPEN_MAX 20 /* max files open at once */ typedef struct _iobuf { int cnt; /* num chars left in buffer */ char *ptr; /* ptr to next char in buffer */ char *base; /* beginning of buffer */ int flag; /* open mode flags, etc. */ char fd; /* file descriptor */ FILE; extern FILE _iob[open_max]; #define stdin (&_iob[0]) #define stdout (&_iob[1]) #define stderr (&_iob[2]) 47 Buffered Output! When does it actually show up on I/O device?:" int main(void) { printf("step 1\n"); sleep(10); printf("step 2\n"); return 0; Run a.out > out.txt & and then tail -f out.txt " To run a.out in the background, outputting to out.txt And then to see the contents on out.txt Neither line appears till ten seconds have elapsed" Because the output is being buffered" Add fflush(stdout) to flush the output buffer (often after printf)" fclose() also flushes the buffer before closing" 48 24

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 I/O and redirection" open() / creat(): to open a file close(): to close a file dup(): to duplicate a file 49 Summary (contd.)! System I/O functions provide simple abstractions" Stream as a source or destination of data" Functions for manipulating streams" stdio library builds on system-level functions" Calls system-level functions for low-level I/O " Adds buffering" Powerful examples of abstraction" Application programs interact with streams at high level" Standard I/O library interacts with streams at lower level" Only the OS deals with the device-specific details" 50 25

Appendix! " " Inter-Process Communication (IPC)" 51 IPC! different machines same machine 52 26

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 53 Pipes! 54 27

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" 55 Creating a Pipe! 56 28

Pipe Example! child parent 57 Pipes and Stdio! child makes stdin (0) the read side of the pipe parent makes stdout (1) the write side of the pipe 58 29

Pipes and Exec! child process invokes a new program 59 30