1 Shared Memory. 2  Introduction  Creating a Shared Memory Segment  Shared Memory Control  Shared Memory Operations  Using a File as Shared Memory.

Slides:



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

More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
1 CSE 380 Computer Operating Systems Instructor: Insup Lee University of Pennsylvania Fall 2003 Lecture Note 2.6: Message-Based Communication.
CSCC69: Operating Systems
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.
The Environment of a UNIX Process. Introduction How is main() called? How are arguments passed? Memory layout? Memory allocation? Environment variables.
Daemon Processes Long lived utility processes Often started at system boot and ended when system shuts down Run in the background with no controlling terminal.
Read vs. mmap Tan Li. Man mmap #include void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset); int munmap(void *start, size_t.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
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.
1 Process Description and Control Chapter 3 = Why process? = What is a process? = How to represent processes? = How to control processes?
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.
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.
Fundamentals CIS 552. Fundamentals Low-level I/O (read/write using system calls)  Opening/Creating files  Reading & Writing files  Moving around in.
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.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
System V IPC Provides three mechanisms for InterProcess Communication (IPC) : Messages : exchange messages with any process or server. Semaphores : allow.
Module 2 Programming with Processes. Processes Process -- a program in execution –May share code segments –Typically do not share data, stack, heap OS.
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.
NCHU System & Network Lab Lab 11 Memory Mapped File.
1 Semaphores Chapter 7 from Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date: January.
Process Control Process identifiers Process creation fork and vfork wait and waitpid Race conditions exec functions system function.
Semaphores Creating and Accessing Semaphore Sets Semaphore Operations
IPC Programming. Process Model Processes can be organized into a parent-child hierarchy. Consider the following example code: /* */
CSNB334 Advanced Operating Systems 4. Concurrency : Mutual Exclusion and Synchronization.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
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.
Processes and Virtual Memory
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,
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
System calls for Process management Process creation, termination, waiting.
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
C Threads and Semaphores
Using Processes.
Unix IPC Unix has three major IPC constructs to facilitate interaction between processes: Message Queues (this PowerPoint document) permit exchange of.
Shared Memory Dr. Yingwu Zhu.
Making Virtual Memory Real: The Linux-x86-64 way
Advanced Network Programming spring 2007
Unix programming Term: Unit-V I PPT Slides
Virtual Memory CSCI 380: Operating Systems Lecture #7 -- Review and Lab Suggestions William Killian.
Virtual Memory.
Process Control B.Ramamurthy 2/22/2019 B.Ramamurthy.
Virtual Memory: Systems CSCI 380: Operating Systems
CSCI 380: Operating Systems William Killian
Unix Process Control B.Ramamurthy 4/11/2019 B.Ramamurthy.
Process Description and Control in Unix
Process Description and Control in Unix
Structure of Processes
Shared Memory Dr. Yingwu Zhu Feb, 2007.
Presentation transcript:

1 Shared Memory

2  Introduction  Creating a Shared Memory Segment  Shared Memory Control  Shared Memory Operations  Using a File as Shared Memory

3 Introduction  Shared memory allows multiple processes to share virtual memory space.  This is the fastest but not necessarily the easiest (synchronization-wise) way for processes to communicate with one another.  In general, one process creates or allocates the shared memory segment.  The size and access permissions for the segment are set when it is created.  The process then attaches the shared segment, causing it to be mapped into its current data space. SHARED MEMORY

4 Introduction  If needed, the creating process then initializes the shared memory.  Once created, and if permissions permit, other processes can gain access to the shared memory segment and map it into their data space.  Each process accesses the shared memory relative to its attachment address.  While the data that these processes are referencing is in common, each process uses different attachment address values. SHARED MEMORY

5 Introduction  For each process involved, the mapped memory appears to be no different from any other of its memory addresses. SHARED MEMORY

6 Creating a Shared Memory Segment  The shmget system call is used to create the shared memory segment and generate the associated system data structure or to gain access to an existing segment.  The shared memory segment and the system data structure are identified by a unique shared memory identifier that the shmget system call returns. (Table 8.1) SHARED MEMORY

7 Creating a Shared Memory Segment Return int shmget(key_t key, int size,int shmflg); Summary 2Manual Section Include File(s) Sets errnoFailureSuccess YesShared memory identifier. Table 8.1. Summary of the shmget System Call. SHARED MEMORY

8 Creating a Shared Memory Segment  The shmget system call creates a new shared memory segment if  The value for its first argument, key, is the symbolic constant IPC_PRIVATE, or  the value key is not associated with an existing shared memory identifier and the IPC_CREAT flag is set as part of the shmflg argument or  the value key is not associated with an existing shared memory identifier and the IPC_CREAT along with the IPC_EXCL flag have been set as part of the shmflg argument. SHARED MEMORY

9 Creating a Shared Memory Segment  As with previous IPC system calls for message queues and semaphores, the ftok library function can be used to generate a key value.  The argument size determines the size in bytes of the shared memory segment.  If we are using shmget to access an existing shared memory segment, size can be set to 0, as the segment size is set by the creating process. SHARED MEMORY

10 Creating a Shared Memory Segment  The last argument for shmget, shmflg, is used to indicate segment creation conditions (e.g., IPC_CREAT, IPC_EXCL) and access permissions (stored in the low order 9 bits of shmflg).  At this time the system does not use the execute permission settings.  To specify creation conditions along with access permissions, the individual items are bitwise ORed. SHARED MEMORY

11 Creating a Shared Memory Segment  The shmget system call does not entitle the creating process to actually use the allocated memory.  It merely reserves the requested memory.  To be used by the process, the allocated memory must be attached to the process using a separate system call. SHARED MEMORY

12 Creating a Shared Memory Segment  If shmget is successful in allocating a shared memory segment, it returns an integer shared memory identifier.  If shmget fails, it returns a value of -1 and sets the value in errno to indicate the specific error condition.  Example 9.1 shows creating of the shared memory segments. You can run it multiple times and see the results by ipcs –m system command Example 9.1 SHARED MEMORY

13 Shared Memory Control  The shmctl system call permits the user to perform a number of generalized control operations on an existing shared memory segment and on the system shared memory data structure. SHARED MEMORY

14 Shared Memory Control Return int shmctl(int shmid, int cmd, struct shmid_ds *buf); Summary 2Manual Section Include File(s) Sets errnoFailureSuccess Yes0 Table 8.4. Summary of the shmctl System Call. SHARED MEMORY

15 Shared Memory Control  There are three arguments for the shmctl system call:  The first, shmid, is a valid shared memory segment identifier generated by a prior shmget system call.  The second argument, cmd, specifies the operation shmctl is to perform.  The third argument, buf, is a reference to a structure of the type shmid_ds. SHARED MEMORY

16 Shared Memory Control  If shmctl is successful, it returns a value of 0; otherwise, it returns a value of -1 and sets the value in errno to indicate the specific error condition. SHARED MEMORY

17 Shared Memory Operations  There are two shared memory operation system calls.  The first, shmat, is used to attach (map) the referenced shared memory segment into the calling process's data segment. (Table 8.6.) SHARED MEMORY

18 Shared Memory Operations Return void *shmat(int shmid, const void *shmaddr, int shmflg); Summary 2Manual Section Include File(s) Sets errnoFailureSuccess YesReference to the data segment Table 8.6. Summary of the shmat System Call. SHARED MEMORY

19 Shared Memory Operations  The first argument to shmat, shmid, is a valid shared memory identifier.  The second argument, shmaddr, allows the calling process some flexibility in assigning the location of the shared memory segment.  If a nonzero value is given, shmat uses this as the attachment address for the shared memory segment.  If shmaddr is 0, the system picks the attachment address.  In most situations, it is advisable to use a value of 0 and have the system pick the address. SHARED MEMORY

20 Shared Memory Operations  The third argument, shmflg, is used to 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.  The values of shmaddr and shmflg are used by the system to determine the attachment address. SHARED MEMORY

21 Shared Memory Operations  When shmat is successful, it returns the address of the actual attachment.  If shmat fails, it returns a value of -1 and sets errno to indicate the source of the error.  Remember that after a fork, the child inherits the attached shared memory segment(s).  After an exec or an exit attached, shared memory segment(s) are detached but are not destroyed. SHARED MEMORY

22 Shared Memory Operations  The second shared memory operation, shmdt, is used to detach the calling process's data segment from the shared memory segment. (Table 8.8.) SHARED MEMORY

23 Shared Memory Operations Return int shmdt ( const void *shmaddr); Summary 2Manual Section Include File(s) Sets errnoFailureSuccess Yes0 Table 8.8. Summary of the shmdt System Call SHARED MEMORY

24 Shared Memory Operations  The shmdt system call has one argument, shmaddr, which is a reference to an attached memory segment.  If shmdt is successful in detaching the memory segment, it returns a value of 0.  If the shmdt call fails, it returns a value of -1 and sets errno.  See Example 9.2 that shows the example of sharing a memory segment by parent and childExample 9.2 SHARED MEMORY

25 Using a File as Shared Memory  mmap system call can be used to map a file to a process's virtual memory address space.  In many ways mmap is more flexible than its shared memory system call counterpart.  Once a mapping has been established, standard system calls rather than specialized system calls can be used to manipulate the shared memory object.  Unlike memory, the contents of a file are nonvolatile and will remain available even after a system has been shut down (and rebooted). (Table 8.11). SHARED MEMORY

26 Using a File as Shared Memory Return #ifdef _POSIX_MAPPED_FILES <-- 1 void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset); #endif (1)If _POSIX_MAPPED_FILES has been defined. Summary 2Manual Section Include File(s) Sets errnoFailureSuccess Yes MAP_FAILED ((void *) -1) A pointer to the mapped area Table Summary of the mmap System Call. SHARED MEMORY

27 Using a File as Shared Memory  The mmap system call requires six arguments.  The first, start, is the address for attachment. As with the shmat system call, this argument is most often set to 0, which directs the system to choose a valid attachment address.  The number of bytes to be attached is indicated by the second argument, length.  The third argument, prot, is used to set the type of access (protection) for the segment. SHARED MEMORY

28 Using a File as Shared Memory  The fifth argument, fd, is a valid open file descriptor. Once the mapping is established, the file can be closed.  The sixth argument, offset, is used to set the starting position for the mapping.  If the mmap system call is successful, it returns a reference to the mapped memory object.  If the call fails, it returns the defined constant MAP_FAILED (which is actually the value -1 cast to a void * ).  See Example 9.3 that uses mmapExample 9.3 SHARED MEMORY

29 Using a File as Shared Memory  While the system will automatically unmap a region when a process terminates, the system call munmap can be used to explicitly unmap pages of memory. (Table 8.16) SHARED MEMORY

30 Using a File as Shared Memory Return #ifdef _POSIX_MAPPED_FILES int munmap(void *start, size_t length); #endif Summary 2Manual Section Include File(s) Sets errnoFailureSuccess Yes 0 Table Summary of the munmap System Call. SHARED MEMORY

31 Using a File as Shared Memory  The munmap system call is passed the starting address of the memory mapping (argument start) and the size of the mapping (argument length).  If the call is successful, it returns a value of 0.  Future references to unmapped addresses generate a SIGVEGV signal.  If the munmap system call fails, it returns the value -1 and sets the value in errno to EINVAL. SHARED MEMORY