User Tools

Site Tools


notes:sharing_pthreads_primitives_between_processes

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

notes:sharing_pthreads_primitives_between_processes [2013/01/31 12:43]
andy created
notes:sharing_pthreads_primitives_between_processes [2013/01/31 13:23]
andy Updated to use mmap() which doesn't leave shared memory hanging around
Line 10: Line 10:
  */  */
  
-#include <sys/ipc.h> +#include <sys/mman.h>
-#include <sys/shm.h>+
 #include <​sys/​stat.h>​ #include <​sys/​stat.h>​
 #include <​sys/​types.h>​ #include <​sys/​types.h>​
Line 73: Line 72:
   pthread_mutex_t *mutex;   pthread_mutex_t *mutex;
   pthread_mutexattr_t mutexattr;   pthread_mutexattr_t mutexattr;
-  int shmid; 
   char *shared_block;​   char *shared_block;​
  
-  /* Create and attach ​shared memory segment and place mutex and condition +  /* mmap() a shared memory segment and place primitives in it */ 
-   * variable within ​it */ +  ​shared_block ​= (char *)mmap(NULL, 
-  ​shmid shmget(IPC_PRIVATE, sizeof(pthread_cond_t) + sizeof(pthread_mutex_t),​ +                              ​sizeof(pthread_cond_t) + sizeof(pthread_mutex_t),​ 
-                 S_IRWXU); +                              ​PROT_READ | PROT_WRITE, 
-  ​shared_block = (char *)shmat(shmidNULL, 0);+                              ​MAP_ANONYMOUS | MAP_SHARED-1, 0);
   cond = (pthread_cond_t *)shared_block;​   cond = (pthread_cond_t *)shared_block;​
   mutex = (pthread_mutex_t *)(shared_block + sizeof(pthread_cond_t));​   mutex = (pthread_mutex_t *)(shared_block + sizeof(pthread_cond_t));​
notes/sharing_pthreads_primitives_between_processes.txt · Last modified: 2013/01/31 13:23 by andy