7 Sema::Sema(int init_value, char *title)
9 sem_init(&sem, 0, init_value);
11 this->init_value = init_value;
17 UNSET_ALL_LOCKS(this);
21 void Sema::lock(char *location)
23 SET_LOCK(this, title, location);
37 sem_getvalue(&sem, &result);
44 sem_init(&sem, 0, init_value);
71 Sema::Sema(int id, int number)
75 if((semid = semget(IPC_PRIVATE, number, IPC_CREAT | 0777)) < 0) perror("Sema::Sema");
76 for(int i = 0; i < number; i++) unlock(i);
78 bc_enter_sema_id(semid);
93 if(semctl(semid, 0, IPC_RMID, arg) < 0) perror("Sema::~Sema");
94 bc_remove_sema_id(semid);
99 int Sema::lock(int number)
103 // decrease the semaphore
104 sop.sem_num = number;
107 if(semop(semid, &sop, 1) < 0) perror("Sema::lock");
111 int Sema::unlock(int number)
115 // decrease the semaphore
116 sop.sem_num = number;
119 if(semop(semid, &sop, 1) < 0) perror("Sema::unlock");
123 int Sema::get_value(int number)
125 return semctl(semid, number, GETVAL, arg);