ECE 650 Systems Programming & Engineering. Spring 2018

Similar documents
POSIX Shared Memory. Linux/UNIX IPC Programming. Outline. Michael Kerrisk, man7.org c 2017 November 2017

Operating systems. Lecture 9

ISA 563: Fundamentals of Systems Programming

Shared Memory Memory mapped files

ECEN 449 Microprocessor System Design. Review of C Programming

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

CSE 410: Systems Programming

Process Creation in UNIX

Prepared by Prof. Hui Jiang Process. Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University

Process. Prepared by Prof. Hui Jiang Dept. of EECS, York Univ. 1. Process in Memory (I) PROCESS. Process. How OS manages CPU usage? No.

Memory management. Single process. Multiple processes. How to: All memory assigned to the process Addresses defined at compile time

IPC and Unix Special Files

MMAP AND PIPE. UNIX Programming 2015 Fall by Euiseong Seo

경희대학교컴퓨터공학과 조진성. UNIX System Programming

CSCI 4061: Virtual Memory

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

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

Interprocess Communication. Originally multiple approaches Today more standard some differences between distributions still exist

Signal Example 1. Signal Example 2

Lab 09 - Virtual Memory

Shared Memory. By Oren Kalinsky

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

Inter-Process Communication. Disclaimer: some slides are adopted from the book authors slides with permission 1

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

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

CSE 333 SECTION 3. POSIX I/O Functions

CISC2200 Threads Spring 2015

UNIX System Calls. Sys Calls versus Library Func

The course that gives CMU its Zip! I/O Nov 15, 2001

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

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

CS240: Programming in C

Pipes. Pipes Implement a FIFO. Pipes (cont d) SWE 545. Pipes. A FIFO (First In, First Out) buffer is like a. Pipes are uni-directional

Outline. 1 Details of paging. 2 The user-level perspective. 3 Case study: 4.4 BSD 1 / 19

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

W4118 Operating Systems. Junfeng Yang

CS240: Programming in C

Programmation Système Cours 5 Memory Mapping

OPERATING SYSTEMS: Lesson 2: Operating System Services

CSC 271 Software I: Utilities and Internals

File Descriptors and Piping

프로세스간통신 (Interprocess communication) i 숙명여대창병모

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

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

Processes COMPSCI 386

Processes and Threads

628 Lecture Notes Week 4

Process Management! Goals of this Lecture!

Operating Systems 2010/2011

Virtual Memory: Systems

UNIT III- INTER PROCESS COMMUNICATIONS Part A

Applications of. Virtual Memory in. OS Design

NETWORK AND SYSTEM PROGRAMMING

CSE 333 SECTION 3. POSIX I/O Functions

Files. Eric McCreath

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

Inter-Process Communication. Disclaimer: some slides are adopted from the book authors slides with permission 1

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

Memory Mapped I/O. Michael Jantz. Prasad Kulkarni. EECS 678 Memory Mapped I/O Lab 1

using namespace std; //Standard classes are in namspace std //Don't add "using namespace" in header files, only in source files

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

File Systems: Consistency Issues

Lecture 18. Log into Linux. Copy two subdirectories in /home/hwang/cs375/lecture18/ $ cp r /home/hwang/cs375/lecture18/*.

Inter-Process Communication

CS370 Operating Systems

COP 4610: Introduction to Operating Systems (Spring 2016) Chapter 3: Process. Zhi Wang Florida State University

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

Programming with Shared Memory PART I. HPC Fall 2010 Prof. Robert van Engelen

NETWORK AND SYSTEM PROGRAMMING. I/O Multiplexing: select and poll function

Page Which had internal designation P5

CONCURRENCY MODEL. UNIX Programming 2014 Fall by Euiseong Seo

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

518 Lecture Notes Week 3

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

Thread. Disclaimer: some slides are adopted from the book authors slides with permission 1

Process. Operating Systems (Fall/Winter 2018) Yajin Zhou ( Zhejiang University

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

CSE 333 Lecture non-blocking I/O and select

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

Lecture 23: System-Level I/O

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

CSE 153 Design of Operating Systems Fall 2018

Processes. Johan Montelius KTH

CSI Module 2: Processes

Interprocess Communication E. Im

A process. the stack

Prepared by Prof. Hui Jiang (COSC3221) 2/9/2007

P6 memory system P6/Linux Memory System October 31, Overview of P6 address translation. Review of abbreviations. Topics. Symbols: ...

I/O Models. Kartik Gopalan

Basic OS Progamming Abstrac7ons

COMP 2355 Introduction to Systems Programming

Today: Process Management. The Big Picture So Far. What's in a Process? Example Process State in Memory

Preview. Process Control. What is process? Process identifier The fork() System Call File Sharing Race Condition. COSC350 System Software, Fall

Basic OS Progamming Abstrac2ons

The Big Picture So Far. Today: Process Management

Introduction to Linux, for Embedded Engineers Tutorial on Virtual Memory. Feb. 22, 2007 Tetsuyuki Kobayashi Aplix Corporation. [translated by ikoma]

Notice: This set of slides is based on the notes by Professor Perrone of Bucknell and the textbook authors Silberschatz, Galvin, and Gagne

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Operating System Labs. Yuanbin Wu

Transcription:

ECE 650 Systems Programming & Engineering Spring 2018 Inter-process Communication (IPC) Tyler Bletsch Duke University Slides are adapted from Brian Rogers (Duke)

Recall Process vs. Thread A process is Execution context PC, SP, Regs Code Data Stack A thread is Execution context Program counter (PC) Stack pointer (SP) Registers 2

Cooperation Two or more threads or processes execute concurrently Sometimes cooperate to perform a task Sometimes independent; not relevant for IPC discussion How do they communicate? Threads of the same process: Shared Memory Recall they share a process context Code, static data, *heap* Can read and write the same memory variables, arrays, structures, etc. What about threads of different processes? They do not have access to each other s memory 3

Models for IPC Shared Memory E.g. what we ve discussed for threads of same process Also possible across processes E.g. memory mapped files (mmap) Message Passing Use the OS as an intermediary E.g. Files, Pipes, FIFOs, Messages, Signals 4

Models for IPC Shared Memory Message Passing Thread A e.g. shared heap Write value Thread A Write value Thread B Read value Thread B Read value OS Kernel Space OS Kernel Space 5

Shared Memory vs. Message Passing Shared Memory Advantages Fast Easy to share data (nothing special to set up) Disadvantages Need synchronization! Can be tricky to eliminate race conditions Message Passing Advantages Trust not required between sender / receiver (receiver can verify) Set of shared data is explicit Is synchronization needed? Disadvantages Explicit programming support needed to share data Performance overhead (e.g. to copy messages through OS space) 6

Shared Memory Across Processes Different OSes have different APIs for this UNIX System V shared memory (shmget) Allows sharing between arbitrary processes http://www.tldp.org/ldp/lpg/node21.html Shared mappings (mmap on a file) Different forms for only related processors or unrelated processes (via filesystem interaction) POSIX shared memory (shm_open + mmap) Sharing between arbitrary processes; no overhead of filesystem I/O Still requires synchronization! 7

mmap #include <sys/mman.h> void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset); Creates new mapping in virtual address space of caller addr: starting address for mapping (or NULL to let kernel decide) length: # bytes to map starting at offset of the file prot: desired memory protection of the mapping PROT_EXEC, PROT_READ, PROT_WRITE, PROT_NONE flags: are updates to mapping are visible to other processes? MAP_SHARED, MAP_PRIVATE Other flags can be added, e.g. MAP_ANON (more later) fd: file descriptor for open file Can close the fd file after calling mmap() Return value is the address where the mapping was made 8

mmap operation Kernel takes an open file (given by FD) Maps that into process address space In unallocated space between stack & heap regions Thus also maps file into physical memory Creates one-to-one correspondence between a memory address and a word in the file Useful even apart from the context of IPC Allows programmer to read/write file contents without read(), write() system calls Multiple (even non-related) processes can share mem They open & mmap the same file 9

munmap #include <sys/mman.h> int munmap(void *addr, size_t length); Removes mapping from process address space addr: address of the mapping length: # bytes in mapped region #include <sys/mman.h> int msync(void *addr, size_t length, int flags); Flushes file contents in memory back out to disk addr: address of the mapping length: # bytes in mapped region flags: control when the update happens 10

Synchronization Semaphores #include <fcntl.h> /* For O_* constants */ #include <sys/stat.h> /* For mode constants */ #include <semaphore.h> sem_t *sem_open(const char *name, int oflag, mode_t mode, int value); or int sem_init(sem_t *sem, int pshared, unsigned int value); sem_t *mutex; mutex = sem_open( my_sem_name, O_CREAT O_EXCL, MAP_SHARED, 1); sem_wait(mutex); //Critical section sem_post(mutex); 11

Example Show code & run in class mmap_basic and mmap_basic2 12

Taking it Further This required some work Create file in file system Open the file & initialize it (e.g. with 0 s) There is a better way if just sharing mem across a fork() Anonymous memory mapping Use mmap flags of MAP_SHARED MAP_ANON File descriptor will be ignored (also offset) Memory initialized to 0 Alternative approach: open /dev/zero & mmap it Can anonymous approach work across non-related processes? 13

Message Passing Messages between processes, facilitated by OS Several approaches: Files Can open the same file between processes Communicate by reading and writing info from the file Can be difficult to coordinate Pipes FIFOs Messages (message passing) 14

Pipes #include <unistd.h> int pipe(int pipefd[2]); Creates a unidirectional channel (pipe) Can be used for IPC between processes / threads Returns 2 file descriptors pipefd[0] is the read end pipefd[1] is the write end Kernel support Data written to write end is buffered by kernel until read Data is read in same order as it was written No synchronization needed (kernel provides this) Must be related processes (e.g. children of same parent) 15

Example #include <stdio.h> #include <stdlib.h> #include <unistd.h> #define N 1024 int main(int argc, char *argv[]) { int pipefd[2]; char data_buffer[n]; pipe(pipefd); int id = fork(); if (id == 0) { //child write(pipefd[1], hello, 6); } else { read(pipefd[0], data_buffer, 6); printf( Received data: %s\n, data_buffer); } } return 0; 16

More Complex Uses of Pipes Can use pipes to coordinate processes For example, chain output of one process to input of next E.g. command pipes in UNIX shell! Requires 1 additional (very useful) piece #include <unistd.h> int dup2(int oldfd, int newfd); Creates a copy of an open file descriptor into a new one After closing the new file descriptor if it was open 17

UNIX Pipes Example Show code & run in class pipe_basic 18

UNIX FIFOs Similar to a pipe Also called a named pipe Persist beyond lifetime of the processes that create them Exist as a file in the file system #include <sys/types.h> #include <sys/stat.h> int mkfifo(const char *pathname, mode_t mode); pathname points to the file Mode specifies the FIFO s permissions (similar to a file) 19

UNIX FIFOs (2) After FIFO is created, processes must open it By default, first open blocks until a second process also opens One process opens for reading and the other process for writing Since FIFOs persist, they can be re-used No synchronization needed (like pipes, OS handles it) 20

Playing with FIFOs on the shell Can create a FIFO using mkfifo command Note: need to be in a UNIX-style filesystem to do this. Your shared Duke home directory is a Windows-style filesystem, so try this in /tmp if using the Duke Linux environment Can read/write fifo using normal commands. tail -f will monitor a file (or fifo) over time 21

Multiple Producers Multiple producers problem: What if >1 producers and 1 consumer Producers are performing write( ) Consumer is performing (blocking) read( ) What if consumer is blocked, but other IPC channels have data? Would like to be notified if one channel is ready 22

Select #include <sys/select.h> int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); nfds = number of file descriptors to monitor readfds, writefds, exceptfds are bit vectors of file descriptors to check timeout is a maximum time to wait Macros are available to work with bit sets: FD_ZERO(&fds), FD_SET(n, &fds), FD_CLEAR(n, &fds) int FD_ISSET(n, &fds); //useful after select() returns 23

Poll #include <poll.h> int poll(struct pollfd *fds, nfds_t nfds, int timeout); nfds = number of file descriptors to monitor fds is an array of descriptor structures File descriptors, desired events, returned events timeout is a maximum time to wait Returns number of descriptors with events 24