The contents of this page are licensed under the following license
Operating System

IPC semaphores
 #include <stdio.h>
 #include <sys/sem.h>
 #include <stdlib.h>

 int main(int argc, char* argv[])
 {
   int id;
   struct sembuf op;

   if (argc < 2)
   {
     printf("usage: %s <sem_op_value>\n", argv[0]);
     exit(1);
   }

   /* create semaphore */
   id = semget(0x13, 1, 0600 | IPC_CREAT);
   if (id == -1)
   {
     perror("Error create semaphore");
     exit(1);
   }

   printf("old value: %d\n", semctl(id, 0, GETVAL, NULL));

   /* set semaphore operation */
   op.sem_num = 0;
   op.sem_op = atoi(argv[1]);
   op.sem_flg = 0;

   /* perform operation */
   semop(id, &op, 1);

   printf("new value: %d\n", semctl(id, 0, GETVAL, NULL));
 }

August, 23rd 2006 © Michał Kalewski