write.c
1 
26 #include <unistd.h> /* for usleep() */
27 #include <shm_arena.h> /* for Shared Memory Arena Library */
28 
29 
30 int main(void)
31 {
32  int *ptr;
33  int running = 1;
34 
35  ptr = (int *) sm_get(sizeof(int), "count");
36  if(!ptr)
37  {
38  printf("sm_get() failed\n");
39  return 1;
40  }
41 
42  sm_wrlock(ptr);
43 
44  *ptr = 0;
45 
46  sm_unlock(ptr);
47 
48  while(running)
49  {
50  sm_wrlock(ptr);
51 
52  if(++*ptr > 10)
53  running = 0;
54 
55  sm_unlock(ptr);
56 
57  sleep(1);
58  }
59 
60  return 0;
61 }
static int sm_unlock(const void *ptr)
release a shared memory segment read-write lock
Definition: shm_arena.h:548
static void * sm_get(size_t size, const char *name)
get a shared memory segment
Definition: shm_arena.h:329
static int sm_wrlock(const void *ptr)
acquire a shared memory segment write lock
Definition: shm_arena.h:501

Shared Memory Arena version RC-0.0.25