Shared Memory. By Oren Kalinsky

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

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

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

Shared Memory Memory mapped files

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

OS Lab Tutorial 1. Spawning processes Shared memory

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

Inter-process communication (IPC)

Shared Memory (8A) Shared Memory

INTER-PROCESS COMMUNICATION. UNIX Programming 2015 Fall by Euiseong Seo

Signal Example 1. Signal Example 2

Operating Systems 2010/2011

Reminder. COP4600 Discussion 8 Segmentation, File System, Fork() Implementation. Question-1. Discussion 7 Recap. Question-1(cont) Question-1

CS 550 Operating Systems Spring Inter Process Communication

ECE 650 Systems Programming & Engineering. Spring 2018

Interprocess Communication

UNIT III- INTER PROCESS COMMUNICATIONS Part A

CS 345 Operating Systems. Tutorial 2: Treasure Room Simulation Threads, Shared Memory, Synchronization

CSci 4061 Introduction to Operating Systems. IPC: Message Passing, Shared Memory

COP 4604 UNIX System Programming IPC. Dr. Sam Hsu Computer Science & Engineering Florida Atlantic University

Interprocess Communication. Bosky Agarwal CS 518

Lecture 8: Inter-process Communication. Lecturer: Prof. Zichen Xu

Part II Processes and Threads Process Basics

Processes. OS Structure. OS Structure. Modes of Execution. Typical Functions of an OS Kernel. Non-Kernel OS. COMP755 Advanced Operating Systems

CSE 380: Homework 2: Synchronization

Processes and Threads

INTER-PROCESS COMMUNICATION Tanzir Ahmed CSCE 313 Fall 2018

COMP 3100 Operating Systems

CSPP System V IPC 1. System V IPC. Unix Systems Programming CSPP 51081

Operating Systems, Concurrency and Time. Synchronization and Communication. Johan Lukkien

Message Queues, Semaphores, Shared Memory

CSCI 4061: Virtual Memory

CS 385 Operating Systems Fall 2011 Homework Assignment 5 Process Synchronization and Communications

Gabrielle Evaristo CSE 460. Lab Shared Memory

COMP 2355 Introduction to Systems Programming

Inter-Process Communication: Message Passing. Thomas Plagemann. Big Picture. message passing communication?

CS 385 Operating Systems Fall 2013 Homework Assignment 2 Inter-Process Communications and Synchronization

CS 385 Operating Systems Spring 2013 Homework Assignment 2 Third Draft Inter-Process Communications and Synchronization

CS 361 Computer Systems Fall 2017 Homework Assignment 4 - Inter-Process Communications & I/O

Inter Process Communication (IPC) Giorgio Richelli

W4118 Operating Systems. Junfeng Yang

CSI Module 2: Processes

Lab 5: Inter-Process Communication

UNIX IPC. Unix Semaphore Unix Message queue

Process. Program Vs. process. During execution, the process may be in one of the following states

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

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

Operating Systems. VI. Threads. Eurecom. Processes and Threads Multithreading Models

Operating Systems. No. 5 ศร ณย อ นทโกส ม Sarun Intakosum

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

Overview. Over the next four weeks, we will look at these topics: Building Blocks. Advanced Authentication Issues.

#include <sys/types.h> #include <sys/wait.h> pid_t wait(int *stat_loc); pid_t waitpid(pid_t pid, int *stat_loc, int options);

Files. Eric McCreath

Message Queues POSIX

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

Mid-Semester Examination, September 2016

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

CS370 Operating Systems

Programmation Système Cours 10 System V IPC

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

Process Synchronization(2)

Dept. of CS, York Univ. 1

Dept. Of Computer Science, Colorado State University

CS631 - Advanced Programming in the UNIX Environment Interprocess Communication I

Frequently asked questions from the previous class survey

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

Memory management. Johan Montelius KTH

AC72/AT72/AC117/AT117 LINUX INTERNALS DEC 2015

Programmation Système Cours 5 Memory Mapping

UNIT III- INTERPROCESS COMMUNICATION

CS 385 Test 1. 1 Apr 2004

2 UNIX interprocess communications

Process Creation and Control

MIT-SHM The MIT Shared Memory Extension

CS345 Opera,ng Systems. Φροντιστήριο Άσκησης 2

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

RCU. ò Walk through two system calls in some detail. ò Open and read. ò Too much code to cover all FS system calls. ò 3 Cases for a dentry:

VFS, Continued. Don Porter CSE 506

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

CSE 410: Systems Programming

Department of Computer Science and Technology, UTU 2014

Lab 09 - Virtual Memory

Applications of. Virtual Memory in. OS Design

mmalloc The GNU memory-mapped malloc package Fred Fish Cygnus Support Mike Haertel Free Software Foundation

PROCESS CONCEPTS. Process Concept Relationship to a Program What is a Process? Process Lifecycle Process Management Inter-Process Communication 2.

File Management 1/34

Introduction to File Systems. CSE 120 Winter 2001

Shared Memory Semaphores. Goals of this Lecture

File Systems: Consistency Issues

MMAP AND PIPE. UNIX Programming 2015 Fall by Euiseong Seo

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

Babu Madhav Institute of Information Technology, UTU

Slide 6-1. Processes. Operating Systems: A Modern Perspective, Chapter 6. Copyright 2004 Pearson Education, Inc.

File Systems Overview. Jin-Soo Kim ( Computer Systems Laboratory Sungkyunkwan University

Processes COMPSCI 386

Last class: Today: Thread Background. Thread Systems

FILE SYSTEMS. Jo, Heeseung

Inter-Process Communication

Page Which had internal designation P5

Process and Its Image An operating system executes a variety of programs: A program that browses the Web A program that serves Web requests

Transcription:

Shared Memory By Oren Kalinsky 1

Overview Shared memory (SHM) - two or more processes can share a given region of memory A form of Inter Process Communication (IPC) Other IPC methods pipes, message queues SHM is the fastest method avoids kernel! Shared needs synchronization (Semaphores)

Shared Memory The virtual memory pages of each process points (mapped) to the same physical address of the shared memory pages Virtual Physical 3

Unix Shared Memory Unix supports multiple shared memory APIs: Shared mappings by using mmap Anonymous between related processes (fork) File between unrelated processes (file on the disk) POSIX shared memory Between unrelated processes No I/O overhead 4

POSIX Shared Memory Implementation map (mmap) files in a dedicated tmpfs (/dev/shm/) filesystem tmpfs virtual memory filesystem (in memory) Employs swap space when needed Objects have kernel persistency Exists until explicitly deleted or system reboots It is possible to map an object, change it and unmap 5

POSIX shared memory APIs shm_open() open an existing or create a new SHM object ftruncate() set the size of SHM object mmap() map SHM object into caller s address space 6

POSIX shared memory APIs munmap() unmap a mapped SHM object from caller s address space close() close the fd returned by shm_open() shm_unlink mark SHM object for deletion(once all processed have closed) fstat() get stat structure of fd(size, ownership, permissions ) 7

Creating/opening a SHM Creates a new object or opens an existing Parameters - name name of SHM ( shareme ) oflag flags (create, rdonly, rdwr, ) mode permission bits RWX for User, group or other If existing object, set as 0 8

Shared Memory size New SHM objects have length 0 Before mapping, set size ftruncate(fd, size) How to get size of existing object? fstat() 9

Mapping a shared memory mmap set memory mapping in caller s virtual address space Parameters: addr suggest a virtual address for object Normally, use NULL to let system choose length size of mapping (will be rounded up to multiple of page size) prot memory protections (RD, WR, ) flags use MAP_SHARED for POSIX shared memory fd fd returned by shm_open Offset offset of mapped underlying file Returns the mapped virtual address of the SHM 10

Example: Create SHM 11

Example Write SHM 12

Example Read SHM 13

Storing Pointers in SHM Warning - mmap() can map SHM objects at arbitrary locations in memory Example: a pointer is stored in SHM Where it points if SHM is mapped to a different location in another process? Absolute addresses has no meaning Use relative offsets, not absolute addresses 14

Storing Pointers in SHM Example: baseaddr = mmap( ) Store pointer to target in *p Wrong way: *p = target; Correct way: *p = target baseaddr; Dereference: target = baseaddr + *p; 15

POSIX SHM New APIs shmget - get a SHM id (new or existing) shmat - attach to an existing SHM shmdt detach from and existing SHM shmctl perform operations on a SHM 16

shmget #include<sys/types.h> #include<sys/ipc.h> #include<sys/shm.h> int shmget(key_t key, int size, int flag); get a SHM id (new or existing) Params: key system wide SHM identifier ( IPC_PRIVATE for new allocation by the kernel) Size the size of the SHM in bytes Flag permissions and creation control flags Returns an SHM id, or -1 on error 17

shmat\shmdt #include<sys/types.h> #include<sys/ipc.h> #include<sys/shm.h> void* shmat(int shmid, const void* addr, int flag); void* shmdt(const void* addr); Attach an existing SHM identified by shmid Params: shmid SHM id addr suggested virtual address for SHM Flag permissions Returns a pointer to SHM segment, or -1 on error Example: char* buf = (char*)shmat(shmid,0,0); 18

shmctl #include<sys/ipc.h> #include<sys/shm.h> int shmctl(int shmid, int cmd, struct shmid_ds *buf); Perform control operation specified by cmd on the shared memory segment whose identifier is given in shmid Params: shmid SHM id cmd command (IPC_STAT, IPC_SET, IPC_RMID, ) buf status buffer (permissions, uid, gid, key, ) Returns 0 on success, or -1 on error 19

SHM example Create a server and client programs that use a shared memory to pass a message between them. Message is all the letters from a to z The server will wait until the client received the message 20

SHM example - server void main() { char c; int shmid; key_t key; char *shm, *s; key = 5678; // name our shared memory segment 5678 if ((shmid = shmget(key, 27/*size*/, IPC_CREAT 0666/*flags*/)) < 0) { // Create the segment perror("shmget"); exit(1); } if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { // attach the segment to our data space perror("shmat"); exit(1); } // put some things into the memory for the other process to read. s = shm; for (c = 'a'; c <= 'z'; c++) *s++ = c; *s = NULL; // wait until the other process changes the first character of our memory to '*' while (*shm!= '*') sleep(1); } 21

SHM example - client void main() { int shmid; key_t key; char *shm, *s; key = 5678; // get the segment named 5678 if ((shmid = shmget(key, 27/*size*/, 0666/*flags*/)) < 0) { perror("shmget"); exit(1); } // attach the segment to our data space if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { perror("shmat"); exit(1); } // read what the server put in the memory for (s = shm; *s!= NULL; s++) putchar(*s); putchar( \n ); // change the first character of the segment to '*' *shm = '*'; } What are the problems? How could we better handle the synchronization between the two processes? Semaphores!!! 22

Summary Share Memory is a form of IPC There are two common SHM mechanisms: File based Memory based (POSIX) Pointers in shared memory save offsets! There are two equivalent POSIX SHM APIs, which implement - create\get, attach and detach operations The new API is more common (no need for truncate to allocate shared memory size) 23