2012-02-06 00:16:59 +04:00
|
|
|
/*
|
|
|
|
* shm-server - client program to demonstrate shared memory.
|
|
|
|
*/
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <syscall.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2012-02-16 13:34:42 +04:00
|
|
|
DEFN_SYSCALL2(shm_obtain, 35, char *, size_t *)
|
2012-02-06 00:16:59 +04:00
|
|
|
DEFN_SYSCALL1(shm_release, 36, char *)
|
|
|
|
|
|
|
|
#define SHMSZ 27
|
|
|
|
|
|
|
|
int main(int argc, char ** argv) {
|
|
|
|
char c;
|
|
|
|
volatile char *shm;
|
|
|
|
volatile char *s;
|
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
fprintf(stderr, "%s: expected argument\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
char * key = argv[1];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Attach to the shared memory chunk
|
|
|
|
*/
|
2012-02-16 13:34:42 +04:00
|
|
|
size_t size = SHMSZ;
|
|
|
|
if ((shm = (char *)syscall_shm_obtain(key, &size)) == (char *) NULL) {
|
2012-02-06 00:16:59 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
printf("Server: mounted to 0x%x\n", shm);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now put some things into the memory for the
|
|
|
|
* other process to read.
|
|
|
|
*/
|
|
|
|
s = shm;
|
|
|
|
|
|
|
|
for (c = 'a'; c <= 'z'; c++)
|
|
|
|
*s++ = c;
|
|
|
|
*s = '\0';
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Finally, we wait until the other process
|
|
|
|
* changes the first character of our memory
|
|
|
|
* to '*', indicating that it has read what
|
|
|
|
* we put there.
|
|
|
|
*/
|
|
|
|
while (*shm != '*') {}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|