text
stringlengths 0
234
|
---|
/* map the object into the caller\(aqs address space. */ |
struct shmbuf *shmp = mmap(null, sizeof(*shmp), |
prot_read | prot_write, |
map_shared, fd, 0); |
if (shmp == map_failed) |
errexit("mmap"); |
/* initialize semaphores as process\-shared, with value 0. */ |
if (sem_init(&shmp\->sem1, 1, 0) == \-1) |
errexit("sem_init\-sem1"); |
if (sem_init(&shmp\->sem2, 1, 0) == \-1) |
errexit("sem_init\-sem2"); |
/* wait for \(aqsem1\(aq to be posted by peer before touching |
shared memory. */ |
if (sem_wait(&shmp\->sem1) == \-1) |
errexit("sem_wait"); |
/* convert data in shared memory into upper case. */ |
for (int j = 0; j < shmp\->cnt; j++) |
shmp\->buf[j] = toupper((unsigned char) shmp\->buf[j]); |
/* post \(aqsem2\(aq to tell the peer that it can now |
access the modified data in shared memory. */ |
if (sem_post(&shmp\->sem2) == \-1) |
errexit("sem_post"); |
/* unlink the shared memory object. even if the peer process |
is still using the object, this is okay. the object will |
be removed only after all open references are closed. */ |
shm_unlink(shmpath); |
exit(exit_success); |
} |
.ee |
.in |
.\" |
.ss program source: pshm_ucase_send.c |
the "send" program takes two command-line arguments: |
the pathname of a shared memory object previously created by the "bounce" |
program and a string that is to be copied into that object. |
.pp |
the program opens the shared memory object |
and maps the object into its address space. |
it then copies the data specified in its second argument |
into the shared memory, |
and posts the first semaphore, |
which tells the "bounce" program that it can now access that data. |
after the "bounce" program posts the second semaphore, |
the "send" program prints the contents of the shared memory |
on standard output. |
.pp |
.in +4n |
.ex |
/* pshm_ucase_send.c |
licensed under gnu general public license v2 or later. |
*/ |
#include <string.h> |
#include "pshm_ucase.h" |
int |
main(int argc, char *argv[]) |
{ |
if (argc != 3) { |
fprintf(stderr, "usage: %s /shm\-path string\en", argv[0]); |
exit(exit_failure); |
} |
char *shmpath = argv[1]; |
char *string = argv[2]; |
size_t len = strlen(string); |
if (len > buf_size) { |
fprintf(stderr, "string is too long\en"); |
exit(exit_failure); |
} |
/* open the existing shared memory object and map it |
into the caller\(aqs address space. */ |
int fd = shm_open(shmpath, o_rdwr, 0); |
if (fd == \-1) |
errexit("shm_open"); |
struct shmbuf *shmp = mmap(null, sizeof(*shmp), |
prot_read | prot_write, |
map_shared, fd, 0); |
if (shmp == map_failed) |
errexit("mmap"); |
/* copy data into the shared memory object. */ |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.