I have a homework project that require the creation of a STATIC library to provide mutual access to a couple of named pipes.
These pipes are used for communication between various clients using the library and a server.
Now, suppose I want to use pthread mutexes; how can I achieve that? How can the processes know which is the shared memory area in which the mutex is stored? And who should require this memory area? The server can't because it's required the library itself to provide mutual exclusion.
Thanks to asveikau i came up with this:
#define SHARED 1
#define MUTEX 1
int main() {
sem_t* mutex = sem_open("mutex", O_CREAT);
sem_init(mutex, SHARED, MUTEX);
fork(), fork(), fork();
sem_wait(mutex);
int i;
for(i = 0; i < 10; i++)
printf("process %d %d\n", getpid(), i), fflush(stdout);
sem_post(mutex);
}
that from the output really seem to solve my problem.
Thank you to everyone.