Skip to content

Instantly share code, notes, and snippets.

@usedbytes
Last active September 18, 2018 21:47
Show Gist options
  • Save usedbytes/0bf272864bfbc8b561c10e48d590116f to your computer and use it in GitHub Desktop.
Save usedbytes/0bf272864bfbc8b561c10e48d590116f to your computer and use it in GitHub Desktop.
Sharing VCSM buffers cross-process
#include <string.h>
#include <stdio.h>
#include <interface/vcsm/user-vcsm.h>
int main(int argc, char *argv[])
{
int ret;
vcsm_init();
int hnd = vcsm_malloc(4096, "Process 1");
if (hnd == 0) {
printf("Alloc failed %d\n", hnd);
}
printf("Allocated handle %d\n", hnd);
char *p = vcsm_lock(hnd);
if (!p) {
printf("Map failed\n");
}
printf("Mapped at %p\n", p);
memset(p, 0, 4096);
sprintf(p, "This is a message in handle %d\n", hnd);
ret = vcsm_unlock_ptr(p);
if (ret != 0) {
printf("Unlock failed %d\n", ret);
}
printf("Now run p2, press a key, and enter...\n");
getchar();
p = vcsm_lock(hnd);
if (!p) {
printf("Map failed\n");
}
printf("Mapped at %p\n", p);
printf("Buffer contains:\n%s\n", p);
ret = vcsm_unlock_ptr(p);
if (ret != 0) {
printf("Unlock failed %d\n", ret);
}
vcsm_free(hnd);
vcsm_exit();
return 0;
}
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <interface/vcsm/user-vcsm.h>
int main(int argc, char *argv[])
{
int ret;
if (argc != 2) {
printf("Usage: %s handle\n", argv[0]);
printf("\t Import VCSM handle 'handle'\n");
return 1;
}
vcsm_init();
int hnd = atoi(argv[1]);
printf("Importing handle %d\n", hnd);
int shared = vcsm_malloc_share(hnd);
printf("Imported as handle %d\n", shared);
char *p = vcsm_lock(shared);
if (!p) {
printf("Map failed\n");
}
printf("Mapped at %p\n", p);
printf("Buffer contains:\n%s\n", p);
sprintf(p + strlen(p), "And this is a response, via handle %d\n", shared);
ret = vcsm_unlock_ptr(p);
if (ret != 0) {
printf("Unlock failed %d\n", ret);
}
vcsm_free(shared);
vcsm_exit();
return 0;
}
Build the two apps:
```
gcc -L/opt/vc/lib -I/opt/vc/include -o p1 -l vcsm p1.c
gcc -L/opt/vc/lib -I/opt/vc/include -o p2 -l vcsm p2.c
```
Run p1, then run p2 in another shell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment