Skip to content

Instantly share code, notes, and snippets.

@AlexanderOMara
Created March 6, 2016 03:46
Show Gist options
  • Save AlexanderOMara/21858c5c93c82bfeb115 to your computer and use it in GitHub Desktop.
Save AlexanderOMara/21858c5c93c82bfeb115 to your computer and use it in GitHub Desktop.
Mac OS X POSIX semaphore test
// clang -Wall -o posixsemtest posixsemtest.c
#include <stdio.h>
#include <semaphore.h>
int main(int argc, char** argv) {
if (argc < 2) {
printf("USAGE: %s <semaphore_name>\n", argv[0]);
return 1;
}
sem_t* semaphore = SEM_FAILED;
semaphore = sem_open(
argv[1],
O_CREAT,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH,
1
);
if (semaphore == (sem_t*)SEM_FAILED) {
printf("sem_open: SEM_FAILED\n");
return 1;
}
printf("Created POSIX semaphore '%s', press enter to remove it.\n", argv[1]);
getchar();
int unlinked = sem_unlink(argv[1]);
if (unlinked) {
printf("sem_unlink: error: %i\n", unlinked);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment