红联首页 凝聚Linux人的力量
菜鸟过关 | 精华文档 | 同城人(交友) | 我与Linux的故事 | Linux新闻 | Linux视频 | Linux人才 | 软件下载 | 大学校园 | English
发新话题
打印

shared memory in Linux

shared memory in Linux

Shared Memory is an efficeint means of passing data between programs. One program will create a memory portion which other processes (if permitted) can access.

In the Solaris 2.x operating system, the most efficient way to implement shared memory applications is to rely on the mmap() function and on the system's native virtual memory facility. Solaris 2.x also supports System V shared memory, which is another way to let multiple processes attach a segment of physical memory to their virtual address spaces. 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.

#include   
#include   
int shmget(key_t key, int size, int shmflg);  
char *shmat ( int shmid, char *shmaddr, int shmflg )  
int shmdt ( char *shmaddr)  
int shmctl(int shmid, int cmd, struct shmid_ds *buf);  

        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 -> SHMMAX */  
                struct vm_area_struct *attaches; /* descriptors for attaches */  
        };  

shm_perm  

   This is an instance of the ipc_perm structure, which is defined for us in  
   linux/ipc.h. This holds the permission information for the segment,  
   including the access permissions, and information about the creator of the  
   segment (uid, etc).  

shm_segsz  

   Size of the segment (measured in bytes).  

shm_atime  

   Time the last process attached the segment.  

shm_dtime  

   Time the last process detached the segment.  

shm_ctime  

   Time of the last change to this structure (mode change, etc).  

shm_cpid  

   The PID of the creating process.  

shm_lpid  

   The PID of the last process to operate on the segment.  

shm_nattch  

   Number of processes currently attached to the segment.  


Shared memory, semophore, mutex will be applied together in the IPC scenario.

TOP

发新话题