SHMOP(2) System Calls Manual SHMOP(2) shmat, shmdt - System V (libc -lc) #include void *shmat(int shmid, const void *_Nullable shmaddr, int shmflg); int shmdt(const void *shmaddr); shmat() shmat() System V shmid . shmaddr : o shmaddr NULL ( ) . o shmaddr NULL SHM_RND shmflg shmaddr SHMLBA. o shmaddr . SHM_RND shmflg: SHM_EXEC ( 2.6.9) . . SHM_RDONLY . . . . SHM_REMAP ( ) shmaddr . ( EINVAL .) shmaddr NULL. brk(2) . . - . shmat() shmid_ds ( shmctl(2)) : o shm_atime . o shm_lpid . o shm_nattch . shmdt() shmdt() shmaddr . shmaddr shmat() . shmdt() shmid_ds : o shm_dtime . o shm_lpid . o shm_nattch . 0 . shmat() (void *) -1 errno . shmdt() 0 -1 errno . shmat() : EACCES CAP_IPC_OWNER IPC . EIDRM shmid . EINVAL shmid ( SHM_RND) shmaddr shmaddr SHM_REMAP shmaddr NULL. ENOMEM . shmdt() : EINVAL shmaddr shmaddr . POSIX.1-2008. POSIX.1-2001 SVr4. SVID 3 ( ) shmaddr char * const void * shmat() char * void *. POSIX.1-2024 (void *) -1 SHM_FAILED. fork(2) . execve(2) . _exit(2) . shmat() shmaddr NULL . . ( ) . . POSIX.1 . shmat(): SHMLBA . shmat() . . SHMLBA . ( SHMLBA .) (SHMSEG). . . . "" System V System V. . $ ./svshm_string_read; shmid = 1114194 semid = 15 "". "" : "" . . $ ./svshm_string_write 1114194 15 'Hello, world'; "" : Hello, world : svshm_string.h "" "": /* svshm_string.h Licensed under GNU General Public License v2 or later. */ #ifndef SVSHM_STRING_H #define SVSHM_STRING_H #include #include #include #include union semun { /* Used in calls to semctl() */ int val; struct semid_ds *buf; unsigned short *array; #if defined(__linux__) struct seminfo *__buf; #endif }; #define MEM_SIZE 4096 #endif // include guard : svshm_string_read.c "" . 1. 0 "". /* svshm_string_read.c Licensed under GNU General Public License v2 or later. */ #include #include #include #include #include #include #include "svshm_string.h" int main(void) { int semid, shmid; char *addr; union semun arg, dummy; struct sembuf sop; /* Create shared memory and semaphore set containing one semaphore. */ shmid = shmget(IPC_PRIVATE, MEM_SIZE, IPC_CREAT | 0600); if (shmid == -1) err(EXIT_FAILURE, "shmget"); semid = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600); if (semid == -1) err(EXIT_FAILURE, "semget"); /* Attach shared memory into our address space. */ addr = shmat(shmid, NULL, SHM_RDONLY); if (addr == (void *) -1) err(EXIT_FAILURE, "shmat"); /* Initialize semaphore 0 in set with value 1. */ arg.val = 1; if (semctl(semid, 0, SETVAL, arg) == -1) err(EXIT_FAILURE, "semctl"); printf("shmid = %d\n", shmid); printf("semid = %d\n", semid); /* Wait for semaphore value to become 0. */ sop.sem_num = 0; sop.sem_op = 0; sop.sem_flg = 0; if (semop(semid, &sop, 1) == -1) err(EXIT_FAILURE, "semop"); /* Print the string from shared memory. */ printf("%s\n", addr); /* Remove shared memory and semaphore set. */ if (shmctl(shmid, IPC_RMID, NULL) == -1) err(EXIT_FAILURE, "shmctl"); if (semctl(semid, 0, IPC_RMID, dummy) == -1) err(EXIT_FAILURE, "semctl"); exit(EXIT_SUCCESS); } : svshm_string_write.c : "" . 0 "" . /* svshm_string_write.c Licensed under GNU General Public License v2 or later. */ #include #include #include #include #include #include #include "svshm_string.h" int main(int argc, char *argv[]) { int semid, shmid; char *addr; size_t size; struct sembuf sop; if (argc != 4) { fprintf(stderr, "Usage: %s shmid semid string\n", argv[0]); exit(EXIT_FAILURE); } size = strlen(argv[3]) + 1; /* +1 to include trailing '\0' */ if (size > MEM_SIZE) { fprintf(stderr, "String is too big!\n"); exit(EXIT_FAILURE); } /* Get object IDs from command-line. */ shmid = atoi(argv[1]); semid = atoi(argv[2]); /* Attach shared memory into our address space and copy string (including trailing null byte) into memory. */ addr = shmat(shmid, NULL, 0); if (addr == (void *) -1) err(EXIT_FAILURE, "shmat"); memcpy(addr, argv[3], size); /* Decrement semaphore to 0. */ sop.sem_num = 0; sop.sem_op = -1; sop.sem_flg = 0; if (semop(semid, &sop, 1) == -1) err(EXIT_FAILURE, "semop"); exit(EXIT_SUCCESS); } brk(2), mmap(2), shmctl(2), shmget(2), capabilities(7), shm_overview(7), sysvipc(7) 3 . . : . 6.18 8 2026 SHMOP(2)