/* sem.h - CreateSem, P, V, DelSem */ typedef int *semaphore; semaphore CreateSem (int count); /* Returns a new semaphore */ void P(semaphore s); /* P, or wait, or down on semaphore */ void V(semaphore s); /* V, or signal, or up on semaphore */ int DelSem(semaphore s); /* Deletes an existing semaphore */ /* sem.c - CreateSem, P, V, DelSem */ /* This implementation of semaphores is strictly for kicks since it assumes the availability of pipes! A semaphore is represented by a pipe. The P operation is simulated by reading a character from the pipe. The V operation is simulated by writing a character to the pipe. The kind of the semaphore is specified when the semaphore is created. If the argument is 0, we get a blocking semaphore, if 1, a boolean semaphore, and if n>1, a counting semaphore. */ #include #include #include "sem.h" semaphore CreateSem (int count){ /* Create and return a new semaphore */ int *fd; int i; fd = (int *)malloc(8); /* Allocating space for two filedescriptors */ if (pipe(fd) < 0) { printf("Cannot create pipe.\n"); exit(0);} for(i=0;i