复制代码代码如下: struct shmid_ds { struct ipc_perm shm_perm;/* Ownership and permissions */ size_tshm_segsz; /* Size of segment (bytes) */ time_tshm_atime; /* Last attach time */ time_tshm_dtime; /* Last detach time */ time_tshm_ctime; /* Last change time */ pid_t shm_cpid;/* PID of creator */ pid_t shm_lpid;/* PID of last shmat(2)/shmdt(2) */ shmatt_tshm_nattch;/* No. of current attaches */ ... };
复制代码代码如下: /* shm_open - open a shared memory file */</p><p>/* Copyright 2002, Red Hat Inc. */</p><p>#include <sys/types.h> #include <sys/mman.h> #include <unistd.h> #include <string.h> #include <fcntl.h> #include <limits.h></p><p>int shm_open (const char *name, int oflag, mode_t mode) { int fd; char shm_name[PATH_MAX+20] = "/dev/shm/";</p><p>/* skip opening slash */ if (*name == "/") ++name;</p><p>/* create special shared memory file name and leave enough space to cause a path/name error if name is too long */ strlcpy (shm_name + 9, name, PATH_MAX + 10);</p><p>fd = open (shm_name, oflag, mode);</p><p>if (fd != -1) { /* once open we must add FD_CLOEXEC flag to file descriptor */ int flags = fcntl (fd, F_GETFD, 0);</p><p>if (flags >= 0) { flags |= FD_CLOEXEC; flags = fcntl (fd, F_SETFD, flags); }</p><p>/* on failure, just close file and give up */ if (flags == -1) { close (fd); fd = -1; } }</p><p>return fd; }
复制代码代码如下: /* shm_unlink - remove a shared memory file */</p><p>/* Copyright 2002, Red Hat Inc. */</p><p>#include <sys/types.h> #include <sys/mman.h> #include <unistd.h> #include <string.h> #include <limits.h></p><p>int shm_unlink (const char *name) { int rc; char shm_name[PATH_MAX+20] = "/dev/shm/";</p><p>/* skip opening slash */ if (*name == "/") ++name;</p><p>/* create special shared memory file name and leave enough space to cause a path/name error if name is too long */ strlcpy (shm_name + 9, name, PATH_MAX + 10);</p><p>rc = unlink (shm_name);</p><p>return rc; }