aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2015-04-14 02:53:47 +0200
committerMattias Andrée <maandree@operamail.com>2015-04-14 02:53:47 +0200
commita2cf308294ef707fdc40f96752be641b0422ec6c (patch)
treeddbde3281c35f4d60b972d14d4361915ea2bbc96 /src
parentadd readme (diff)
downloadbus-a2cf308294ef707fdc40f96752be641b0422ec6c.tar.gz
bus-a2cf308294ef707fdc40f96752be641b0422ec6c.tar.bz2
bus-a2cf308294ef707fdc40f96752be641b0422ec6c.tar.xz
create semaphore array with random key
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src')
-rw-r--r--src/semaphore.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/semaphore.c b/src/semaphore.c
new file mode 100644
index 0000000..5447c82
--- /dev/null
+++ b/src/semaphore.c
@@ -0,0 +1,58 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <time.h>
+#include <errno.h>
+
+#include <sys/ipc.h>
+#include <sys/sem.h>
+
+
+#ifdef _SEM_SEMUN_UNDEFINED
+union semun {
+ int val;
+ struct semid_ds *buf;
+ unsigned short *array;
+};
+#endif
+
+
+char *argv0;
+
+
+int main(int argc, char *argv[])
+{
+ key_t key;
+ int id = -1;
+
+ (void) argc;
+ argv0 = *argv;
+
+ srand(time(NULL));
+ for (;;) {
+ double r = (double)rand();
+ r /= (double)RAND_MAX + 1;
+ r *= (1 << (8 * sizeof(key_t) - 2)) - 1;
+ key = (key_t)r + 1;
+ if (key == IPC_PRIVATE)
+ continue;
+ id = semget(key, 3, IPC_CREAT | IPC_EXCL | 0600);
+ if (id != -1)
+ break;
+ if ((errno != EEXIST) && (errno != EINTR))
+ goto fail;
+ }
+
+ printf("key:%zx, id:%i\n", (size_t)key, id);
+
+ if (semctl(id, 0, IPC_RMID) == -1)
+ goto fail;
+
+ return 0;
+
+fail:
+ perror(argv0);
+ if ((id != -1) && (semctl(id, 0, IPC_RMID) == -1))
+ perror(argv0);
+ return 1;
+}
+