#include <stdio.h>1#include <fcntl.h> /* For O_* constants */2#include <sys/stat.h> /* For mode constants */3#include <semaphore.h>45#define SEM_NAME "/my_semaphore"67int main(void)8{9sem_t *my_sem;10int rc, pvalue;1112/* create semaphore with initial value of 1 */13my_sem = sem_open(SEM_NAME, O_RDWR);14if(my_sem == SEM_FAILED) {15printf("sem_open failed\n");16return 0;17}1819printf("Asteapta la semafor!\n");2021/* get the semaphore */22sem_wait(my_sem);2324/* do important stuff protected by the semaphore */25rc = sem_getvalue(my_sem, &pvalue);26printf("sem is %d\n", pvalue);2728/* release the lock */29sem_post(my_sem);3031rc = sem_close(my_sem);32if(rc == -1)33printf("sem_close failed\n");3435return 0;36}37383940