text
stringlengths 0
234
|
---|
the posix shared memory object implementation on linux makes use |
of a dedicated |
.br tmpfs (5) |
filesystem that is normally mounted under |
.ir /dev/shm . |
.sh examples |
the programs below employ posix shared memory and posix unnamed semaphores |
to exchange a piece of data. |
the "bounce" program (which must be run first) raises the case |
of a string that is placed into the shared memory by the "send" program. |
once the data has been modified, the "send" program then prints |
the contents of the modified shared memory. |
an example execution of the two programs is the following: |
.pp |
.in +4n |
.ex |
$ \fb./pshm_ucase_bounce /myshm &\fp |
[1] 270171 |
$ \fb./pshm_ucase_send /myshm hello\fp |
hello |
.ee |
.in |
.pp |
further detail about these programs is provided below. |
.\" |
.ss program source: pshm_ucase.h |
the following header file is included by both programs below. |
its primary purpose is to define a structure that will be imposed |
on the memory object that is shared between the two programs. |
.pp |
.in +4n |
.ex |
#include <sys/mman.h> |
#include <fcntl.h> |
#include <semaphore.h> |
#include <sys/stat.h> |
#include <stdio.h> |
#include <stdlib.h> |
#include <unistd.h> |
#define errexit(msg) do { perror(msg); exit(exit_failure); \e |
} while (0) |
#define buf_size 1024 /* maximum size for exchanged string */ |
/* define a structure that will be imposed on the shared |
memory object */ |
struct shmbuf { |
sem_t sem1; /* posix unnamed semaphore */ |
sem_t sem2; /* posix unnamed semaphore */ |
size_t cnt; /* number of bytes used in \(aqbuf\(aq */ |
char buf[buf_size]; /* data being transferred */ |
}; |
.ee |
.in |
.\" |
.ss program source: pshm_ucase_bounce.c |
the "bounce" program creates a new shared memory object with the name |
given in its command-line argument and sizes the object to |
match the size of the |
.i shmbuf |
structure defined in the header file. |
it then maps the object into the process's address space, |
and initializes two posix semaphores inside the object to 0. |
.pp |
after the "send" program has posted the first of the semaphores, |
the "bounce" program upper cases the data that has been placed |
in the memory by the "send" program and then posts the second semaphore |
to tell the "send" program that it may now access the shared memory. |
.pp |
.in +4n |
.ex |
/* pshm_ucase_bounce.c |
licensed under gnu general public license v2 or later. |
*/ |
#include <ctype.h> |
#include "pshm_ucase.h" |
int |
main(int argc, char *argv[]) |
{ |
if (argc != 2) { |
fprintf(stderr, "usage: %s /shm\-path\en", argv[0]); |
exit(exit_failure); |
} |
char *shmpath = argv[1]; |
/* create shared memory object and set its size to the size |
of our structure. */ |
int fd = shm_open(shmpath, o_creat | o_excl | o_rdwr, |
s_irusr | s_iwusr); |
if (fd == \-1) |
errexit("shm_open"); |
if (ftruncate(fd, sizeof(struct shmbuf)) == \-1) |
errexit("ftruncate"); |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.