diff options
Diffstat (limited to 'man2/shmop.2')
-rw-r--r-- | man2/shmop.2 | 74 |
1 files changed, 37 insertions, 37 deletions
diff --git a/man2/shmop.2 b/man2/shmop.2 index a28d249e1..083b039ab 100644 --- a/man2/shmop.2 +++ b/man2/shmop.2 @@ -316,7 +316,7 @@ The following header file is included by the "reader" and "writer" programs: .\" SRC BEGIN (svshm_string.h) .EX /* svshm_string.h - +\& Licensed under GNU General Public License v2 or later. */ #include <sys/types.h> @@ -326,10 +326,10 @@ The following header file is included by the "reader" and "writer" programs: #include <stdio.h> #include <stdlib.h> #include <string.h> - +\& #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e } while (0) - +\& union semun { /* Used in calls to semctl() */ int val; struct semid_ds * buf; @@ -338,7 +338,7 @@ union semun { /* Used in calls to semctl() */ struct seminfo * __buf; #endif }; - +\& #define MEM_SIZE 4096 .EE .\" SRC END @@ -357,7 +357,7 @@ shared memory segment by the "writer". .\" SRC BEGIN (svshm_string_read.c) .EX /* svshm_string_read.c - +\& Licensed under GNU General Public License v2 or later. */ #include <stdio.h> @@ -365,9 +365,9 @@ shared memory segment by the "writer". #include <sys/ipc.h> #include <sys/sem.h> #include <sys/shm.h> - +\& #include "svshm_string.h" - +\& int main(void) { @@ -375,52 +375,52 @@ main(void) 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) errExit("shmget"); - +\& semid = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600); if (semid == \-1) errExit("semget"); - +\& /* Attach shared memory into our address space. */ - +\& addr = shmat(shmid, NULL, SHM_RDONLY); if (addr == (void *) \-1) errExit("shmat"); - +\& /* Initialize semaphore 0 in set with value 1. */ - +\& arg.val = 1; if (semctl(semid, 0, SETVAL, arg) == \-1) errExit("semctl"); - +\& printf("shmid = %d; semid = %d\en", shmid, 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) errExit("semop"); - +\& /* Print the string from shared memory. */ - +\& printf("%s\en", addr); - +\& /* Remove shared memory and semaphore set. */ - +\& if (shmctl(shmid, IPC_RMID, NULL) == \-1) errExit("shmctl"); if (semctl(semid, 0, IPC_RMID, dummy) == \-1) errExit("semctl"); - +\& exit(EXIT_SUCCESS); } .EE @@ -439,7 +439,7 @@ and then decrements the semaphore value to 0 in order to inform the .\" SRC BEGIN (svshm_string_write.c) .EX /* svshm_string_write.c - +\& Licensed under GNU General Public License v2 or later. */ #include <stdio.h> @@ -447,9 +447,9 @@ and then decrements the semaphore value to 0 in order to inform the #include <string.h> #include <sys/sem.h> #include <sys/shm.h> - +\& #include "svshm_string.h" - +\& int main(int argc, char *argv[]) { @@ -457,41 +457,41 @@ main(int argc, char *argv[]) char *addr; size_t len; struct sembuf sop; - +\& if (argc != 4) { fprintf(stderr, "Usage: %s shmid semid string\en", argv[0]); exit(EXIT_FAILURE); } - +\& len = strlen(argv[3]) + 1; /* +1 to include trailing \[aq]\e0\[aq] */ if (len > MEM_SIZE) { fprintf(stderr, "String is too big!\en"); 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) errExit("shmat"); - +\& memcpy(addr, argv[3], len); - +\& /* Decrement semaphore to 0. */ - +\& sop.sem_num = 0; sop.sem_op = \-1; sop.sem_flg = 0; - +\& if (semop(semid, &sop, 1) == \-1) errExit("semop"); - +\& exit(EXIT_SUCCESS); } .EE |