Shared Memory - PowerPoint PPT Presentation

1 / 4
About This Presentation
Title:

Shared Memory

Description:

The shmget system call is used to create or access a shared ... Semaphores: s 0 0X000187cf --ra-ra-ra- root root 2. s 1 0X000187ce --ra-ra-ra- root root 1 ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 5
Provided by: drdaniel7
Category:
Tags: memory | rara | shared

less

Transcript and Presenter's Notes

Title: Shared Memory


1
Shared Memory
  • Creating a Shared Memory Segment
  • Allocated in byte amounts
  • Shared Memory Operations
  • Create
  • Attach
  • Detach
  • Shared Memory Control
  • Remove

2
shmget System Call
  • Function
  • The shmget system call is used to create or
    access a shared memory segment.
  • Include ltsys/types.hgt ltsys/ipc.hgt ltsys/shm.hgt
  • Command int shmget (key_t key, int size,
    int shmflg)
  • Returns Success unique shared memory
    identifier.
  • Failure -1 Sets errno Yes.
  • Arguments
  • key_t key key for creating or accessing shared
    memory
  • int size size in bytes of shared memory segment
    to create. Use 0 for accessing an existing
    segment.
  • int shmflg segment creation condition and access
    permission.

3
shmget() Argument Values
  • key
  • same as before we use getuid() to make it unique
  • size
  • number of bytes to allocate
  • shmflag
  • Creating IPC_CREAT and permissions
  • 0 for accessing only

4
Shared Memory Limits
  • Shared Memory Segment Defaults
    Value
  • Maximum segment size
    1,048,576 bytes
  • Minimum segment size
    1 byte
  • System wide maximum number of segments 100
  • Maximum number of segments per process 6?
  • that can be attached

5
shmat System Call
  • Function
  • used to attach (map) the referenced shared memory
    segment into the calling processs data segment.
    The pointer returned is to the first byte of the
    segment
  • Include ltsys/types.hgt ltsys/ipc.hgt ltsys/shm.hgt
  • Command void shmat ( int shmid, void
    shmaddr, int shmflg)
  • Returns
  • SuccessReference to the data segment address of
    shared memory
  • Failure-1 Sets errnoYes.
  • Arguments
  • int shmid a valid shared memory identifier.
  • void shmaddr allows the calling process some
    flexibility in assigning the location of the
    shared memory.
  • int shmflg access permissions and attachment
    conditions.

6
Notes on shmat() arguments
  • shmid is as before
  • 2nd and 3rd arguments can be used to attach to a
    specific address within shmem, but it is an
    absolute address, requiring knowledge of the
    address of the shared memory segment
  • Almost always, 2nd and 3rd arguments are 0

7
shmdt System Call
  • Function
  • shmdt is used to detach the calling processs
    data segment from the shared memory segment.
  • Include ltsys/types.hgt ltsys/ipc.hgt lt sys/shm.hgt
  • Command int shmdt (void shmaddr)
  • Returns Success 0 Failure -1 Sets errno
    Yes.
  • Argument
  • void shmaddr a reference to an attached memory
    segment (the shared memory pointer).

8
shmctl call - Shared Memory Control
  • Function shmctl 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.
  • Include ltsys/types.hgt ltsys/ipc.hgt ltsys/shm.hgt
  • Command
  • int shmctl(int shmid, int cmd, struct shmid_ds
    buf)
  • Return Success 0 Failure -1 Sets errno
    Yes.
  • Arguments
  • int shmid a valid shared memory segment
    identifier.
  • int cmd the operation shmctl is t perform.
  • struct shmid_ds buf a reference to the
    shmid_ds structure

9
Operations of shmctl()
  • IPC_STAT Return the current value of the
    shmid_ds structure for the shared memory segment
    indicated by the shmid value.
  • IPC_SET Modify a limited number of members in
    the permission structure found within the
    shmid_ds structure.
  • IPC_RMID Remove the system data structure for
    the referenced shared memory identifier (shmid).
    Once all references to the shared memory segment
    are eliminated, the system will remove the actual
    shared memory segment.
  • SHM_LOCK Lock, in memory, the shared memory
    segment referenced by the shmid argument.
    Superuser access required
  • SHM_UNLOCK Unlock the shared memory segment
    referenced by the shmid argument. Superuser
    access required

10
Shared Memory Data Structure
  • / One shmid data structure for each shared
    memory segment in the system. /
  • struct shmid_ds
  • struct ipc_perm shm_perm / operation perms
    /
  • int shm_segsz / size of segment (bytes) /
  • time_t shm_atime / last attach time /
  • time_t shm_dtime / last detach time /
  • time_t shm_ctime / last change time /
  • unsigned short shm_cpid / pid of creator /
  • unsigned short shm_lpid / pid of last
    operator /
  • short shm_nattch / no. of current attaches
    /
  • / the following are private /
  • unsigned short shm_npages / size of segment
    (pages) /
  • unsigned long shm_pages / array of ptrs to
    frames -gt SHMMAX /
  • struct vm_area_struct attaches / descriptors
    for attaches /

11
Virtual Memory Area Structure
  • vm_area_struct
  • defines a memory VMM memory area. There is one of
    these per VM-area/task. A VM area is any part of
    the process virtual memory space that has a
    special rule for the page-fault handlers (ie a
    shared library, the executable area etc).
  • struct vm_area_struct
  • struct mm_struct vm_mm
  • unsigned long vm_start
  • unsigned long vm_end
  • struct vm_area_struct vm_next
  • pgprot_t vm_page_prot
  • unsigned long vm_flags
  • rb_node_t vm_rb
  • struct vm_area_struct vm_next_share
  • struct vm_area_struct vm_pprev_share
  • struct vm_operations_struct vm_ops
  • unsigned long vm_pgoff
  • struct file vm_file
  • unsigned long vm_raend
  • void vm_private_data

12
ipcs ipcrm Command
  • ipcs -b (-b to display the maximum number of
    bytes )
  • T ID KEY MODE OWNER
    GROUP QBYTES
  • Message Queues
  • q 50 0X67028a01 -Rrw-rw---- gray
    other 4096
  • Shared Memory facility not in system
  • T ID KEY MODE OWNER
    GROUP NSEMS
  • Semaphores
  • s 0 0X000187cf --ra-ra-ra- root
    root 2
  • s 1 0X000187ce --ra-ra-ra- root
    root 1
Write a Comment
User Comments (0)
About PowerShow.com