Best example of using mutex semaphores in C ever.
#include <semaphore.h> #include <stdlib.h> #include <stdio.h> #include <sys/types.h> #include <unistd.h> int main(int argc, char **argv) { sem_t *sem; const char *name; name = (argc > 1) ? argv[1] : "foo"; sem = sem_open(name, O_CREAT, 0777, 1); if (sem == (sem_t *)SEM_FAILED) { perror(name); exit(EXIT_FAILURE); } fprintf(stderr, "%d waits...", getpid()); if (sem_wait(sem) == -1) { perror(name); exit(EXIT_FAILURE); } fprintf(stderr, "Got %s ", name); sleep(5); sem_post(sem); return EXIT_SUCCESS; }
Best example of using mutex semaphores in C ever.
C and C++ programming reference library for the standard libraries.