Beanalby

testServer.c for http://home.beanalby.net/blog/2012/valgrind

Jun 8th, 2012
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/shm.h>
  3. #include <sys/sem.h>
  4.  
  5. int main() {
  6.   const char *IPC_PATH="/home/jason/ipcfile";
  7.   int *buf;
  8.   int ret, shm, semid;
  9.   key_t memKey, semaphoreKey;
  10.   union semun {
  11.     int val;
  12.     struct semid_ds *buf;
  13.     unsigned short  *array;
  14.   } arg;
  15.   arg.val=42;
  16.  
  17.   /* set up the semaphore, set its value */
  18.   semaphoreKey = ftok(IPC_PATH, 1);
  19.   semid = semget(semaphoreKey, 1, 0660 | IPC_CREAT);
  20.   ret = semctl(semid, 0, SETVAL, arg);
  21.  
  22.   /* set up the shared memory */
  23.   memKey = ftok(IPC_PATH, 1);
  24.   shm = shmget(memKey, sizeof(int), 0660 | IPC_CREAT);
  25.   buf = shmat(shm, NULL, 0);
  26.  
  27.   /* Stick the semaphore id in shared mem for the client */
  28.   *buf = semid;
  29.  
  30.   printf("Semaphore id %i put in shared mem %i for client\n", semid, shm);
  31.   printf("Enter a character to stop.\n");
  32.   getc(stdin);
  33.  
  34.   /* detach & remove shared mem, remove semaphore */
  35.   ret = shmdt(buf);
  36.   ret = shmctl(shm, IPC_RMID, 0);
  37.   ret = semctl(semid, 0, IPC_RMID);
  38.  
  39.   printf("Done.\n");
  40.   return 0;
  41. }
Add Comment
Please, Sign In to add comment