Inter-Process Communication

Similar documents
CS 220: Introduction to Parallel Computing. Input/Output II. Lecture 8

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

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

IPC and Unix Special Files

Processes COMPSCI 386

UNIX input and output

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

Process Management 1

628 Lecture Notes Week 4

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

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

Process Management! Goals of this Lecture!

UNIX Kernel. UNIX History

CS 3733 Operating Systems

Operating Systems. Review ENCE 360

Process Management! Goals of this Lecture!

ECE 598 Advanced Operating Systems Lecture 23

CS 43: Computer Networks. 07: Concurrency and Non-blocking I/O Sep 17, 2018

Lecture 23: System-Level I/O

CSE 410: Systems Programming

Operating Systemss and Multicore Programming (1DT089)

Process Creation in UNIX

Logistics. Recitation 11: I/O Problems. Today s Plan. Why Use Robust I/O. Andrew Faulring Section A 18 November 2002

Operating System Labs. Yuanbin Wu

Design Overview of the FreeBSD Kernel CIS 657

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

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

CS 326: Operating Systems. Process Execution. Lecture 5

Creating a Shell or Command Interperter Program CSCI411 Lab

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

Figure 1 Ring Structures

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

CSE 153 Design of Operating Systems Fall 2018

Pipelines, Forks, and Shell

CS 201. Files and I/O. Gerson Robboy Portland State University

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

File Descriptors and Piping

ENGR 3950U / CSCI 3020U Midterm Exam SOLUTIONS, Fall 2012 SOLUTIONS

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

Unix File and I/O. Outline. Storing Information. File Systems. (USP Chapters 4 and 5) Instructor: Dr. Tongping Liu

Operating System Labs. Yuanbin Wu

The Classical OS Model in Unix

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

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

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

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

Operating System Labs. Yuanbin Wu

IC221: Systems Programming 12-Week Written Exam [SOLUTIONS]

CSE 410: Systems Programming

CS 153 Design of Operating Systems Winter 2016

Basic OS Progamming Abstrac7ons

CSE 120 Principles of Operating Systems

Operating System Labs. Yuanbin Wu

Unix Processes. What is a Process?

CS 33. Shells and Files. CS33 Intro to Computer Systems XX 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Basic OS Progamming Abstrac2ons

ECE 650 Systems Programming & Engineering. Spring 2018

Goals of this Lecture

CS450/550 Operating Systems

Files and Directories

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

CSC209H Lecture 7. Dan Zingaro. February 25, 2015

Recitation 8: Tshlab + VM

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

(MCQZ-CS604 Operating Systems)

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

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

Lecture 7: file I/O, more Unix

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

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

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

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

Physical Files and Logical Files. Opening Files. Chap 2. Fundamental File Processing Operations. File Structures. Physical file.

W4118 Operating Systems. Junfeng Yang

CPS 310 first midterm exam, 10/6/2014

COSC Operating Systems Design, Fall Lecture Note: Unnamed Pipe and Shared Memory. Unnamed Pipes

Topic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional)

Processes. Processes (cont d)

Lecture 8: Unix Pipes and Signals (Feb 10, 2005) Yap

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

Inter-Process Communications (IPC)

Lecture 19: File System Implementation. Mythili Vutukuru IIT Bombay

CSE 43: Computer Networks Structure, Threading, and Blocking. Kevin Webb Swarthmore College September 14, 2017

CSE 333 SECTION 3. POSIX I/O Functions

Naked C Lecture 6. File Operations and System Calls

Processes, Context Switching, and Scheduling. Kevin Webb Swarthmore College January 30, 2018

CSE 120 Principles of Operating Systems Spring 2017

CS 318 Principles of Operating Systems

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.

UNIX Files. How UNIX Sees and Uses Files

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

File Management 1/34

Process control. Game plan: Fork. Scribe notes for 11/11/14 by Diane Yang, Kara Shen, and Chris Lim. fork exec pipes

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

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

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

Transcription:

CS 326: Operating Systems Inter-Process Communication Lecture 10

Today s Schedule Shared Memory Pipes 2/28/18 CS 326: Operating Systems 2

Today s Schedule Shared Memory Pipes 2/28/18 CS 326: Operating Systems 3

Shared Memory When we use pthreads, we have accessed to a shared memory pool Heap memory We can also share regions of memory between processes! Unlike with pthreads, we can restrict sharing to a particular region of memory 2/28/18 CS 326: Operating Systems 4

mmap Mapping a region of memory is accomplished by the mmap function mmap asks the OS kernel to reserve a portion of memory and allow other process(es) to access it It s also possible to memory map a file In fact, mmap() takes in a file descriptor More efficient than reading/writing buffered data to/from disk manually 2/28/18 CS 326: Operating Systems 5

Working with Mapped Memory After the call to mmap completes, participating processes are given a void pointer This pointer provides the first offset of the shared memory region Processes can read/write directly as if they are working with their own memory space 2/28/18 CS 326: Operating Systems 6

Use Cases Mapped memory is extremely efficient Once set up, processes do not have to go through the kernel to communicate Better performance It s also more error-prone than other IPC mechanisms If a cooperating program writes to memory incorrectly, it s game over 2/28/18 CS 326: Operating Systems 7

Today s Schedule Shared Memory Pipes 2/28/18 CS 326: Operating Systems 8

Pipes Pipes are a common way for programs to communicate on Unix systems Most useful for sharing unstructured data (such a text) between processes Work like how they sound: if you want to send data to another process, send it through the pipe Pipes fit in to the general Unix I/O landscape 2/28/18 CS 326: Operating Systems 9

Input/Output Streams Each program gets allocated three streams by default: stdout (standard output) stderr (standard error) stdin (standard input) As with most things in Unix, these are represented as files (file descriptors) These streams have different functions 2/28/18 CS 326: Operating Systems 10

stdout When you call printf, you are writing to stdout This stream is designed for general program output; for example, if you type ls then the list of files should display on stdout You can pipe stdout to other programs: ls -l / grep 'bin' or redirect to a file: ls -l / > list_of_files.txt 2/28/18 CS 326: Operating Systems 11

stderr The standard error stream is used for diagnostic information This way, program output can still be passed to other programs/files but we ll still see diagnostics printed to the terminal Helps us know when something went wrong Unlike stdout, stderr is not buffered 2/28/18 CS 326: Operating Systems 12

stdin The final stream, stdin, one way to provide program input (via scanf, for example) This can be entered by the user, or we can pipe input directly into a program: ls -l / grep 'bin' Writes file list to stdout Reads file list from stdin, Writes matching files to stdout 2/28/18 CS 326: Operating Systems 13

I/O Buffering (1/2) Input/output operations are expensive: they have high latency Printing to the terminal is outputting data to the standard output stream Writing to disk or controlling an external hardware device are also I/O operations These devices generally operate on buffers Example: our serial console has a 2-byte buffer; we fill up the buffer before asking it to print the text 2/28/18 CS 326: Operating Systems 14

I/O Buffering (2/2) You may have used buffered streams in Java to get better performance You might have also noticed what happens when you forget to close a stream Buffered I/O collects multiple I/O operations, combines them, and then executes them Important: matching up with hardware capabilities 2/28/18 CS 326: Operating Systems 15

fprintf We can control where printed output goes with fprintf File printf stderr is actually a file in fact, on Unix systems most devices are represented as files Try ls /dev to view the devices on your machine (includes macos) So if we want to write data to a file, just pass it in: fprintf(file, "My name is: %s", "Bob"); 2/28/18 CS 326: Operating Systems 16

The pipe function Now back to pipes: we can create them with the pipe() function Returns a set of file descriptors: the input and output sides of the pipe Pipes aren t very useful if they aren t connected to anything, though We can do this by fork()ing another process 2/28/18 CS 326: Operating Systems 17

Piping to another Process After calling fork(), both processes have a copy of the pipe file descriptors Pipes only operate in one direction, though, so we need to close the appropriate ends of the pipe You can think of a forked() pipe as one with four ends: two input and output ends each We eliminate the ends we don t need to control the direction of data flow > --- < 2/28/18 CS 326: Operating Systems 18

Controlling Flow To control data flow through the pipe, we have to close the ends we won t use For example: Child process closes FD 0 and reads from FD 1 Parent process closes FD 1 and writes to FD 0 2/28/18 CS 326: Operating Systems 19

Async Process Creation You may be wondering: what good are pipes when we have to start all the cooperating processes? There s actually another option: FIFOs, aka named pipes Create with the mkfifo command, then open as you would a regular file descriptor 2/28/18 CS 326: Operating Systems 20

Redirecting Output: dup2 dup2 allows us to redirect output streams int dup2(int fildes, int fildes2); Let s say we want to make our standard output stream go through the pipe we just created We ll do: dup2(fd[0], STDOUT_FILENO); 2/28/18 CS 326: Operating Systems 21

Redirecting to a File We can also redirect output to a file instead: int output = open("output.txt", O_CREAT O_WRONLY, 0666); dup2(output, STDOUT_FILENO); Cool, right? Ok, let s try this out 2/28/18 CS 326: Operating Systems 22