Shared Memory Dr. Yingwu Zhu.

Slides:



Advertisements
Similar presentations
Florida State UniversityCOP5570 – Advanced Unix Programming IPC mechanisms Pipes Sockets System V IPC –Message Queues –Semaphores –Shared Memory.
Advertisements

CS 450 Module R4. R4 Overview Due on March 11 th along with R3. R4 is a small yet critical part of the MPX system. In this module, you will add the functionality.
1 CSE 380 Computer Operating Systems Instructor: Insup Lee University of Pennsylvania Fall 2003 Lecture Note 2.6: Message-Based Communication.
Unix IPC and Synchronization. Pipes and FIFOs Pipe: a circular buffer of fixed size written by one process and read by another int pipe(int fildes[2])
Shared Memory  Creating a Shared Memory Segment Allocated in byte amounts  Shared Memory Operations Create Attach Detach  Shared Memory Control Remove.
XSI IPC Message Queues Semaphores Shared Memory. XSI IPC Each XSI IPC structure has two ways to identify it An internal (within the Kernel) non negative.
Shared memory. Process A Process B Physical Memory Virtual address space A Virtual address space B Share Instruction memory Data Instruction memory Data.
Inter-process communication (IPC) using Shared Memory & Named Pipes CSE 5520/4520 Wireless Networks.
System V IPC (InterProcess Communication) Messages Queue, Shared Memory, and Semaphores.
1 Tuesday, June 27, 2006 "If the 8086 architects had designed a car, they would have produced one with legs, to be compatible with the horse." - Anonymous.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
UNIX IPC CSE 121 Spring 2003 Keith Marzullo. CSE 121 Spring 2003Review of Concurrency2 Creating a UNIX process A process is created by making an exact.
University of Pennsylvania 10/3/00CSE 380 Interprocess Communication CSE 380 Lecture Note 8 Insup Lee.
CSE 451 Section 4 Project 2 Design Considerations.
NCHU System & Network Lab Lab 10 Message Queue and Shared Memory.
Inter Process Communication. Introduction Traditionally describe mechanism for message passing between different processes that are running on some operating.
Unix IPC Unix has three major IPC constructs to facilitate interaction between processes: Message Queues (this PowerPoint document) permit exchange of.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Operating Systems, 132 Practical Session 9, Memory 1.
CS162B: Semaphores (and Shared Memory) Jacob T. Chan.
Lecture 6 Introduction to Distributed Programming System V IPC: Message Queues, Shared Memory, Semaphores.
Inter-Process Communication Mechanisms CSE331 Operating Systems Design.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 3: Processes 170 UCSB T. Yang Some of slides are from the Chapter.
The Third Assignment COSC 4330/6310 Spring Implementing delays To be able to test the semaphores, we must run the program in real time To be able.
S -1 Shared Memory. S -2 Motivation Shared memory allows two or more processes to share a given region of memory -- this is the fastest form of IPC because.
System V IPC Provides three mechanisms for InterProcess Communication (IPC) : Messages : exchange messages with any process or server. Semaphores : allow.
2.3 InterProcess Communication (IPC)
TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION2 OPERATING SYSTEMS.
Lecture 7 Introduction to Distributed Programming System V IPC: Message Queues, Shared Memory, Semaphores.
Florida State UniversityCOP5570 – Advanced Unix Programming Today’s topics System V Interprocess communication (IPC) mechanisms –Message Queues –Semaphores.
Inter-process Communication:
1 Shared Memory. 2  Introduction  Creating a Shared Memory Segment  Shared Memory Control  Shared Memory Operations  Using a File as Shared Memory.
Semaphores Creating and Accessing Semaphore Sets Semaphore Operations
Interprocess Communication Bosky Agarwal CS 518. Presentation Layout Introduction Pipes FIFOs System V IPC 1.Using pipes 2.Working of pipes 3.Pipe Data.
IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /* */
Socket Programming Lab 1 1CS Computer Networks.
Operating Systems Process Creation
2.3 interprocess communcation (IPC) (especially via shared memory & controlling access to it)
File descriptor table File descriptor (integer)File name 0stdin 1stdout 2stderr Use open(), read(), write() system calls to access files Think what happens.
UNIX IPC CSC345.
Inter Process Comunication in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Semaphores Chapter 7 from Inter-process Communications in Linux:
Advanced UNIX IPC Facilities After Haviland, et al.’s book.
Slide 9-1 Copyright © 2004 Pearson Education, Inc. Inter process Communication Mechanisms  Allow arbitrary processes to exchange data and synchronize.
Interprocess Communication
IPC Message Queue. Data Structure msgque IP_NOUSED.
Message Queues. Unix IPC Package ● Unix System V IPC package consists of three things – Messages – allows processes to send formatted data streams to.
Shared Memory Dr. Yingwu Zhu. Overview System V shared memory Let multiple processes attach a segment of physical memory to their virtual address spaces,
Operating Systems Practical Session 8, Memory 1. Quick recap - Swapping Swapping basically means we bring the entire process to memory, use it, and possibly.
Operating Systems: Inter-Process Communication Pål Halvorsen 13 March 2016 INF1060: Introduction to Operating Systems and Data Communication.
Textbook: Advanced Programming in the UNIX Environment, 2 nd Edition, W. Richard Stevens and Stephen A. Rago 1 Chapter 15. Interprocess Communication System.
Operating Systems Inter-Process Communication Shared Memory & Semaphores Moti Geva
Distributed and Parallel Processing George Wells.
C Threads and Semaphores
Lecture 7 Introduction to Distributed Programming System V IPC:
Practical Session 7, Memory
Message queue Inter process communication primitive
Lesson One – Creating a thread
Operating Systems: Inter-Process Communication
Threads Threads.
Unix IPC Unix has three major IPC constructs to facilitate interaction between processes: Message Queues (this PowerPoint document) permit exchange of.
Hwajung Lee ITEC452 Distributed Computing Lecture 7 Interprocess Communication (IPC): An Overview.
Interprocess Communication and Synchronization using System V IPC
Hwajung Lee ITEC452 Distributed Computing Lecture 2 Interprocess Communication (IPC): An Overview.
Message Queues.
Unix programming Term: Unit-V I PPT Slides
Inter-Process Communication
Introduction to Distributed Programming System V IPC:
| Website for Students | VTU -NOTES -Question Papers
Shared Memory Dr. Yingwu Zhu Feb, 2007.
Presentation transcript:

Shared Memory Dr. Yingwu Zhu

Overview System V shared memory Let multiple processes attach a segment of physical memory to their virtual address spaces, an efficient means of passing data between processes When write access is allowed for more than one process, an outside protocol or mechanism such as a semaphore can be used to prevent inconsistencies and collisions.

Overview: Shared Memory An efficient means of process communication Creation: shmget() Access control: shmctl() Attached to addr. space: shmat() Detached from addr. space : shmdt() Header files #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h>

Notes A process creates a shared memory segment using shmget(). The original owner of a shared memory segment can assign ownership to another user with shmctl(). It can also revoke this assignment. Other processes with proper permission can perform various control functions on the shared memory segment using shmctl(). Once created, a shared segment can be attached to a process address space using shmat(). It can be detached using shmdt() (see shmop()). The attaching process must have the appropriate permissions for shmat(). Once attached, the process can read or write to the segment, as allowed by the permission requested in the attach operation. A shared memory segment is described by a control structure with a unique ID that points to an area of physical memory. The identifier of the segment is called the shmid. The structure definition for the shared memory segment control structures and prototypes can be found in <sys/shm.h>.

Accessing a share memory int shmget(key_t key, size_t size, int shmflg) key: share memory identifier size: number of bytes shmflg: access permissions flags and creation control flags specify a key of IPC_PRIVATE that will be used only by your process and it's children. specify IPC_CREAT, if a segment for the key does not exist, it is created If you also specify IPC_EXCL, you are guranteed a new segment or an error will be returned. #define PERM_UREAD 0400 #define PERM_UWRITE 0200 #define PERM_GREAD 0040 #define PERM_GWRITE 0020 #define PERM_OREAD 0004 #define PERM_OWRITE 0002 return: shared memory segment ID

Controlling a Shared Mem. Seg. int shmctl(int shmid, int cmd, struct shmid_ds *buf); cmd SHM_LOCK -- Lock the specified shared memory segment in memory. The process must have the effective ID of superuser to perform this command. SHM_UNLOCK -- Unlock the shared memory segment. The process must have the effective ID of superuser to perform this command. IPC_STAT -- Return the status information contained in the control structure and place it in the buffer pointed to by buf. The process must have read permission on the segment to perform this command. IPC_SET -- Set the effective user and group identification and access permissions. The process must have an effective ID of owner, creator or superuser to perform this command. IPC_RMID -- Remove the shared memory segment. The buf is a sructure of type struct shmid_ds which is defined in <sys/shm.h>

Attaching a Shared Mem. Seg. void *shmat (int shmid, const void*shmaddr, int shmflg); returns a pointer, shmaddr, to the head of the shared segment associated with a valid shmid Normally we set the 2nd argument, shmaddr, 0, the system picks the attachment address Normally we set the 3rd argument, shmflg, 0, by default, attached segments are accessible for reading and writing Specify the access permissions for the shared memory segment and to request special attachment conditions, such as an aligned address or a read-only segment

Detaching a Share Mem. Seg. int shmdt(const void* shmaddr); Detaches the shared memory segment located at the address indicated by shmaddr

Deleting Segment When you detach from the segment, it isn't destroyed. Nor is it removed when everyone detaches from it. You have to specifically destroy it using a call to shmctl(), similar to the control calls for the other System V IPC functions: shmctl(shmid, IPC_RMID, NULL);

Examples: client-server communication main() { char c; int shmid; key_t key; char *shm, *s; /* * We'll name our shared memory segment * "5678". */ key = 5678; /* * Create the segment. */ if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) { perror("shmget"); exit(1); } /* * Now we attach the segment to our data space. */ if ((shm = (char*)shmat(shmid, NULL, 0)) == (char *) -1) { perror("shmat"); exit(1); } /* * Now put some things into the memory for the * other process to read. */ s = (char*)shm; for (c = 'a'; c <= 'z'; c++) *s++ = c; *s = NULL; /* * Finally, we wait until the other process * changes the first character of our memory * to '*', indicating that it has read what * we put there. */ while (*shm != '*') sleep(1); exit(0); } //client.c main() { int shmid; key_t key; char *shm, *s; /* * We need to get the segment named * "5678", created by the server. */ key = 5678; /* * Locate the segment. */ if ((shmid = shmget(key, SHMSZ, 0666)) < 0) { perror("shmget"); exit(1); } /* * Now we attach the segment to our data space. */ if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) { perror("shmat"); exit(1); } /* * Now read what the server put in the memory. */ for (s = shm; *s != NULL; s++) putchar(*s); putchar('\n'); /* * Finally, change the first character of the * segment to '*', indicating we have read * the segment. */ *shm = '*'; exit(0); }

Fork and shmat After fork(), the child inherits the attached shared memory segments